How to Split Payment into Different WePay Accounts Using Pre-Approval id
Split or chained payment is used while you are taking fees or commission from merchant account. It will collect credit card information once and send money across multiple merchant accounts. For an easy example, let’s say there are multiple merchants’ in a single website and you are an owner of the website, now you want to collect your commission or fee from each merchant while they get paid.
Let’s start with an example,
First Download WePay SDK from here.
Create a PHP file and use below code:
Step1:
// WePay PHP SDK - http://git.io/mY7iQQ
require 'wepay.php';
// application settings
$account_id = 123456789; //It is your account id
$client_id = 9543125; //It is your client id
$client_secret = "1a3b5c7d9"; //It is your client secret
$access_token = “STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20”;
// change to useProduction for live environments
Wepay::useStaging($client_id, $client_secret);
$wepay = new WePay(NULL); // Don't pass an access_token for this call
// create the pre-approval
$response = $wepay->request('preapproval/create', array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'period' => 'once',
'mode' => 'regular',
'short_description' => 'Donation to merchant A and merchant B'
'redirect_uri' => ‘https://example.com/confirm_payment/?amount=150, //Pass an amount through variable
));
redirect($response->preapproval_uri); //it will redirect to wepay URL
Now it will redirect to WePay website’s page, it will look like this:
Now it will redirect to https://example.com/confirm_payment/?amount=150&pre_approvalid=XXXXXX page which we have added in step1, preapproval id and the amount you will get in your URL will use in step2:
Step 2:
This step will use for payment of the first merchant.
// WePay PHP SDK – http://git.io/mY7iQQ
require ‘wepay.php’; // application settings
$account_id = 123456789; // the FIRST merchant’s account_id
$client_id = 9543125;
$client_secret = '1a3b5c7d9';
$access_token = 'STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20'; // the FIRST merchant’s access_token
$preapproval_id = $_GET['pre_approvalid']; // The preapproval_id from step 1
// change to useProduction for live environments
Wepay::useStaging($client_id, $client_secret); $wepay = new WePay($access_token); // create the checkout
$response = $wepay->request(‘checkout/create’, array(
'account_id' => $account_id,
'amount' => $_GET['amount'], //Amount will get 1st merchant
'short_description' => 'Payment for test project',
'type' => 'donation',
'currency' => 'USD',
'payment_method' => array(
'type’ => 'preapproval',
'preapproval' => array(
'id' => $preapproval_id
)
)
));
redirect('https://example.com/confirm_other_payment/?amount=150&pre_approvalid=' . $_GET['pre_approvalid']);
Step 3: Redirect to other page and use 2nd merchant’s account id, access token
// WePay PHP SDK – http://git.io/mY7iQQ
require ‘wepay.php’; // application settings
$account_id = 123456789; // the 2nd merchant/Owner’s account_id
$client_id = 9543125;
$client_secret = '1a3b5c7d9';
$access_token = 'STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20'; // the 2nd merchant’s/Owner’s access_token
$preapproval_id = $_GET['pre_approvalid']; // The preapproval_id from step 1
// change to useProduction for live environments
Wepay::useStaging($client_id, $client_secret); $wepay = new WePay($access_token); // create the checkout
$response = $wepay->request('checkout/create', array(
'account_id' => $account_id,
'amount' => $_GET['amount'], //Amount will get by 2nd merchant or owner
'short_description' => 'Payment for test project',
'type' => 'donation',
'currency' => 'USD',
'payment_method' => array(
'type' => 'preapproval',
'preapproval' => array(
'id' => $preapproval_id
)
)
));
redirect('https://example.com/thank_you');
Summary:
Create an account with WePay, it will provide you account id, client id, client secret and tokens. Use step one, it will provide an array of preapproval_id and preapproval_uri. Preapproval_uri will redirects to WePay website, the user will fill card details and it will redirect to redirect_uri provided in step1.
Now redirect_uri redirects with preapproval_id which you can use it into step2, you can use multiple account ids for each payment with amount details.
It is very important for your business to have a fully functional and error free online presence. Being a Web Application Development Company we can provide you a unique web solution you required. Contact us to hire web developers or you can reach us at inquiry@techcompose.com