+This method creates the payment profile, then replaces the customer_code
+generated by Bambora with the card number resembling token.
+
+=cut
+
+sub submit_tokenize {
+ my $self = shift;
+ my $content = $self->{_content};
+
+ # Check if given card number is already a bambora customer_code
+ # under this module's token rules
+ croak "card_number is already tokenized"
+ if $content->{card_number} =~ /^99\d{14}$/;
+
+ my %post = (
+ customer_code => $self->generate_token,
+ card => $self->jhref_card,
+ billing => $self->jhref_billing_address,
+ validate => 0,
+ );
+
+ # jhref_card may have generated an exception
+ return if $self->error_message;
+
+ $self->path('/v1/profiles');
+
+ my $post_body = encode_json( \%post );
+
+ if ( $DEBUG ) {
+ warn Dumper({
+ path => $self->path,
+ post_body => $post_body,
+ post_href => \%post,
+ });
+ }
+
+ my $response = $self->submit_api_request( $post_body );
+ if ( $DEBUG ) {
+ warn Dumper({
+ response => $response,
+ is_success => $self->is_success,
+ error_message => $self->error_message,
+ });
+ }
+ return unless $self->is_success;
+
+ my $customer_code = $response->{customer_code};
+ if ( !$customer_code ) {
+ # Should not happen...
+ # API reported success codes, but
+ # customer_code value is missing
+ $self->error_message(
+ "Fatal error: API reported success, but did not return customer_code"
+ );
+ return $self->is_success(0);
+ }
+
+ if ( $customer_code ne $post{customer_code} ) {
+ # Should not happen...
+ # API reported success codes, but
+ # customer_code attached to created profiles does not match
+ # the token value we attempted to assign to the customer profile
+ $self->error_message(
+ "Fatal error: API failed to set payment profile customer_code value"
+ );
+ return $self->is_success(0);
+ }
+
+ $self->card_token( $customer_code );
+
+ return $response;