initial import
[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     customer_id    => 'tfb',
29 );
30
31 my $voidable;
32 my $voidable_auth;
33 my $voidable_amount = 0;
34
35 # valid card number test
36 {
37   my $tx = Business::OnlinePayment->new("IPPay", @opts);
38   $tx->content(%content);
39   tx_check(
40     $tx,
41     desc          => "valid card_number",
42     is_success    => 1,
43     result_code   => '000',
44     error_message => 'APPROVED',
45     authorization => qr/TEST\d{2}/,
46     avs_code      => 'U',          # so rather pointless :\
47     cvv2_response => 'P',          # ...
48   );
49   $voidable = $tx->order_number if $tx->is_success;
50   $voidable_auth = $tx->authorization if $tx->is_success;
51   $voidable_amount = $content{amount} if $tx->is_success;
52 }
53
54 # invalid card number test
55 {
56   my $tx = Business::OnlinePayment->new("IPPay", @opts);
57   $tx->content(%content, card_number => "4111111111111112" );
58   tx_check(
59     $tx,
60     desc          => "invalid card_number",
61     is_success    => 0,
62     result_code   => '912',
63     error_message => 'INVALID CARD NUMBER',
64     authorization => qr/^$/,
65     avs_code      => '',           # so rather pointless :\
66     cvv2_response => '',           # ...
67   );
68 }
69
70 # authorization only test
71 {
72   my $tx = Business::OnlinePayment->new("IPPay", @opts);
73   $tx->content(%content, action => 'authorization only',  amount => '3.00' );
74   tx_check(
75     $tx,
76     desc          => "authorization only",
77     is_success    => 1,
78     result_code   => '000',
79     error_message => 'APPROVED',
80     authorization => qr/TEST\d{2}/,
81     avs_code      => 'U',          # so rather pointless :\
82     cvv2_response => 'P',          # ...
83   );
84   $postable = $tx->order_number if $tx->is_success;
85   $postable_auth = $tx->authorization if $tx->is_success;
86   $postable_amount = $content{amount} if $tx->is_success;
87 }
88
89 # post authorization test
90 SKIP: {
91   my $tx = new Business::OnlinePayment( "IPPay", %opts );
92   $tx->content( %content, 'action'       => "post authorization", 
93                           'amount'       => $postable_amount,    # not required
94                           'order_number' => $postable,
95               );
96   tx_check(
97     $tx,
98     desc          => "post authorization",
99     is_success    => 1,
100     result_code   => '000',
101     error_message => 'APPROVED',
102     authorization => qr/^$postable_auth$/,
103     avs_code      => '',
104     cvv2_response => '',
105     );
106 }
107
108 # void test
109 SKIP: {
110   my $tx = new Business::OnlinePayment( "IPPay", %opts );
111   $tx->content( %content, 'action' => "Void",
112                           'order_number' => $voidable,
113                           'authorization' => $voidable_auth,
114               );
115   tx_check(
116     $tx,
117     desc          => "void",
118     is_success    => 1,
119     result_code   => '000',
120     error_message => 'VOID PROCESSED',
121     authorization => qr/^$voidable_auth$/,
122     avs_code      => '',
123     cvv2_response => '',
124     );
125 }
126
127 # credit test
128 SKIP: {
129   my $tx = new Business::OnlinePayment( "IPPay", %opts );
130   $tx->content( %content, 'action' => "credit");
131   tx_check(
132     $tx,
133     desc          => "credit",
134     is_success    => 1,
135     result_code   => '000',
136     error_message => 'RETURN ACCEPTED',
137     authorization => qr/\d{6}/,
138     avs_code      => '',
139     cvv2_response => '',
140     );
141 }
142
143
144 sub tx_check {
145     my $tx = shift;
146     my %o  = @_;
147
148     $tx->test_transaction(1);
149     $tx->submit;
150
151     is( $tx->is_success,    $o{is_success},    "$o{desc}: " . tx_info($tx) );
152     is( $tx->result_code,   $o{result_code},   "result_code(): RESULT" );
153     is( $tx->error_message, $o{error_message}, "error_message() / RESPMSG" );
154     like( $tx->authorization, $o{authorization}, "authorization() / AUTHCODE" );
155     is( $tx->avs_code,  $o{avs_code},  "avs_code() / AVSADDR and AVSZIP" );
156     is( $tx->cvv2_response, $o{cvv2_response}, "cvv2_response() / CVV2MATCH" );
157     like( $tx->order_number, qr/^\w{18}/, "order_number() / PNREF" );
158 }
159
160 sub tx_info {
161     my $tx = shift;
162
163     no warnings 'uninitialized';
164
165     return (
166         join( "",
167             "is_success(",     $tx->is_success,    ")",
168             " order_number(",  $tx->order_number,  ")",
169             " error_message(", $tx->error_message, ")",
170             " result_code(",   $tx->result_code,   ")",
171             " auth_info(",     $tx->authorization, ")",
172             " avs_code(",      $tx->avs_code,      ")",
173             " cvv2_response(", $tx->cvv2_response, ")",
174         )
175     );
176 }
177
178 sub expiration_date {
179     my($month, $year) = (localtime)[4,5];
180     $year++;       # So we expire next year.
181     $year %= 100;  # y2k?  What's that?
182
183     return sprintf("%02d/%02d", $month, $year);
184 }