force country to ISO-3166-alpha-3
[Business-OnlinePayment-IPPay.git] / t / card.t
1 #!/usr/bin/perl -w
2
3 use Test::More;
4
5 my($login, $password, @opts) = ('TESTMERCHANT', '',
6                                 'default_Origin' => 'RECURRING' );
7 plan tests => 43;
8   
9 use_ok 'Business::OnlinePayment';
10
11 my %content = (
12     type           => 'VISA',
13     login          => $login,
14     password       => $password,
15     action         => 'Normal Authorization',
16     description    => 'Business::OnlinePayment visa test',
17 #    card_number    => '4007000000027',
18     card_number    => '4111111111111111',
19     cvv2           => '123',
20     expiration     => expiration_date(),
21     amount         => '49.95',
22     name           => 'Tofu Beast',
23     email          => 'ippay@weasellips.com',
24     address        => '123 Anystreet',
25     city           => 'Anywhere',
26     state          => 'UT',
27     zip            => '84058',
28     country        => 'US',      # will be forced to USA
29     customer_id    => 'tfb',
30 );
31
32 my $voidable;
33 my $voidable_auth;
34 my $voidable_amount = 0;
35
36 # valid card number test
37 {
38   my $tx = Business::OnlinePayment->new("IPPay", @opts);
39   $tx->content(%content);
40   tx_check(
41     $tx,
42     desc          => "valid card_number",
43     is_success    => 1,
44     result_code   => '000',
45     error_message => 'APPROVED',
46     authorization => qr/TEST\d{2}/,
47     avs_code      => 'U',          # so rather pointless :\
48     cvv2_response => 'P',          # ...
49   );
50   $voidable = $tx->order_number if $tx->is_success;
51   $voidable_auth = $tx->authorization if $tx->is_success;
52   $voidable_amount = $content{amount} if $tx->is_success;
53 }
54
55 # invalid card number test
56 {
57   my $tx = Business::OnlinePayment->new("IPPay", @opts);
58   $tx->content(%content, card_number => "4111111111111112" );
59   tx_check(
60     $tx,
61     desc          => "invalid card_number",
62     is_success    => 0,
63     result_code   => '912',
64     error_message => 'INVALID CARD NUMBER',
65     authorization => qr/^$/,
66     avs_code      => '',           # so rather pointless :\
67     cvv2_response => '',           # ...
68   );
69 }
70
71 # authorization only test
72 {
73   my $tx = Business::OnlinePayment->new("IPPay", @opts);
74   $tx->content(%content, action => 'authorization only',  amount => '3.00' );
75   tx_check(
76     $tx,
77     desc          => "authorization only",
78     is_success    => 1,
79     result_code   => '000',
80     error_message => 'APPROVED',
81     authorization => qr/TEST\d{2}/,
82     avs_code      => 'U',          # so rather pointless :\
83     cvv2_response => 'P',          # ...
84   );
85   $postable = $tx->order_number if $tx->is_success;
86   $postable_auth = $tx->authorization if $tx->is_success;
87   $postable_amount = $content{amount} if $tx->is_success;
88 }
89
90 # post authorization test
91 SKIP: {
92   my $tx = new Business::OnlinePayment( "IPPay", %opts );
93   $tx->content( %content, 'action'       => "post authorization", 
94                           'amount'       => $postable_amount,    # not required
95                           'order_number' => $postable,
96               );
97   tx_check(
98     $tx,
99     desc          => "post authorization",
100     is_success    => 1,
101     result_code   => '000',
102     error_message => 'APPROVED',
103     authorization => qr/^$postable_auth$/,
104     avs_code      => '',
105     cvv2_response => '',
106     );
107 }
108
109 # void test
110 SKIP: {
111   my $tx = new Business::OnlinePayment( "IPPay", %opts );
112   $tx->content( %content, 'action' => "Void",
113                           'order_number' => $voidable,
114                           'authorization' => $voidable_auth,
115               );
116   tx_check(
117     $tx,
118     desc          => "void",
119     is_success    => 1,
120     result_code   => '000',
121     error_message => 'VOID PROCESSED',
122     authorization => qr/^$voidable_auth$/,
123     avs_code      => '',
124     cvv2_response => '',
125     );
126 }
127
128 # credit test
129 SKIP: {
130   my $tx = new Business::OnlinePayment( "IPPay", %opts );
131   $tx->content( %content, 'action' => "credit");
132   tx_check(
133     $tx,
134     desc          => "credit",
135     is_success    => 1,
136     result_code   => '000',
137     error_message => 'RETURN ACCEPTED',
138     authorization => qr/\d{6}/,
139     avs_code      => '',
140     cvv2_response => '',
141     );
142 }
143
144
145 sub tx_check {
146     my $tx = shift;
147     my %o  = @_;
148
149     $tx->test_transaction(1);
150     $tx->submit;
151
152     is( $tx->is_success,    $o{is_success},    "$o{desc}: " . tx_info($tx) );
153     is( $tx->result_code,   $o{result_code},   "result_code(): RESULT" );
154     is( $tx->error_message, $o{error_message}, "error_message() / RESPMSG" );
155     like( $tx->authorization, $o{authorization}, "authorization() / AUTHCODE" );
156     is( $tx->avs_code,  $o{avs_code},  "avs_code() / AVSADDR and AVSZIP" );
157     is( $tx->cvv2_response, $o{cvv2_response}, "cvv2_response() / CVV2MATCH" );
158     like( $tx->order_number, qr/^\w{18}/, "order_number() / PNREF" );
159 }
160
161 sub tx_info {
162     my $tx = shift;
163
164     no warnings 'uninitialized';
165
166     return (
167         join( "",
168             "is_success(",     $tx->is_success,    ")",
169             " order_number(",  $tx->order_number,  ")",
170             " error_message(", $tx->error_message, ")",
171             " result_code(",   $tx->result_code,   ")",
172             " auth_info(",     $tx->authorization, ")",
173             " avs_code(",      $tx->avs_code,      ")",
174             " cvv2_response(", $tx->cvv2_response, ")",
175         )
176     );
177 }
178
179 sub expiration_date {
180     my($month, $year) = (localtime)[4,5];
181     $year++;       # So we expire next year.
182     $year %= 100;  # y2k?  What's that?
183
184     return sprintf("%02d/%02d", $month, $year);
185 }