Update subscription payment information
Overview
Use the updateSubscriptionPaymentInformation method to update the credit card information related to the payment to be made for a subscription. To be able to do this, you need to generate a payment token using the credit card information via the 2Pay.js library, and then use this token in the request of the method as indicated below.
Request Parameters
Parameter Name | Type | Required/Optional | Description |
---|---|---|---|
sessionID |
String |
Required |
The session identifier, the output of the Login method. Include sessionID into all your requests. 2Checkout throws an exception if the values are incorrect. The sessionID expires in 10 minutes. |
subscriptionReference |
String |
Required |
The reference of the subscription for which you want to retrieve the payment information. |
paymentInformation |
Object |
Required |
The payment information object, details below: |
PaymentDetails |
Object |
Required |
The payment details object, details below: |
Type |
String |
Required |
The payment details type. Restricted to EES_TOKEN_PAYMENT for now. |
PaymentMethod |
Object |
Required |
The payment method object, details below: |
EesToken |
String |
Required |
The 2Pay token obtained by integrating the 2Pay.js library. |
Vendor3DSReturnURL | String | Required | URL address to which customers are redirected after the 3DS details get validated by the bank and the order is successfully authorized. |
Vendor3DSCancelURL | String | Required | URL address to which customers are redirected after the 3DS details get validated by the bank and the order is successfully authorized. |
Request Example
<?php
declare(strict_types=1);
class Configuration
{
public const MERCHANT_CODE = '';
public const MERCHANT_KEY = '';
public const URL = 'http://api.2checkout.com/rpc/6.0';
public const ACTION = 'updateSubscriptionPaymentInformation';
public const ADDITIONAL_OPTIONS = null;
public const SUBSCRIPTION_REF = 'YC9XXMGOYO';
//array or JSON
public const PAYLOAD = <<<JSON
{
"PaymentDetails": {
"Type": "EES_TOKEN_PAYMENT",
"PaymentMethod": {
"EesToken": "b8de22d1-1378-4a9a-be21-aa2145a6eb54",
"Vendor3DSReturnURL": "www.3dsReturnURL.com",
"Vendor3DSCancelURL": "www.3dsCancelURL.com"
}
}
}
JSON;
}
class Client
{
private const LOGIN_METHOD = 'login';
private $calls = 1;
private $sessionId;
private function generateAuth(): array
{
$merchantCode = Configuration::MERCHANT_CODE;
$key = Configuration::MERCHANT_KEY;
$date = gmdate('Y-m-d H:i:s');
$string = strlen($merchantCode) . $merchantCode . strlen($date) . $date;
$hash = hash_hmac('md5', $string, $key);
return compact('merchantCode', 'date', 'hash');
}
public function login(string $url)
{
$payload = $this->generateAuth();
$response = $this->call($url, array_values($payload), self::LOGIN_METHOD);
$this->sessionId = $response['result'];
}
public function call(
string $url = Configuration::URL,
$payload = Configuration::PAYLOAD,
string $action = Configuration::ACTION
): ?array {
if (empty($this->sessionId) && $action !== self::LOGIN_METHOD) {
$this->login($url);
}
if(is_string($payload)) {
$payload = json_decode($payload, true);
}
if (!empty($this->sessionId)) {
$payload = [$this->sessionId, Configuration::SUBSCRIPTION_REF, $payload];
}
$payload = array_filter($payload);
$request = json_encode([
'jsonrpc' => '2.0',
'method' => $action,
'params' => $payload,
'id' => $this->calls++,
]);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json', 'Cookie: XDEBUG_SESSION=PHPSTORM'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($curl);
if(empty($response)) {
die('Server unavailable');
}
echo $response . '</br>';
return json_decode($response, true);;
}
}
$client = new Client();
$result = $client->call();
var_dump($result);
Response
The method responds with Boolean True when the payment information update was successfully performed.
Otherwise, in case of validation or access rights failures, it returns the usual error response with specific messages indicating the cause of the failure.
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"FirstDigits": "4012",
"LastDigits": "9936",
"Authorize3DSUrl": "http://...",
"CardType": "visa"
}
}