user4033772
user4033772

Reputation:

Generate client token with braintree in iOS + php

I'm working on Brain tree for the first time and getting issues in very first step. I'm not able to access drop-in functionality with iOS + php

I need to create client token its not working with these code and I really don't understand what is problem. please tell me hows generate client token.

$clientToken = Braintree_ClientToken::generate(array(
"customerId" => $aCustomerId
));

Upvotes: 1

Views: 1482

Answers (1)

contool
contool

Reputation: 1074

I just came across this same error in iOS - xCode was telling me that the client_token was in the wrong format, it was expecting an associative array but the code above simply returns a single object. Try this instead - worked for me:

$aCustomerId = '';

$clientToken["client_token"] = Braintree_ClientToken::generate(array("customerId" => $aCustomerId));
return ($clientToken);

Note I'm not setting a customerId here - you could put one in, or you could remove it entirely from the code i.e. remove 'array("customerId" => $aCustomerId)' completely. it should work either way.

NB: This is in the server side PHP script, not XCode by the way

Edit: When you create a customer using a braintree function (either ::create or ::sale) you can assign an 'id' of your choosing under the 'customer' array e.g.:

$result = Braintree_Transaction::sale(array(
        'amount' => $value,
        'customer' => array(
            'id' => $anIdOfYourChoosing,
)
));

Then the next time you create a ::sale you can call 'customerId' within the sale array and it will use that customer's previously set payment details e.g.

$result = Braintree_Transaction::sale(array(
            'amount' => $value,
            'customerId' => $anIdOfYourChoosing,
    )
    ));

So in the clientToken case if you pass a value into $aCustomerId, it will search the Braintree vault for that customerId and give you a token for that customer (provided the customer was previously created). It isn't explained very well in the Braintree guides at the moment. Hope that helps

Upvotes: 1

Related Questions