Retrieve a customer’s subscriptions
Overview
Use the getCustomerSubscriptions method to retrieve details about the subscriptions belonging to a specific customer.
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
Session identifier, the output of the Login method. Include sessionID into all your requests. Avangate throws an exception if the values are incorrect. The sessionID expires in 10 minutes. |
AvangateCustomerReference |
Required (int) |
System-generated customer reference. Required unless you prefer to use ExternalCustomerReference. |
|
ExternalCustomerReference |
Optional (string) |
|
External customer reference that you control. Optional when you use AvangateCustomerReference. If you include it, it needs to belong to the same customer as the AvangateCustomerReference. |
Request
<?php
require ('PATH_TO_AUTH');
$AvangateCustomerReference = CUSTOMER_REFERENCE;
$ExternalCustomerReference = 'EXTERNAL_CUSTOMER_REFERENCE'; // optional
$jsonRpcRequest = array (
'method' => 'getCustomerSubscriptions',
'params' => array($sessionID, $AvangateCustomerReference,ExternalCustomerReference),
'id' => $i++,
'jsonrpc' => '2.0');
var_dump (callRPC((Object)$jsonRpcRequest, $host, true));
Response
Parameters | Type/Description |
---|---|
Array of objects |
Remove coupon
Overview
Use the deletePromotionCoupon method to remove multiple coupons from a promotion. You cannot delete a single coupon. The coupon is required for the promotion.
Parameters
Parameter | Type/Description | |
---|---|---|
promotionCode |
Required (string) |
|
|
The code corresponding to the promotion that you want to remove coupons from. |
|
promotionCoupon |
Required (object) |
|
|
Type |
Required (string) |
|
|
Coupon type. Available values:
|
|
Code/Codes |
Required (string/array of strings) |
|
|
Array of coupon codes (for MULTIPLE). |
Response
Parameter | Type/Description |
---|---|
promotionCoupon | Object |
Request
<?php
require ('PATH_TO_AUTH');
// Promotion code corresponding to the promotion you want to remove coupons from
$promotionCode = '';
// Define multiple coupon object
$promotionCoupon = new stdClass;
$promotionCoupon->Type = 'MULTIPLE';
$promotionCoupon->Codes = ['YOUR_CODE_1', 'YOUR_CODE_2'];
$jsonRpcRequest = array (
'jsonrpc' => '2.0',
'id' => $i++,
'method' => 'deletePromotionCoupon',
'params' => array($sessionID, $promotionCode, $promotionCoupon)
);
var_dump (callRPC($jsonRpcRequest, $host));
Get customer subscriptions
Overview
Use the getCustomerSubscriptions method to retrieve details about the subscriptions belonging to a specific customer.
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
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. |
AvangateCustomerReference |
Required (int) |
System-generated customer reference. Required unless you prefer to use ExternalCustomerReference. |
|
ExternalCustomerReference |
Optional (string) |
|
External customer reference that you control. Optional when you use AvangateCustomerReference. If you include it, it needs to belong to the same customer as the AvangateCustomerReference. |
Response
Parameters | Type/Description |
---|---|
Array of objects |
Request
<?php
require ('PATH_TO_AUTH');
$AvangateCustomerReference = CUSTOMER_REFERENCE;
//$ExternalCustomerReference = 'EXTERNAL_CUSTOMER_REFERENCE'; // optional
try {
$ListofSubscriptions = $client->getCustomerSubscriptions($sessionID, $AvangateCustomerReference);
}
catch (SoapFault $e) {
echo "ListofSubscriptions: " . $e->getMessage();
exit;
}
var_dump("ListofSubscriptions", $ListofSubscriptions);
Place a renewal order
Overview
Renew a subscription by using the Avangate Subscription Reference. Collect recurring revenue with one of the following payment methods:
- Credit cards
- PayPal
- WeChat Pay
- iDEAL
- Purchase Order
- Wire
Use the PaymentDetails object to control the payment method used in the renewal process.
Requirements
To place a renewal order, you need to provide a valid subscription reference number.
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
Session identifier, the output of the Login method. Include sessionID into all your requests. Avangate throws an exception if the values are incorrect. The sessionID expires in 10 minutes. |
Required (Object) |
|
|
Object designed to collect all data necessary for an order, including billing, product/subscription plan and payment details. To place an order with PayPal rather than PayPal Express, use PAYPAL as the type of the PaymentDetails object and send the shopper email and a return URL as part of the PaymentMethod object. See code sample. |
SubscriptionReference |
Required (String) Avangate generated subscription reference number. E.q. A8C5671BFE. |
Response
Parameters | Type/Description |
---|---|
Object |
Request
<?php
require ('PATH_TO_AUTH');
$Order = new stdClass();
$Order->Currency = 'USD';
$Order->Language = "EN";
$Order->Country = 'US';
$Order->CustomerIP = '91.220.121.21';
$Order->Source = "sourceAPI.net";
$Order->LocalTime = date('Y-m-d H:i:s');
$Order->Items = array();
/**/
$Order->Items[0]->RenewalInformation = new stdClass();
$Order->Items[0]->RenewalInformation->SubscriptionReference = 'A8C5671BFE'; //subscription used in the renewal process
$Order->Items[0]->Price = new stdClass();
$Order->Items[0]->Price->Type = 'CUSTOM';
$Order->Items[0]->Price->Amount = '10';
$Order->Items[0]->PriceOptions = array('uniqscale1=4');//
$Order->MachineId = "MachineID";
$Order->BillingDetails = new stdClass();
$Order->BillingDetails->Address1 = 'Bil1ing address';
$Order->BillingDetails->Address2 = 'Billing address 2';
$Order->BillingDetails->City = 'Billing City';
$Order->BillingDetails->State = 'Billing State';
$Order->BillingDetails->CountryCode = 'US';
$Order->BillingDetails->Phone = 1231232123;
$Order->BillingDetails->Email = 'customer_details@test.com';
$Order->BillingDetails->FirstName = 'First';
$Order->BillingDetails->LastName = 'Customer';
$Order->BillingDetails->Company = 'Billing Company';
$Order->BillingDetails->Zip = '55104';
$Order->DeliveryDetails = new stdClass();
$Order->DeliveryDetails->Address1 = 'Bil1ing address';
$Order->DeliveryDetails->Address2 = 'Billing address 2';
$Order->DeliveryDetails->City = 'Billing City';
$Order->DeliveryDetails->State = 'Billing State';
$Order->DeliveryDetails->CountryCode = 'US';
$Order->DeliveryDetails->Phone = '12345';
$Order->DeliveryDetails->Email = 'customer_details@test.com';
$Order->DeliveryDetails->FirstName = 'First';
$Order->DeliveryDetails->LastName = 'Customer';
$Order->DeliveryDetails->Zip = "55104";
$Order->PaymentDetails = new stdClass();
$Order->PaymentDetails->Type = "CC";
$Order->PaymentDetails->Currency = $currency;
$Order->PaymentDetails->PaymentMethod = new stdClass();
/**/
$Order->PaymentDetails->PaymentMethod->CardNumber = "4111111111111111";
$Order->PaymentDetails->PaymentMethod->CardType = "VISA";
$Order->PaymentDetails->PaymentMethod->ExpirationYear = "2019";
$Order->PaymentDetails->PaymentMethod->ExpirationMonth = "12";
$Order->PaymentDetails->PaymentMethod->CCID = "123";
$Order->PaymentDetails->PaymentMethod->HolderName = "John Doe";
$Order->PaymentDetails->PaymentMethod->RecurringEnabled = TRUE;
$Order->PaymentDetails->PaymentMethod->HolderNameTime = 1;
$Order->PaymentDetails->PaymentMethod->CardNumberTime = 1;
/**/
$jsonRpcRequest = new stdClass();
$jsonRpcRequest->jsonrpc = '2.0';
$jsonRpcRequest->method = 'placeOrder';
$jsonRpcRequest->params = array($sessionID, $Order);
$jsonRpcRequest->id = $i++;
$placeOrder = callRPC($jsonRpcRequest, $host);
var_dump ($placeOrder);
Add promotion coupon
Overview
Use the addPromotionCoupon method via SOAP API 4.0 to add single or multiple coupons to a promotion.
Parameters
Parameter | Type/Description | |
---|---|---|
sessionID |
Required (string) |
|
|
Output of the Login method. |
|
promotionCode |
Required (string) |
|
|
The code corresponding to the promotion that you want to add coupons to. |
|
promotionCoupon |
Required (object) |
|
|
type |
Required (string) |
|
|
Coupon type. Available values:
|
|
Code/Codes |
Required (string / array of strings) |
|
|
Coupon code (for SINGLE) or array of coupon codes (for MULTIPLE). |
Response
Parameter | Type/Description |
---|---|
promotionCoupon | Object |
Request
<?php
class Client
{
protected static $merchantCode;
protected static $loginDate;
protected static $hash;
protected static $baseUrl;
protected static $callCount = 0;
protected static $sessionId = '';
protected static $client;
public static function setCredentials($code, $key)
{
static::$merchantCode = $code;
static::$loginDate = gmdate('Y-m-d H:i:s');
static::$hash = hash_hmac('md5', strlen($code) . $code . strlen(static::$loginDate) . static::$loginDate, $key);
static::$sessionId = static::login();
}
public static function setBaseUrl($url)
{
static::$baseUrl = $url;
}
public static function login()
{
$client = static::getClient();
return $client->login(static::$merchantCode, static::$loginDate, static::$hash);
}
public static function __callStatic($name, $arguments = array())
{
$client = static::getClient();
array_unshift($arguments, static::$sessionId);
$response = call_user_func_array(array($client, $name), $arguments);
return $response;
}
protected static function getClient()
{
$opts = array(
'http'=> ['user_agent' => 'PHPSoapClient'],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
);
if (null === static::$client) {
static::$client = new \SoapClient(static::$baseUrl . '?wsdl', [
'location' => static::$baseUrl,
'cache_wsdl' => WSDL_CACHE_NONE,
'stream_context' => stream_context_create($opts),
]);
}
return static::$client;
}
}
Client::setBaseUrl('https://api.avangate.com/soap/3.1/');
Client::setCredentials('YOUR_MERCHANT_CODE', 'YOUR_SECRET_KEY');
Client::login();
$promotionCode = 'YOUR_PROMOTION_CODE'; // code of the promotion that you want to update
// // Define single coupon object
// $promotionCoupon = new stdClass;
// $promotionCoupon->Type = 'SINGLE';
// $promotionCoupon->Code = 'YOUR_CODE_HERE';
// Define multiple coupon object
// $promotionCoupon = new stdClass;
// $promotionCoupon->Type = 'MULTIPLE';
// $promotionCoupon->Codes = ['YOUR_CODE_1', 'YOUR_CODE_2'];
$response = Client::addPromotionCoupon ($promotionCode,$promotionCoupon); // Add coupons to the existing promotion
var_dump($response);
Retrieve a promotion
Overview
Use the getPromotion method to extract information on a promotion you set up for your account.
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
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. |
PromotionCode |
Required (string) |
|
System-generated identifier 2Checkout associated with promotion campaigns. |
Response
Promotion |
Object |
Request
<?php
$host = "https://api.2checkout.com";
$client = new SoapClient($host . "/soap/4.0/?wsdl", array(
'location' => $host . "/soap/4.0/",
"stream_context" => stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
))
));
function hmac($key, $data)
{
$b = 64; // byte length for md5
if (strlen($key) > $b) {
$key = pack("H*", md5($key));
}
$key = str_pad($key, $b, chr(0x00));
$ipad = str_pad('', $b, chr(0x36));
$opad = str_pad('', $b, chr(0x5c));
$k_ipad = $key ^ $ipad;
$k_opad = $key ^ $opad;
return md5($k_opad . pack("H*", md5($k_ipad . $data)));
}
$merchantCode = "YOURCODE123"; //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'); //date_default_timezone_set('UTC')
$string = strlen($merchantCode) . $merchantCode . strlen($now) . $now;
$hash = hmac($key, $string);
try {
$sessionID = $client->login($merchantCode, $now, $hash);
}
catch (SoapFault $e) {
echo "Authentication: " . $e->getMessage();
exit;
}
$PromotionCode = '123456789c'; //case sensitive
try {
$Discount = $client->getPromotion($sessionID, $PromotionCode);
}
catch (SoapFault $e) {
echo "Discount: " . $e->getMessage();
exit;
}
var_dump("Discount", $Discount);
?>
Use Purchase order flow
Overview
Place an order using catalog products, and collect the payment using the Purchase Order flow.
Requirements
You have to send the InternalPONumber and AutoApprove parameters to place a Purchase Order. The company name is mandatory when placing an order via PO.
Currency support
Check with Avangate support for a list of currencies available for Purchase Order.
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
Session identifier, the output of the Login method. Include sessionID into all your requests. Avangate throws an exception if the values are incorrect. The sessionID expires in 10 minutes. |
Required (Object) |
|
|
Object designed to collect all data necessary for an order, including billing, product/subscription plan, and payment details.
|
Workflow
- The customer is placing the order using Purchase Order as the ordering flow, filling in the internal PO number as mandatory information.
- You are able to set the order to auto-approve as soon as the purchase order is submitted. Otherwise, you have to approve/cancel it manually from Control Panel.
- After the order is placed, the order confirmation e-mail is sent, including the payment instructions and payment terms agreed.
- The order is finalized as soon as the payment is confirmed.
- If payment is not received according to the terms, the order is canceled.
Response
Object |
Request
<?php
require ('PATH_TO_AUTH');
$Order = new stdClass();
$Order->RefNo = NULL;
$Order->Currency = 'usd';
$Order->Country = 'US';
$Order->Language = 'en';
$Order->CustomerIP = '91.220.121.21';
$Order->ExternalReference = NULL;
$Order->Source = NULL;
$Order->Affiliate = new stdClass();
$Order->Affiliate->AffiliateCode = 'Partner123'
$Order->Affiliate->AffiliateSource = 'MobilePlatform'
$Order->CustomerReference = NULL;
$Order->Items = array();
$Order->Items[0] = new stdClass();
$Order->Items[0]->Code = 'my_subscription_1';
$Order->Items[0]->Quantity = 1;
$Order->Items[0]->PriceOptions = NULL;
$Order->Items[0]->SKU = NULL;
$Order->Items[0]->Price = NULL;
$Order->Items[0]->CrossSell = NULL;
$Order->Items[0]->Trial = false;
$Order->Items[0]->AdditionalFields = NULL;
$Order->Items[0]->SubscriptionStartDate = NULL; //If empty or null, subscriptions become active when purchase is made.
$Order->BillingDetails = new stdClass();
$Order->BillingDetails->FirstName = 'Test Cosmin API';
$Order->BillingDetails->LastName = 'Cosmin API';
$Order->BillingDetails->CountryCode = 'us';
$Order->BillingDetails->State = 'California';
$Order->BillingDetails->City = 'LA';
$Order->BillingDetails->Address1 = 'Address example';
$Order->BillingDetails->Address2 = NULL;
$Order->BillingDetails->Zip = '90210';
$Order->BillingDetails->Email = 'cosmin.deftu@avangate.com';
$Order->BillingDetails->Phone = NULL;
$Order->BillingDetails->Company = 'ABC Company';
$Order->DeliveryDetails = NULL;
$Order->PaymentDetails = new stdClass ();
$Order->PaymentDetails->Type = 'PURCHASEORDER';
$Order->PaymentDetails->Currency = 'USD';
$Order->PaymentDetails->PaymentMethod = new stdClass();
$Order->PaymentDetails->PaymentMethod->AutoApprove = true;
$Order->PaymentDetails->PaymentMethod->InternalPONumber = 84864848;
$Order->AdditionalFields = NULL;
$Order->LocalTime = NULL;
$Order->GiftDetails = NULL;
$jsonRpcRequest = array (
'method' => 'placeOrder',
'params' => array($sessionID, $Order),
'id' => $i++,
'jsonrpc' => '2.0'
);
$order = callRPC($jsonRpcRequest, $host);
Retrieve price based on geolocation
Overview
Use the getPrice method to retrieve product prices based on geolocation.
To display product prices on your website without having the geolocation feature, you can use the getPrice API method, as described below.
Request example
var data = {
PRODS: ' 12345678', // product ID
OPTIONS 12345678: '', // product pricing options if any
QTY: 1, //product quantity
CURRENCY: 'EUR', //product currency
COUPON: null, //discount to be applied
RESPONSE_TYPE: 'SOAP'
};
$.ajax({
type: 'GET',
url: 'https://' + window.location.host + '/action/get_price.php',
data: data,
success: function (data) {
// your code here
},
error: function (error) {
console.error(error);
},
});
A built URL that displays prices based on the customer's geolocation would look like this:
https://secure.2checkout.com/action/get_price.php?PRODS=12345678&OPTIONS12345678&QTY=1&CURRENCY=EUR&COUPON=null&RESPONSE_TYPE=SOAP
The quantity parameter is used only to retrieve the unit price with volume discounts if this was applied to the product. Setting quantity=2 in the request will not return the price for quantity 2, but it will return the unit price corresponding to the quantity interval specified.
For example if the unit price is:
Quantity =1 to 10 = 100USD
Quantity= 10+ = 90USD
A request with quantity 11 will not return 1100USD, it will return 90USD. While a call with quantity= 5, will return 100USD, not 500USD.
Create customer
Overview
Use the createCustomer method to add the details of a customer entity into the 2Checkout system. By default, customer status is Inactive until you assign a specific subscription to the customer. Following that action, customer status reflects the status of the attached subscription(s).
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
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. |
Object |
Response
Parameters | Type/Description |
---|---|
AvangateCustomerReference |
Int |
|
System-generated customer reference. |
Request
<?php
require ('PATH_TO_AUTH');
$newCustomer = new stdClass();
$newCustomer->AvangateCustomerReference = null;
$newCustomer->ExternalCustomerReference = 'ThisIsATestReference123456';
$newCustomer->FirstName = 'NewCustomer';
$newCustomer->LastName = 'NewCustomerLastName';
$newCustomer->Company = null;
$newCustomer->FiscalCode = null;
$newCustomer->Address1 = 'Address';
$newCustomer->Address2 = null;
$newCustomer->City = 'LA';
$newCustomer->Zip = '90210';
$newCustomer->CountryCode = 'us';
$newCustomer->Phone = null;
$newCustomer->Fax = null;
$newCustomer->Email = 'newcustomer@email.com';
$newCustomer->ExistingCards = null;
$newCustomer->Enabled = null;
$newCustomer->Trial = null;
$newCustomer->Language = 'en';
try {
$newCustomerInfo = $client->createCustomer($sessionID, $newCustomer);
}
catch (SoapFault $e) {
echo "newCustomerInfo: " . $e->getMessage();
exit;
}
var_dump("newCustomerInfo", $newCustomerInfo);