Skip to main content

Attach PayPal to a subscription

Attach PayPal to a subscription

Last updated: 29-May-2024
Rate this article:

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
 
/**
 * @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

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"
}
Rate this article:

Need help?

Do you have a question? If you didn’t find the answer you are looking for in our documentation, you can contact our Support teams for more information. If you have a technical issue or question, please contact us. We are happy to help.

Not yet a Verifone customer?

We’ll help you choose the right payment solution for your business, wherever you want to sell, in-person or online. Our team of experts will happily discuss your needs.

Verifone logo