I plan to update the default credit card through my system but I read here that it's not possible and that it should just be a newly created credit card. I'm following this documentation. I have 2 payment methods in my sandbox account.
Here's my code:
function create_cc($customer_id, $attributes){
//Array ( [token] => j699th [cvv] => 222 [number] => 4005519200000004 [expirationMonth] => 12 [expirationYear] => 22 )
$result = Braintree_Configuration::gateway()->paymentMethodNonce()->create($attributes['token']);
return Braintree_Configuration::gateway()->paymentMethod()->create([
'customerId' => $customer_id,
'paymentMethodNonce' => $result->paymentMethodNonce->nonce,
'cvv' => $attributes['cvv'],
'number' => $attributes['number'],
'expirationMonth' => $attributes['expirationMonth'],
'expirationYear' => $attributes['expirationYear'],
'options' => [
'makeDefault' => true,
'verifyCard' => true
]
]);
}
When the function above is called, it returns true so I'm expecting Payment Methods to be 3 in Braintree. But when I reload the page, I still get 2 payment methods. I made sure I have paymentMethodNonce'
in my array data.
UPDATE:
Sandbox: It needs to be 'fake-valid-nonce' and not an actual one.
Production: not working.
Here's my code:
function create_cc($customer_id, $attributes){
$nonce = '';
if($this->environment == 'production'){
$result = $this->gateway->paymentMethodNonce()->create($this->create_client_token());//$this->gateway->paymentMethodNonce()->create($this->create_client_token());
$nonce = $result->paymentMethodNonce->nonce;
} else if($this->environment == 'sandbox'){
$nonce = 'fake-valid-nonce';
}
return $this->gateway->paymentMethod()->create([
'customerId' => $customer_id,
'paymentMethodNonce' => $nonce,
//'cvv' => $attributes['cvv'],
'number' => $attributes['number'],
'expirationMonth' => $attributes['expirationMonth'],
'expirationYear' => $attributes['expirationYear'],
'options' => [
'makeDefault' => true,
'failOnDuplicatePaymentMethod' => true,
//'verifyCard' => true
]
]);
}
What could I be doing wrong?