Creating a Transaction

Follow step-by-step instructions to create and search transactions, and practice it yourself.

Create a transaction

To create a transaction, we use the transaction.sale() method from the Braintree gateway. This method requires, at least, the following two parameters:

  1. Amount
  2. Payment method nonce

In the code below, we are creating a transaction in lines 12–19. We have specified the amount and the payment method nonce in line 13 and line 15 respectively. Moreover, we provide an option in line 17 as an instruction that the transaction should be submitted for settlement immediately.

Apart from that, the code also requires an API key named order_id. For now, you can provide any random value for this key.

The sale() method returns a transaction result object containing details regarding the request, including the transaction amount, status, and customer information. We have saved this object in result in line 12. This object is used to check whether the transaction has been created and an appropriate message is printed in lines 21–24.

Get hands-on with 1200+ tech skills courses.