161030c6dd37e65c5541d28101ddbcf837f78c57
[Business-OnlinePayment-Bambora.git] / t / 041-tokenize-card.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5
6 use lib 't';
7 use TestFixtures;
8 use Business::OnlinePayment;
9 use Data::Dumper;
10     $Data::Dumper::Sortkeys = 1;
11     $Data::Dumper::Indent = 1;
12
13 my $merchant_id = $ENV{BAMBORA_MERCHANT_ID};
14 my $api_key     = $ENV{BAMBORA_API_KEY};
15
16 SKIP: {
17   skip 'Missing env vars BAMBORA_MERCHANT_ID and BAMBORA_API_KEY', 36
18     unless $merchant_id && $api_key;
19
20   my %content = (
21     common_content(),
22
23     login => $merchant_id,
24     password => $api_key,
25
26     action => 'Tokenize',
27   );
28
29   #
30   # Create a payment profile with Tokenize
31   #
32
33   my ( $tr, $response ) = make_api_request( \%content );
34
35   inspect_response(
36     $response,
37     {
38       code => 1,
39       message => 'Operation Successful',
40     },
41     [qw/ customer_code /],
42   );
43
44   ok(
45     $response->{customer_code} eq $tr->card_token,
46     '$tr->card_token eq $response->{customer_code}'
47   );
48
49   #
50   # Create a charge against the payment profile
51   # with the token set as 'card_number'
52   #
53
54   my %content_ch1 = (
55     %content,
56     action => 'Normal Authorization',
57     card_number => $tr->card_token,
58     amount => '2.95',
59   );
60
61   my ( $tr_ch1, $response_ch1 ) = make_api_request( \%content_ch1 );
62
63   # warn Dumper({
64   #   response_ch1 => $response_ch1,
65   # });
66
67   inspect_response(
68     $response_ch1,
69     {
70       amount => $content_ch1{amount},
71       approved => 1,
72       auth_code => 'TEST',
73       authorizing_merchant_id => $content{login},
74       message => 'Approved',
75       payment_method => 'CC',
76       type => 'P',
77     },
78     [qw/
79       card
80       created
81       order_number
82     /],
83   );
84
85
86   #
87   # Create a charge against the payment profile
88   # with the token set as 'card_token'
89   #
90
91   my %content_ch2 = (
92     login => $content{login},
93     password => $content{password},
94     action => 'Normal Authorization',
95     #card_token => '9915559773829941',
96     card_token => $tr->card_token,
97     amount => '7.77',
98   );
99
100   my ( $tr_ch2, $response_ch2 ) = make_api_request( \%content_ch2 );
101
102   # warn Dumper({
103   #   response_chs => $response_ch2
104   # });
105
106   inspect_response(
107     $response_ch2,
108     {
109       amount => $content_ch2{amount},
110       approved => 1,
111       auth_code => 'TEST',
112       authorizing_merchant_id => $content{login},
113       message => 'Approved',
114       payment_method => 'CC',
115       type => 'P',
116     },
117     [qw/
118       card
119       created
120       order_number
121     /],
122   );
123
124   #
125   # Attempt charge with a normal credit card number as card_token
126   # Expect fail
127   #
128
129   my %content_fail = (
130     %content_ch2,
131     card_token => '4242424242424242',
132     amount => '24.95',
133   );
134
135   my ( $tr_fail, $response_fail ) = make_api_request( \%content_fail );
136
137   inspect_transaction(
138     $tr_fail,
139     { is_success => 0 },
140     [qw/ error_message /],
141   );
142
143 }
144
145 done_testing;