don't die when we do not know how to perform the transaction
[Business-OnlinePayment-TransFirsteLink.git] / t / live_card.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use POSIX qw(strftime);
6 use Test::More;
7
8 use Business::OnlinePayment;
9
10 my $runinfo =
11     "to test set environment variables:"
12   . " (required) ELINK_ACCOUNT, ELINK_PASSWORD, ELINK_CARD,"
13   . " ELINK_CVV2, ELINK_EXP, ELINK_CARD_NAME, ELINK_CARD_ADDRESS,"
14   . " ELINK_CARD_CITY, ELINK_CARD_STATE, ELINK_CARD_ZIP, and ELINK_DO_LIVE ";
15
16 plan(
17       ( $ENV{"ELINK_ACCOUNT"} && $ENV{"ELINK_PASSWORD"} &&
18         $ENV{"ELINK_CARD"} && $ENV{"ELINK_CVV2"} &&
19         $ENV{"ELINK_EXP"} && $ENV{"ELINK_CARD_NAME"} &&
20         $ENV{"ELINK_CARD_ADDRESS"} && $ENV{"ELINK_CARD_CITY"} &&
21         $ENV{"ELINK_CARD_STATE"} && $ENV{"ELINK_CARD_ZIP"} &&
22         $ENV{"ELINK_DO_LIVE"}
23       )
24     ? ( tests => 56 )
25     : ( skip_all => $runinfo )
26 );
27
28 my %opts = (
29     "debug"    => 0,
30     "merchantcustservnum" => "8005551212",
31 );
32
33 my %content = (
34     login          => $ENV{"ELINK_ACCOUNT"},
35     password       => $ENV{"ELINK_PASSWORD"},
36     action         => "Normal Authorization",
37     type           => "CC",
38     description    => "Business::OnlinePayment::TransFirsteLink live test",
39     card_number    => $ENV{"ELINK_CARD"},
40     cvv2           => $ENV{"ELINK_CVV2"},
41     expiration     => $ENV{"ELINK_EXP"},
42     amount         => "0.01",
43     invoice_number => "LiveTest",
44     name           => $ENV{"ELINK_CARD_NAME"},
45     address        => $ENV{"ELINK_CARD_ADDRESS"},
46     city           => $ENV{"ELINK_CARD_CITY"},
47     state          => $ENV{"ELINK_CARD_STATE"},
48     zip            => $ENV{"ELINK_CARD_ZIP"},
49 );
50
51 my $voidable;
52 my $credit_amount = 0;
53
54 {    # valid card number test
55     my $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
56     $tx->content(%content);
57     tx_check(
58         $tx,
59         desc          => "valid card_number",
60         is_success    => 1,
61         result_code   => "000",
62         authorization => qr/^\w{6}$/,
63         avs_code      => "Y",      # useless
64         cvv2_response => "M",
65     );
66    $voidable = $tx->order_number if $tx->is_success;
67 }
68
69 {    # invalid card number test
70
71     my $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
72     $tx->content( %content, card_number => "4111111111111112" );
73     tx_check(
74         $tx,
75         desc          => "invalid card_number",
76         is_success    => 0,
77         result_code   => 214,
78         authorization => qr/^$/,
79         avs_code      => '',
80         cvv2_response => '',
81     );
82 }
83
84
85 {    # avs_code() / AVSZIP and AVSADDR tests
86
87     my $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
88
89     $tx->content( %content, "address" => "500 Any street" );
90     tx_check(
91         $tx,
92         desc          => "AVSADDR=N,AVSZIP=Y",
93         is_success    => 1,
94         result_code   => "000",
95         authorization => qr/^\w{6}$/,
96         avs_code      => "Z",
97         cvv2_response => "M",
98     );
99     $credit_amount += $content{amount} if $tx->is_success;
100
101     $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
102     $tx->content( %content, "zip" => "99999" );
103     tx_check(
104         $tx,
105         desc          => "AVSADDR=Y,AVSZIP=N",
106         is_success    => 1,
107         result_code   => "000",
108         authorization => qr/^\w{6}$/,
109         avs_code      => "A",
110         cvv2_response => "M",
111     );
112     $credit_amount += $content{amount} if $tx->is_success;
113
114     $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
115     $tx->content( %content, "address" => "500 Any street", "zip" => "99999" );
116     tx_check(
117         $tx,
118         desc          => "AVSADDR=N,AVSZIP=N",
119         is_success    => 1,
120         result_code   => "000",
121         authorization => qr/^\w{6}$/,
122         avs_code      => "N",
123         cvv2_response => "M",
124     );
125     $credit_amount += $content{amount} if $tx->is_success;
126 }
127
128 {    # cvv2_response() / CVV2MATCH
129
130     my $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
131
132     $tx->content( %content, "cvv2" => $content{cvv2}+1 );
133     tx_check(
134         $tx,
135         desc          => "wrong cvv2",
136         is_success    => 1,
137         result_code   => "000",
138         authorization => qr/^\w{6}$/,
139         avs_code      => "Y",
140         cvv2_response => "N",
141     );
142
143 }
144
145 {    # refund test
146
147     my $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
148     $tx->content( %content, 'action' => "Credit",
149                             'amount' => sprintf("%.2f", $credit_amount),
150                 );
151     tx_check(
152         $tx,
153         desc          => "refund/credit",
154         is_success    => 1,
155         result_code   => "000",
156         authorization => qr/^$/,
157         avs_code      => '',
158         cvv2_response => '',
159     );
160 }
161
162 {    # void test
163
164     my $tx = new Business::OnlinePayment( "TransFirsteLink", %opts );
165     $tx->content( %content, 'action' => "Void",
166                             'order_number' => $voidable
167                 );
168     tx_check(
169         $tx,
170         desc          => "void",
171         is_success    => 1,
172         result_code   => "000",
173         authorization => qr/^$/,
174         avs_code      => '',
175         cvv2_response => '',
176     );
177 }
178
179 sub tx_check {
180     my $tx = shift;
181     my %o  = @_;
182
183     $tx->submit;
184
185     is( $tx->is_success,    $o{is_success},    "$o{desc}: " . tx_info($tx) );
186     is( $tx->result_code,   $o{result_code},   "result_code(): RESULT" );
187     like( $tx->authorization, $o{authorization}, "authorization() / AUTHCODE" );
188     is( $tx->avs_code,  $o{avs_code},  "avs_code() / AVSADDR and AVSZIP" );
189     is( $tx->cvv2_response, $o{cvv2_response}, "cvv2_response() / CVV2MATCH" );
190     is( scalar(@{$tx->junk}), 0, "junk() / JUNK " );
191     like( $tx->order_number, qr/^(\d{14}|)$/, "order_number() / PNREF" );
192 }
193
194 sub tx_info {
195     my $tx = shift;
196
197     no warnings 'uninitialized';
198
199     return (
200         join( "",
201             "is_success(",     $tx->is_success,    ")",
202             " order_number(",  $tx->order_number,  ")",
203             " result_code(",   $tx->result_code,   ")",
204             " auth_info(",     $tx->authorization, ")",
205             " avs_code(",      $tx->avs_code,      ")",
206             " cvv2_response(", $tx->cvv2_response, ")",
207             $tx->junk ? " junk(". join('|', @{$tx->junk}). ")" : '',
208         )
209     );
210 }