Set Up Your Server
Install and configure
Add the braintree
gem to your project's Gemfile:
- Ruby
gem "braintree", "~> {{serverSdk.ruby.release.configVersion}}"
And run bundle install
to install it.
Construct your gateway object
In your code, you must first construct a gateway
object with the environment-specific (sandbox or production) access token provided in your PayPal dashboard:
- Ruby
gateway = Braintree::Gateway.new(
:access_token => use_your_access_token
)
This must be called before performing any method (e.g. client token generation, authorizations, captures).
Generate a client token
Your server is responsible for generating a client token, which contains all the authorization and configuration information your client needs to initialize the client SDK to communicate with Braintree. The client-side page covers the client side of the exchange.
Send a client token to your client
Here is an example of how your server would generate and expose a client token:
- Ruby
get "/client_token" do
gateway.client_token.generate
end
How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, although you could load the client token from an AJAX call instead. These methods are discussed in the client token setup section.
Receive a payment method nonce from your client
Once your customer has successfully completed the PayPal checkout flow your client will receive 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 creating a transaction.
- Ruby
post "/checkout" do
nonce = params[:payment_method_nonce]
# Use payment method nonce here...
end
Create a transaction
You can create a transaction using a payment_method_nonce
and an amount
.
Collect device data from the client and include the device_data_from_the_client
in the transaction.
Below is an example of all relevant PayPal parameters you can pass in your transaction call:
- Ruby
gateway = Braintree::Gateway.new(
:access_token => use_your_access_token
)
result = gateway.transaction.sale(
:amount => "10.00",
:merchant_account_id => "USD",
:payment_method_nonce => params[:payment_method_nonce],
:device_data => params[:device_data],
:order_id => "Mapped to PayPal Invoice Number",
:descriptor => {
:name => "Descriptor"
},
:shipping => {
:first_name => "Jen",
:last_name => "Smith",
:company => "Braintree",
:street_address => "1 E 1st St",
:extended_address => "Suite 403",
:locality => "Bartlett",
:region => "IL",
:postal_code => "60103",
:country_code_alpha2 => "US"
},
:options => {
:paypal => {
:custom_field => "PayPal custom field",
:description => "Description for PayPal email receipt",
},
},
)
if result.success?
"Success ID: #{result.transaction.id}"
else
result.message
end
Specify currency
The customer will be charged in the currency you specify in the merchant_account_id
field on the transaction call. You should pass the same currency here that you specified on the client side.
The merchant_account_id
field accepts the 3-letter currency code of each of the currencies currently enabled for your account. You can see these currencies on the My Apps & Credentials page in your PayPal Developer Dashboard.
Shipping addresses
PayPal validates the shipping address, so it must follow the PayPal address conventions.
Seller Protection
By passing a shipping address, you may also be eligible for PayPal Seller Protection. You can check the status of Seller Protection as follows:
transaction = braintree.gateway.find("the_transaction_id")
transaction.paypal_details.seller_protection_status
# => "ELIGIBLE"
See also
The below are links to reference pages used by merchants performing direct integrations with Braintree. If you have questions about your Express Checkout integration, please be sure to contact PayPal Support rather than Braintree Support.
- Braintree's transaction result object
- Braintree's refund reference
- Braintree's submit for settlement reference
- Braintree's void reference
- Braintree's validation error overview and PayPal validation errors
- Braintree's statuses
Next Page: Testing and Go Live →