x

Creating Customer Card

This is my code:

 

$nonce = $_POST['nonce'];  // from square form on prev. page

 

$request_body = array(
'card_nonce' => $nonce,
'amount_money' => array(
'amount' => (int)$_POST['amount'],
'currency' => 'USD'
),
'idempotency_key' => uniqid()
);

 

$cust_obj = array(
'given_name' => $_SESSION['firstname'],
'family_name' => $_SESSION['lastname'],
'email_address' => $_SESSION['email'],
'phone_number' => $_SESSION['cellPhone']
);

 

$result = $transaction_api->charge($access_token, $location_id, $request_body);
$newCustomer = $customer_api->createCustomer($access_token, $cust_obj)->getCustomer();

$body = array(
'card_nonce' => $nonce,
'cardholder_name' => $customer['given_name'].' '.$customer['family_name']
);
$customerCard = $customerCard_api->createCustomerCard($access_token, $newCustomer->getId(), $body);

 

This is my error:

 

object(stdClass)#43 (3) {
      ["category"]=>
      string(21) "INVALID_REQUEST_ERROR"
      ["code"]=>
      string(15) "CARD_TOKEN_USED"
      ["detail"]=>
      string(50) "Card nonce already used; please request new nonce."

 

I use the same nonce that was generated from the SquareForm on prev. page to create the the transaction, then the customer and then the customerCard. 

The transaction and customer get recorded fine, but creating the customerCard fails with that error. What am I missing here? Shouldn't/couldn't the same nonce be used to create the customerCard as well?

Tags (1)
5,318 Views
Message 1 of 6
Report
1 Best Answer
Alumni

Best Answer

This Error means that you are trying to use the card nonce more than once. 

 

You should create the customer first, create the customer card, and then charge that card on file. Does that make sense? 

 

Instead of charging, creating customer and adding the card on file. If you wan't to charge first, you'll have to request a new nonce. 

View Best Answer >

7,499 Views
Message 2 of 6
Report
5 REPLIES 5
Alumni

Best Answer

This Error means that you are trying to use the card nonce more than once. 

 

You should create the customer first, create the customer card, and then charge that card on file. Does that make sense? 

 

Instead of charging, creating customer and adding the card on file. If you wan't to charge first, you'll have to request a new nonce. 

7,500 Views
Message 2 of 6
Report

Ok, cool, thank you.

5,287 Views
Message 3 of 6
Report

Hi tristansokol,

I am using this same flow but still getting the "Card nonce already used; please request new nonce." error.

 

I'm requesting and receiving a nonce, creating a customer, saving the card, then attempting an authorization but it fails. If I do NOT save the customer card, it works.

 

I can see the response of saving the card returns an id. Do I need to use that id as the customer_card_id and customer_id to create the transaction instead?

 

Is it saying the nonce has already been used to save the card therefor it cannot be used to charge as well?

4,137 Views
Message 4 of 6
Report
Alumni

Correct, you can either charge a chard or create a card on file with a card nonce. 

 

After you save the card on file you should create a charge request with the customer card id. You can see an example in Ruby here: Recurring payments

4,133 Views
Message 5 of 6
Report

Great, thank you for confirming this!

4,131 Views
Message 6 of 6
Report