947d0b3b832a7f86709d3cbeb3dab311763a8f9c
[Business-OnlinePayment-Bambora.git] / t / TestFixtures.pm
1 #!/usr/bin/env perl
2 package TestFixtures;
3 use strict;
4 use warnings;
5
6 use Exporter;
7 use vars qw/ @ISA @EXPORT /;
8 @ISA = 'Exporter';
9 @EXPORT = qw/
10     common_content
11     inspect_response
12     inspect_transaction
13     make_api_request
14 /;
15
16 use Business::OnlinePayment;
17 use Data::Dumper;
18 use Test::More;
19
20 =head1 NAME
21
22 TestFixtures
23
24 =head1 DESCRIPTION
25
26 Common helper methods for all test units
27
28 =head1 BAMBORA DEVELOPER ACCOUNT
29
30 Do not use live credentials with these test units.  Bambora
31 provides no way to specifiy a test payment gateway.  They issue
32 test accounts instead.  See L<https://dev.na.bambora.com>
33
34 =head1 USAGE
35
36     # set environment variables with credentials
37     export BAMBORA_MERCHANT_ID=8675309
38     export BAMBORA_API_KEY=XOXOXOXOXOXOXOX
39     
40     # run all tests
41     prove -lv t
42     
43     # run a single test
44     prove -lv t/031-payments-card-normal-authorizaiton.t
45
46 =head1 FUNCTIONS
47
48 =head2 common_content
49
50 A basic Business::OnlinePayment content hash, containing a
51 valid Bambora test card number, with Bambora's specified
52 correct billing address for their test cards
53
54 See L<https://dev.na.bambora.com/docs/references/payment_APIs/test_cards>
55
56 =cut
57
58 sub common_content {
59     (
60         #action         => 'Normal Authorization',
61
62         amount         => '9.99',
63
64         owner          => 'Business OnlinePayment',
65         name           => 'Mitch Jackson',
66         address        => '1407 Graymalkin Lane',
67         city           => 'Vancouver',
68         state          => 'BC',
69         zip            => '111 111',
70         country        => 'CA',
71
72         invoice_number => time(),
73         card_number    => '4030000010001234',
74         cvv2           => '123',
75         expiration     => '1122',
76         phone          => '251-300-1300',
77         email          => 'mitch@freeside.biz',
78     )
79 }
80
81 =head2 inspect_response $response, \%expect, \@expect
82
83 Given $response, a decoded json api response, check the
84 response contains the keys/value defined in %expect, and
85 that response keys exist for keynames defined in @expect
86
87 =cut
88
89 sub inspect_response {
90     no warnings 'uninitialized';
91
92     my $response = shift;
93     my $expect_href = shift || {};
94     my $expect_aref = shift || [];
95
96     die 'Expected $response hashref parameter'
97         unless ref $response;
98
99     for my $k ( keys %{$expect_href} ) {
100         ok(
101             $response->{$k} eq $expect_href->{$k},
102             sprintf '$response->%s: %s eq %s',
103                 $k,
104                 $response->{$k},
105                 $expect_href->{$k}
106         );
107     }
108
109     for my $k ( @{$expect_aref} ) {
110         ok(
111             defined $response->{$k},
112             sprintf '$response->%s defined: %s',
113                 $k, $response->{$k}
114         );
115     }
116 }
117
118 =head2 inspect_transaction $transaction, \%expect, \@expect
119
120 Given a B::OP $tr, call methods defined as keys within %expect,
121 and validate the returned values match the values in %expect.
122 Check the methods defined in @expect return true values
123
124 =cut
125
126 sub inspect_transaction {
127     no warnings 'uninitialized';
128     my $tr = shift;
129     my $expect_href = shift || {};
130     my $expect_aref = shift || [];
131
132     die 'Expected $tr B::OP transaction parameter'
133         unless ref $tr;
134
135     for my $k ( keys %{$expect_href} ) {
136         ok(
137             $tr->can($k) && $tr->$k() eq $expect_href->{$k},
138             sprintf '$tr->%s: %s eq %s',
139                 $k,
140                 $tr->can($k) ? $tr->$k() : 'METHOD MISSING',
141                 $expect_href->{$k}
142         );
143     }
144
145     for my $k ( @{$expect_aref} ) {
146         ok(
147             $tr->can($k) && defined $tr->$k(),
148             sprintf '$tr->%s defined: %s',
149                 $k,
150                 $tr->can($k) ? $tr->$k() : 'METHOD MISSING',
151         );
152     } 
153     
154 }
155
156 =head2 make_api_request \%content
157
158 Given a %content href, create a B::OP transaction and submit it
159
160 Returns the transaction object, and the decoded json response
161
162 =cut
163
164 sub make_api_request {
165     my $content = shift;
166     die 'expected href' unless ref $content;
167
168   my $tr;
169   ok( $tr = Business::OnlinePayment->new('Bambora'), 'Instantiatiate transaction' );
170   ok( $tr->content( %$content ), 'Hand %content to transaction' );
171   {
172     local $@;
173     eval { $tr->submit };
174     ok( !$@, "Submit request to create Payment Profile, action: $content->{action}" );
175     if ( $@ ) {
176         warn Dumper({
177             content => $content,
178             error => $@,
179         });
180     }
181   }
182
183   my $response = $tr->response_decoded || {};
184
185   return ( $tr, $response );
186 }
187
188 1;