# 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

<table><thead><tr><th width="170.2666015625">Parameter</th><th>Type / Description</th></tr></thead><tbody><tr><td><code>subscriptionCode</code></td><td><p><strong>Required (string)</strong></p><p>The subscription code that will be attached to the PayPal account.</p></td></tr><tr><td><code>paypalEmail</code></td><td><strong>Required (string)</strong><br>The PayPal email associated with the subscription.</td></tr><tr><td><code>returnURL</code></td><td><strong>Required (string)</strong><br>The URL the user will be redirected to if the subscription is successfully attached to PayPal.</td></tr><tr><td><code>cancelURL</code></td><td><strong>Required (string)</strong><br>The URL the user will be redirected to if the subscription fails to attach to PayPal.</td></tr></tbody></table>

### Request sample

```php
<?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

<table><thead><tr><th width="153.199951171875">Parameter</th><th>Type / Description</th></tr></thead><tbody><tr><td><code>status</code></td><td><p><strong>String</strong></p><p>Can be "success" if no errors occurred or "error" otherwise.</p></td></tr><tr><td><code>url</code></td><td><strong>String</strong><br>The PayPal redirect URL if no errors occurred or empty otherwise.</td></tr><tr><td><code>error</code></td><td><strong>Array</strong><br>Empty if no errors occurred or the error message otherwise.</td></tr></tbody></table>

### Error response parameters

<table><thead><tr><th width="145.7332763671875">Parameter</th><th>Type / Description</th></tr></thead><tbody><tr><td><code>error_code</code></td><td><p><strong>String</strong></p><p>The error code of the returned exception</p></td></tr><tr><td><code>message</code></td><td><strong>String</strong><br>The error message of the returned exception</td></tr></tbody></table>

If the API throws an error, an error response will be received, similar to:

```php
{
    "error_code": "SUBSCRIPTION_PAYPAL_ATTACH_ERR",
    "message": "Failed to link subscription to PayPal account"
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.2checkout.com/json-rpc-api-reference/json-rpc-api-6.0/api-requests/subscription/attach-paypal-to-a-subscription.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
