Creating Credit Cards
Credit cards stored in the vault need to be associated to a customer. If you want to create both the customer and credit card simultaneously, you should use the customer create API. This API is for adding a credit card to an existing customer. Credit cards can also be created using Transparent Redirect. The server-to-server API is easier to understand, so even if you’re planning to use TR, you may want to read this page first.
Example
The only attributes required on a credit card are number and expiration date. You can pass expirationDate as a single field or as expirationMonth and expirationYear separately.
$result = Braintree_CreditCard::create(array(
'customerId' => '12345',
'number' => '5105105105105100',
'expirationDate' => '05/12'
'cardholderName' => 'The Cardholder'
));
$result->success;
# true
$result->creditCard->token;
# Generated credit card token
This will add the credit card to the existing customer, but will not verify that the credit card number is for a valid, open account. To do that, you will need to use the card verification API.
If you do not specify the credit card token, as in the example above, the gateway will generate a random one. To specify what you would like the token to be pass it with the other parameters.
$result = Braintree_CreditCard::create(array(
'customerId' => '12345',
'number' => '5105105105105100',
'expirationDate' => '05/2011',
'token' => 'the_token'
));
$result->success;
# true
$result->creditCard->token;
# Generated credit card token
Create with Billing Address
You can also specify the billing address when creating the credit card.
$result = Braintree_CreditCard::create(array(
'customerId' => '12345',
'number' => '5105105105105100',
'expirationDate' => '05/2012',
'billingAddress' => array(
'firstName' => 'Jenna',
'lastName' => 'Smith',
'company' => 'Braintree',
'streetAddress' => '1 E Main St',
'extendedAddress' => 'Suite 403'
'locality' => 'Chicago',
'region' => 'IL',
'postalCode' => '60622',
'countryCodeAlpha2' => 'US'
)
));
$result->success;
# true
$result->creditCard->billingAddress->streetAddress;
# '1 E Main St'
Create with Existing Billing Address
If a customer already has an address you’d like to use, you can attach the existing address to the new credit card.
$result = Braintree_CreditCard::create(array(
'customerId' => '12345',
'number' => '5105105105105100',
'expirationDate' => '05/2012',
'billingAddressId' => 'theBillingAddressId'
));
New Default Credit Card
If a customer has multiple credit cards, the first card created will be the customer’s default card. The default card is used when creating transactions from vault customersTo set a new card as the default, use the makeDefault option.
$result = Braintree_CreditCard::create(array(
'customerId' => '12345',
'number' => '5105105105105100',
'expirationDate' => '05/12'
'cardholderName' => 'The Cardholder',
'options' => array(
'makeDefault' => true
)
));
$result->creditCard->isDefault();
# true