package Business::OnlinePayment::Jety;

use strict;
use Carp;
use Business::OnlinePayment 3;
use Business::OnlinePayment::HTTPS;
use vars qw($VERSION @ISA $me $DEBUG);

use Date::Format;
use Tie::IxHash;

@ISA = qw(Business::OnlinePayment::HTTPS);
$VERSION = '0.01';
$me = 'Business::OnlinePayment::Jety';

$DEBUG = 1;

my @fields = (qw(
  username
  password
  function
  firstname
  lastname
  address1
  address2
  city
  state
  zip
  email
  phone
  programdesc
  ref
  bankname
  accountaba
  accountdda
  amount
));

my %map = (
  login      => 'username',
  first_name => 'firstname',
  last_name  => 'lastname',
  address    => 'address1',
  bank_name  => 'bankname',
  account_number => 'accountdda',
  routing_code => 'accountaba',
);
  

sub set_defaults {
  my $self = shift;
  $self->server('api.cardservicesportal.com');
  $self->port(443);
  $self->path('/servlet/drafts.echeck');
  return;
}

sub map_fields {
  my $self = shift;
  my $content = $self->{_content};

  $self->remap_fields(%map);

  die "Jety API only supports ECHECK payments.\n" 
    if(lc($content->{type}) ne 'echeck');
  die "Jety interface only supports Normal Authorization.\n"
    if(lc($content->{action}) ne 'normal authorization');

  $content->{'function'} = 'ach';
  $content->{'programdesc'} = '415-462-1624 Business::OnlinePayment::Jety';
  $content->{'ref'} = time2str('%Y%m%d',time).'-'.int(rand(1000000));
  return;
}

sub submit {
  my $self = shift;
  $Business::OnlinePayment::HTTPS::DEBUG = $DEBUG;
  $DB::single = $DEBUG; 

  $self->required_fields(qw(
    type
    action
    first_name
    last_name
    address
    city
    state
    zip
    email
    phone
    account_number
    routing_code
    amount
    ) );
  $self->map_fields;
  tie my %request, 'Tie::IxHash', (
    map { $_, $self->{_content}->{$_} } @fields
    );

  $DB::single = $DEBUG;
  if($self->test_transaction()) {
    print "https://".$self->server.$self->path."\n";
    print "$_\t".$request{$_}."\n" foreach keys(%request);
    $self->error_message('test mode not supported');
    $self->is_success(0);
    return;
  }
  my ($reply, $response, %reply_headers) = $self->https_post(\%request);
  
  if(not $response =~ /^200/) {
    croak "HTTPS error: '$response'";
  }

  # string looks like this:
  # P1=1234&P2=General Status&P3=Specific Status
  # P3 is not always there, though.
  if($reply =~ /^P1=(\d+)&P2=([\w ]*)(&P3=(\S+))?/) {
    if($1 == 0) {
      $self->is_success(1);
      $self->authorization($4);
    }
    else {
      $self->is_success(0);
      $self->error_message($2.($4 ? "($4)" : ''));
    }
  }
  else {
    croak "Malformed server response: '$reply'";
  }

  return;
}

1;
__END__

=head1 NAME

Business::OnlinePayment::Jety - Jety Payments ACH backend for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

  ####
  # Electronic check authorization.  We only support 
  # 'Normal Authorization'.
  ####

  my $tx = new Business::OnlinePayment("Jety");
  $tx->content(
      type           => 'ECHECK',
      login          => 'testdrive',
      password       => 'testpass',
      action         => 'Normal Authorization',
      description    => 'Business::OnlinePayment test',
      amount         => '49.95',
      invoice_number => '100100',
      first_name     => 'Jason',
      last_name      => 'Kohles',
      address        => '123 Anystreet',
      city           => 'Anywhere',
      state          => 'UT',
      zip            => '84058',
      account_type   => 'personal checking',
      account_number => '1000468551234',
      routing_code   => '707010024',
      check_number   => '1001', # optional
  );
  $tx->submit();

  if($tx->is_success()) {
      print "Check processed successfully: ".$tx->authorization."\n";
  } else {
      print "Check was rejected: ".$tx->error_message."\n";
  }

=head1 SUPPORTED TRANSACTION TYPES

=head2 ECHECK

Content required: type, login, password, action, amount, first_name, last_name, account_number, routing_code.

=head1 DESCRIPTION

For detailed information see L<Business::OnlinePayment>.

=head1 METHODS AND FUNCTIONS

See L<Business::OnlinePayment> for the complete list. The following methods either override the methods in L<Business::OnlinePayment> or provide additional functions.  

=head2 result_code

Returns the four-digit result code.

=head2 error_message

Returns a useful error message.

=head1 Handling of content(%content) data:

=head2 action

The following actions are valid:

  normal authorization

=head1 AUTHOR

Mark Wells <mark@freeside.biz>

=head1 SEE ALSO

perl(1). L<Business::OnlinePayment>.

=cut

