Simple Ruby Server

Create a sandbox account

If you haven't already, sign up for a free Braintree sandbox account:

Sign Up for a Braintree Sandbox Account

Log in to obtain your sandbox API credentials. You'll need your:

  • Sandbox Merchant ID
  • Public Key
  • Private Key

Use these credentials for your development and testing. When you go live, you will need to replace your sandbox API credentials with production API credentials.

Install and configure

Add the braintree gem to your project's Gemfile:

gem "braintree"
Ruby

Supports ruby 1.8 (>= 1.8.6), 1.9, and 2.0

And run bundle install to install it.

In your code, configure the environment and API credentials:

Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = "use_your_merchant_id"
Braintree::Configuration.public_key = "use_your_public_key"
Braintree::Configuration.private_key = "use_your_private_key"
Ruby

Generate a client token

Your server is responsible for generating a client token, which contains all authorization and configuration information your client needs to initialize the client SDK to communicate with Braintree. Hello Client! covers the client side of the exchange.

Send a client token to your client

Mobile clients need to make a request to your server to fetch a client token. Create a server endpoint to send client tokens to the device.

get "/client_token" do
  Braintree::ClientToken.generate(
    :customer_id => a_customer_id
  )
end
Ruby

Receive a payment method nonce from your client

Once your client successfully obtains a customer payment method, it receives a payment_method_nonce representing customer payment authorization, which it then sends to your server.

Your server implementation is then responsible for receiving the payment method nonce and using it appropriately.

post "/purchases" do
  nonce = params[:payment_method_nonce]
  # Use payment method nonce here...
end
Ruby

Create a transaction

You can create a transaction using a payment_method_nonce and an amount:

result = Braintree::Transaction.sale(
  :amount => "100.00",
  :payment_method_nonce => "nonce-from-the-client"
)
Ruby

To verify your integration, you can check in the Sandbox control panel, where transactions will immediately appear on success.

Testing in sandbox

Develop and test your code against your sandbox account before processing live transactions against a production account.

Braintree's sandbox environment provides test values (card numbers, amounts, CVVs, and postal codes) for you to simulate transactions with validated cards.

Test values

In order to simplify testing your server side code, the Braintree library provides static payment_method_nonce values for you to use in the Braintree sandbox.

Nonce Description
fake-valid-nonce A valid nonce that can be used to create a transaction
fake-consumed-nonce A nonce that has already been consumed (this will return a validation error)
fake-apple-pay-amex-nonce A nonce representing an Apple Pay request for an AmEx card number
fake-apple-pay-visa-nonce A nonce representing an Apple Pay request for an Visa card number
fake-apple-pay-mastercard-nonce A nonce representing an Apple Pay request for an MasterCard card number
fake-coinbase-nonce A nonce representing a Coinbase account
fake-paypal-one-time-nonce A nonce representing an unvaulted PayPal account that a user has authorized for the one time payment flow. You can use any email address and password you like when testing PayPal in Sandbox, but there are some restrictions to consider if doing end-to-end testing.
fake-paypal-future-nonce A nonce representing an unvaulted PayPal account that a user has authorized for the future payment flow. You can use any email address and password you like when testing PayPal in Sandbox, but there are some restrictions to consider if doing end-to-end testing.

When writing integrations tests against the Braintree sandbox environment, you may use these nonces in place of the nonce that would normally be returned from the client side integration.

Braintree::Test::Nonce::Transactable
Braintree::Test::Nonce::Consumed
Braintree::Test::Nonce::PayPalOneTimePayment
Braintree::Test::Nonce::PayPalFuturePayment
Ruby

Conclusion

At this point, you should be able to accept a payment method nonce and create a transaction in our Sandbox. When you're ready to start charging real money, transition over to our Production environment. We'll explain that process next.

Next: Go Live →