documentation and other minor fixes
[Business-OnlinePayment-VirtualNet.git] / VirtualNet.pm
1 package Business::OnlinePayment::VirtualNet;
2
3 use strict;
4 use Carp;
5 use File::CounterFile;
6 use Date::Format;
7 use Business::OnlinePayment;
8 #use Business::CreditCard;
9 use Net::SSLeay qw( make_form post_https );
10 use String::Parity qw(setEvenParity isEvenParity);
11 use String::LRC;
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
13
14 require Exporter;
15
16 @ISA = qw(Exporter AutoLoader Business::OnlinePayment);
17 @EXPORT = qw();
18 @EXPORT_OK = qw();
19 $VERSION = '0.01';
20
21 $DEBUG ||= 0;
22
23 use vars qw( $STX $ETX $FS $ETB );
24 $STX = pack("C", 0x02 );
25 $ETX = pack("C", 0x03 );
26 $FS = pack("C", 0x1c );
27 $ETB = pack("C", 0x17 );
28 #$EOT = pack("C", 0x04 );
29
30 ##should be configurable **FIXME**
31 my $industry_code = '0';
32
33 sub set_defaults {
34     my $self = shift;
35     $self->server('ssl.pgs.wcom.net');
36     $self->port('443');
37     $self->path('/scripts/gateway.dll?Transact');
38
39     $self->build_subs(qw( authorization_source_code returned_ACI
40                           transaction_sequence_num transaction_identifier
41                           validation_code ));
42 }
43
44 sub revmap_fields {
45     my($self,%map) = @_;
46     my %content = $self->content();
47     foreach(keys %map) {
48         $content{$_} = ref($map{$_})
49                          ? ${ $map{$_} }
50                          : $content{$map{$_}};
51     }
52     $self->content(%content);
53 }
54
55 sub get_fields {
56     my($self,@fields) = @_;
57
58     my %content = $self->content();
59     my %new = ();
60     foreach( grep defined $content{$_}, @fields) { $new{$_} = $content{$_}; }
61     return %new;
62 }
63
64 sub submit {
65     my($self) = @_;
66     my %content = $self->content;
67
68     my $action = lc($content{'action'});
69     #die 'eSec only supports "Authorization Only" transactions'
70     #  unless $action eq 'authorization only';
71
72     #my %typemap = (
73     #  "VISA card"                  => 'visa',
74     #  "MasterCard"                 => 'mastercard',
75     #  "Discover card"              => 'discover', #not supported...
76     #  "American Express card"      => 'amex',
77     #  "Diner's Club/Carte Blanche" => 'dinersclub',
78     #  "enRoute"                    => 'enroute', #not supported...
79     #  "JCB"                        => 'jcb',
80     #  "BankCard"                   => 'bankcard',
81     #);
82     #my $cardtype = $self->test_transaction
83     #                 ? 'testcard'
84     #                 : $typemap{cardtype($content{'card_number'})};
85
86    #? what's supported
87    if (  $self->transaction_type() =~
88            /^(cc|visa|mastercard|american express|discover)$/i ) {
89       $self->required_fields(qw/type action amount card_number expiration/);
90     } else {
91       croak("VirtualNet can't handle transaction type: ".
92             $self->transaction_type());
93     }
94
95     #my %content = $self->content;
96     if ( $DEBUG ) {
97       warn " \n";
98       warn "content:$_ => $content{$_}\n" foreach keys %content;
99     }
100
101     my( $message, $mimetype );
102     if ( $action eq 'authorization only' ) {
103       $message = $self->eis1080_request( \%content );
104       $mimetype = 'x-Visa-II/x-auth';
105     } elsif ( $action eq 'post authorization' ) { 
106       $message = $self->eis1081_request( \%content );
107       $mimetype = 'x-Visa-II/x-settle';
108     } elsif ( $action eq 'normal authorization' ) {
109       croak 'Normal Authorization not supported';
110     } elsif ( $action eq 'credit' ) {
111       croak 'Credit not (yet) supported';
112     }
113
114     if ( $DEBUG ) {
115       warn "post_data:$message\n";
116     }
117
118     my $server = $self->server();
119     my $port = $self->port();
120     my $path = $self->path();
121     my($page,$response,%headers) =
122       post_https($server,$port,$path,'',$message, $mimetype );
123
124     #warn "Response: $page";
125
126     if ( $page eq '' ) {
127       die "protocol unsucessful: empty response, status $response\n";
128     }
129
130     if ( $page =~ /^(\d+)\s+\-\s+(\S.*)$/ ) {
131       die "VirtualNet protocol error: $page";
132       #$self->is_success(0);
133       #$self->result_code($1);
134       #$self->error_message($2);
135       #$self->error_message($page);
136     } else {
137       warn "protocol sucessful, decoding VisaNet-II response\n" if $DEBUG;
138
139       isEvenParity($page) or die "VisaNet-II response not even parity";
140
141       $page =~ s/(.)/pack('C', unpack('C',$1) & 0x7f)/ge; #drop parity bits
142
143       #warn $page;
144
145       my %response;
146       if ( $action eq 'authorization only' ) {
147         %response = $self->eis1080_response( $page );
148       } elsif ( $action eq 'post authorization' ) { 
149         %response = $self->eis1081_response( $page );
150       #} elsif ( $action eq 'normal authorization' ) {
151       #  croak 'Normal Authorization not supported';
152       #} elsif ( $action eq 'credit' ) {
153       #  croak 'Credit not (yet) supported';
154       }
155
156 #      $self->is_success($response{is_success});
157 #      $self->result_code($response{result_code});
158 #      $self->error_message($response{error_message});
159 #      $self->authorization($response{authorization});
160
161        for my $field ( qw( is_success result_code error_message authorization
162                            authorization_source_code returned_ACI
163                            transaction_identifier validation_code
164                            transaction_sequence_num ) ) {
165          $self->$field($response{$field});
166        }
167
168     }
169
170 }
171
172 sub testhost {
173   my $self = shift;
174
175   my $content = 'D4.999995';
176   #my $content = 'D2.999995';
177   #my $content = 'D0.999995';
178   my $message = 
179     $STX.
180     $content.
181     $ETX.
182     lrc($content.$ETX)
183   ;
184   $message = setEvenParity $message;
185   
186   if ( $DEBUG ) {
187     warn "post_data: $message\n";
188     warn "post_data hex dump: ". join(" ", unpack("H*", $message) ). "\n";
189   }
190
191   my $server = $self->server();
192   my $port = $self->port();
193   my $path = $self->path();
194   my($page,$response,%headers) =
195     post_https($server,$port,$path,'',$message, 'x-Visa-II/x-auth');
196
197   #warn "Response: $page";
198
199   if ( $page =~ /^(\d+)\s+\-\s+(\S.*)$/ ) {
200     die "VirtualNet protocol error: $page";
201     #$self->is_success(0);
202     #$self->result_code($1);
203     #$self->error_message($2);
204     #$self->error_message($page);
205   } else {
206     warn "protocol sucessful, not decoding VisaNet-II response" if $DEBUG;
207     $self->is_success(1);
208   }
209
210 }
211
212 sub eis1080_request {
213   my( $self, $param ) = @_;
214   # card_number expiration address zip amount
215
216   #D-Format    Authorization Request Message  (Non-Set Electronic Commerce) 
217
218 #  my $zip = $param->{zip};
219 #  $zip =~ s/\D//g;
220 #  $zip = substr("$zip         ",0,9); #Left-justified/Space-filled
221
222   $param->{expiration} =~ /^(\d{1,2})\D+(\d{2})?(\d{2})$/
223     or croak "unparsable expiration ". $param->{expiration};
224   my ($month, $year) = ( $1, $3 );
225   $month = "0$month" if length($month) < 2;
226   my $exp= "$month$year";
227
228   #my $zip = $param->{zip};
229   #$zip =~ s/\D//g;
230   #$zip = substr("$zip         ",0,9);
231
232   my $amount = $param->{amount};
233   $amount =~ s/\.//;
234
235   my $zip = substr( $self->zip. "         ", 0, 9 );
236
237   my $seq_file = $self->seq_file;
238   my $counter = File::CounterFile->new($seq_file, '0001')
239     or die "can't create sequence file $seq_file: $!";
240
241   $counter->lock();
242   my $seq = substr('0000'.$counter->inc, -4);
243   $seq = substr('0000'.$counter->inc, -4) if $seq eq '0000';
244   $counter->unlock();
245
246                                 # Byte Length Field: Content
247
248   my $content = 'D4.';            # 1     1    Record format: D
249                                   # 2     1    Application Type: 4=Interleaved
250                                   # 3     1    Message Delimiter: .
251   $content .= $self->bin;         # 4-9   6    Acquirer BIN
252   $content .= $self->merchant_id; # 10-21 12   Merchant Number
253   $content .= $self->store;       # 22-25 4    Store Number
254   $content .= $self->terminal;    # 26-29 4    Terminal Number
255   $content .= 'C';                # 30    1    Device Code: C="P.C."
256   #$content .= 'M';                # 30    1    Device Code: M="Main Frame"
257   $content .= $industry_code;      # 31    1    Industry Code
258   $content .= '840';              # 32-34 3    Currency Code: 840=U.S. Dollars
259   $content .= '840';              # 35-37 3    Country Code: 840=United States
260   $content .= $zip;               # 38-46 9    (Merchant) City Code(Zip);
261   $content .= '00';               # 47-48 2    Language Indicator: 00=English
262                                   # ***FIXME***
263   $content .= '705';              # 49-51 3    Time Zone Differential: 705=EST
264   $content .= $self->mcc;         # 52-55 4    Metchant Category Code: 5999
265   $content .= 'N';                # 56    1    Requested ACI (Authorization
266                                   #            Characteristics Indicator):
267                                   #            N=Device is not CPS capable
268   $content .= $seq;               # 57-60 4    Tran Sequence Number
269   $content .= '56';               # 61-62 2    Auth Transaction Code:
270                                   #            56=Card Not Present
271   $content .= 'N';                # 63    1    Cardholder ID Code: N=AVS
272                                   #            (Address Verification Data or
273                                   #            CPS/Card Not Present or
274                                   #            Electronic Commerce)
275   $content .= '@';                # 64    1    Account Data Source:
276                                   #            @=No Cardreader
277
278   die "content-length should be 64!" unless length($content) == 64;
279
280   # - 5-76 Customer Data Field: Acct#<FS>ExpDate<FS>
281   $content .= $param->{card_number}. $FS. $exp. $FS;
282
283   # - 1 Field Separator
284   $content .= $FS;
285
286   # - 0-29 Address Verification Data
287   $content .= substr($param->{address}, 0, 23)." ". substr($param->{zip}, 0, 5);
288
289   $content .= $FS; # - 1 Field Separator
290   $content .= $FS; # - 1 Field Separator
291
292   $content .= $amount; # - 1-12 Transaction Amount
293
294   $content .= $FS; # - 1 Field Separator
295   $content .= $FS; # - 1 Field Separator
296   $content .= $FS; # - 1 Field Separator
297
298   # - 25 Merchant Name
299   $content .= substr($self->merchant_name.(' 'x25),0,25);
300
301   # - 13 Merchant City
302   $content .= substr($self->merchant_city.(' 'x13),0,13);
303
304   # - 2 Merchant State
305   $content .= substr($self->merchant_state.('X'x2),0,2);
306
307   $content .= $FS; # - 1 Field Separator
308   $content .= $FS; # - 1 Field Separator
309   $content .= $FS; # - 1 Field Separator
310
311   #-----
312
313   $content .= '014'; # - 3 Group III Version Number:
314                      #014=MOTO/Electronic Commerce
315
316   $content .= '7'; # - 1 MOTO/Electronic Com. Ind: 7= Non-Authenticated
317                    # Security transaction, such as a channel-encrypted
318                    # transaction (e.g., ssl, DES or RSA)
319
320
321   my $message = 
322     $STX.
323     $content.
324     $ETX.
325     lrc($content.$ETX)
326   ;
327
328   $message = setEvenParity $message;
329
330   $message;
331 }
332
333 sub eis1080_response {
334   my( $self, $response) = @_;
335   my %response;
336
337   $response =~ /^$STX(.{67})([\w ]{0,15})$FS([\w ]{0,4})$FS.*$ETX(.)$/
338     or die "can't decode (eis1080) response: $response\n". join(' ', map { sprintf("%x", unpack('C',$_)) } split('', $response) );
339   ( $response{transaction_identifier},
340     $response{validation_code},
341     my $lrc
342   ) = ($2, $3, $4);
343
344   warn "$response\n".
345        join(' ', map { sprintf("%x", unpack('C',$_)) } split('', $response) ).
346        "\n"
347     if $DEBUG;
348
349   (
350     $response{record_format},
351     $response{application_type},
352     $response{message_delimiter},
353     $response{returned_ACI},
354     $response{store_number},
355     $response{terminal_number},
356     $response{authorization_source_code},
357     $response{transaction_sequence_num},
358     $response{response_code},
359     $response{approval_code},
360     $response{local_transaction_date},
361     $response{local_transaction_time},
362     $response{auth_response_text},
363     $response{AVS_result_code},
364     $response{retrieval_reference_num},
365     $response{market_specific_data_id},
366   ) = unpack "AAAAA4A4A1A4A2A6A6A6A16A1A12A1", $1;
367
368   if ( $response{record_format} ne "E" ) {
369     die "unknown response record_format $response{record_format}";
370   }
371   if ( $response{application_type} ne "4" ) {
372     die "unknown response record_format $response{application_type}";
373   }
374   if ( $response{message_delimiter} ne "." ) {
375     die "unknown response record_format $response{message_delimiter}";
376   }
377
378   $response{is_success} = $response{response_code} =~ /^(00|85)$/;
379   $response{result_code} = $response{response_code};
380   $response{error_message} = $response{auth_response_text};
381   $response{authorization} = $response{approval_code};
382   #$response{returned_ACI} = $response{returned_ACI};
383   #$response{authorization_source_code} = $response{authorization_source_code};
384   #$response{transaction_sequence_num} = $response{transaction_sequence_num};
385
386   %response;
387 }
388
389 sub eis1081_request {
390   my( $self, $param ) = @_;
391
392   my $batchnum_file = $self->batchnum_file;
393   my $counter = File::CounterFile->new($batchnum_file, '001')
394     or die "can't create batchnumuence file $batchnum_file: $!";
395
396   $counter->lock();
397   my $batchnum = substr('000'.$counter->inc, -3);
398   $batchnum = substr('000'.$counter->inc, -3) if $batchnum eq '000';
399   $counter->unlock();
400
401   #K-Format Header Record (Base Group)
402 #Byte Length Frmt Field description Content Section
403                                   # Byte Length Field: Content (section)
404   my $header = 'K1.ZH@@@@';   # 1     1  A/N Record Format: K (4.154)
405                               # 2     1  NUM Application Type: 1=Single Batch
406                               #                                          (4.10)
407                               # 3     1  A/N Message Delimiter: . (4.123)
408                               # 4     1  A/N X.25 Routing ID: Z (4.226)
409                               # 5-9   5  A/N Record Type: H@@@@ (4.155)
410   $header .= $self->bin;      # 10-15 6  NUM Acquirer BIN  (4.2)
411   $header .= $self->agent;    # 16-21 6  NUM Agent Bank Number (4.5)
412   $header .= $self->can('chain') ? $self->chain : '000000';
413                               # 22-27 6  NUM Agent Chain Number (4.6)
414   $header .= $self->merchant_id; 
415                               # 28-39 12 NUM Merchant Number (4.121)
416   $header .= $self->store;    # 40-43 4  NUM Store Number (4.187)
417   $header .= $self->terminal; # 44-47 4  NUM Terminal Number 9911 (4.195)
418   $header .= 'C';             # 48    1  A/N Device Code: C="P.C." (4.62)
419   #$header .= 'M';            # 48    1  A/N Device Code M="Main Frame" (4.62)
420   $header .= $industry_code;  # 49    1  A/N Industry Code (4.94)
421   $header .= '840';           # 50-52 3  NUM Currency Code (4.52)
422   $header .= '00';            # 53-54 2  NUM Language Indicator: 00=English
423                               #                                         (4.104)
424                               # ***FIXME***
425   $header .= '705';           # 55-57 3  NUM Time Zone Differential (4.200)
426
427   my $mmdd = substr(time2str('0%m%d',time),-4);
428   $header .= $mmdd;           # 58-61 4  NUM Batch Transmission Date MMDD (4.22)
429
430   $header .= $batchnum;       # 62-64 3  NUM Batch Number 001 - 999 (4.18)
431   $header .= '0';             # 65    1  NUM Blocking Indicator 0=Not Blocked
432                               #                                          (4.23)
433
434   die "header length should be 65!" unless length($header) == 65;
435
436   my $message = 
437     $STX.
438     $header.
439     $ETB.
440     lrc($header.$ETB)
441   ;
442
443   my $zip = substr( $self->zip. "         ", 0, 9 );
444
445   #K-Format Parameter Record (Base Group)
446 #Byte Length Frmt Field Description Content Section
447
448   my $parameter = 'K1.ZP@@@@'; # 1   1 A/N Record Format: K (4.154)
449                                # 2   1 NUM Application Type: 1=Single Batch
450                                #                                         (4.10)
451                                # 3   1 A/N Message Delimiter: . (4.123)
452                                # 4   1 A/N X.25 Routing ID: Z (4.226)
453                                # 5-9 5 A/N Record Type: P@@@@ (4.155)
454   $parameter .= '840';         # 10-12 3 NUM Country Code 840 4.47
455   $parameter .= $zip;          # 13-21 9 A/N City Code
456                                #    Left-Justified/Space-Filled 4.43
457   $parameter .= $self->mcc;    # 22-25 4 NUM Merchant Category Code (4.116)
458
459   # 26-50 25 A/N Merchant Name Left-Justified/Space-Filled (4.27.1)
460   $parameter .= substr($self->merchant_name.(' 'x25),0,25);
461
462   #51-63 13 A/N Merchant City Left-Justified/Space-Filled (4.27.2)
463   $parameter .= substr($self->merchant_city.(' 'x13),0,13);
464
465   # 64-65 2 A/N Merchant State (4.27.3)
466   $parameter .= substr($self->merchant_state.('X'x2),0,2);
467
468   $parameter .= '00001'; # 66-70 5 A/N Merchant Location Number 00001 4.120
469
470   $parameter .= $self->v; # 71-78 8 NUM Terminal ID Number 00000001 4.194
471
472   die "parameter length should be 78 (is ". length($parameter). ")!"
473     unless length($parameter) == 78;
474
475   $message .= 
476     $STX.
477     $parameter.
478     $ETB.
479     lrc($parameter.$ETB)
480   ;
481
482 # K-Format Detail Record (Electronic Commerce)
483 #Byte Size Frmt Field Description Content Section
484 #D@@'D'  `
485   my $detail = 'K1.ZD@@`D';  # 1   1 A/N Record Format: K (4.154)
486                               # 2   1 NUM Application Type 1=Single Batch
487                               #                                          (4.10)
488                               # 3   1 A/N Message Delimiter: . (4.123)
489                               # 4   1 A/N X.25 Routing ID: Z (4.226)
490                               # 5-9 5 A/N Record Type: D@@`D (4.155)
491
492   $detail .= '56';               # 10-11 2 A/N Transaction Code:
493                                  #             56 = Card Not Present
494                                  #             (4.205)
495   $detail .= 'N';                # 12 1 A/N Cardholder Identification Code N 4.32
496                                  #            (Address Verification Data or
497                                  #            CPS/Card Not Present or
498                                  #            Electronic Commerce)
499   $detail .= '@';                # 13 1 A/N Account Data Source Code @ = No Cardreader 4.1
500                                  #            @=No Cardreader
501
502   #14-35 22 A/N Cardholder Account Number Left-Justified/Space-Filled 4.30
503   $detail .= substr( $param->{card_number}.'                      ', 0, 22 );
504
505   $detail .= 'N';                # 36    1    Requested ACI (Authorization
506                                  #            Characteristics Indicator):
507                                  #            N (4.163)
508
509   # 37 1 A/N Returned ACI (4.168)
510   $detail .= $param->{returned_ACI} || 'N';
511
512   # *** 38 1 A/N Authorization Source Code (4.13)
513   $detail .= $param->{authorization_source_code} || '6';
514
515   # 39-42 4 NUM Transaction Sequence Number Right-Justified/Zero-Filled (4.207)
516   die "missing transaction_sequence_num"
517     unless $param->{transaction_sequence_num};
518   $detail .= $param->{transaction_sequence_num};
519   
520   $detail .= '00'; # ###FIXME (from auth)*** 43-44 2 A/N Response Code 4.164
521   
522   # 45-50 6 A/N Authorization Code Left-Justified/Space-Filled (4.12)
523   $detail .= $param->{authorization};
524
525   my $time = time;
526
527   my $mmdd = substr(time2str('0%m%d',$time),-4);
528   $detail .= $mmdd; # 51-54 4 NUM Local Transaction Date MMDD (4.113)
529
530   my $hhmmss = time2str('%H%M%S',$time);
531   $detail .= $hhmmss; # 55-60 6 NUM Local Transaction Time HHMMSS (4.114)
532   
533   $detail .= '0'; #***FIXME (from auth) 61 1 A/N AVS Result Code 4.3
534
535   # 62-76 15 A/N Transaction Identifier Left-Justified/Space-Filled 4.206
536   $detail .= substr($param->{transaction_identifier}. (' 'x15), 0, 15);
537
538   # 77-80 4 A/N Validation Code 4.218
539   $detail .= substr($param->{validation_code}.'    ', 0, 4);
540   
541   $detail .= ' '; # 81 1 A/N Void Indicator <SPACE> = Not Voided 4.224
542   $detail .= '00'; # 82-83 2 NUM Transaction Status Code 00 4.208
543   $detail .= '0'; # 84 1 A/N Reimbursement Attribute 0 4.157
544
545   my $amount = $param->{amount};
546   $amount =~ s/\.//;
547   $amount = substr('000000000000'.$amount,-12);
548
549   $detail .= $amount; # 85-96 12 NUM Settlement Amount
550                       # Right-Justified/Zero-Filled 4.175
551
552   $detail .= $amount; # 97-108 12 NUM Authorized Amount
553                       # Right-Justified/Zero-Filled 4.14
554
555   $detail .= $amount; # 109-120 12 NUM Total Authorized Amount
556                       # Right-Justified/Zero-Filled 4.201
557
558 #  $detail .= '1'; # 121 1 A/N Purchase Identifier Format Code 1 4.150
559 #
560 #  # 122-146 25 A/N Purchase Identifier Left-Justified/Space-Filled 4.149
561 #  $detail .= 'Internet Services        ';
562 #             #1234567890123456789012345
563
564   $detail .= '0'; # 121 1 A/N Purchase Identifier Format Code 1 4.150
565
566   # 122-146 25 A/N Purchase Identifier Left-Justified/Space-Filled 4.149
567   $detail .= '                         ';
568              #1234567890123456789012345
569
570   $detail .= '01'; # ??? 147-148 2 NUM Multiple Clearing Sequence Number 4.129
571   $detail .= '01'; # ???  149-150 2 NUM Multiple Clearing Sequence Count 1.128
572   $detail .= '7'; # 151 1 A/N MOTO/Electronic Commerce Indicator 7 = Channel Encrypted 4.127
573
574   die "detail length should be 151 (is ". length($detail). ")"
575     unless length($detail) == 151;
576
577   $message .= 
578     $STX.
579     $detail.
580     $ETB.
581     lrc($detail.$ETB)
582   ;
583
584 # K-Format     Trailer Record
585 #Byte    Length    Frmt    Field Description    Content    Section
586
587   my $trailer = 'K1.ZT@@@@';
588 #1    1    A/N    Record Format    K    4.154
589 #2    1    NUM    Application Type    1=Single 3=Multiple Batch    4.10
590 #3    1    A/N    Message Delimiter    .    4.123
591 #4    1    A/N    X.25 Routing ID    Z    4.226
592 #5-9    5    A/N    Record Type    T@@@@    4.155
593
594   $trailer .= $mmdd;           # 10-13  4 NUM Batch Transmission Date MMDD 4.22
595   $trailer .= $batchnum;       # 14-16  3 NUM Batch Number    001 - 999    4.18
596   $trailer .= '000000004';        # 17-25  9 NUM Batch Record Count
597                                   #Right-Justified/Zero-Filled    4.19
598   $trailer .= '0000'.$amount;     # 26-41 16 NUM Batch Hashing Total
599                                   #Purchases + Returns    4.16
600   $trailer .= '0000000000000000'; # 42-57 16 NUM Cashback Total 4.38
601   $trailer .= '0000'.$amount;     # 58-73 16 NUM Batch Net Deposit
602                                   # Purchases - Returns    4.17
603
604   die "trailer length should be 73!" unless length($trailer) == 73;
605
606   $message .= 
607     $STX.
608     $trailer.
609     $ETX.
610     lrc($trailer.$ETX)
611   ;
612
613   ####
614
615   $message = setEvenParity $message;
616
617   $message;
618
619 }
620
621 sub eis1081_response {
622   my( $self, $response ) = @_;
623   my %response;
624
625   $response =~ /^$STX(.{41})(.*)$ETX(.)$/
626     or die "can't decode (eis1081) response: $response";
627   my $remainder = $2;
628   my $lrc = $3;
629
630   (
631     $response{record_format},
632     $response{application_type},
633     $response{message_delimiter},
634     $response{x25_routing_id},
635     $response{record_type},
636     $response{batch_record_count},
637     $response{batch_net_deposit},
638     $response{batch_response_code},
639     $response{filler},
640     $response{batch_number},
641   ) = unpack "AAAAA5A9A16A2A2A3", $1;
642   warn "$1\n" if $DEBUG;
643
644   if ( $response{record_format} ne "K" ) {
645     die "unknown response record_format $response{record_format}";
646   }
647   if ( $response{application_type} ne "1" ) {
648     die "unknown response record_format $response{application_type}";
649   }
650   if ( $response{message_delimiter} ne "." ) {
651     die "unknown response record_format $response{message_delimiter}";
652   }
653
654   if ( $response{is_success} = $response{batch_response_code} eq 'GB' ) {
655     $response{result_code} = $response{batch_response_code};
656     $response{error_message} = '';
657   } elsif ( $response{batch_response_code} eq 'RB' ) {
658     $response{result_code} = $response{batch_response_code};
659     #$remainder =~ /^(.)(.{4})(.)(..)(.{32})$/
660     $remainder =~ /^(.)(.{4})(.)(..)(.*)$/
661       or die "can't decode (eis1081) RB response (41+ ". length($remainder).
662              "): $remainder";
663     my( $error_type, $error_record_sequence_number, $error_record_type,
664         $error_data_field_number, $error_data ) = ( $1, $2, $3, $4, $5 );
665     my %error_type = (
666       B => 'Blocked Terminal',
667       C => 'Card Type Error',
668       D => 'Device Error',
669       E => 'Error in Batch',
670       S => 'Sequence Error',
671       T => 'Transmission Error',
672       U => 'Unknown Error',
673       V => 'Routing Error',
674     );
675     my %error_record_type = (
676       H => 'Header Record',
677       P => 'Parameter Record',
678       D => 'Detail Record',
679       T => 'Trailer Record',
680     );
681     $response{error_message} = 'Auth sucessful but capture rejected: '.
682       $error_type{$error_type}. ' in '. $error_record_type{$error_record_type}.
683       ' #'. $error_record_sequence_number. ' field #'. $error_data_field_number.
684       ': '. $error_data;
685   } else {
686     $response{result_code} = $response{batch_response_code};
687     $response{error_message} = $remainder;
688   }
689
690   %response;
691 }
692
693 1;
694
695 __END__
696
697 =head1 NAME
698
699 Business::OnlinePayment::VirtualNet - Vital VirtualNet backend for Business::OnlinePayment
700
701 =head1 SYNOPSIS
702
703   use Business::OnlinePayment;
704
705   my $tx = new Business::OnlinePayment("VirtualNet",
706     'merchant_id' => '999999999911',
707     'store'       => '0011',
708     'terminal'    => '9911',
709     'mcc'         => '5999', #merchant category code
710     'bin'         => '999995', #acquirer BIN (Bank Identification Number)
711     'zip'         => '543211420', #merchant zip (US) or assigned city code
712
713     'agent'       => '000000', #agent bank
714     'v'           => '00000001',
715
716     'merchant_name'  => 'Internet Service Provider', #25 char max
717     'merchant_city'  => 'Gloucester', #13 char max
718     'merchant_state' => 'VA', #2 char
719
720     'seq_file'      => '/tmp/bop-virtualnet-sequence',
721     'batchnum_file' => '/tmp/bop-virtualnet-batchnum', # :/  0-999 in 5 days
722
723   );
724   $tx->content(
725       type           => 'CC',
726       login          => 'test',
727       action         => 'Authorization Only',
728       description    => 'Business::OnlinePayment test',
729       amount         => '49.95',
730       invoice_number => '100100',
731       name           => 'Tofu Beast',
732       card_number    => '4111111111111111',
733       expiration     => '09/03',
734   );
735   $tx->submit();
736
737   if( $tx->is_success() ) {
738       print "Card authorized successfully: ".$tx->authorization."\n";
739   } else {
740       print "Error: ".$tx->error_message."\n";
741   }
742
743  if( $tx->is_success() ) {
744
745       my $capture = new Business::OnlinePayment("VirtualNet",
746         'agent'       => '000001',
747         'chain'       => '000000', #optional?
748         'v'           => '00000001',
749
750         'merchant_id' => '999999999911',
751         'store'       => '0011',
752         'terminal'    => '9911',
753         'mcc'         => '5999', #merchant category code
754         'bin'         => '999995', #acquirer BIN (Bank Identification Number)
755       );
756
757       $capture->content(
758         type           => 'CC',
759         action         => 'Post Authorization',
760         amount         => '49.95',
761         card_number    => '4111111111111111',
762         expiration     => '09/03',
763         authorization             => $tx->authorization,
764         authorization_source_code => $tx->authorization_source_code,
765         returned_ACI              => $tx->returned_ACI,
766         transaction_identifier    => $tx->transaction_identifier,
767         validation_code           => $tx->validation_code,
768         transaction_sequence_num  => $tx->transaction_sequence_num,
769         #description    => 'Business::OnlinePayment::BankOfAmerica visa test',
770
771           action         => 'Post Authorization',
772       #    order_number   => $ordernum,
773       #    amount         => '0.01',
774       #    authorization  => $auth,
775       #    description    => 'Business::OnlinePayment::BankOfAmerica visa test',
776       );
777
778       $capture->submit();
779
780       if( $capture->is_success() ) { 
781           print "Card captured successfully\n";
782       } else {
783           print "Error: ".$capture->error_message."\n";
784       }
785
786   }
787
788 =head1 DESCRIPTION
789
790 For detailed information see L<Business::OnlinePayment>.
791
792 =head1 NOTE
793
794 =head1 COMPATIBILITY
795
796 This module implements the interface documented at
797 http://www.vitalps.com/sections/int/int_Interfacespecs.html
798
799 Specifically, start with
800 http://www.vitalps.com/pdfs_specs/VirtualNet%020Specification%0200011.pdf
801 and then http://www.vitalps.com/pdfs_specs/EIS%0201080%020v6_4_1.pdf and
802 http://www.vitalps.com/pdfs_specs/EIS_1081_v_6_4.pdf and maybe even
803 http://www.vitalps.com/pdfs_specs/EIS%0201051.pdf and
804 http://www.vitalps.com/pdfs_specs/EIS%0201052.pdf
805
806 =head1 AUTHOR
807
808 Ivan Kohler <ivan-virtualnet@420.am>
809
810 =head1 SEE ALSO
811
812 perl(1). L<Business::OnlinePayment>.
813
814 =cut
815