Create price option groups
Last updated: 11-Dec-2020
Rate this article:
Overview
Use the addPriceOptionGroup method to create price option groups for your subscription plans/products.
- Send options for each pricing group or 2Checkout throws an exception.
- When adding an interval with no min/max values or overlapping values, 2Checkout throws an exception.
Parameters
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. |
PriceOptionsGroup |
Required (object) |
|
Use this object to create a new price option group for your account. |
Response
bool(true)
Request
<?php
$host = "https://api.avangate.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.avangate.com/cpanel/account_settings.php
$key = "SECRET_KEY"; //your account's secret key available in the 'System settings' area of the cPanel: https://secure.avangate.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;
}
$PriceOptionGroup = new stdClass();
$PriceOptionGroup->Name = 'New Multi Users';
$PriceOptionGroup->Description = 'Quos aut ipsam ipsum omnis aut molestiae. Et quod molestias distinctio. Fugiat sit asperiores reprehenderit officia eaque quae quia. Aperiam quia quia illo eos nesciunt accusamus.';
$PriceOptionGroup->Translations = array();
$PriceOptionGroup->Translations[0] = new stdClass();
$PriceOptionGroup->Translations[0]->Name = 'xdrki7ljix';
$PriceOptionGroup->Translations[0]->Description = 'Beatae doloribus ipsam voluptatem et. Iure dignissimos non amet. Quibusdam fugiat dolor repudiandae temporibus harum.';
$PriceOptionGroup->Translations[0]->Language = 'en';
$PriceOptionGroup->Translations[1] = new stdClass();
$PriceOptionGroup->Translations[1]->Name = '37wr8ie2dj';
$PriceOptionGroup->Translations[1]->Description = 'Esse distinctio voluptatibus omnis et et quia dolor. Quibusdam dicta dolores odio consequatur velit voluptate. Laboriosam reiciendis libero vel quae molestiae ad.';
$PriceOptionGroup->Translations[1]->Language = 'ru';
$PriceOptionGroup->Type = 'RADIO';
$PriceOptionGroup->Options = array();
$PriceOptionGroup->Options[0] = new stdClass();
$PriceOptionGroup->Options[0]->Name = 'SingleUser';
$PriceOptionGroup->Options[0]->Description = 'Nisi ea autem a labore similique. Minus natus cumque nemo. Aut aliquam laboriosam dolorem ad.';
$PriceOptionGroup->Options[0]->Translations = array();
$PriceOptionGroup->Options[0]->Translations[0] = new stdClass();
$PriceOptionGroup->Options[0]->Translations[0]->Name = '0q2r3kcj0q';
$PriceOptionGroup->Options[0]->Translations[0]->Description = 'Voluptatem in vitae rerum ea tempore. Non cumque ullam optio quis. Laborum maxime sunt facere. Dolor fugit a fugiat quasi facere totam.';
$PriceOptionGroup->Options[0]->Translations[0]->Language = 'en';
$PriceOptionGroup->Options[0]->Translations[1] = new stdClass();
$PriceOptionGroup->Options[0]->Translations[1]->Name = 'dwfxuw4lrn';
$PriceOptionGroup->Options[0]->Translations[1]->Description = 'Debitis omnis maiores quia praesentium totam error corrupti. Consectetur eum magnam quam vero. Sit aperiam natus perspiciatis iusto sint ut fugit. Adipisci illum non voluptatem voluptas.';
$PriceOptionGroup->Options[0]->Translations[1]->Language = 'it';
$PriceOptionGroup->Options[0]->Code = 'singleuser1';
$PriceOptionGroup->Options[0]->SubscriptionImpact = new stdClass();
$PriceOptionGroup->Options[0]->SubscriptionImpact->Impact = 'Add';
$PriceOptionGroup->Options[0]->SubscriptionImpact->Months = 1;
$PriceOptionGroup->Options[0]->PriceImpact = new stdClass();
$PriceOptionGroup->Options[0]->PriceImpact->Method = 'FIXED';
$PriceOptionGroup->Options[0]->PriceImpact->Amounts = array();
$PriceOptionGroup->Options[0]->PriceImpact->Amounts[0] = new stdClass();
$PriceOptionGroup->Options[0]->PriceImpact->Amounts[0]->Currency = 'USD';
$PriceOptionGroup->Options[0]->PriceImpact->Amounts[0]->Amount = 90.61;
$PriceOptionGroup->Options[0]->PriceImpact->Amounts[1] = new stdClass();
$PriceOptionGroup->Options[0]->PriceImpact->Amounts[1]->Currency = 'EUR';
$PriceOptionGroup->Options[0]->PriceImpact->Amounts[1]->Amount = 6.70;
$PriceOptionGroup->Options[0]->PriceImpact->ImpactOn = 'BASE';
$PriceOptionGroup->Options[0]->PriceImpact->Impact = 'ADD';
$PriceOptionGroup->Options[0]->PriceImpact->Percent = 39;
$PriceOptionGroup->Options[0]->Default = false;
$PriceOptionGroup->Options[1] = new stdClass();
$PriceOptionGroup->Options[1]->Name = 'MultiUser';
$PriceOptionGroup->Options[1]->Description = 'Vero voluptatum fuga et repellendus sed qui. Dolores molestiae error non ad aperiam. In error quos eum quas repudiandae pariatur et suscipit.';
$PriceOptionGroup->Options[1]->Translations = array();
$PriceOptionGroup->Options[1]->Translations[0] = new stdClass();
$PriceOptionGroup->Options[1]->Translations[0]->Name = 'MultiUser';
$PriceOptionGroup->Options[1]->Translations[0]->Description = 'Debitis et saepe facere blanditiis. Tempore et nemo aut ullam possimus ipsum nisi. Ad libero et consequuntur aliquam libero. Rerum aut illum eveniet earum.';
$PriceOptionGroup->Options[1]->Translations[0]->Language = 'en';
$PriceOptionGroup->Options[1]->Translations[1] = new stdClass();
$PriceOptionGroup->Options[1]->Translations[1]->Name = 'dw5zgkcki9';
$PriceOptionGroup->Options[1]->Translations[1]->Description = 'Vel et excepturi veniam. In iusto eveniet pariatur hic labore. Et qui dolorem accusantium molestias iusto.';
$PriceOptionGroup->Options[1]->Translations[1]->Language = 'pt';
$PriceOptionGroup->Options[1]->Code = 'multiuser999';
$PriceOptionGroup->Options[1]->SubscriptionImpact = new stdClass();
$PriceOptionGroup->Options[1]->SubscriptionImpact->Impact = 'ADD';
$PriceOptionGroup->Options[1]->SubscriptionImpact->Months = 2;
$PriceOptionGroup->Options[1]->PriceImpact = new stdClass();
$PriceOptionGroup->Options[1]->PriceImpact->Method = 'FIXED';
$PriceOptionGroup->Options[1]->PriceImpact->Amounts = array();
$PriceOptionGroup->Options[1]->PriceImpact->Amounts[0] = new stdClass();
$PriceOptionGroup->Options[1]->PriceImpact->Amounts[0]->Currency = 'USD';
$PriceOptionGroup->Options[1]->PriceImpact->Amounts[0]->Amount = 65.03;
$PriceOptionGroup->Options[1]->PriceImpact->Amounts[1] = new stdClass();
$PriceOptionGroup->Options[1]->PriceImpact->Amounts[1]->Currency = 'EUR';
$PriceOptionGroup->Options[1]->PriceImpact->Amounts[1]->Amount = 64.58;
$PriceOptionGroup->Options[1]->PriceImpact->ImpactOn = 'BASE';
$PriceOptionGroup->Options[1]->PriceImpact->Impact = 'ADD';
$PriceOptionGroup->Options[1]->PriceImpact->Percent = 51;
$PriceOptionGroup->Options[1]->Default = true;
$PriceOptionGroup->Code = null;
$PriceOptionGroup->Required = false;
try {
$NewPriceOptionGroup = $client->addPriceOptionGroup($sessionID, $PriceOptionGroup);
}
catch (SoapFault $e) {
echo "NewPriceOptionGroup: " . $e->getMessage();
exit;
}
var_dump("NewPriceOptionGroup", $NewPriceOptionGroup);
?>
Pay-per-Usage
Use this option to create and assign PAYPERUSE price options to your portfolio.
For non PAYPERUSAGE cases, the Usage and UsagePricingModel fields are not required.
Request sample with PAYPERUSE
<?php
declare(strict_types=1);
class Configuration
{
public const MERCHANT_CODE = '';
public const MERCHANT_KEY = '';
public const URL = 'http://api.2checkout.com/soap/6.0';
public const ACTION = 'addPriceOptionGroup';
public const ADDITIONAL_OPTIONS = null;
//array or JSON
public const PAYLOAD = <<<JSON
{
"Type": "INTERVAL",
"Code": "PayPerUsage-CODE",
"Required": false,
"Name": "Pay per usage pricing option group",
"Description": "Test option description",
"Usage": "PAYPERUSAGE",
"UsagePricingModel": "STEPPED",
"Options": [
{
"Code": "9876545678",
"ScaleMin": "1",
"ScaleMax": "9",
"SubscriptionImpact": {
"Months": "0.00",
"Impact": null
},
"PriceImpact": {
"Amounts": {
"USD": {
"Currency": "USD",
"Amount": "1.00"
},
"EUR": {
"Currency": "EUR",
"Amount": "6.00"
}
},
"ImpactOn": null,
"Method": "FIXED",
"Percent": null,
"Impact": null
},
"Default": false,
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Translations": [
{
"Name": "translation_5f90150268bf7",
"Description": "Translation value",
"Language": "RO"
},
{
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Language": "EN"
},
{
"Name": "translation_5f90150268bf9",
"Description": "Translation value",
"Language": "FR"
},
{
"Name": "translation_5f90150268bf5",
"Description": "Translation value",
"Language": "RU"
}
]
},
{
"Code": "98765456789",
"ScaleMin": "10",
"ScaleMax": "19",
"SubscriptionImpact": {
"Months": "0.00",
"Impact": null
},
"PriceImpact": {
"Amounts": {
"USD": {
"Currency": "USD",
"Amount": "1.00"
},
"EUR": {
"Currency": "EUR",
"Amount": "6.00"
}
},
"ImpactOn": null,
"Method": "FIXED",
"Percent": null,
"Impact": null
},
"Default": false,
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Translations": [
{
"Name": "translation_5f90150268bf7",
"Description": "Translation value",
"Language": "RO"
},
{
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Language": "EN"
},
{
"Name": "translation_5f90150268bf9",
"Description": "Translation value",
"Language": "FR"
},
{
"Name": "translation_5f90150268bf5",
"Description": "Translation value",
"Language": "RU"
}
]
}
],
"Translations": [
{
"Name": "Pay per usage pricing option group",
"Description": "Test option description",
"Language": "EN"
}
]
}
JSON;
}
class Client
{
public function call(
string $url = Configuration::URL,
$payload = Configuration::PAYLOAD,
string $action = Configuration::ACTION
): ?object {
if (is_array($payload)) {
$payload = json_encode($payload);
}
if (!empty($payload)) {
// SoapClient works with objects(StdClass)
$payload = json_decode($payload);
}
$soapClient = $this->getClient($url);
$sessionId = $this->getSession($soapClient);
$args = array_filter([$sessionId, $payload]);
return $soapClient->$action(...$args);
}
public function getClient(string $url): SoapClient
{
return new SoapClient(
$url.'?wsdl',
[
'location' => $url,
'cache_wsdl' => WSDL_CACHE_NONE,
]
);
}
public function getSession(SoapClient $client)
{
$date = gmdate('Y-m-d H:i:s');
$merchantCode = Configuration::MERCHANT_CODE;
$key = Configuration::MERCHANT_KEY;
$string = strlen($merchantCode).$merchantCode.strlen($date).$date;
$hash = hash_hmac('md5', $string, $key);
$client->__setCookie('XDEBUG_SESSION', 'PHPSTORM');
return $client->login($merchantCode, $date, $hash);
}
}
try {
$client = new Client();
var_dump($client->call());
} catch (Exception $ex) {
var_dump($ex);
}
Request sample without PAYPERUSE
<?php
declare(strict_types=1);
class Configuration
{
public const MERCHANT_CODE = '';
public const MERCHANT_KEY = '';
public const URL = 'http://api.2checkout.com/soap/6.0';
public const ACTION = 'addPriceOptionGroup';
public const ADDITIONAL_OPTIONS = null;
//array or JSON
public const PAYLOAD = <<<JSON
{
"Type": "RADIO",
"Code": "RADIO-CODE",
"Required": false,
"Name": "Radio pricing option group",
"Description": "Test option description",
"Options": [
{
"Code": "9876545678",
"ScaleMin": "1",
"ScaleMax": "9",
"SubscriptionImpact": {
"Months": "0.00",
"Impact": null
},
"PriceImpact": {
"Amounts": {
"USD": {
"Currency": "USD",
"Amount": "1.00"
},
"EUR": {
"Currency": "EUR",
"Amount": "6.00"
}
},
"ImpactOn": null,
"Method": "FIXED",
"Percent": null,
"Impact": null
},
"Default": false,
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Translations": [
{
"Name": "translation_5f90150268bf7",
"Description": "Translation value",
"Language": "RO"
},
{
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Language": "EN"
},
{
"Name": "translation_5f90150268bf9",
"Description": "Translation value",
"Language": "FR"
},
{
"Name": "translation_5f90150268bf5",
"Description": "Translation value",
"Language": "RU"
}
]
},
{
"Code": "98765456789",
"ScaleMin": "10",
"ScaleMax": "19",
"SubscriptionImpact": {
"Months": "0.00",
"Impact": null
},
"PriceImpact": {
"Amounts": {
"USD": {
"Currency": "USD",
"Amount": "1.00"
},
"EUR": {
"Currency": "EUR",
"Amount": "6.00"
}
},
"ImpactOn": null,
"Method": "FIXED",
"Percent": null,
"Impact": null
},
"Default": false,
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Translations": [
{
"Name": "translation_5f90150268bf7",
"Description": "Translation value",
"Language": "RO"
},
{
"Name": "translation_5f90150268bef",
"Description": "Translation value",
"Language": "EN"
},
{
"Name": "translation_5f90150268bf9",
"Description": "Translation value",
"Language": "FR"
},
{
"Name": "translation_5f90150268bf5",
"Description": "Translation value",
"Language": "RU"
}
]
}
],
"Translations": [
{
"Name": "Radio pricing option group",
"Description": "Test option description",
"Language": "EN"
}
]
}
JSON;
}
class Client
{
public function call(
string $url = Configuration::URL,
$payload = Configuration::PAYLOAD,
string $action = Configuration::ACTION
): ?object {
if (is_array($payload)) {
$payload = json_encode($payload);
}
if (!empty($payload)) {
// SoapClient works with objects(StdClass)
$payload = json_decode($payload);
}
$soapClient = $this->getClient($url);
$sessionId = $this->getSession($soapClient);
$args = array_filter([$sessionId, $payload]);
return $soapClient->$action(...$args);
}
public function getClient(string $url): SoapClient
{
return new SoapClient(
$url.'?wsdl',
[
'location' => $url,
'cache_wsdl' => WSDL_CACHE_NONE,
]
);
}
public function getSession(SoapClient $client)
{
$date = gmdate('Y-m-d H:i:s');
$merchantCode = Configuration::MERCHANT_CODE;
$key = Configuration::MERCHANT_KEY;
$string = strlen($merchantCode).$merchantCode.strlen($date).$date;
$hash = hash_hmac('md5', $string, $key);
$client->__setCookie('XDEBUG_SESSION', 'PHPSTORM');
return $client->login($merchantCode, $date, $hash);
}
}
try {
$client = new Client();
var_dump($client->call());
} catch (Exception $ex) {
var_dump($ex);
}
Rate this article: