Skip to main content
knowledgecenter.2checkout.com

Retrieve usage

Overview

Use the getSubscriptionUsages method via JSON-RPC APIv6 to search through subscription usages and retrieve all the usages information attached to a subscription.

Request Parameters

Parameters Type Required/Optional Description
merchantCode String Required The system-generated merchant ID.
hash String Required The MD5 hmac key for the request.
sessionID String Required The system-generated code of the session.
SubscriptionReference String Required The system-generated reference code of the subscription.
Page Number Required The page to be returned.
Limit Number Required The number of results per page.
IntervalStart Datetime Required Filters all usages where UsageEnd >= IntervalStart. Format "YYYY-MM-DD". The timezone used is the one on the server.
IntervalEnd Datetime Required Filters all usages where UsageEnd <= IntervalEnd. Format "YYYY-MM-DD". The timezone used is the one on the server.
OptionCode String Optional Unique 2Checkout option code. The pay-per-usage price options group for which the usage is uploaded. Example: metered pricing. 
RenewalOrderReference Number Optional The renewal order unique identifier.

Request Sample

<?php
$host = 'http://api.sandbox58.avangate.local/rpc/6.0/';
$merchantCode = "666999";
$key = "%y~8|m]T84p[W4+O1]_?";
$string = strlen($merchantCode) . $merchantCode . strlen(gmdate('Y-m-d H:i:s')) . gmdate('Y-m-d H:i:s');
$hash = hash_hmac('md5', $string, $key);
$i = 1; // counter for api calls
// call login
$jsonRpcRequest = new stdClass();
$jsonRpcRequest->jsonrpc = '2.0';
$jsonRpcRequest->method = 'login';
$jsonRpcRequest->params = array($merchantCode, gmdate('Y-m-d H:i:s'), $hash);
$jsonRpcRequest->id = $i++;
$sessionID = callRPC($jsonRpcRequest, $host);
var_dump("session id:" . $sessionID);
// call api methods below
$SubscriptionReference = 'B7D8E72224';
$jsonRpcRequest = array(
    'jsonrpc' => '2.0',
    'method'  => 'getSubscriptionUsages',
    'params'  =>
        array(
            $sessionID,
            [
                'SubscriptionReference' => $SubscriptionReference,
                "Page"                  => 1,
                "Limit"                 => 10,
                "IntervalStart"         => "2020-07-01 10:40:00",
                "IntervalEnd"           => "2020-08-01 10:40:00",
                "RenewalOrderReference" => "11749701"
            ]
        ),
    'id'      => $i++
);
var_dump ("getSubscriptionUsages", callRPC((Object)$jsonRpcRequest, $host));

function callRPC($Request, $hostUrl, $Debug = false) {
    $curl = curl_init($hostUrl);
    curl_setopt( $curl, CURLOPT_POST, 1);
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
    $RequestString = json_encode($Request);
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $RequestString);
    curl_setopt( $curl, CURLOPT_COOKIE, "XDEBUG_SESSION=PHPSTORM");
    if (stristr($hostUrl, 'sandboxdev') || stristr($hostUrl, 'git-sb')) {
        curl_setopt( $curl, CURLOPT_PROXY, 'localhost:3128');
        //  curl_setopt( $curl, CURLOPT_PROXY, 'proxy.avangate.local:8080');
        //  curl_setopt( $curl, CURLOPT_PROXYUSERPWD, 'andrei.avramica:pw'); // for authenticated proxies
    }
    $ResponseString = curl_exec ($curl);
    if (!empty($ResponseString)) {
        $Response = json_decode ($ResponseString,false);
        if (isset($Response->error) && !is_null($Response->error)) {
            var_dump($Response->error) ;
            return null;
        }
        return $Response->result;
    } else {
        return null;
    }
}
exit;

Response

The getUsages method via JSON-RPC APIv6 returns the Usage object when successful.

class stdClass#4 (2) {
      public $Items =>
      array(1) {
        [0] =>
        class stdClass#3 (8) {
          public $UsageReference =>
          string(12) "120011114371"
          public $SubscriptionReference =>
          string(10) "67F3AD6A32"
          public $OptionCode =>
          string(6) "USG_MN"
          public $UsageStart =>
          string(19) "2020-07-06 12:00:00"
          public $UsageEnd =>
          string(19) "2020-07-07 12:00:00"
          public $Units =>
          int(7)
          public $Description =>
          string(15) "Response sample"
          public $RenewalOrderReference =>
          int(0)
        }
      }
      public $Pagination =>
      class stdClass#5 (3) {
        public $Page =>
        int(1)
        public $Limit =>
        int(10)
        public $Count =>
        int(1)
      }
    }

Error Handling

The below errors are returned when the getUsages method via JSON-RPC APIv6 is unsuccessful.

Error message description Error code Error message

 

 

 

 

 

 

 

 

 

 

 

The provided parameters lack the required type or format.

SEARCH_PAGE_INVALID The Page parameter must be a positive integer higher than or equal to 1.
SEARCH_LIMIT_INVALID The Limit parameter must be a positive integer lower than 100.
MANDATORY_FIELDS_MISSING Both 'IntervalStart' and 'IntervalEnd' parameters must be provided.
FILTER_INVALID If provided, 'RenewalOrderReference' must be a positive integer.
FILTER_INVALID 'IntervalStart' must be provided in the following format: YYYY-MM-DD HH:MM:SS.
FILTER_INVALID 'IntervalStart' must be provided in the following format: YYYY-MM-DD HH:MM:SS.
FILTER_INVALID 'IntervalEnd' must be provided in the following format: YYYY-MM-DD HH:MM:SS.
FILTER_INVALID 'IntervalEnd' must be provided in the following format: YYYY-MM-DD HH:MM:SS.
SEARCH_PAGE_INVALID 'The Page parameter must be a positive integer higher than or equal to 1.
SUBSCRIPTION_NOT_FOUND Subscription not found.
Unexpected error happens INTERNAL_ERROR <unexpected_error>