Welcome to PayPal Ruby SDK. This repository contains PayPal's Ruby SDK and samples for v1/payments/payouts APIs.
This is a part of the next major PayPal SDK. It includes a simplified interface to only provide simple model objects and blueprints for HTTP calls. This repo currently contains functionality for PayPal Payouts APIs which includes Payouts.
Please refer to the PayPal Payouts Integration Guide for more information. Also refer to Setup your SDK for additional information about setting up the SDK.
- Ruby 2.0.0 or above
- Bundler
Add this line to your application's Gemfile:
gem 'paypal-payouts-sdk'
And then execute:
$ bundle
Or install it yourself as:
$ gem install paypal-payouts-sdk
It is not mandatory to fork this repository for using the PayPal SDK. You can refer PayPal Payouts SDK for configuring and working with SDK without forking this code.
For contributing or referring the samples, You can fork/refer this repository.
Get client ID and client secret by going to https://developer.paypal.com/developer/applications and generating a REST API app. Get Client ID and Secret from there.
require 'paypal-payouts-sdk'
# Creating Access Token for Sandbox
client_id = "PAYPAL-CLIENT-ID"
client_secret = "PAYPAL-CLIENT-SECRET"
# Creating an environment
environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
client = PayPal::PayPalHttpClient.new(environment)
This code creates a Payout and prints the batch_id for the Payout.
# Construct a request object and set desired parameters
# Here, PayoutsPostRequest.new creates a POST request to /v1/payments/payouts
body = {
sender_batch_header: {
recipient_type: 'EMAIL',
email_message: 'SDK payouts test txn',
note: 'Enjoy your Payout!!',
sender_batch_id: 'Test_SDK_1',
email_subject: 'This is a test transaction from SDK'
},
items: [{
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.00'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_1'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.00'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_2'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.00'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_3'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.00'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_4'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.00'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_5'
}]
}
request = PaypalPayoutsSdk::Payouts::PayoutsPostRequest.new
request.request_body(body)
begin
# Call API with your client and get a response for your call
response = client.execute(request)
# If call returns body in response, you can get the deserialized version from the result attribute of the response
batch_id = response.result.batch_header.payout_batch_id
puts batch_id
rescue PayPalHttp::HttpError => ioe
# Something went wrong server-side
puts ioe.status_code
puts ioe.headers["debug_id"]
end
This will create a Payout with validation failure to showcase how to parse the failed response entity. Refer samples for more scenarios
# Construct a request object and set desired parameters
# Here, PayoutsPostRequest.new creates a POST request to /v1/payments/payouts
body = {
sender_batch_header: {
recipient_type: 'EMAIL',
email_message: 'SDK payouts test txn',
note: 'Enjoy your Payout!!',
sender_batch_id: 'Test_SDK_1',
email_subject: 'This is a test transaction from SDK'
},
items: [{
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.0.0'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_1'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.0.0'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_2'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.0.0'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_3'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.0.0'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_4'
}, {
note: 'Your $1 Payout!',
amount: {
currency: 'USD',
value: '1.0.0'
},
receiver: '[email protected]',
sender_item_id: 'Test_txn_5'
}]
}
request = PaypalPayoutsSdk::Payouts::PayoutsPostRequest.new
request.request_body(body)
begin
# Call API with your client and get a response for your call
client.execute(request)
rescue PayPalHttp::HttpError => ioe
# Something went wrong server-side
puts "Status Code: #{ioe.status_code}"
puts "Response: #{ioe.result}"
puts "Name: #{ioe.result.name}"
puts "Message: #{ioe.result.message}"
puts "Information link: #{ioe.result.information_link}"
puts "Debug Id: #{ioe.result.debug_id}"
puts "Details: "
ioe.result.details.each { |detail|
puts "Error Location: #{detail["location"]}"
puts "Error Field: #{detail["field"]}"
puts "Error issue: #{detail["issue"]}"
}
end
Pass the batch_id from the previous sample to retrieve Payouts batch details
# Here, PayoutsGetRequest.new creates a GET request to /v1/payments/payouts/<batch-id>
request = PaypalPayoutsSdk::Payouts::PayoutsGetRequest.new("PAYOUT-BATCH-ID")
request.page(1)
request.page_size(10)
request.total_required(true)
begin
# Call API with your client and get a response for your call
response = client.execute(request)
# If call returns body in response, you can get the deserialized version from the result attribute of the response
batch_status = response.result.batch_header.batch_status
puts batch_status
rescue PayPalHttp::HttpError => ioe
# Something went wrong server-side
puts ioe.status_code
puts ioe.headers["debug_id"]
end
To run integration tests using your client id and secret, clone this repository and run the following command:
$ bundle install
$ PAYPAL_CLIENT_ID=YOUR_SANDBOX_CLIENT_ID PAYPAL_CLIENT_SECRET=YOUR_SANDBOX_CLIENT_SECRET rspec spec
You can start off by trying out Payouts Samples
To try out different samples head to this link
Note: Update the paypal_client.rb
with your sandbox client credentials or pass your client credentials as environment variable while executing the samples.
Code released under SDK LICENSE