summaryrefslogtreecommitdiff
path: root/lib/Business/OnlinePayment/FirstDataGlobalGateway.pm
blob: e9b0659fbddfb3d35844048287e9dc5b498b1df1 (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
package Business::OnlinePayment::FirstDataGlobalGateway;
use base qw( Business::OnlinePayment );

use warnings;
use strict;
use Data::Dumper;
use Business::CreditCard;
use SOAP::Lite; #+trace => 'all';
#SOAP::Lite->import(+trace=>'debug');

our $VERSION = '0.03';
$VERSION = eval $VERSION; # modperlstyle: convert the string into a number

our @alpha = ( 'a'..'z', 'A'..'Z', '0'..'9' );

our %failure_status = (
  302 => 'nsf',
  501 => 'pickup',
  502 => 'stolen',
  503 => 'stolen', # "Fraud/Security violation"
  504 => 'blacklisted',
  509 => 'nsf',
  510 => 'nsf',
  519 => 'blacklisted',
  521 => 'nsf',
  522 => 'expired',
  530 => 'blacklisted',
  534 => 'blacklisted',
  # others are all "declined"
);
  

sub _info {
  {
    'info_compat'       => '0.01',
    'gateway_name'      => 'First Data Global Gateway e4',
    'gateway_url'       => 'https://www.firstdata.com/en_us/products/merchants/ecommerce/online-payment-processing.html',
    'module_version'    => $VERSION,
    'supported_types'   => [ 'CC' ], #, 'ECHECK' ],
    #'token_support'     => 1, # "Transarmor" is this, but not implemented yet
    'test_transaction'  => 1,

    'supported_actions' => [ 'Normal Authorization',
                             'Authorization Only',
                             'Post Authorization',
                             'Credit',
                             'Void',
                           ],
  };
}

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

    $self->build_subs(qw( order_number avs_code cvv2_response
                          authorization failure_status result_code
                          ));
}

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

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

    # TYPE MAP
    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'});
    
    # ACTION MAP 
    my $action = lc($content{'action'});
    my %actions =
      ( 'normal authorization' => '00', # Purchase
        'authorization_only'   => '01', # 
        'post authorization'   => '02', # Pre-Authorization Completion
        # '' => '03', # Forced Post
        'credit'               => '04', # Refund
        # '' => '05', # Pre-Authorization Only
        'void'                 => '13', # Void
        #'reverse authorization' => '',

        # '' => '07', # PayPal Order
        # '' => '32', # Tagged Pre-Authorization Completion
        # '' => '33', # Tagged Void
        # '' => '34', # Tagged Refund
        # '' => '83', # CashOut (ValueLink, v9 or higher end point only)
        # '' => '85', # Activation (ValueLink, v9 or higher end point only)
        # '' => '86', # Balance Inquiry (ValueLink, v9 or higher end point only)
        # '' => '88', # Reload (ValueLink, v9 or higher end point only)
        # '' => '89', # Deactivation (ValueLink, v9 or higher end point only)
      );

    $content{'action'} = $actions{$action} || $action;

    # make sure there's a combined name
    $content{name} ||= $content{first_name} . ' ' . $content{last_name};

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

}

sub remap_fields {
    my($self,%map) = @_;

    my %content = $self->content();
    foreach(keys %map) {
        $content{$map{$_}} = $content{$_};
    }
    $self->content(%content);
}

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

  $self->map_fields;

  $self->remap_fields(
        'login'             => 'ExactID',
        'password'          => 'Password',

        'action'            => 'Transaction_Type',

        'amount'            => 'DollarAmount',
        'currency'          => 'Currency',
        'card_number'       => 'Card_Number',
        'track1'            => 'Track1',
        'track2'            => 'Track2',
        'expiration'        => 'Expiry_Date',
        'name'              => 'CardHoldersName',
        'cvv2'              => 'VerificationStr2',

        'authorization'     => 'Authorization_Num',
        'order_number'      => 'Reference_No',

        'zip'               => 'ZipCode',
        'tax'               => 'Tax1Amount',
        'customer_id'       => 'Customer_Ref',
        'customer_ip'       => 'Client_IP',
        'email'             => 'Client_Email',

        #account_type      => 'accountType',

  );

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

  $content{Expiry_Date} =~ s/\///;

  $content{country} ||= 'US';

  $content{VerificationStr1} =
    join('|', map $content{$_}, qw( address zip city state country ));
  $content{VerificationStr1} .= '|'. $content{'phone'}
    if $content{'type'} eq 'ECHECK';

  $content{CVD_Presence_Ind} = '1' if length($content{VerificationStr2});

  $content{'Reference_No'} ||= join('', map $alpha[int(rand(62))], (1..20) );

  #XXX this should be exposed as a standard B:OP field, not just recurring/no
  if ( defined($content{'recurring_billing'})
       && $content{'recurring_billing'} =~ /^[y1]/ ) {
    $content{'Ecommerce_Flag'} = '2';
  } else {
    #$content{'Ecommerce_Flag'} = '1'; 7?  if there's an IP?
  }

  my $base_uri;
  if ( $self->test_transaction ) {
    $base_uri =
      'https://api.demo.globalgatewaye4.firstdata.com/transaction';
  } else {
    $base_uri =
      'https://api.globalgatewaye4.firstdata.com/transaction';
  }

  my $proxy = "$base_uri/v11";

  my @transaction = map { SOAP::Data->name($_)->value( $content{$_} ) }
  grep { defined($content{$_}) }
  (qw(
    ExactID Password Transaction_Type DollarAmount Card_Number Transaction_Tag
    Track1 Track2 Authorization_Num Expiry_Date CardHoldersName
    VerificationStr1 VerificationStr2 CVD_Presence_Ind Reference_No ZipCode
    Tax1Amount Tax1Number Tax2Amount Tax2Number Customer_Ref Reference_3
    Language Client_IP Client_Email User_Name Currency PartialRedemption
    CAVV XID Ecommerce_Flag
 ));
   #TransarmorToken CardType EAN VirtualCard CardCost FraudSuspected
   #CheckNumber CheckType BankAccountNumber BankRoutingNumber CustomerName
   #CustomerIDType CustomerID

  my $wsdl = "$proxy/wsdl";
  $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
  my $client = SOAP::Lite->service($wsdl)->proxy($proxy)->readable(1);
  my $action_prefix = 'http://secure2.e-xact.com/vplug-in/transaction/rpc-enc';
  my $type_prefix = $action_prefix . '/encodedTypes';
  $client->on_action( sub { $action_prefix . '/' . $_[1] } );
  my $source = SOAP::Data->name('SendAndCommitSource')
                         ->value(\@transaction)
                         ->type("$type_prefix:Transaction");
  local $@;
  my $som = eval { $client->call('SendAndCommit', $source) };
  die $@ if $@;
  if ($som->fault) { # indicates a protocol error
    die $som->faultstring;
  }

  $DB::single = 1;
  $som->match('/Envelope/Body/SendAndCommitResponse/SendAndCommitResult');
  my $result = $som->valueof; # hashref of the result properties
  $self->is_success( $result->{Transaction_Approved} );
  $self->authorization( $result->{Authorization_Num} );
  $self->order_number( $result->{SequenceNo} );
  $self->avs_code( $result->{AVS} );
  $self->cvv2_response( $result->{CVV2} );

  if (!$self->is_success) {
    # note spelling of "EXact_Resp_Code"
    if ($result->{EXact_Resp_Code} ne '00') {
      # then there's something wrong with the transaction inputs
      # (invalid card number, malformed amount, attempt to refund a 
      # transaction that didn't happen, etc.)
      $self->error_message($result->{EXact_Message});
      $self->result_code($result->{EXact_Resp_Code});
      $self->failure_status('');
      # not a decline, as the transaction was never really detected
    } else {
      $self->error_message($result->{Bank_Message});
      $self->result_code($result->{Bank_Resp_Code});
      $self->failure_status(
        $failure_status{$result->{Bank_Resp_Code}} || 'declined'
      );
    }
  }
}

1;

__END__

=head1 NAME

Business::OnlinePayment::FirstDataGlobalGateway - First Data Global Gateway e4 backend for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

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

  $tx->content(
      login          => 'TEST88', # ExactID
      password       => 'TEST88', #password 

      type           => 'CC',
      action         => 'Normal Authorization',
      amount         => '1.00',

      first_name     => 'Tofu',
      last_name      => 'Beast',
      address        => '123 Anystreet',
      city           => 'Anywhere',
      state          => 'UT',
      zip            => '84058',

      card_number    => '4111111111111111',
      expiration     => '09/20',
      cvv2           => '124',

      #optional
      customer_ip    => '1.2.3.4',
  );
  $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 CC, Visa, MasterCard, American Express, Discover

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

=head2 (NOT YET) Check

Content required: type, login, action, amount, 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 response error code.

=head2 error_message

Returns the response error number.

=head2 action

The following actions are valid

  Normal Authorization
  Authorization Only
  Post Authorization
  Credit
  Void

=head1 COMPATIBILITY

Business::OnlinePayment::FirstDataGlobalGateway uses the v11 version of the API
at this time.

=head1 AUTHORS

Ivan Kohler <ivan-firstdataglobalgateway@freeside.biz>

=head1 SEE ALSO

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

=cut