summaryrefslogtreecommitdiff
path: root/t/TestFixtures.pm
blob: 947d0b3b832a7f86709d3cbeb3dab311763a8f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env perl
package TestFixtures;
use strict;
use warnings;

use Exporter;
use vars qw/ @ISA @EXPORT /;
@ISA = 'Exporter';
@EXPORT = qw/
    common_content
    inspect_response
    inspect_transaction
    make_api_request
/;

use Business::OnlinePayment;
use Data::Dumper;
use Test::More;

=head1 NAME

TestFixtures

=head1 DESCRIPTION

Common helper methods for all test units

=head1 BAMBORA DEVELOPER ACCOUNT

Do not use live credentials with these test units.  Bambora
provides no way to specifiy a test payment gateway.  They issue
test accounts instead.  See L<https://dev.na.bambora.com>

=head1 USAGE

    # set environment variables with credentials
    export BAMBORA_MERCHANT_ID=8675309
    export BAMBORA_API_KEY=XOXOXOXOXOXOXOX
    
    # run all tests
    prove -lv t
    
    # run a single test
    prove -lv t/031-payments-card-normal-authorizaiton.t

=head1 FUNCTIONS

=head2 common_content

A basic Business::OnlinePayment content hash, containing a
valid Bambora test card number, with Bambora's specified
correct billing address for their test cards

See L<https://dev.na.bambora.com/docs/references/payment_APIs/test_cards>

=cut

sub common_content {
    (
        #action         => 'Normal Authorization',

        amount         => '9.99',

        owner          => 'Business OnlinePayment',
        name           => 'Mitch Jackson',
        address        => '1407 Graymalkin Lane',
        city           => 'Vancouver',
        state          => 'BC',
        zip            => '111 111',
        country        => 'CA',

        invoice_number => time(),
        card_number    => '4030000010001234',
        cvv2           => '123',
        expiration     => '1122',
        phone          => '251-300-1300',
        email          => 'mitch@freeside.biz',
    )
}

=head2 inspect_response $response, \%expect, \@expect

Given $response, a decoded json api response, check the
response contains the keys/value defined in %expect, and
that response keys exist for keynames defined in @expect

=cut

sub inspect_response {
    no warnings 'uninitialized';

    my $response = shift;
    my $expect_href = shift || {};
    my $expect_aref = shift || [];

    die 'Expected $response hashref parameter'
        unless ref $response;

    for my $k ( keys %{$expect_href} ) {
        ok(
            $response->{$k} eq $expect_href->{$k},
            sprintf '$response->%s: %s eq %s',
                $k,
                $response->{$k},
                $expect_href->{$k}
        );
    }

    for my $k ( @{$expect_aref} ) {
        ok(
            defined $response->{$k},
            sprintf '$response->%s defined: %s',
                $k, $response->{$k}
        );
    }
}

=head2 inspect_transaction $transaction, \%expect, \@expect

Given a B::OP $tr, call methods defined as keys within %expect,
and validate the returned values match the values in %expect.
Check the methods defined in @expect return true values

=cut

sub inspect_transaction {
    no warnings 'uninitialized';
    my $tr = shift;
    my $expect_href = shift || {};
    my $expect_aref = shift || [];

    die 'Expected $tr B::OP transaction parameter'
        unless ref $tr;

    for my $k ( keys %{$expect_href} ) {
        ok(
            $tr->can($k) && $tr->$k() eq $expect_href->{$k},
            sprintf '$tr->%s: %s eq %s',
                $k,
                $tr->can($k) ? $tr->$k() : 'METHOD MISSING',
                $expect_href->{$k}
        );
    }

    for my $k ( @{$expect_aref} ) {
        ok(
            $tr->can($k) && defined $tr->$k(),
            sprintf '$tr->%s defined: %s',
                $k,
                $tr->can($k) ? $tr->$k() : 'METHOD MISSING',
        );
    } 
    
}

=head2 make_api_request \%content

Given a %content href, create a B::OP transaction and submit it

Returns the transaction object, and the decoded json response

=cut

sub make_api_request {
    my $content = shift;
    die 'expected href' unless ref $content;

  my $tr;
  ok( $tr = Business::OnlinePayment->new('Bambora'), 'Instantiatiate transaction' );
  ok( $tr->content( %$content ), 'Hand %content to transaction' );
  {
    local $@;
    eval { $tr->submit };
    ok( !$@, "Submit request to create Payment Profile, action: $content->{action}" );
    if ( $@ ) {
        warn Dumper({
            content => $content,
            error => $@,
        });
    }
  }

  my $response = $tr->response_decoded || {};

  return ( $tr, $response );
}

1;