summaryrefslogtreecommitdiff
path: root/lib/Business/OnlinePayment/PPIPayMover.pm
blob: 459d141da437fbc827abea511469e12f77c963f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package Business::OnlinePayment::PPIPayMover;

use strict;
use vars qw($VERSION @ISA $DEBUG);
use Carp;
use Business::OnlinePayment::PPIPayMover::constants;
use Business::OnlinePayment::PPIPayMover::TransactionClient;
use Business::OnlinePayment::PPIPayMover::CreditCardRequest;
use Business::OnlinePayment::PPIPayMover::CountryCodes;
use Business::OnlinePayment::PPIPayMover::CreditCardResponse;

$VERSION = '0.02';
@ISA = qw(Business::OnlinePayment);
$DEBUG = 0;

my $tranclient = new Business::OnlinePayment::PPIPayMover::TransactionClient;
#my $ccreq = new Business::OnlinePayment::PPIPayMover::CreditCardRequest;

sub set_defaults {
  my $self = shift;

    #$self->server('secure.linkpt.net');
    #$self->port('1129');

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

}

sub map_fields {
  my $self = shift;

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

  # ACTION MAP
  #    target types: SALE, ADJUSTMENT, AUTH, CAPTURE, CREDIT, FORCE_AUTH,
  #                  FORCE_SALE, QUERY_CREDIT, QUERY_PAYMENT or VOID
  my %actions = (
    'normal authorization' => 'SALE',
    'authorization only'   => 'AUTH',
    'credit'               => 'CREDIT',
    'post authorization'   => 'CAPTURE',
    'void'                 => 'VOID',
  );
  $content{'action'} = $actions{lc($content{'action'})} || $content{'action'};

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

  # stuff it back into %content
  $self->content(%content);
}

sub submit {
  my $self = shift;

    #type          =>
    #login         =>
    #password      =>
    #authorization =>

		    #name

    #order_number

		    #currency          =>

		    #check_type        =>
		    #account_name      =>
		    #account_number    => 
		    #account_type      =>
 		   #bank_name         => 
 		   #routing_code      =>
		    #customer_org      =>
  		  #customer_ssn      =>
  		  #license_num       =>
		    #license_state     =>
  		  #license_dob       =>
 		   #get from new() args instead# payee             =>
   		 #check_number      =>

		    #recurring_billing => 'cnp_recurring',

  $self->map_fields();

  my %content = $self->content;

  my($month, $year);
  unless ( $content{action} eq 'CAPTURE'
           || ( $content{'action'} =~ /^(CREDIT|VOID)$/
                && exists $content{'order_number'} )
         ) {

    if (  $self->transaction_type() =~
            /^(cc|visa|mastercard|american express|discover)$/i
       ) {
    } else {
        Carp::croak("PPIPayMover can't handle transaction type: ".
                    $self->transaction_type());
    }

    $content{'expiration'} =~ /^(\d+)\D+\d*(\d{2})$/
      or croak "unparsable expiration $content{expiration}";

    ( $month, $year ) = ( $1, "20$2" );
    $month = '0'. $month if $month =~ /^\d$/;
  }

  my $ccreq = new Business::OnlinePayment::PPIPayMover::CreditCardRequest;

  $self->revmap_fields( $ccreq,

    'ChargeTotal'                  => 'amount',
    'ChargeType'                   => 'action',
    'CreditCardNumber'             => 'card_number',
    'CreditCardVerificationNumber' => 'cvv2',
    'ExpireMonth'                  => \$month,
    'ExpireYear'                   => \$year,

    'BillAddressOne'               => 'address',
    #'BillAddressTwo'               => '',
    'BillCity'                     => 'city',
    'BillCompany'                  => 'company',
    'BillCountryCode'              => 'country',
    #'BillCustomerTitle'            => '',
    'BillEmail',                   => 'email',
    'BillFax'                      => 'fax',
    'BillFirstName'                => 'first_name',
    'BillLastName'                 => 'last_name',
    #'BillMiddleName'               => '',
    'BillNote'                      => '',
    'BillPhone'                    => 'phone',
    'BillPostalCode'               => 'zip',
    'BillStateOrProvince'          => 'state',

    'ShipAddressOne'               => 'ship_address',
    #'ShipAddressTwo'               => '',
    'ShipCity'                     => 'ship_city',
    'ShipCompany'                  => 'ship_company',
    'ShipCountryCode'              => 'ship_country',
    #'ShipCustomerTitle'            => '',
    'ShipEmail',                   => 'ship_email',
    'ShipFax'                      => 'ship_fax',
    'ShipFirstName'                => 'ship_first_name',
    'ShipLastName'                 => 'ship_last_name',
    #'ShipMiddleName'               => '',
    'ShipNote'                      => '',
    'ShipPhone'                    => 'ship_phone',
    'ShipPostalCode'               => 'ship_zip',
    'ShipStateOrProvince'          => 'ship_state',

    #'OrderId'                      => 'order_number',
    'OrderId'                      => (int (rand 999999998) + 1 ), # XXX This can result in duplicate order ids.  You should use your own sequence instead.
    'BuyerCode'                    => '83487235',
    'CustomerIPAddress'            => 'customer_ip',
    'OrderCustomerId'              => 'customer_id',
    'OrderDescription'             => 'description',
    #'OrderUserId'                  => '',
    #'PurchaseOrderNumber'          => '',
    'TransactionConditionCode'     => \( TCC_CARDHOLDER_NOT_PRESENT_SECURE_ECOMMERCE ),
    #'ShippingCharge'               => '',
    #'StateTax'                     => '',
    #'TaxAmount'                    => '',
    #'TaxExempt'                    => '',

    'InvoiceNumber'                => 'invoice_number',
    'Industry'                     => \( RETAIL ),
    #'FolioNumber'                  => '',

    #'ChargeTotalIncludesRestaurant'
    #'ChargeTotalIncludesGiftshop'
    #'ChargeTotalIncludesMinibar'
    #'ChargeTotalIncludesPhone'
    #'ChargeTotalIncludesLaundry'
    #'ChargeTotalIncludesOther'

    #'ServiceRate'

    #'ServiceStartDay'
    #'ServiceStartMonth'
    #'ServiceStartYear'
    #'ServiceEndMonth'
    #'ServiceEndYear'
    #'ServiceEndDay'

    #'ServiceNoShow'

    #'ReferenceId'    => '', # XXX Use reference ID for follow-on transactions (CAPTURE, VOID)
    #'CAVV'
    #'XID'
    #'Track1'
    #'Track2'


  );

  # Send the transaction! (test token)

  my $token = $content{'login'};
  $token = "TEST$token" if $self->test_transaction();
  
  my $ccresponse = $tranclient->doTransaction( 
    "",     # transaction key (?)
    $ccreq, #cc request
    $token, #token
  );

  die $tranclient->GetErrorString unless defined $ccresponse;

  $self->result_code($ccresponse->GetResponseCode);
  $self->avs_code($ccresponse->GetAVSCode);
  $self->order_number($ccresponse->GetOrderId);

  if ( $self->result_code == 1 ) { # eq '1' ?
    $self->is_success(1);
    #$self->authorization($ccresponse->GetBankApprovalCode);
    $self->authorization($ccresponse->GetReferenceId); #"Identifier for follow-on transactions"
  } else {
    $self->is_success(0);
    $self->error_message($ccresponse->GetResponseCodeText);
  }

}

##  print "ResponseCode            : ", $ccresponse->GetResponseCode, "\n";
##  print "ResponseCodeText        : ", $ccresponse->GetResponseCodeText, "\n";
#  print "Timestamp               : ", $datetime, "\n";
#  print "IsoCode                 : ", $ccresponse->GetIsoCode, "\n";
##  print "OrderId                 : ", $ccresponse->GetOrderId, "\n";
##  print "BankApprovalCode        : ", $ccresponse->GetBankApprovalCode, "\n";
#  print "State                   : ", $ccresponse->GetState, "\n";
#  print "AuthorizedAmount        : ", $ccresponse->GetAuthorizedAmount, "\n";
#  print "OriginalAuthorizedAmount: ", $ccresponse->GetOriginalAuthorizedAmount,
#"\n";
#  print "CapturedAmount          : ", $ccresponse->GetCapturedAmount, "\n";
#  print "CreditedAmount          : ", $ccresponse->GetCreditedAmount, "\n";
#  print "TimeStampCreated        : ", $ccresponse->GetTimeStampCreated, "\n";
##  print "ReferenceId             : ", $ccresponse->GetReferenceId, "\n";
#  print "BankTransactionId       : ", $ccresponse->GetBankTransactionId, "\n";
#  print "BatchId                 : ", $ccresponse->GetBatchId, "\n";
#  #print "AVS Code                : ", $ccresponse->GetAVSCode, "\n";

#this is different from a "normal" B:OP revmap, it sets things in $ccreq
sub revmap_fields {
    my($self, $ccreq, %map) = @_;
    my %content = $self->content();
    foreach(keys %map) {
      my $method = "Set$_";
      my $content = ref($map{$_}) ? ${ $map{$_} } : $content{$map{$_}};
      $ccreq->$method($content);
    }
}


1;
__END__

=head1 NAME

Business::OnlinePayment::PPIPayMover - PPI PayMover backend for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

  my $tx = new Business::OnlinePayment( 'PPIPayMover' );

  $tx->content(
      login          => '195325FCC230184964CAB3A8D93EEB31888C42C714E39CBBB2E541884485D04B', #token
      type           => 'VISA',
      action         => 'Normal Authorization',
      description    => 'Business::OnlinePayment test',
      amount         => '49.95',
      invoice_number => '100100',
      customer_id    => 'jsk',
      name           => 'Grub Tetris',
      address        => '123 Anystreet',
      city           => 'Anywhere',
      state          => 'UT',
      zip            => '84058',
      email          => 'ivan-ppipaymover@420.am',
      card_number    => '4007000000027',
      expiration     => '09/12',
  );
  $tx->submit();

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

=head1 SUPPORTED TRANSACTION TYPES

=head2 Visa, MasterCard, American Express, JCB, Discover/Novus, Carte blanche/Di
ners Club

=head1 DESCRIPTION

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

=head1 BUGS

=head1 AUTHOR

Ivan Kohler <ivan-ppipaymover@420.am>

=head1 COPYRIGHT AND LICENSE

Based on API components from PPI PayMover provided without clear licensing, so,
probably not freely licensable at the moment... assuming that can be resolved:

Business::OnlinePayment conversion copyright (c) 2006 Ivan Kohler
All rights reserved. This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO

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

=cut