Trying to set up iOS and PHP payment system using Braintree.
I can set up a clientToken with
$clientToken["client_token"] = Braintree_ClientToken::generate()); return ($clientToken);
And I can process a first payment with:
$result = Braintree_Transaction::sale(array( 'amount' => '1', 'paymentMethodNonce' => $nonce, 'customer' => array( 'id' => 'testId', 'firstName' => 'John', 'lastName' => 'Doe', 'email' => '[email protected]', ), 'options' => array( 'submitForSettlement' => true, 'storeInVaultOnSuccess' => true, ) ));
However when I try to process a second payment I get the error:
91609 – Customer ID has already been taken.
How can I process a second payment for the same customer with the same customerId (‘testId’) – why does it throw an error when I try to pass the payment with an existing customer ID? Surely it should just attach the payment to that same customer? Isn’t that what it’s there for?
So after looking around a bit more I found another field I can include in the Braintree_Transaction::sale as follows:
'customerId' => 'testId',
So this will allow me to re-use a customerId stored in the Braintree vault. However for a first time transaction I get the error:
91510 – Customer ID is invalid.
So I end up in a catch 22 – I can use the first set of code for a new customer but not repeat customers and I can use the second for repeat customers but not new. I cannot use both together. So my solution is to create my own local database entry that determines whether a user has paid via braintree before or not and substitute the code accorrdingly. Is there a more streamlined approach?
I work at Braintree. If you need more help, you can always reach out to our support team.
You’ve got the right idea. You need track on your side whether a customer ID exists with Braintree.
There is an alternative, but I don’t recommend it, as it takes an extra API call.
You can first try to create the customer with Braintree, ignoring an error if the error code is
$result = Braintree_Customer::create(array( 'id' => 'testId', 'firstName' => 'John', 'lastName' => 'Doe', 'email' => '[email protected]', ));
Then, you know that either the customer already existed, or you just created it, and you can use your second method to create a transaction.
There is other alternative, instead of looking in your database, you can use find(‘a_customer_id’) in order to see if Braintree has already the user id. And then chose your first approach or the second one.