1 package Business::OnlinePayment::Bambora;
4 use base qw/ Business::OnlinePayment::HTTPS /;
5 use feature 'unicode_strings';
9 use Data::Dumper; $Data::Dumper::Sortkeys = 1;
11 use Unicode::Truncate qw( truncate_egc );
14 use vars qw/ $VERSION $DEBUG /;
19 $Data::Dumper::Sortkeys = 1;
22 =head1 INTERNAL METHODS
26 See L<Business::OnlinePayment/set_defaults>
33 $self->server('api.na.bambora.com');
36 # Create accessors for
53 Dispatch to the appropriate hanlder based on the given action
57 my %action_dispatch_table = (
58 'normal authorization' => 'submit_normal_authorization',
59 'authorization only' => 'submit_authorization_only',
60 'post authorization' => 'submit_post_authorization',
61 'reverse authorization' => 'rsubmit_everse_authorization',
62 'void' => 'submit_viod',
63 'credit' => 'submit_credit',
64 'tokenize' => 'submit_tokenize',
65 'recurring authorization' => 'submit_recurring_authorization',
66 'modify recurring authorization' => 'modify_recurring_authorization',
72 my $action = lc $self->{_content}->{action}
73 or croak 'submit() called with no action set';
75 my $method = $action_dispatch_table{$action};
77 $self->submit_action_unsupported()
79 && $self->can($method);
84 =head2 submit_normal_authorization
86 Compliete a payment transaction by with an API POST to B</payments>
88 See L<https://dev.na.bambora.com/docs/references/payment_APIs/v1-0-5>
92 sub submit_normal_authorization {
95 # Series of methods to populate or format field values
96 $self->make_invoice_number;
97 $self->set_payment_method;
98 $self->set_expiration;
100 my $content = $self->{_content};
102 # Build a JSON string
103 my $post_body = encode_json({
104 order_number => $self->truncate( $content->{invoice_number}, 30 ),
105 amount => $content->{amount},
106 payment_method => $content->{payment_method},
108 billing => $self->jhref_billing_address,
111 number => $self->truncate( $content->{card_number}, 20 ),
112 name => $self->truncate( $content->{owner}, 64 ),
113 expiry_month => sprintf( '%02d', $content->{expiry_month} ),
114 expiry_year => sprintf( '%02d', $content->{expiry_year} ),
115 cvd => $content->{cvv2},
116 recurring_payment => $content->{recurring_payment} ? 1 : 0,
121 warn Dumper({ post_body => $post_body })."\n";
124 $self->path('/v1/payments');
126 my ( $response_body, $response_code, %response_headers ) = $self->https_post(
128 headers => { $self->authorization_header },
129 'Content-Type' => 'application/json',
133 $self->server_response( $response_body );
138 eval{ $response = decode_json( $response_body ) };
142 response_body => $response_body,
143 response => $response,
144 response_code => $response_code,
145 # response_headers => \%response_headers,
149 # API should always return a JSON response,
150 die $response_body || 'connection error'
153 $self->response_decoded( $response );
155 if ( $response->{code} && $response->{code} != 1 ) {
157 $self->is_success( 0 );
158 $self->result_code( $response->{code} );
159 return $self->error_message(
167 # Populate transaction result values
168 $self->message_id( $response->{message_id} );
169 $self->authorization( $response->{auth_code} );
170 $self->order_number( $response->{id} );
171 $self->txn_date( $response->{created} );
172 $self->avs_code( $response->{card}{avs_result} );
173 $self->is_success( 1 );
176 =head2 submit_api_request json_string
178 Make the appropriate API request with the given JSON string
182 sub submit_api_request {
184 my $json_string = shift
185 or die 'submit_api_request() requires a json_string parameter';
190 =head2 submit_action_unsupported
192 Croak with the error message Action $action unsupported
196 sub submit_action_unsupported {
197 croak sprintf 'Action %s unsupported', shift->action
200 =head2 authorization_header
202 Bambora POST requests authenticate via a HTTP header of the format:
203 Authorization: Passcode Base64Encoded(merchant_id:passcode)
205 Returns a hash representing the authorization header derived from
206 the merchant id (login) and API passcode (password)
210 sub authorization_header {
212 my $content = $self->{_content};
214 my %authorization_header = (
215 Authorization => 'Passcode ' . MIME::Base64::encode_base64(
216 join( ':', $content->{login}, $content->{password} )
221 warn Dumper({ authorization_header => \%authorization_header })."\n";
224 %authorization_header;
227 =head2 jhref_billing_address
229 Return a hashref for inclusion into a json object
230 representing the RequestBillingAddress for the API
234 sub jhref_billing_address {
239 $self->set_phone_number;
241 my $content = $self->{_content};
244 name => $self->truncate( $content->{name}, 64 ),
245 address_line1 => $self->truncate( $content->{address}, 64 ),
246 city => $self->truncate( $content->{city}, 64 ),
247 province => $self->truncate( $content->{province}, 2 ),
248 country => $self->truncate( $content->{country}, 2 ),
249 postal_code => $self->truncate( $content->{zip}, 16 ),
250 phone_number => $self->truncate( $content->{phone_number}, 20 ),
251 email_address => $self->truncate( $content->{email}, 64 ),
255 =head2 make_invoice_number
257 If an invoice number has not been specified, generate one using
258 the current epoch timestamp
262 sub make_invoice_number {
263 shift->{_content}{invoice_number} ||= time();
268 Country is expected to be set as an ISO-3166-1 2-letter country code
270 Sets string to upper case.
272 Dies unless country is a two-letter string.
274 In the future, could be extended to convert country names to their respective
277 See: L<https://en.wikipedia.org/wiki/ISO_3166-1>
283 my $content = $self->{_content};
284 my $country = uc $content->{country};
286 if ( $country !~ /^[A-Z]{2}$/ ) {
287 croak sprintf 'country is not a 2 character string (%s)',
291 $content->{country} = $country;
294 =head2 set_expiration_month_year
296 Split standard expiration field, which may be in the format
297 MM/YY or MMYY, into separate expiry_month and expiry_year fields
299 Will die if values are not numeric
305 my $content = $self->{_content};
306 my $expiration = $content->{expiration};
310 ? split( /\//, $expiration )
311 : unpack( 'A2 A2', $expiration )
314 croak 'card expiration must be in format MM/YY'
315 if $mm =~ /\D/ || $yy =~ /\D/;
318 $content->{expiry_month} = sprintf( '%02d', $mm ),
319 $content->{expiry_year} = sprintf ('%02d', $yy ),
323 =head2 set_payment_method
325 Set payment_method value to one of the following strings
338 sub set_payment_method {
339 # todo - determine correct payment method
340 warn "set_payment_method() STUB FUNCTION ALWAYS RETURNS card!\n";
341 shift->{_content}->{payment_method} = 'card';
344 =head2 set_phone_number
348 sub set_phone_number {
350 my $content = $self->{_content};
352 my $phone = $content->{phone}
353 or return $content->{phone_number} = undef;
356 $content->{phone_number} = $phone;
361 Outside the US/Canada, API expect province set to the string "--",
362 otherwise to be a 2 character string
368 my $content = $self->{_content};
369 my $country = uc $content->{country};
371 return $content->{province} = '--'
373 && ( $country eq 'US' || $country eq 'CA' );
375 $content->{province} = uc $content->{state};
378 =head2 truncate string, bytes
380 When given a string, truncate to given string length in a unicode safe way
385 my ( $self, $string, $bytes ) = @_;
387 # truncate_egc dies when asked to truncate undef
388 return $string unless $string;
390 truncate_egc( "$string", $bytes, '' );