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 handler 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' => 'submit_reverse_authorization',
62 'void' => 'submit_void',
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 Complete 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 {
94 my $content = $self->{_content};
96 # Use epoch time as invoice_number, if none is specified
97 $content->{invoice_number} ||= time();
99 # Clarifying Bambora API and Business::OnlinePayment naming conflict
102 # - order_number: user supplied identifier for the order, displayed on reports
103 # - transaction_id: bambora supplied identifier for the order.
104 # this number must be referenced for future actions like voids,
107 # Business::OnlinePayment
108 # - invoice_number: contains the bambora order number
109 # - order_number: contains the bambora transaction id
112 order_number => $self->truncate( $content->{invoice_number}, 30 ),
113 amount => $content->{amount},
114 billing => $self->jhref_billing_address,
118 if ( $content->{card_number} ) {
119 $post{payment_method} = 'card';
121 # Parse the expiration date into expiry_month and expiry_year
122 $self->set_expiration;
125 number => $self->truncate( $content->{card_number}, 20 ),
126 name => $self->truncate( $content->{owner}, 64 ),
127 expiry_month => sprintf( '%02d', $content->{expiry_month} ),
128 expiry_year => sprintf( '%02d', $content->{expiry_year} ),
129 cvd => $content->{cvv2},
130 recurring_payment => $content->{recurring_payment} ? 1 : 0,
135 die 'unknown/unsupported payment method!';
138 my $action = lc $content->{action};
140 if ( $action eq 'normal authorization' ) {
141 $self->path('/v1/payments');
142 } elsif ( $action eq 'authorization only' ) {
143 $self->path('/v1/payments');
144 if ( ref $post{card} ) {
145 $post{card}->{complete} = 0;
147 } elsif ( $action eq 'post authorization' ) {
149 croak 'post authorization cannot be completed - '.
150 'bambora transaction_id must be set as order_number '.
151 'before using submit()'
152 unless $content->{order_number};
155 sprintf '/v1/payments/%s/completions',
156 $content->{order_number}
159 if ( ref $post{card} ) {
160 $post{card}->{complete} = 1
163 die "unsupported action $action";
166 # Parse %post into a JSON string, to be attached to the request POST body
167 my $post_body = encode_json( \%post );
171 post_body => $post_body,
176 my $response = $self->submit_api_request( $post_body );
178 # Error messages already populated upon failure
179 return unless $self->is_success;
181 # Populate transaction result values
182 $self->message_id( $response->{message_id} );
183 $self->authorization( $response->{auth_code} );
184 $self->order_number( $response->{id} );
185 $self->txn_date( $response->{created} );
186 $self->avs_code( $response->{card}{avs_result} );
187 $self->is_success( 1 );
192 =head2 submit_authorization_only
194 Capture a card authorization, but do not complete transaction
198 sub submit_authorization_only {
201 $self->submit_normal_authorization;
203 my $response = $self->response_decoded;
209 && $response->{type} ne 'PA'
212 # Bambora API uses nearly identical API calls for normal
213 # card transactions and pre-authorization. Sanity check
214 # that response reported a pre-authorization code
215 die "Expected API Respose type=PA, but type=$response->{type}! ".
216 "Pre-Authorization attempt may have charged card!";
220 =head2 submit_post_authorization
222 Complete a card pre-authorization
226 sub submit_post_authorization {
227 shift->submit_normal_authorization;
230 =head2 submit_reverse_authorization
232 Reverse a pre-authorization
236 sub submit_reverse_authorization {
242 Process a return against a transaction for the given amount
248 my $content = $self->{_content};
250 for my $f (qw/ order_number amount/) {
251 unless ( $content->{$f} ) {
252 $self->error_message("Cannot process void - missing required content $f");
253 warn $self->error_message if $DEBUG;
255 return $self->is_success(0);
260 # order_number => $self->truncate( $content->{invoice_number}, 30 ),
261 amount => $content->{amount},
263 my $post_body = encode_json( \%post );
268 post_body => $post_body,
271 $self->path( sprintf '/v1/payments/%s/returns', $content->{order_number} );
273 my $response = $self->submit_api_request( $post_body );
277 =head2 submit_api_request json_string
279 Make the appropriate API request with the given JSON string
283 sub submit_api_request {
285 my $post_body = shift
286 or die 'submit_api_request() requires a json_string parameter';
288 my ( $response_body, $response_code, %response_headers ) = $self->https_post(
290 headers => { $self->authorization_header },
291 'Content-Type' => 'application/json',
295 $self->server_response( $response_body );
300 eval{ $response = decode_json( $response_body ) };
304 response_body => $response_body,
305 response => $response,
306 response_code => $response_code,
307 # response_headers => \%response_headers,
311 # API should always return a JSON response, likely network problem
312 if ( $@ || !$response ) {
313 $self->error_message( $response_body || 'connection error' );
314 $self->is_success( 0 );
318 $self->response_decoded( $response );
320 # Response returned an error
321 if ( $response->{code} && $response->{code} != 1 ) {
322 $self->is_success( 0 );
323 $self->result_code( $response->{code} );
325 return $self->error_message(
333 # Return the decoded json of the response back to handler
334 $self->is_success( 1 );
339 =head2 submit_action_unsupported
341 Croak with the error message Action $action unsupported
345 sub submit_action_unsupported {
346 croak sprintf 'Action %s unsupported', shift->{_content}{action}
349 =head2 authorization_header
351 Bambora REST requests authenticate via a HTTP header of the format:
352 Authorization: Passcode Base64Encoded(merchant_id:passcode)
354 Returns a hash representing the authorization header derived from
355 the merchant id (login) and API passcode (password)
359 sub authorization_header {
361 my $content = $self->{_content};
363 my %authorization_header = (
364 Authorization => 'Passcode ' . MIME::Base64::encode_base64(
365 join( ':', $content->{login}, $content->{password} )
370 warn Dumper({ authorization_header => \%authorization_header })."\n";
373 %authorization_header;
376 =head2 jhref_billing_address
378 Return a hashref for inclusion into a json object
379 representing the RequestBillingAddress for the API
383 sub jhref_billing_address {
388 $self->set_phone_number;
390 my $content = $self->{_content};
393 name => $self->truncate( $content->{name}, 64 ),
394 address_line1 => $self->truncate( $content->{address}, 64 ),
395 city => $self->truncate( $content->{city}, 64 ),
396 province => $self->truncate( $content->{province}, 2 ),
397 country => $self->truncate( $content->{country}, 2 ),
398 postal_code => $self->truncate( $content->{zip}, 16 ),
399 phone_number => $self->truncate( $content->{phone_number}, 20 ),
400 email_address => $self->truncate( $content->{email}, 64 ),
406 Country is expected to be set as an ISO-3166-1 2-letter country code
408 Sets string to upper case.
410 Dies unless country is a two-letter string.
412 Could be extended to convert country names to their respective
415 See: L<https://en.wikipedia.org/wiki/ISO_3166-1>
421 my $content = $self->{_content};
422 my $country = uc $content->{country};
424 if ( $country !~ /^[A-Z]{2}$/ ) {
425 croak sprintf 'country is not a 2 character string (%s)',
429 $content->{country} = $country;
432 =head2 set_expiration_month_year
434 Split B::OP expiration field, which may be in the format
435 MM/YY or MMYY, into separate expiry_month and expiry_year fields
437 Will die if values are not numeric
443 my $content = $self->{_content};
444 my $expiration = $content->{expiration};
446 unless ( $expiration ) {
447 $content->{expiry_month} = undef;
448 $content->{expiry_year} = undef;
454 ? split( /\//, $expiration )
455 : unpack( 'A2 A2', $expiration )
458 croak 'card expiration must be in format MM/YY'
459 if $mm =~ /\D/ || $yy =~ /\D/;
462 $content->{expiry_month} = sprintf( '%02d', $mm ),
463 $content->{expiry_year} = sprintf ('%02d', $yy ),
467 =head2 set_payment_method
469 Set payment_method value to one of the following strings
482 sub set_payment_method {
483 # todo - determine correct payment method
484 warn "set_payment_method() STUB FUNCTION ALWAYS RETURNS card!\n";
485 shift->{_content}->{payment_method} = 'card';
488 =head2 set_phone_number
490 Set value for field phone_number, from value in field phone
492 Bambora API expects only digits in a phone number. Strips all non-digit
497 sub set_phone_number {
499 my $content = $self->{_content};
501 my $phone = $content->{phone}
502 or return $content->{phone_number} = undef;
505 $content->{phone_number} = $phone;
510 Set value for field province, from value in field state
512 Outside the US/Canada, API expect province set to the string "--",
513 otherwise expects a 2 character string. Value for province is
514 formatted to upper case, and truncated to 2 characters.
520 my $content = $self->{_content};
521 my $country = uc $content->{country};
523 return $content->{province} = '--'
525 && ( $country eq 'US' || $country eq 'CA' );
527 $content->{province} = uc $content->{state};
530 =head2 truncate string, bytes
532 When given a string, truncate to given string length in a unicode safe way
537 my ( $self, $string, $bytes ) = @_;
539 # truncate_egc dies when asked to truncate undef
540 return $string unless $string;
542 truncate_egc( "$string", $bytes, '' );