Server-side Implementation
Create payment method
Once the customer has successfully authenticated with PayPal, you can use one of the following calls
to create a payment method. You will be using this payment method for processing transactions
against the PayPal Order.
Create a new customer with a payment method
If you do not already have a record for this customer in your Vault, you can create a new customer
with a payment method using
Customer: Create with the
payment_method_nonce
parameter.
- Ruby
result = gateway.customer.create({
:first_name => "Charity",
:last_name => "Smith",
:payment_method_nonce => nonce_from_the_client
})
if result.success?
puts result.customer.id
puts result.customer.payment_methods[0].token
else
p result.errors
endUpdate an existing customer with a payment method
If the customer already exists in your Vault, you can add a new payment method to that customer
using Customer: Update with the
payment_method_nonce
parameter.
- Ruby
transaction = gateway.transaction.find("the_transaction_id")
transaction.paypal_details.seller_protection_status
# "ELIGIBLE"Braintree::NotFoundError. Alternatively, you can use
Payment Method: Create to accomplish the
same thing as above.
Updating PayPal Payment Resource After Buyer Approval
In certain edge cases, merchants may need to update some transaction parameters, such as the total amount, after the buyer has approved the payment in the PayPal checkout window. In such cases, it is essential to include updated line items in the server-side call to ensure the final amount reflects the correct total.
These updates can include modifications to line items, currency, custom fields, or any other supported parameter.
To complete an update, use the PayPalPaymentResource.update() method on the server side after tokenization of the payment resource and before calling Customer.create() to vault the payment method.
Note The paymentMethodNonce is a required parameter. The update() call returns a new nonce, which must be used with Customer.create() to ensure the updated payment resource is properly vaulted. In addition to the required parameter, merchants can include any optional fields in the update call based on what needs to be modified. The sample code snippet below includes the full list of supported parameters. You can update only the fields relevant to your specific use case.
- Ruby
describe Braintree::PayPalPaymentResource do
describe "self.update" do
it "successfully updates a payment resource" do
nonce = nonce_for_paypal_account(
:intent => "order",
:payment_token => "fake-paypal-payment-token",
:payer_id => "fake-paypal-payer-id",
)
result = Braintree::PayPalPaymentResource.update(
:amount => BigDecimal("55.00"),
:amount_breakdown => {
:discount => BigDecimal("15.00"),
:handling => BigDecimal("0.00"),
:insurance => BigDecimal("5.00"),
:item_total => BigDecimal("45.00"),
:shipping => BigDecimal("10.00"),
:shipping_discount => BigDecimal("0.00"),
:tax_total => BigDecimal("10.00"),
},
:currency_iso_code => "USD",
:custom_field => "0437",
:description => "This is a test",
:line_items => [{
:description => "Shoes",
:image_url => "https://example.com/products/23434/pic.png",
:kind => "debit",
:name => "Name #1",
:product_code => "23434",
:quantity => "1",
:total_amount => BigDecimal("45.00"),
:unit_amount => BigDecimal("45.00"),
:unit_tax_amount => BigDecimal("10.00"),
:url => "https://example.com/products/23434",
}],
:order_id => "order-123456789",
:payee_email => "[email protected]",
:payment_method_nonce => nonce,
:shipping => {
:country_name => "United States",
:country_code_alpha2 => "US",
:country_code_alpha3 => "USA",
:country_code_numeric => "484",
:extended_address => "Apt. #1",
:first_name => "John",
:international_phone => {
:country_code => "1",
:national_number => "4081111111",
},
:last_name => "Doe",
:locality => "Chicago",
:postal_code => "60618",
:region => "IL",
:street_address => "123 Division Street",
},
:shipping_options => [{
:amount => BigDecimal("10.00"),
:id => "option1",
:label => "fast",
:selected => true,
:type => "SHIPPING"
}],
)
expect(result.success?).to eq(true)
expect(result.payment_method_nonce).not_to be_nil
expect(result.payment_method_nonce.nonce).not_to be_nil
endProcess transactions
Use the transaction API to process customer payments against the PayPal Order:
- Create a transaction using payment information
- Submit a transaction for settlement to get paid
- Find a transaction
- Search for transactions that match specific criteria
- Void a transaction to cancel it before it settles
- Refund a settled transaction to return funds to the customer
Currency support
The customer will be charged in the currency associated with the
merchant_account_id passed in the
Transaction: Sale call. We support
all currencies that PayPal REST APIs support. For details on accepting foreign currencies with PayPal, see our
PayPal account setup guide.
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:
- Ruby
transaction = gateway.transaction.find("the_transaction_id")
transaction.paypal_details.seller_protection_status
# => "ELIGIBLE"Void an order
As the PayPal Order is represented by a customer's payment method, to void an order you need to
delete the payment method by calling
Payment Method: Delete.