Implement payment profile creation
[Business-OnlinePayment-Bambora.git] / t / 032-payments-card-pre-authorization-complete-void.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5
6 use lib 't';
7 require 'TestFixtures.pm';
8 use Business::OnlinePayment;
9
10 my $merchant_id = $ENV{BAMBORA_MERCHANT_ID};
11 my $api_key     = $ENV{BAMBORA_API_KEY};
12
13 SKIP: {
14   skip 'Missing env vars BAMBORA_MERCHANT_ID and BAMBORA_API_KEY', 32
15     unless $merchant_id && $api_key;
16
17   my %content = (
18     login          => $merchant_id,
19     password       => $api_key,
20     action         => 'Authorization Only',
21     amount         => '9.99',
22
23     owner          => 'Freeside Internet Services',
24     name           => 'Mitch Jackson',
25     address        => '1407 Graymalkin Lane',
26     city           => 'Vancouver',
27     state          => 'BC',
28     zip            => '111 111',
29     country        => 'CA',
30
31     invoice_number => time(),
32     card_number    => '4030000010001234',
33     cvv2           => '123',
34     expiration     => '1122',
35     phone          => '251-300-1300',
36     email          => 'mitch@freeside.biz',
37   );
38
39   my $tr;
40   ok( $tr = Business::OnlinePayment->new('Bambora'), 'Instantiatiate $tr' );
41   ok( $tr->content( %content ), 'Set transaction content onto $tr' );
42   {
43     local $@;
44     eval { $tr->submit };
45     ok( !$@, "Submit pre-auth (expect approve)" );
46   }
47
48   my $response;
49   my %expect = (
50     amount => '9.99',
51     approved => 1,
52     auth_code => 'TEST',
53     message => 'Approved',
54     message_id => 1,
55     payment_method => 'CC',
56     type => 'PA',
57   );
58   my @expect = qw(
59     card
60     created
61     order_number
62     risk_score
63     id
64   );
65
66   ok( $response = $tr->response_decoded, 'response_decoded' );
67
68   for my $k ( keys %expect ) {
69     ok(
70       $response->{$k} eq $expect{$k},
71       sprintf '$tr->%s == %s', $k, $expect{$k}
72     );
73   }
74
75   for my $k ( @expect ) {
76     ok(
77       defined $response->{$k},
78       sprintf '$r->%s (%s)',
79         $k, $response->{$k}
80     );
81   }
82
83   %content = (
84     %content,
85     action => 'Post Authorization',
86     order_number => $tr->order_number,
87     amount => '8.99', # $1 Less than pre-auth
88   );
89
90   my $tr_pa;
91   ok( $tr_pa = Business::OnlinePayment->new('Bambora'), 'Instantiate $tr_pa' );
92   ok( $tr_pa->content( %content ), 'Set transaction content onto $tr_pa' );
93   {
94     local $@;
95     eval { $tr_pa->submit };
96     ok( !$@, "Submit post-auth" );
97     warn "Error: $@" if $@;
98   }
99
100   %expect = (
101     amount => '8.99',
102     approved => '1',
103     message => 'Approved',
104     message_id => '1',
105     type => 'PAC',
106   );
107   @expect = (qw/
108     authorizing_merchant_id
109     card
110     created
111     order_number
112     id
113   /);
114
115   my $response_pa;
116   ok( $response_pa = $tr_pa->response_decoded, 'response_decoded' );
117
118   for my $k ( keys %expect ) {
119     ok(
120       $response_pa->{$k} eq $expect{$k},
121       sprintf '$tr->%s == %s', $k, $expect{$k}
122     );
123   }
124
125   for my $k ( @expect ) {
126     ok(
127       defined $response_pa->{$k},
128       sprintf '$r->%s (%s)',
129         $k, $response_pa->{$k}
130     );
131   }
132
133   #
134   # Void Transaction
135   #
136
137   my %content_void = (
138     action => 'Void',
139     login => $content{login},
140     password => $content{password},
141     order_number => $tr_pa->order_number,
142     amount => '8.99',
143   );
144
145   my $tr_void;
146   ok( $tr_void = Business::OnlinePayment->new('Bambora'), 'Instantiate $tr_void' );
147   ok( $tr_void->content( %content_void ), 'Set transaction content onto $tr_void' );
148   {
149       local $@;
150       eval { $tr_void->submit };
151       ok( !$@, "Submit void" );
152       warn "Error: $@" if $@;
153   }
154
155   %expect = (
156     amount => '8.99',
157     approved => '1',
158     message => 'Approved',
159     message_id => '1',
160     type => 'R',
161   );
162   @expect = (qw/
163     authorizing_merchant_id
164     card
165     created
166     order_number
167     id
168   /);
169
170 }
171
172 done_testing;