WePay

Batch Calls

Before reading this tutorial...

You should be familiar with the following concepts:

Batch calls allow API applications to make multiple API calls within a single API call. In addition, each call can be tied to a different access_token meaning batch API calls work with multiple users.

The maximum number of API calls that you can make in a single batch call is 50. In addition, you can make these calls without being throttled.

Create a batch object

To create a batch object, make the /batch/create call.

Arguments:

Parameter Required Type Description
client_id Yes Integer (64 bits) The ID for your API application. You can find it on your application's dashboard.
client_secret Yes String (255 chars) The secret for your API application. You can find it on your application's dashboard.
calls Yes Array of API calls An array of the API calls that you would like to make (see API call arguments below).

Within each index of the calls parameter, each API call has its own separate structure as follows:

Must pass access_token for each call

For each individual API call, you must pass the access_token of the user you are making the call on behalf of. If you do not, then you will you get an "access_denied" error with error code 1002.

The API call arguments:

Parameter Required Type Description
call Yes String (255 chars) The name of the API call you want to make (ie. /checkout/find).
authorization Yes if the API call requires authorization String (255 chars) The access token of the user that is making the API call.
reference_id No String (255 chars) A unique id that you can attach to an API call so that you can specifically identify that call.
parameters Only if the API call requires parameters Array The parameters required by the API call that you specified in the "call" parameter.
  • PHP
  • cURL
  • Ruby
  • Python
<?php
    // WePay PHP SDK - http://git.io/mY7iQQ
    require 'wepay.php';

    // application settings
    $account_id = 123456789;
    $client_id = 123456789;
    $client_secret = "1a3b5c7d9";
    $access_token = "1a3b5c7d9";

    // change to useProduction for live environments
    Wepay::useStaging($client_id, $client_secret);

    $wepay = new WePay($access_token);

    // create the pre-approval
    $response = $wepay->request('batch/create', array(
        'client_id'         => $client_id,
        'client_id'         => $client_secret,
        'calls'             => array(
                            array(
                                'call'          => '/checkout/create',
                                'authorization' => 'asd3522342f',
                                'reference_id'  => '1',
                                'parameters'    => array(
                                    'account_id' => '123',
                                    'amount'     => '10',
                                    'type'       => 'donation',
                                    'short_description' => 'batch call test'
                                )
                            ),
                            array(
                                'call'          => '/withdrawal/create',
                                'authorization' => 'bas123123a',
                                'reference_id'  => '2',
                                'parameters'    => array(
                                    'account_id' => '159',
                                    'amount'     => '100',
                                    'mode'       => 'iframe'
                                )
                            ),
                            array(
                                'call'          => '/preapproval/create',
                                'authorization' => 'abc1234567a',
                                'reference_id'  => '3',
                                'parameters'    => array(
                                    'account_id' => '1091',
                                    'amount'     => '50',
                                    'mode'       => 'iframe',
                                    'short_description' => 'batch call testing'
                                )
                            ),
                            )
    ));

    // display the response
    print_r($response);
?>
curl https://stage.wepay.com/v2/batch/create \
    -H "Authorization: Bearer STAGE_7106db01142fe50bdddbf457f7e34db6a7cfe487d4748a1377ba645b47ff68b6" \
    -H "Content-Type: application/json" \
    -d '{
    "client_secret": "1a3b5c7d9",
    "calls": [
        {
            "call": "/checkout/create",
            "parameters": {
                "account_id": "123",
                "amount": "10",
                "type": "donation"
           },
            "authorization": "STAGE_bca4151b046ba269363b76a0ad49d7168cd9e44597eecb41e792d78d10ee198f",
            "reference_id": "1"
        },
        {
            "call": "/withdrawal/create",
            "parameters": {
                "account_id": "159",
                "amount": "100",
                "mode": "iframe"
            },
            "authorization": "STAGE_33a7232e281fdc86584178ba8f3a8d01f1eff86015fe7eb9f00504ded46185ac",
            "reference_id": "2"
        },
        {
            "call": "/preapproval/create",
            "parameters": {
                "account_id": "1091",
                "amount": "50",
                "mode": "iframe",
                "short_description": "batch call testing"
            },
            "authorization": "STAGE_33a7232e281fdc86584178ba8f3a8d01f1eff86015fe7eb9f00504ded46185ac",
            "reference_id": "3"
        }
    ],
    "client_id": "123456789"
}'

				
# WePay Ruby SDK - http://git.io/a_c2uQ
require 'WePay_API_v2_Ruby_SDK.rb'

# application settings
account_id = 123456789
client_id = 123456789
client_secret = '1a3b5c7d9'
access_token = '1a3b5c7d9'

# set _use_stage to false for live environments
wepay = WePay.new(client_id, client_secret, _use_stage = true)

# create the pre-approval
response = wepay.call('/batch/create', access_token, {
    :client_id          => client_id,
    :client_secret      => client_secret,
    :calls              => {
            {
                :call => '/checkout/create',
                :authorization => 'asd3522342f',
                :reference_id  => '1',
                :parameters    => {
                    :account_id => '123',
                    :amount     => '10',
                    :type       => 'donation',
                    :short_description => 'batch call test'
                }
            }
            {
                :call => '/withdrawal/create',
                :authorization => 'bas123123a',
                :reference_id  => '2',
                :parameters    => {
                    :account_id => '159',
                    :amount     => '100',
                    :mode       => 'iframe'
                }
            }
            {
                :call => '/preapproval/create',
                :authorization => 'abc1234567a',
                :reference_id  => '3',
                :parameters    => {
                    :account_id => '1091',
                    :amount     => '50',
                    :mode       => 'iframe',
                    :short_description => 'batch call testing'
                }
            }
    }
})

# display the response
p response
# WePay Python SDK - http://git.io/v7Y1jA
from wepay import WePay

# application settings
account_id = 123456789
access_token = '1a3b5c7d9'
production = False

# set production to True for live environments
wepay = WePay(production, access_token)

# create the pre-approval
response = wepay.call('/batch/create', {
    'client_id': client_id,
    'client_secret': client_secret,
    'calls': {
            {
                'call': '/checkout/create',
                'authorization': 'asd3522342f',
                'reference_id': '1',
                'parameters': {
                    'account_id': '123',
                    'amount': '10',
                    'type': 'donation',
                    'short_description': 'batch call test'
                }
            }
            {
                'call': '/withdrawal/create',
                'authorization': 'bas123123a',
                'reference_id': '2',
                'parameters': {
                    'account_id': '159',
                    'amount': '100',
                    'mode': 'iframe'
                }
            }
            {
                'call': '/preapproval/create',
                'authorization': 'abc1234567a',
                'reference_id': '3',
                'parameters': {
                    'account_id': '1091',
                    'amount': '50',
                    'short_description': 'batch call testing',
                    'mode': 'iframe'
                }
            }
    }
})

# display the response
print response

Response:

Response Type Description
calls Array An array of responses to all the API calls made in the batch call.
{
   "calls": [
       {
         "call":"/checkout/create",
         "reference_id":"1",
         "response":{
            "checkout_id":12345,
            "checkout_uri":"https://stage.wepay.com/api/checkout/12345/4560de9",
         }
      },
      {
         "call":"/withdrawal/create",
         "reference_id":"2",
         "response":{
            "withdrawal_id":54321,
            "withdrawal_uri":"https://stage.wepay.com/api/withdrawal/54321/4560de9",
         }
      }
      {
         "call":"/preapproval/create",
         "reference_id":"3",
         "response":{
            "preapproval_id":22222,
            "preapproval_uri":"https://stage.wepay.com/api/preapproval/22222/4560de9",
         }
      },
   ]
}

Complete the Batch Call

You now have the responses to all the batch calls you made in the calls response object. Now, you can make multiple API calls at once, enjoy!