See also the Braintree_PaymentMethod response object.

To create a new payment method for an existing customer, the only required attributes are the customer ID and payment method nonce.

note

You can also create a payment method when you create a customer.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => '12345',
    'paymentMethodNonce' => nonceFromTheClient
]);
Parameters
'billingAddress'

A billing address associated with a specific customer ID. It can be further associated with a specific payment method. The maximum number of addresses per customer is 50.

'company' string

Company name. 255 character maximum.

'countryCodeAlpha2' string

The ISO 3166-1 alpha-2 country code specified in an address. The gateway only accepts specific alpha-2 values.

'countryCodeAlpha3' string

The ISO 3166-1 alpha-3 country code specified in an address. The gateway only accepts specific alpha-3 values.

'countryCodeNumeric' string

The ISO 3166-1 numeric country code specified in an address. The gateway only accepts specific numeric values.

'countryName' string

The country name specified in an address. Braintree only accepts specific country names.

'extendedAddress' string

The extended address information—such as apartment or suite number. 255 character maximum.

'firstName' string

The first name. The first name value must be less than or equal to 255 characters.

'lastName' string

The last name. The last name value must be less than or equal to 255 characters.

'locality' string

The locality/city. 255 character maximum.

'postalCode' string

The postal code. Postal code must be a string of 4-9 alphanumeric characters, optionally separated by a dash or a space. Spaces, hyphens, and all other special characters are ignored.

'region' string

The state or province. For PayPal addresses, the region must meet PayPal's state restrictions; for all other payment methods, it must be less than or equal to 255 characters.

'streetAddress' string

The street address. Street address must be less than or equal to 255 characters. Required to perform card verification when AVS rules are configured to require street address.

'billingAddressId' string

The two-letter value for an address associated with a specific customer ID. The maximum number of addresses per customer is 50.

'cardholderName' string

The cardholder name associated with the credit card. 175 character maximum.

'customerId' required, string

A string value representing an existing customer in your Vault that you want to create a payment method for.

'cvv' string

Typically requires PCI SAQ D compliance

We recommend using paymentMethodNonce to avoid any PCI concerns with raw credit card data being present on your server.

A 3 or 4 digit card verification value assigned to a credit card. The CVV will never be stored in the gateway, but it can be provided with one-time requests to verify the card.

'deviceData' string

Customer device information. Passing this value is strongly recommended when using Advanced Fraud Tools and adding credit card data to your Vault. Not recommended when vaulting PayPal or Venmo accounts.

'expirationDate' string

Typically requires PCI SAQ D compliance

We recommend using paymentMethodNonce to avoid any PCI concerns with raw credit card data being present on your server.

The expiration date of a credit card, formatted MM/YY or MM/YYYY. May be used instead of expirationMonth and expirationYear.

'expirationMonth' string

Typically requires PCI SAQ D compliance

We recommend using paymentMethodNonce to avoid any PCI concerns with raw credit card data being present on your server.

The expiration month of a credit card, formatted MM. May be used with expirationYear, and instead of expirationDate.

'expirationYear' string

Typically requires PCI SAQ D compliance

We recommend using paymentMethodNonce to avoid any PCI concerns with raw credit card data being present on your server.

The two or four digit year associated with a credit card, formatted YYYY or YY. May be used with expirationMonth, and instead of expirationDate.

'number' string

Typically requires PCI SAQ D compliance

We recommend using paymentMethodNonce to avoid any PCI concerns with raw credit card data being present on your server.

The 12-19 digit value on a credit card consisting of a bank identification number (BIN) and primary account number (PAN).

'options'

Optional values that can be passed with a request.

'failOnDuplicatePaymentMethod' bool

If this option is passed and the payment method has already been added to the Vault, the request will fail. This option will not work with PayPal payment methods.

'makeDefault' bool

This option makes the specified payment method the default for the customer.

'verificationAmount' string

Specify a non-negative amount that you want to use to verify a card. If you do not pass this option, the gateway will automatically use a verification amount of $0 or $1, depending on the processor and/or card type.

'verificationMerchantAccountId' string

Specify the merchant account ID that you want to use to verify a card. See the merchantAccountId on Braintree_Transaction::sale() to learn more. The merchant account can't be a marketplace sub-merchant account. See the Braintree Marketplace Guide to learn more.

'verifyCard' bool

If the payment method is a credit card, this option prompts the gateway to verify the card's number and expiration date. It also verifies the AVS and CVV information if you've enabled AVS and CVV rules. If you want to verify all cards before they are stored in your Vault, you can turn on card verification for your entire account in the Control Panel.

In some cases, cardholders may see a temporary authorization on their account after their card has been verified. The authorization will fall off the cardholder's account within a few days and will never settle.

Only returns a Braintree_CreditCardVerification result if verification runs and is unsuccessful.

'paymentMethodNonce' required, string

One-time-use reference to payment information provided by your customer, such as a credit card or PayPal account.

The ability to transact on credit card information will not be verified automatically on create – you must explicitly perform card verification.

'token' string

An alphanumeric value that references a specific payment method stored in your Vault. Must be less than or equal to 36 characters. If using a custom integration, you can specify what you want the token to be. If not specified, the gateway will generate one that can be accessed on the result. If using our Drop-in UI, you can't specify your own token. Length and format of gateway-generated tokens and IDs may change at any time.

Examples

Specify a token

If you are using a custom integration and want to specify what you would like the token to be, pass it with the other parameters, as in the example below. If you do not specify a payment method token, the gateway will generate a random one. Note that if you are using our Drop-in UI, you can't specify your own token.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => '12345',
    'paymentMethodNonce' => nonceFromTheClient,
    'token' => 'the_token'
]);

New default payment method

If a customer has multiple payment methods, the first payment method created will be the customer's default. The default payment method is used when creating transactions with customerId. To set a new payment method as the default, use the makeDefault option.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => '12345',
    'paymentMethodNonce' => nonceFromTheClient,
    'options' => [
      'makeDefault' => true
    ]
]);
$result->paymentMethod->isDefault();
# true

New payment method with billing address

To specify a billing address when creating a new payment method, use the billingAddress option (ignored if the payment method is a PayPal account). Note that the specified billing address will override any address specified during nonce creation.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => '12345',
    'paymentMethodNonce' => nonceFromTheClient,
    'billingAddress' => [
      'streetAddress' => '123 Abc Way'
    ]
]);

New payment method with existing billing address

If a customer already has an address you'd like to use, you can create a new payment method with that address.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => '12345',
    'paymentMethodNonce' => nonceFromTheClient,
    'billingAddressId' => 'the-billing-address-id'
]);

Preventing duplicate payment methods

If a payment method exists in your Vault and you do not want to create a new payment method, you can reject duplicates by using the failOnDuplicatePaymentMethod option. Note: This option will not work with PayPal payment methods.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => '12345',
    'paymentMethodNonce' => nonceFromTheClient,
    'options' => [
      'failOnDuplicatePaymentMethod' => true
    ]
]);

Card verification

By default we will run credit card validations but not perform verification. Set the $verifyCard option to verify the card.

PHP
Copy
Copied
$result = Braintree_PaymentMethod::create([
    'customerId' => 'the_customer_id',
    'paymentMethodNonce' => nonceFromTheClient,
    'options' => [
        'verifyCard' => true
    ]
]);
important

If you are using our Advanced Fraud Tools, we strongly recommend passing $deviceData each time you verify a card.

Payment method nonces vs. raw card data

While it is possible to pass both raw card data and a payment method nonce in the same call, we recommend passing only a payment method nonce.

Passing both will result in a payment method that has a mix of their attributes, with precedence given to the fields individually, then to the attributes of the payment method nonce. For example, if you pass both a card number and a payment method nonce, the payment method will have the number you passed explicitly, but the rest of the attributes will be obtained through the nonce.

Still have questions?

If you can’t find an answer, contact our Support team.