Attach PayPal to a subscription
Overview
Use the attachToPayPal method to build the PayPal URL that can be used to attach a subscription to a PayPal account.
Request parameters
Parameters |
Type |
Required |
Description |
---|---|---|---|
subscriptionCode |
String |
Yes |
The subscription code that will be attached to the PayPal account. |
paypalEmail |
String |
Yes |
The PayPal email associated with the subscription. |
returnURL |
String |
Yes |
The URL the user will be redirected to if the subscription is successfully attached to PayPal. |
cancelURL |
String |
Yes |
The URL the user will be redirected to if the subscription fails to attach to PayPal. |
Request example
<?php
$host = "https://api.2checkout.com";
$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
$now = gmdate('Y-m-d H:i:s'); //GMT date format)
$algo = "sha256";
$string = strlen($merchantCode) . $merchantCode . strlen($now) . $now;
$hash = hash_hmac($algo, $string, $key);
try {
$client = new SoapClient($host . "/soap/6.0/?wsdl", [
'location' => $host . "/soap/6.0/",
"stream_context" => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
])
]);
$sessionID = $client->login($merchantCode, $now, $hash, $algo);
echo("Auth token: {$sessionID}" . PHP_EOL);
$subscriptionCode = 'SUBSCRIPTIONCODE';
$paypalEmail = 'PAYPALEMAIL';
$returnURL = 'RETURNURL';
$cancelURL = 'CANCELURL';
var_dump($res);
} catch (SoapFault $e) {
echo "Authentication: " . $e->getMessage() . PHP_EOL;
exit;
} catch (Exception $ex) {
echo $ex->getMessage() . PHP_EOL;
exit;
}
Response parameters
Parameters |
Type |
Description |
---|---|---|
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
Parameters |
Type |
Description |
---|---|---|
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:
{
"error_code": "SUBSCRIPTION_PAYPAL_ATTACH_ERR",
"message": "Failed to link subscription to PayPal account"
}