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');
125 my $response = $self->submit_api_request( $post_body );
127 # Error messages already populated upon failure
128 return unless $self->is_success;
130 # Populate transaction result values
131 $self->message_id( $response->{message_id} );
132 $self->authorization( $response->{auth_code} );
133 $self->order_number( $response->{id} );
134 $self->txn_date( $response->{created} );
135 $self->avs_code( $response->{card}{avs_result} );
136 $self->is_success( 1 );
139 =head2 submit_api_request json_string
141 Make the appropriate API request with the given JSON string
145 sub submit_api_request {
147 my $post_body = shift
148 or die 'submit_api_request() requires a json_string parameter';
150 my ( $response_body, $response_code, %response_headers ) = $self->https_post(
152 headers => { $self->authorization_header },
153 'Content-Type' => 'application/json',
157 $self->server_response( $response_body );
162 eval{ $response = decode_json( $response_body ) };
166 response_body => $response_body,
167 response => $response,
168 response_code => $response_code,
169 # response_headers => \%response_headers,
173 # API should always return a JSON response, likely network problem
174 if ( $@ || !$response ) {
175 $self->error_message( $response_body || 'connection error' );
176 $self->is_success( 0 );
180 $self->response_decoded( $response );
182 # Response returned an error
183 if ( $response->{code} && $response->{code} != 1 ) {
184 $self->is_success( 0 );
185 $self->result_code( $response->{code} );
187 return $self->error_message(
195 # Return the decoded json of the response back to handler
196 $self->is_success( 1 );
201 =head2 submit_action_unsupported
203 Croak with the error message Action $action unsupported
207 sub submit_action_unsupported {
208 croak sprintf 'Action %s unsupported', shift->action
211 =head2 authorization_header
213 Bambora POST requests authenticate via a HTTP header of the format:
214 Authorization: Passcode Base64Encoded(merchant_id:passcode)
216 Returns a hash representing the authorization header derived from
217 the merchant id (login) and API passcode (password)
221 sub authorization_header {
223 my $content = $self->{_content};
225 my %authorization_header = (
226 Authorization => 'Passcode ' . MIME::Base64::encode_base64(
227 join( ':', $content->{login}, $content->{password} )
232 warn Dumper({ authorization_header => \%authorization_header })."\n";
235 %authorization_header;
238 =head2 jhref_billing_address
240 Return a hashref for inclusion into a json object
241 representing the RequestBillingAddress for the API
245 sub jhref_billing_address {
250 $self->set_phone_number;
252 my $content = $self->{_content};
255 name => $self->truncate( $content->{name}, 64 ),
256 address_line1 => $self->truncate( $content->{address}, 64 ),
257 city => $self->truncate( $content->{city}, 64 ),
258 province => $self->truncate( $content->{province}, 2 ),
259 country => $self->truncate( $content->{country}, 2 ),
260 postal_code => $self->truncate( $content->{zip}, 16 ),
261 phone_number => $self->truncate( $content->{phone_number}, 20 ),
262 email_address => $self->truncate( $content->{email}, 64 ),
266 =head2 make_invoice_number
268 If an invoice number has not been specified, generate one using
269 the current epoch timestamp
273 sub make_invoice_number {
274 shift->{_content}{invoice_number} ||= time();
279 Country is expected to be set as an ISO-3166-1 2-letter country code
281 Sets string to upper case.
283 Dies unless country is a two-letter string.
285 In the future, could be extended to convert country names to their respective
288 See: L<https://en.wikipedia.org/wiki/ISO_3166-1>
294 my $content = $self->{_content};
295 my $country = uc $content->{country};
297 if ( $country !~ /^[A-Z]{2}$/ ) {
298 croak sprintf 'country is not a 2 character string (%s)',
302 $content->{country} = $country;
305 =head2 set_expiration_month_year
307 Split standard expiration field, which may be in the format
308 MM/YY or MMYY, into separate expiry_month and expiry_year fields
310 Will die if values are not numeric
316 my $content = $self->{_content};
317 my $expiration = $content->{expiration};
321 ? split( /\//, $expiration )
322 : unpack( 'A2 A2', $expiration )
325 croak 'card expiration must be in format MM/YY'
326 if $mm =~ /\D/ || $yy =~ /\D/;
329 $content->{expiry_month} = sprintf( '%02d', $mm ),
330 $content->{expiry_year} = sprintf ('%02d', $yy ),
334 =head2 set_payment_method
336 Set payment_method value to one of the following strings
349 sub set_payment_method {
350 # todo - determine correct payment method
351 warn "set_payment_method() STUB FUNCTION ALWAYS RETURNS card!\n";
352 shift->{_content}->{payment_method} = 'card';
355 =head2 set_phone_number
359 sub set_phone_number {
361 my $content = $self->{_content};
363 my $phone = $content->{phone}
364 or return $content->{phone_number} = undef;
367 $content->{phone_number} = $phone;
372 Outside the US/Canada, API expect province set to the string "--",
373 otherwise to be a 2 character string
379 my $content = $self->{_content};
380 my $country = uc $content->{country};
382 return $content->{province} = '--'
384 && ( $country eq 'US' || $country eq 'CA' );
386 $content->{province} = uc $content->{state};
389 =head2 truncate string, bytes
391 When given a string, truncate to given string length in a unicode safe way
396 my ( $self, $string, $bytes ) = @_;
398 # truncate_egc dies when asked to truncate undef
399 return $string unless $string;
401 truncate_egc( "$string", $bytes, '' );