summaryrefslogtreecommitdiff
path: root/IPaymentTPG.pm
blob: 88c6b6d88e58c4a83aab7ec3e0a7ff157bea83b8 (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
package Business::OnlinePayment::IPaymentTPG;

use strict;
use vars qw($VERSION @ISA);
use AutoLoader;
use IO::Socket::INET;
use Business::OnlinePayment;
#use Crypt::Blowfish;

@ISA = qw(Business::OnlinePayment);

$VERSION = '0.01';

sub set_defaults {
  my $self = shift;
  $self->server('205.137.49.250');
  $self->port('8003');

  $self->build_subs('order_number');

}

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

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

    #ACTION MAP
    my %actions = ('normal authorization' => 'B', #both
                   'authorization only'   => 'O', #open
                   'credit'               => 'C', #credit
                   'post authorization'   => 'S', #sell
                  );
    $content{'action'} = $actions{lc($content{'action'})} || $content{'action'};

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


sub build_subs {
    my $self = shift;
    foreach(@_) {
        #no warnings; #not 5.005
        local($^W)=0;
        eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
    }
}

sub get_fields {
    my($self,@fields) = @_;

    my %content = $self->content();
    my %new = ();
    foreach( grep defined $content{$_}, @fields) { $new{$_} = $content{$_}; }
    return %new;
}


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

    $self->map_fields();

#    my %content = $self->content;

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

    my %d = $self->get_fields(qw(
      login password action card_number expiration amount
      phone invoice_number zip email
    ));

    $d{expiration} =~ s/\D//g;

    substr($d{expiration},2,2,'') if length($d{expiration}) == 6;

    $d{amount} = sprintf("%.2f",$d{amount});
    $d{amount} =~ s/\.//;

    my($address1,$address2);
    {
      local($^W)=0;
      $d{phone} =~ s/\D//g;

      $d{zip} =~ s/\W//g;

      ($address1,$address2) = split(/,\s*/, $d{address}, 2); #hmm

    }

    #66
    my $req4 = pack("A4A10A20A1A16A4A10A1",
      'REQ4',          #SEGMENT_ID
      $d{login},       #ID_CODE
      $d{password},    #PASSWORD
      $d{action},      #TYPE
      $d{card_number}, #CC
      $d{expiration},  #EXP
      $d{amount},      #AMOUNT
      1,               #OPT
    );

    #372
    my $opt1;
    {
      local($^W)=0;
      $opt1 = pack("A4A5A10A10A9A3A40A40A40A40A25A2A25A40A40A4A10A25",
        'OPT1',             #SEGMENT_ID
        '30000',            #TIMEOUT
        $d{phone},          #ANI
        $d{invoice_number}, #SEQNO
        $d{zip},            #ZIP
        '',                 #CVV2
        '',                 #DESCRIPTOR
        $d{email},          #EMAIL
        $address1,          #ADDRESS1
        $address2,          #ADDRESS2
        $d{city},           #CITY
        $d{state},          #STATE
        $d{country},        #COUNTRY
        $d{first_name},     #FIRST_NAME
        $d{last_name},      #LAST_NAME
        'US',               #CURRENCY
        $d{order_number},   #TRACKING_NO
        'CRYPT_KEY',        #CRYPT_KEY
      );
    }

    #send to server
    my $sock = new IO::Socket::INET
      PeerAddr => $self->server,
      PeerPort => $self->port,
      Proto    => 'tcp',
      Timeout  => 10,
    or die "can't connect to ". $self->server. ":". $self->port. " : $!";

    print $sock "000438$req4$opt1"; ; #66 + 372, only one record for now

    my $message_length;
    my $num = read $sock, $message_length, 6;
    die "expected 6 byte MESSAGE_LENGTH response from server, got $num: $!"
      unless $num == 6;

    #34 - only one record for now
    die "expected MESSAGE_LENGTH 34 from server, got $message_length"
      unless $message_length == 34;

    my $res1;
    my $read_length = read $sock, $res1, $message_length;
    die "expected 34 byte RES1 message from server, got $read_length"
      unless $read_length == 34;

    my( $segment_id, $response, $tracking_no, $seqno ) =
      unpack("A4A10A10A10", $res1);

    die "expected SEGMENT_ID=RES1 from server, got $segment_id"
      unless $segment_id eq 'RES1';

    my($result,$mapp,$avs,$card,$ani,$new,$transaction,$cvv2,$ssv,$bin_blocker)=
      split('',$response);

    if ( $result =~ /^[127]$/ ) {
      $self->is_success(1);
      $self->result_code($response);
      $self->authorization($tracking_no);
      $self->order_number($tracking_no);
      #$seq_no ?
    } else {
      $self->is_success(0);
      $self->result_code($response);

      my %resultmap = (
        '0' => 'Not available or System Down',
        '1' => 'Optn Approved',
        '2' => 'Sell Approved', 
        '3' => 'Open Declined', 
        '4' => 'Sell Decliend', 
        '5' => 'Invalid Message', 
        '6' => 'Closed Merchant Account', 
        '7' => 'Credit Approved', 
        '8' => 'Credit Declined',
      );

      my %cardmap = (
        '0' => 'N/A',
        '1' => 'Good',
        #'2' => 'Card Limit',
        '2' => 'Card Over Limit',
        #'3' => 'Exp',
        '3' => 'Expired',
        '4' => 'Card Blocked',
        #'5' => 'MOD-10',
        '5' => 'Invalid credit card number',
        '6' => 'Test card',
        '7' => 'Chargeback',
      );

      my $error = $resultmap{$result};
      $error .= ": $cardmap{$card}" if $card;

      $self->error_message($error);
    }

}

1;
__END__

=head1 NAME

Business::OnlinePayment::IPaymentTPG - iPayment TPG backend for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

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

  $tx->content(
      type           => 'VISA',
      action         => 'Normal Authorization',
      description    => 'Business::OnlinePayment test',
      amount         => '49.95',
      invoice_number => '100100',
      customer_id    => 'jsk',
      first_name     => 'Tofu',
      last_name      => 'Beast',
      address        => '123 Anystreet',
      city           => 'Anywhere',
      state          => 'UT',
      zip            => '84058',
      email          => 'ivan-ipaymenttpg@420.am',
      card_number    => '4007000000027',
      expiration     => '09/02',
  );
  $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/Diners Club

=head1 DESCRIPTION

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

=head1 NOTE

Unlike Business::OnlinePayment or pre-3.0 verisons of
Business::OnlinePayment::AuthorizeNet, 3.1 requires separate first_name and
last_name fields.

=head1 COMPATIBILITY

This module implements the interface documented in
https://tpg1.ipaymenttechnologies.com/docs/

=head1 BUGS

The documentation lacks specifics on the encryption implementation.

=head1 AUTHOR

Ivan Kohler <ivan-ipaymenttpg@420.am>

=head1 SEE ALSO

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

=cut