Creating Transactions

Examples on this page are for creating transactions using the server-to-server API. Transactions 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.

Minimal Example

The only required attributes to create a transaction are the amount, credit card number, and credit card expiration date.

$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationDate' => '05/12'
  )
));

Note: Before you actually receive funds for authorization transactions, you have to submit them for settlement.

For other transaction details that can be provided, see the full example.

Result Handling

See the page on transaction result handling for information on how to check for validation errors and examine processor responses.

Full Example

Here is an example of passing in everything possible. Some parameters are mutually exclusive, so we’ll show those in separate examples. If you’re creating transactions using Transparent Redirect, all of these attributes can be included in the TR data, or you can create html input fields for them. If there is anything you want to store with the transaction details that we don’t have a field for, you can always use custom fields.

$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'orderId' => 'order id',
  'merchantAccountId' => 'a_merchant_account_id',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationDate' => '05/2012',
    'cardholderName' => 'The Cardholder',
    'cvv' => 'cvv'
  ),
  'customer' => array(
    'firstName' => 'Drew',
    'lastName' => 'Smith',
    'company' => 'Braintree',
    'phone' => '312-555-1234',
    'fax' => '312-555-1235',
    'website' => 'http://www.example.com',
    'email' => 'drew@example.com'
  ),
  'billing' => array(
    'firstName' => 'Paul',
    'lastName' => 'Smith',
    'company' => 'Braintree',
    'streetAddress' => '1 E Main St',
    'extendedAddress' => 'Suite 403',
    'locality' => 'Chicago',
    'region' => 'Illinois',
    'postalCode' => '60622',
    'countryCodeAlpha2' => 'US'
  ),
  'shipping' => array(
    'firstName' => 'Jen',
    'lastName' => 'Smith',
    'company' => 'Braintree',
    'streetAddress' => '1 E 1st St',
    'extendedAddress' => 'Suite 403',
    'locality' => 'Bartlett',
    'region' => 'Illinois',
    'postalCode' => '60103',
    'countryCodeAlpha2' => 'US'
  ),
  'options' => array(
    'submitForSettlement' => true
  )
));

Expiration Date

Expiration month and year can be passed as separate fields rather than as a single field.

$result = Braintree_Transaction::sale(array(
  'amount' => '10.00',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationMonth' => '05',
    'expirationYear' => '2012',
  )
));

Country

You can provide country using any of the ISO 3166-1 country codes.

In version 2.4.0 you can specify the country using the name, alpha-2 code, alpha-3 code, or numeric code. In earlier verions, you can only use the country name.

$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationDate' => '05/2012',
  ),
  'billing' => array(
    'countryCodeAlpha2' => 'US'
  )
));
$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationDate' => '05/2012',
  ),
  'billing' => array(
    'countryCodeAlpha3' => 'USA'
  )
));
$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationDate' => '05/2012',
  ),
  'billing' => array(
    'countryCodeNumeric' => '840'
  )
));
$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'creditCard' => array(
    'number' => '5105105105105100',
    'expirationDate' => '05/2012',
  ),
  'billing' => array(
    'countryName' => 'United States of America'
  )
));

Storing in Vault

When storing in the vault can you specify customer ID and credit card token.

$result = Braintree_Transaction::sale(array(
  'amount' => '10.00',
  'creditCard' => array(
    'token' => 'a_token',
    'number' => '5105105105105100',
    'expirationDate' => '05/2012'
  ),
  'customer' => array(
    'id' => 'a_customer_id'
  ),
  'billing' => array(
    'postalCode' => '60090'
  ),
  'shipping' => array(
    'streetAddress' => '1 E Main St',
    'locality' => 'Chicago',
    'region' => 'Illinois',
    'postalCode' => '60622'
  ),
  'options' => array(
    'storeInVault' => true,
    'addBillingAddressToPaymentMethod' => true,
    'storeShippingAddressInVault' => true
  )
));

Creating from Vault

When creating transactions from the vault you can provide the customer ID and/or payment method token instead of credit card details.

$result = Braintree_Transaction::sale(array(
  'amount' => '10.00',
  'customerId' => 'the_customer_id',
  'paymentMethodToken' => 'the_payment_method_token'
));

See Also

Options that are available when creating a transaction: