summaryrefslogtreecommitdiff
path: root/eSelectPlus.pm
blob: 806fb65ad6a7127ffe85badc19e55d54dde1b92c (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package Business::OnlinePayment::eSelectPlus;

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

@ISA = qw(Business::OnlinePayment::HTTPS);
$VERSION = '0.08_02';
$DEBUG = 0;

sub set_defaults {
    my $self = shift;
    my %opts = @_;

    #USD
    #$self->server('esplusqa.moneris.com');  # development
    $self->server('esplus.moneris.com');   # production
    $self->path('/gateway_us/servlet/MpgRequest');

    ##CAD
    ##$self->server('esqa.moneris.com');  # development
    #$self->server('www3.moneris.com');   # production
    #$self->path('/gateway2/servlet/MpgRequest');

    $self->port('443');

    $self->build_subs(qw( order_number avs_code skip_avs skip_cvv ));
    # avs_code order_type md5 cvv2_response cavv_response

    $self->skip_avs( $opts{skip_avs} );
    $self->skip_cvv( $opts{skip_cvv} );
}

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

    if ( defined( $self->{_content}{'currency'} )
              &&  $self->{_content}{'currency'} eq 'CAD' ) {
      $self->server('www3.moneris.com');
      $self->path('/gateway2/servlet/MpgRequest');
    } else { #sorry, default to USD
      $self->server('esplus.moneris.com');
      $self->path('/gateway_us/servlet/MpgRequest');
    }

    if ($self->test_transaction)  {
       if ( defined( $self->{_content}{'currency'} )
                 &&  $self->{_content}{'currency'} eq 'CAD' ) {
         $self->server('esqa.moneris.com');
         $self->{_content}{'login'} = 'store2';   # store[123]
         $self->{_content}{'password'} = 'yesguy';
       } else { #sorry, default to USD
         $self->server('esplusqa.moneris.com');
         $self->{_content}{'login'} = 'monusqa002';   # monusqa00[123]
         $self->{_content}{'password'} = 'qatoken';
       }
    }

    my %cust_id = ( 'invoice_number' => 'cust_id' );

    my $invoice_number = $self->{_content}{invoice_number};

    # BOP field => eSelectPlus field
    #$self->map_fields();
    $self->remap_fields(
        #                => 'order_type',
        #                => 'transaction_type',
        #login            => 'store_id',
        #password         => 'api_token',

        #authorization   => 
        #name            =>
        #first_name      =>
        #last_name       =>
        #company         =>
        #address         => 'avs_street_number'/'avs_street_name' handled below
        #city            => 
        #state           => 
        zip             => 'avs_zipcode',
        #country         =>
        phone            => 'avs_custphone',
        #fax             =>
        email            => 'avs_email',
        customer_ip      => 'avs_custip',

        card_number      => 'pan',
        #expiration        => 'expdate', #handled below
        cvv2             => 'cvd_value',

        'amount'         => 'amount',
        customer_id      => 'cust_id',
        order_number     => 'order_id',   # must be unique number
        authorization    => 'txn_number', # reference to previous trans
    );

    my $action = $self->{_content}{'action'};
    if ( $self->{_content}{'action'} =~ /^\s*normal\s*authorization\s*$/i ) {
      $action = 'purchase';
    } elsif ( $self->{_content}{'action'} =~ /^\s*authorization\s*only\s*$/i ) {
      $action = 'preauth';
    } elsif ( $self->{_content}{'action'} =~ /^\s*post\s*authorization\s*$/i ) {
      $action = 'completion';
    } elsif ( $self->{_content}{'action'} =~ /^\s*void\s*$/i ) {
      $action = 'purchasecorrection';
    } elsif ( $self->{_content}{'action'} =~ /^\s*credit\s*$/i ) {
      if ( $self->{_content}{'authorization'} ) {
        $action = 'refund';
      } else {
        $action = 'ind_refund';
      }
    }

    if ( $action =~ /^(purchase|preauth|ind_refund)$/ ) {

      $self->required_fields(qw(
        login password amount card_number expiration
      ));

      #cardexpiremonth & cardexpireyear
      $self->{_content}{'expiration'} =~ /^(\d+)\D+\d*(\d{2})$/
        or croak "unparsable expiration ". $self->{_content}{expiration};
      my( $month, $year ) = ( $1, $2 );
      $month = '0'. $month if $month =~ /^\d$/;
      $self->{_content}{expdate} = $year.$month;

      #CVD Indicator
      #0 = CVD value is deliberately bypassed or is not provided by the merchant
      #1 = CVD value is present.
      #2 = CVD value is on the card, but is illegible.
      #9 = Cardholder states that the card has no CVD imprint.
      $self->{_content}{cvd_indicator} = $self->{_content}{cvd_value} ? 1 : 0;

      $self->generate_order_id;

      $self->{_content}{order_id} .= '-'. ($invoice_number || 0);

      $self->{_content}{amount} = sprintf('%.2f', $self->{_content}{amount} );

    } elsif ( $action =~ /^(completion|purchasecorrection|refund)$/ ) {

      $self->required_fields(qw(
        login password order_number authorization
      ));

      if ( $action eq 'completion' ) {
        $self->{_content}{comp_amount} = delete $self->{_content}{amount};
      } elsif ( $action eq 'purchasecorrection' ) {
        delete $self->{_content}{amount};
      #} elsif ( $action eq 'refund' ) {
      } 

    }

    if ( $self->{_content}{address} ) {
      my $number = '';
      my $name = $self->{_content}{address};
      if ( $name =~ s/^\s*(\d+)\w\s+// ) {
        $number = $1;
      }
      $name = substr( $name, 0, 19 - length($number) );
      $self->{_content}{avs_street_number} = $number;
      $self->{_content}{avs_street_name} = $name;
    }

    $self->{_content}{avs_zipcode} =~ s/\W//g
      if defined $self->{_content}{avs_zipcode};

    # E-Commerce Indicator (see eSelectPlus docs)
    $self->{_content}{'crypt_type'} ||= 7;

    $action = "us_$action"
      unless defined( $self->{_content}{'currency'} )
                   && $self->{_content}{'currency'} eq 'CAD';

    #no, values aren't escaped for XML.  their "mpgClasses.pl" example doesn't
    #appear to do so, i dunno
    tie my %fields, 'Tie::IxHash', $self->get_fields( $self->fields );
    my $post_data =
      '<?xml version="1.0"?>'.
      '<request>'.
      '<store_id>'.  $self->{_content}{'login'}. '</store_id>'.
      '<api_token>'. $self->{_content}{'password'}. '</api_token>'.
      "<$action>".
        join('', map "<$_>$fields{$_}</$_>", keys %fields );

    if ( $action =~ /^(purchase|preauth|ind_refund)$/ ) {
      tie my %avs_fields, 'Tie::IxHash', $self->get_fields( $self->avs_fields );
      $post_data .=
          '<avs_info>'.  
            join('', map "<$_>$avs_fields{$_}</$_>", keys %avs_fields ).
          '</avs_info>'
       if ! $self->skip_avs && grep $_, values %avs_fields;

      tie my %cvd_fields, 'Tie::IxHash', $self->get_fields( $self->cvd_fields );
      $post_data .=
          '<cvd_info>'.  
            join('', map "<$_>$cvd_fields{$_}</$_>", keys %cvd_fields ).
          '</cvd_info>'
        if ! $self->skip_cvv && grep $_, values %cvd_fields;
    }

    $post_data .=
      "</$action>".
      '</request>';

    warn "POSTING: ".$post_data if $DEBUG > 1;

    my( $page, $response, @reply_headers) = $self->https_post( $post_data );

    if ($DEBUG > 1) {
      my %reply_headers = @reply_headers;
      warn join('', map { "  $_ => $reply_headers{$_}\n" } keys %reply_headers)
    }

    if ($response !~ /^200/)  {
        # Connection error
        $response =~ s/[\r\n]+/ /g;  # ensure single line
        $self->is_success(0);
        my $diag_message = $response || "connection error";
        die $diag_message;
    }

    # avs_code - eSELECTplus_Perl_IG.pdf Appendix F
    my %avsTable = ('A' => 'A',
                    'B' => 'A',
                    'C' => 'E',
                    'D' => 'Y',
                    'G' => '',
                    'I' => '',
                    'M' => 'Y',
                    'N' => 'N',
                    'P' => 'Z',
                    'R' => 'R',
                    'S' => '',
                    'U' => 'E',
                    'W' => 'Z',
                    'X' => 'Y',
                    'Y' => 'Y',
                    'Z' => 'Z',
                    );
    my $AvsResultCode = $self->GetXMLProp($page, 'AvsResultCode');
    $self->avs_code( defined($AvsResultCode) && exists $avsTable{$AvsResultCode}
                         ?  $avsTable{$AvsResultCode}
                         :  $AvsResultCode
                   );

    #md5 cvv2_response cavv_response ...?

    $self->server_response($page);

    my $result = $self->GetXMLProp($page, 'ResponseCode');

    die "gateway error: ". $self->GetXMLProp( $page, 'Message' )
      if $result =~ /^null$/i;

    # Original order_id supplied to the gateway
    $self->order_number($self->GetXMLProp($page, 'ReceiptId'));

    # We (Whizman & DonorWare) do not have enough info about "ISO"
    # response codes to make use of them.
    # There may be good reasons why the ISO codes could be preferable,
    # but we would need more information.  For now, the ResponseCode.
    # $self->result_code( $self->GetXMLProp( $page, 'ISO' ) );
    $self->result_code( $result );

    if ( $result =~ /^\d+$/ && $result < 50 ) {
        $self->is_success(1);
        $self->authorization($self->GetXMLProp($page, 'TransID'));
    } elsif ( $result =~ /^\d+$/ ) {
        $self->is_success(0);
        my $tmp_msg = $self->GetXMLProp( $page, 'Message' );
        $tmp_msg =~ s/\s{2,}//g;
        $tmp_msg =~ s/[\*\=]//g;
        $self->error_message( $tmp_msg );
    } else {
        die "unparsable response received from gateway (response $result)".
            ( $DEBUG ? ": $page" : '' );
    }

}

use vars qw(@oidset);
@oidset = ( 'A'..'Z', '0'..'9' );
sub generate_order_id {
    my $self = shift;
    #generate an order_id if order_number not passed
    unless (    exists ($self->{_content}{order_id})
             && defined($self->{_content}{order_id})
             && length ($self->{_content}{order_id})
           ) {
      $self->{_content}{'order_id'} =
        join('', map { $oidset[int(rand(scalar(@oidset)))] } (1..23) );
    }
}

sub fields {
        my $self = shift;

        #order is important to this processor
        qw(
          order_id
          cust_id
          amount
          comp_amount
          txn_number
          pan
          expdate
          crypt_type
          cavv
        );
}

sub avs_fields {
        my $self = shift;

        #order is important to this processor
        qw(
          avs_street_number
          avs_street_name
          avs_zipcode
          avs_email
          avs_hostname
          avs_browser
          avs_shiptocountry
          avs_shipmethod
          avs_merchprodsku
          avs_custip
          avs_custphone
        );
}

sub cvd_fields {
        my $self = shift;

        #order is important to this processor
        qw(
          cvd_indicator
          cvd_value
        );
}

sub GetXMLProp {
        my( $self, $raw, $prop ) = @_;
        local $^W=0;

        my $data;
        ($data) = $raw =~ m"<$prop>(.*?)</$prop>"gsi;
        #$data =~ s/<.*?>/ /gs;
        chomp $data;
        return $data;
}

1;

__END__

=head1 NAME

Business::OnlinePayment::eSelectPlus - Moneris eSelect Plus backend module for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

  ####
  # One step transaction, the simple case.
  ####

  my $tx = new Business::OnlinePayment("eSelectPlus");
  $tx->content(
      type           => 'VISA',
      login          => 'eSelect Store ID,
      password       => 'eSelect API Token',
      action         => 'Normal Authorization',
      description    => 'Business::OnlinePayment test',
      amount         => '49.95',
      currency       => 'USD', #or CAD for compatibility with previous releases
      name           => 'Tofu Beast',
      address        => '123 Anystreet',
      city           => 'Anywhere',
      state          => 'UT',
      zip            => '84058',
      phone          => '420-867-5309',
      email          => 'tofu.beast@example.com',
      card_number    => '4005550000000019',
      expiration     => '08/06',
      cvv2           => '1234', #optional
  );
  $tx->submit();

  if($tx->is_success()) {
      print "Card processed successfully: ".$tx->authorization."\n";
  } else {
      print "Card was rejected: ".$tx->error_message."\n";
  }
  print "AVS code: ". $tx->avs_code. "\n"; # Y - Address and ZIP match
                                           # A - Address matches but not ZIP
                                           # Z - ZIP matches but not address
                                           # N - no match
                                           # E - AVS error or unsupported
                                           # R - Retry (timeout)
                                           # (empty) - not verified

=head1 SUPPORTED TRANSACTION TYPES

=head2 CC, Visa, MasterCard, American Express, Discover

Content required: type, login, password, action, amount, card_number, expiration.

=head1 PREREQUISITES

  URI::Escape
  Tie::IxHash

  Net::SSLeay _or_ ( Crypt::SSLeay and LWP )

=head1 DESCRIPTION

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

=head1 NOTES

=head2 Note for Canadian merchants upgrading to 0.03

As of version 0.03, this module now defaults to the US Moneris.  Make sure to
pass currency=>'CAD' for Canadian transactions.

=head2 Note for upgrading to 0.05

As of version 0.05, the bank authorization code is discarded (AuthCode),
so that authorization() and order_number() can return the 2 fields needed
for capture.  See also
cpansearch.perl.org/src/IVAN/Business-OnlinePayment-3.02/notes_for_module_writers_v3

=head2 Note for upgrading to 0.08 without AVS/CVV enabled with Moneris

This version now passes AVS and CVV info (previous versions did not).  If your
Moneris account is not enabled for these services, you can omit them by passing
the "skip_avs" and/or "skip_cvv" options set to a true value:

  my $tx = new Business::OnlinePayment('eSelectPlus',
                                         'skip_avs' => 1,
                                         'skip_cvv' => 1,
                                      );

=head1 AUTHOR

Ivan Kohler <ivan-eselectplus@420.am>
Randall Whitman L<whizman.com|http://whizman.com>

=head1 SEE ALSO

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

=cut