package Business::OnlinePayment::USAePay;

use strict;
use Carp;
use Business::OnlinePayment 3;
use Business::OnlinePayment::HTTPS;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
use vars qw($VERSION @ISA $DEBUG);

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

$DEBUG = 0;

my $default_path = '/gate.php';
my $default_cert_path = '/secure/gate.php';

sub set_defaults {
    my $self = shift;
    $self->server('www.usaepay.com');
    $self->port('443');
    $self->path($default_path);

    $self->build_subs(qw(order_number));
}

sub map_fields {
  my($self) = shift;

  my %content = $self->content();

  my %types = ('visa'             => 'CC',
               'mastercard'       => 'CC',
               'american express' => 'CC',
               'discover'         => 'CC',
               'check'            => 'ECHECK',
              );
  $content{'type'} = $types{lc($content{'type'})} || $content{'type'};
  $self->transaction_type($content{'type'});

  my %actions;
  my %cc_actions = ('normal authorization' => 'sale',
                    'authorization only'   => 'authonly',
                    'post authorization'   => 'postauth',
                   );
  my %ec_actions = ('normal authorization' => 'check',
                    'credit'               => 'checkcredit',
                   );
  if ($content{'type'} eq 'CC') {
    (%actions) = (%cc_actions);
  }elsif ($content{'type'} eq 'ECHECK') {
    (%actions) = (%ec_actions);
  }
  $content{'action'} = $actions{lc($content{'action'})} || $content{'action'};
                 
  $content{'expiration'} =~ s/\D//g;

  $content{'md5hash'} = md5_hex(join(':', map "$content{$_}", qw(action password amount invoice_number md5key))) if defined $content{'password'};

  $self->content(%content);
}

sub submit {
    my($self) = @_;

    $self->map_fields();

    $self->remap_fields(
      login            => 'UMkey',
      md5key           => 'UMmd5key',
      md5hash          => 'UMmd5hash',
      card_number      => 'UMcard',
      expiration       => 'UMexpir',
      amount           => 'UMamount',
      invoice_number   => 'UMinvoice',
      description      => 'UMdescription',
      customer_id      => 'UMcustid',
      cvv2             => 'UMcvv2',
      email            => 'UMcustemail',
      name             => 'UMname',
      address          => 'UMstreet',
      zip              => 'UMzip',
      customer_ip      => 'UMip',
      order_number     => 'UMrefNum',
      authorization    => 'UMauthCode',
      routing_code     => 'UMrouting',
      account_number   => 'UMaccount',
      customer_ssn     => 'UMssn',
    );
    my %content = $self->content;
    if ( $DEBUG ) {
      warn "content:$_ => $content{$_}\n" foreach keys %content;
    }

    my @required_fields = qw(type action login);

    if ($self->transaction_type() eq 'CC' ) {
      push @required_fields, qw/card_number expiration amount invoice_number name address zip/;
      if ($self->{_content}->{action} eq 'postauth') {
        push @required_fields, qw/authorization/;
      }
      if (    $self->{_content}->{action} eq 'void'
           || $self->{_content}->{action} eq 'capture') {
        push @required_fields, qw/order_number/;
      }
    }elsif ($self->transaction_type() eq 'ECHECK' ) {
      push @required_fields, qw/routing_code account_number amount invoice_number name customer_ssn/;
    } else {
      croak("USAePay can't handle transaction type: ".
            $self->transaction_type());
    }

    $self->required_fields(@required_fields);

    my %post_data = $self->get_fields( map "$_", qw(
      UMcommand UMkey UMmd5hash UMmd5key UMauthCode UMrefNum UMcard UMexpir
      UMrouting UMaccount UMamount Umtax UMnontaxable UMtip UMshipping
      UMdiscount UMsubtotal UMcustid UMinvoice UMorderid UMponum UMdescription
      UMcvv2 UMcustemail UMcustreceipt UMname UMStreet UMzip UMssn UMdlnum
      UMdlstate UMclerk UMterminal UMtable UMip UMsoftware UMredir
      UMredirApproved UMredirDeclined UMechofields UMtestmode
    ) );
    $post_data{'UMtestmode'} = $self->test_transaction() ? 1 : 0;
    $post_data{'UMsoftware'} = __PACKAGE__. " $VERSION";
    if ( $DEBUG ) {
      warn "post_data:$_ => $post_data{$_}\n" foreach keys %post_data;
    }

    my($page,$server_response) = $self->https_post(%post_data);
    if ( $DEBUG ) {
      warn "response page: $page\n";
    }

    my $response;
    if ($server_response =~ /200/){
      $response = {map { split '=', $_, 2 } split '&', $page};
    }else{
      $response->{UMstatus} = 'Error';
      $response->{UMerror} = $server_response;
    }

    $response->{$_} =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg
      foreach keys %$response;

    if ( $DEBUG ) {
      warn "response:$_ => $response->{$_}\n" foreach keys %$response;
    }

    if ( $response->{UMstatus} =~ /^Approved/ ) {
      $self->is_success(1);
      $self->authorization($response->{UMauthCode});
    } else {
      $self->is_success(0);
    }
    $self->result_code($response->{UMresult});
    $self->error_message($response->{UMerror});
    $self->server_response($response);
}

1;
__END__

=head1 NAME

Business::OnlinePayment::USAePay - USA ePay backend for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

  my $tx = new Business::OnlinePayment("USAePay");
  $tx->content(
      login          => 'igztOatyqbpd1wsxijl4xnxjodldwdxR', #USA ePay source key
      action         => 'Normal Authorization',
      description    => 'Business::OnlinePayment test',
      amount         => '49.95',
      invoice_number => '100100',
      name           => 'Tofu Beast',
      card_number    => '46464646464646',
      expiration     => '11/08',
      address        => '1234 Bean Curd Lane, San Francisco',
      zip            => '94102',
  );
  $tx->submit();

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

=head1 DESCRIPTION

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

=head1 NOTE

=head1 COMPATIBILITY

This module implements USAePay's CGI Gateway API v2.9.5.  See
http://www.usaepay.com/topics/api.html for details.

=head1 AUTHOR

Jeff Finucane <jeff@cmh.net>

=head1 SEE ALSO

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

=cut

