Giter Site home page Giter Site logo

payouts-ruby-sdk's Introduction

PayPal Payouts API SDK for Ruby

PayPal Developer

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.

Prerequisites

  • Ruby 2.0.0 or above
  • Bundler

Installations

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

Usage

Binaries

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.

Setting up credentials

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)

Examples

Creating a Payouts

This code creates a Payout and prints the batch_id for the Payout.

Code:

# 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

Handle API Failure

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

Retrieve a Payout Batch

Pass the batch_id from the previous sample to retrieve Payouts batch details

Code:

# 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

Running tests

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

Samples

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.

License

Code released under SDK LICENSE

payouts-ruby-sdk's People

Contributors

rajkumarpalani avatar hdlahlou avatar hlahlou-pp-dev avatar artfuldodger avatar dependabot[bot] avatar sriramswamy avatar thnatesan-hw-dev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.