/Payouts-Python-SDK

Python SDK for Payouts RESTful APIs

Primary LanguagePythonOtherNOASSERTION

PayPal Payouts API SDK for Python

PayPal Developer

Welcome to PayPal Python SDK. This repository contains PayPal's NodeJS 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's.

Prerequisites

Python 2.0+ or Python 3.0+

An environment which supports TLS 1.2 (see the TLS-update site for more information)

Requirements

PayPalHttp can be found at https://pypi.org/project/paypalhttp/

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.

from paypalpayoutssdk.core import PayPalHttpClient, SandboxEnvironment


# Creating Access Token for Sandbox
client_id = "<<PAYPAL-CLIENT-ID>>"
client_secret = "<<PAYPAL-CLIENT-SECRET>>"
# Creating an environment
environment = SandboxEnvironment(client_id=client_id, client_secret=client_secret)
client = PayPalHttpClient(environment)

Examples

Creating a Payouts

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

Code:

from paypalpayoutssdk.payouts import PayoutsPostRequest
from paypalhttp import HttpError

# Construct a request object and set desired parameters
# Here, PayoutsPostRequest()() 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 5$ Payout!",
        "amount": {
            "currency": "USD",
            "value": "1.00"
        },
        "receiver": "payout-sdk-1@paypal.com",
        "sender_item_id": "Test_txn_1"
    }, {
        "note": "Your 5$ Payout!",
        "amount": {
            "currency": "USD",
            "value": "1.00"
        },
        "receiver": "payout-sdk-2@paypal.com",
        "sender_item_id": "Test_txn_2"
    }, {
        "note": "Your 5$ Payout!",
        "amount": {
            "currency": "USD",
            "value": "1.00"
        },
        "receiver": "payout-sdk-3@paypal.com",
        "sender_item_id": "Test_txn_3"
    }, {
        "note": "Your 5$ Payout!",
        "amount": {
            "currency": "USD",
            "value": "1.00"
        },
        "receiver": "payout-sdk-4@paypal.com",
        "sender_item_id": "Test_txn_4"
    }, {
        "note": "Your 5$ Payout!",
        "amount": {
            "currency": "USD",
            "value": "1.00"
        },
        "receiver": "payout-sdk-5@paypal.com",
        "sender_item_id": "Test_txn_5"
    }]
}

request = PayoutsPostRequest()
request.request_body (body)

try:
    # 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
    print batch_id        
except IOError as ioe:
    print ioe
    if isinstance(ioe, HttpError):
        # Something went wrong server-side
        print ioe.status_code

Retrieve a Payout Batch

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

Code:

from paypalpayoutssdk.payouts import PayoutsGetRequest
# Here, PayoutsGetRequest() creates a GET request to /v1/payments/payouts/<batch-id>
request = PayoutsGetRequest("PAYOUT-BATCH-ID")

try:
    # 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
    print batch_status
except IOError as ioe:
    if isinstance(ioe, HttpError):
        # Something went wrong server-side
        print ioe.status_code
        print ioe.headers
	print ioe
    else:
        # Something went wrong client side
        print ioe

Running tests

To run integration tests using your client id and secret, clone this repository and run the following command:

$ pip install nose # if not already installed
$ PAYPAL_CLIENT_ID=your_client_id PAYPAL_CLIENT_SECRET=your_client_secret nosetests --exe

Samples

You can start off by trying out Payouts Samples

To try out different samples head to this link

Note: Update the paypal_client.py with your sandbox client credentials or pass your client credentials as environment variable whie executing the samples.

License

Code released under SDK LICENSE