x

Error: The postal code in billing address doesn't match the one used for card nonce creation.

Hi, I don't know what is wrong with my code, and I can't understand. I'm getting this error.

 

 

Exception when calling customerApi->createCustomerCard: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"BAD_REQUEST","detail":"The postal code in billing address doesn't match the one used for card nonce creation."}]}

 

My index.html is same https://github.com/square/connect-api-examples/blob/master/connect-examples/v2/php_payment/index.htm... with aditionals fields like "name on card, billing address, email ..." and was working ok just with transactionApi.

 

Now I'm creating customer and linking with the payment 

 

this is my process-card.php 

 

<?php
require 'payment/autoload.php';
# Replace these values. You probably want to start with your Sandbox credentials
# to start: https://docs.connect.squareup.com/articles/using-sandbox/
# The access token to use in all Connect API requests. Use your *sandbox* access
# token if you're just testing things out.

$access_token = 'sandbox-sq0atb-TQL7A9idNrXQ'; ?>

<script>
function goBack() {
    window.history.back();
}
</script>
<?php
# Helps ensure this code has been reached via form submission

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  error_log("Received a non-POST request");
  echo "Request not allowed";
  http_response_code(405);
  return;
}
# Fail if the card form didn't send a value for `nonce` to the server
$nonce = $_POST['nonce'];
$postal_code = $_POST['sq-postal-code'];
$amount =$_POST['amount'];
$amount = (int)$amount*100;
$address1 = $_POST['address1'];
$state1 =  $_POST['state1'];
$city1 =  $_POST['city1'];
$address2 = $_POST['address2'];
$state2 =  $_POST['state2'];
$city2 =  $_POST['city2'];
$email = $_POST['email'];
$name = $_POST['fullname'];

if (is_null($nonce)) {
  echo "Invalid card data";
  http_response_code(422);
  return;
}

$locations_api = new \SquareConnect\Api\LocationsApi();
$location_id = "CBASELMdFqVDDRAh3RDChcgAQ";// sandbox

SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);

$api_instance = new SquareConnect\Api\CustomersApi();
try {
 $result = $api_instance->createCustomer(
  array(
   "given_name" => $name,
   "email_address" => $email,
   "address" => array (
        "address_line_1" => $address1,
        "locality" => $city1,
        "administrative_district_level_1" => $state1
   ),
   )
  );
 $customer = $result->getCustomer(); 
} catch (Exception $e) {
 echo 'Exception when calling CustomersApi->createCustomer: ', $e->getMessage(), PHP_EOL;
}


$api_instance1 = new SquareConnect\Api\CustomersApi();
$customer_id = $customer['id'];

try {
 $result = $api_instance1->createCustomerCard(
  $customer_id,
  array(
   "card_nonce" =>$nonce,
   "cardholder_name" => $name,
    "billing_address" => array (
      "address_line_1" => $address2,
      "locality" => $city2,
      "postalCode" => $postal_code,
      "administrative_district_level_1" => $state2
      
    ),
   )
  );
 $customer_card = $result->getCard();
}catch (Exception $e) {
 echo 'Exception when calling customerApi->createCustomerCard: ', $e->getMessage(), PHP_EOL;
}

$transaction_api = new SquareConnect\Api\TransactionsApi();
try {
 $result = $transaction_api->charge(
  $location_id,
  array(
   'idempotency_key' => uniqid(),
   'amount_money' => array(
    'amount' => $amount, 'currency' => 'USD'
    ),
   'customer_id' => $customer->getId(),
   'customer_card_id' => $customer_card->getId(),
   'note' => 'No. invoice '.$invoice
   ));
 print_r($result);
} catch (Exception $e) {
 echo 'Exception when calling TransactionsApi->charge: ', $e->getMessage(), PHP_EOL;
}

I can't figure out what is wrong with the postal-code, and of course I test with 94103 following documentation 

please help! :'(

4,937 Views
Message 1 of 5
Report
1 Best Answer

Best Answer

I know I'm late to the party but maybe I can help a few people who are struggling with this. Notice where it says 'postalCode' => ... it should be 'postal_code' => instead. I am very new to the Square API and sorting through the different documentation articles and examples and sifting through typos and incomplete examples... I think Square is a great processing system and the less time us devs have to sift through incomplete info to get working templates, the faster it will grow, and the more supported it will eventually be, hopefully. 

 

I also didn't manage to get the variable postal code from the nonce, I request the billing zip code in another input box and then process that after the form is submitted. 

View Best Answer >

4,512 Views
Message 4 of 5
Report
4 REPLIES 4
Alumni

Hi @Yulianita - thanks for making your first post to the 👥 community. 

 

I'll be honest - I don't have experience as a developer, but I wanted to make sure your post got a *bump* in case there is another seller on the community that can help you out. 

 

We do have a developer community on Stack Overflow - make sure to reach out there for help too. 

4,891 Views
Message 2 of 5
Report

Okay @Russell, thank you, I appreciate it.

4,884 Views
Message 3 of 5
Report

Best Answer

I know I'm late to the party but maybe I can help a few people who are struggling with this. Notice where it says 'postalCode' => ... it should be 'postal_code' => instead. I am very new to the Square API and sorting through the different documentation articles and examples and sifting through typos and incomplete examples... I think Square is a great processing system and the less time us devs have to sift through incomplete info to get working templates, the faster it will grow, and the more supported it will eventually be, hopefully. 

 

I also didn't manage to get the variable postal code from the nonce, I request the billing zip code in another input box and then process that after the form is submitted. 

4,513 Views
Message 4 of 5
Report

A life saver. I spend 2 hrs researching the problem until I found this post.

 

Thanks!

4,215 Views
Message 5 of 5
Report