Attach PayPal to a subscription
Use the attachToPayPal method to build the PayPal URL that can be used to attach a subscription to a PayPal account.
Request parameters
subscriptionCode
Required (string)
The subscription code that will be attached to the PayPal account.
paypalEmail
Required (string) The PayPal email associated with the subscription.
returnURL
Required (string) The URL the user will be redirected to if the subscription is successfully attached to PayPal.
cancelURL
Required (string) The URL the user will be redirected to if the subscription fails to attach to PayPal.
Request sample
<?php
/**
* @throws JsonException
*/
function callRPC($Request, $host)
{
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json']);
$RequestString = json_encode($Request, JSON_THROW_ON_ERROR);
curl_setopt($curl, CURLOPT_POSTFIELDS, $RequestString);
$ResponseString = curl_exec($curl);
if (!empty($ResponseString)) {
echo($ResponseString);
$Response = json_decode($ResponseString, false, 512, JSON_THROW_ON_ERROR);
if (isset($Response->result)) {
return $Response->result;
}
if (!is_null($Response->error)) {
echo("Method: {$Request->method}" . PHP_EOL);
echo("Error: {$Request->error}" . PHP_EOL);
}
} else {
return null;
}
return null;
}
$host = 'https://api.avangate.com/rpc/6.0/';
$merchantCode = "MERCHANT_CODE"; // your account's merchant code available in the 'System settings' area of the cPanel: https://secure.2checkout.com/cpanel/account_settings.php
$key = "SECRET_KEY"; // your account's secret key available in the 'System settings' area of the cPanel: https://secure.2checkout.com/cpanel/account_settings.php
$string = strlen($merchantCode) . $merchantCode . strlen(gmdate('Y-m-d H:i:s')) . gmdate('Y-m-d H:i:s');
$algo = "sha256";
$hash = hash_hmac($algo, $string, $key);
$i = 1;
$jsonRpcRequest = new stdClass();
$jsonRpcRequest->jsonrpc = '2.0';
$jsonRpcRequest->method = 'login';
$jsonRpcRequest->params = [$merchantCode, gmdate('Y-m-d H:i:s'), $hash, $algo];
$jsonRpcRequest->id = $i++;
try {
$sessionID = callRPC($jsonRpcRequest, $host);
echo("Auth token: {$sessionID}" . PHP_EOL);
} catch (JsonException $e) {
echo("Error: {$e->getMessage()}" . PHP_EOL);
}
$subscriptionCode = 'SUBSCRIPTIONCODE';
$paypalEmail = 'PAYPALEMAIL';
$returnURL = 'RETURNURL';
$cancelURL = 'CANCELURL';
$jsonRpcRequest = array (
'method' => 'attachToPayPal',
'params' => array($sessionID, $subscriptionCode, $paypalEmail, $returnURL, $cancelURL),
'id' => $i++,
'jsonrpc' => '2.0');
var_dump (callRPC((Object)$jsonRpcRequest, $host, true));Response parameters
status
String
Can be "success" if no errors occurred or "error" otherwise.
url
String The PayPal redirect URL if no errors occurred or empty otherwise.
error
Array Empty if no errors occurred or the error message otherwise.
Error response parameters
error_code
String
The error code of the returned exception
message
String The error message of the returned exception
If the API throws an error, an error response will be received, similar to:
Last updated
Was this helpful?