Guest User

Untitled

a guest
Apr 12th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.85 KB | None | 0 0
  1. <?php
  2. //TODO: set your API key found in http://wyday.com/limelm/settings/
  3. $api_key = 'PASTE THE API KEY HERE';
  4.  
  5. //TODO: set the version id to generate & find keys for
  6. // the version id is found in the url. For example http://wyday.com/limelm/version/100/
  7. // the version id is 100.
  8. $version_id = '100';
  9.  
  10. date_default_timezone_set('America/Toronto');
  11.  
  12. function urlEncodePostData($post_data)
  13. {
  14.     $post_string = '';
  15.     foreach ($post_data as $key => $value)
  16.     {
  17.         if (is_array($value))
  18.         {
  19.             foreach ($value as $sub_value)
  20.             {
  21.                 $post_string .= $key.'[]='.urlencode($sub_value).'&';
  22.             }
  23.         }
  24.         else
  25.             $post_string .= $key.'='.urlencode($value).'&';
  26.     }
  27.     $post_string = rtrim($post_string, '& ');
  28.  
  29.     $request = curl_init('https://wyday.com/limelm/api/rest/');
  30.     curl_setopt($request, CURLOPT_HEADER, 0);
  31.     curl_setopt($request, CURLOPT_ENCODING, "");
  32.     curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
  33.     curl_setopt($request, CURLOPT_POSTFIELDS, $post_string);
  34.     curl_setopt($request, CURLOPT_SSL_VERIFYPEER, TRUE);
  35.     return $request;
  36. }
  37.  
  38. function getProductKeyDetails($post_data)
  39. {
  40.     $request = urlEncodePostData($post_data);
  41.    
  42.     $jObj = json_decode(curl_exec($request));
  43.  
  44.     if ($jObj->stat == 'ok')
  45.     {
  46.         foreach ($jObj->pkeys->pkey as $pkey)
  47.         {
  48.             return array
  49.             (
  50.                 'key' => $pkey->key,
  51.                 'id' => $pkey->id
  52.             );
  53.         }
  54.     }
  55.     else
  56.     {
  57.         // Uncomment the next line to see the error LimeLM is giving you.
  58.         //echo $jObj->message;
  59.     }
  60.     return null;
  61. }
  62.  
  63. function getProductKeyExpiry($post_data)
  64. {
  65.     $request = urlEncodePostData($post_data);
  66.     $jObj = json_decode(curl_exec($request));
  67.  
  68.     if ($jObj->stat == 'ok')
  69.     {
  70.         foreach ($jObj->pkey->features->feature as $feature)
  71.         {
  72.             if($feature->name == 'subscription_expiry')
  73.                 return $feature->value;
  74.         }
  75.     }
  76.     else
  77.     {
  78.         // Uncomment the next line to see the error LimeLM is giving you.
  79.         //echo $jObj->message;
  80.     }
  81.     return null;
  82. }
  83.  
  84. function setProductKeyDetails($post_data)
  85. {
  86.     $request = urlEncodePostData($post_data);
  87.     $jObj = json_decode(curl_exec($request));
  88.  
  89.     if ($jObj->stat !== 'ok')
  90.     {
  91.         // Uncomment the next line to see the error LimeLM is giving you.
  92.         //echo $jObj->message;
  93.     }
  94. }
  95.    
  96. try
  97. {
  98.     $post_data = array(
  99.         'method' => 'limelm.pkey.advancedSearch',
  100.         'api_key' => $api_key,
  101.         'version_id' => $version_id,
  102.         'email' => $email,
  103.         'feature_name' => array('fastspring_subscription_id'),
  104.         'feature_value' => array($reference),
  105.         'feature_match' => array('exact'),
  106.         'nojsoncallback' => 1,
  107.         'format' => 'json'
  108.     );
  109.     $product_key_details = getProductKeyDetails($post_data);   
  110.    
  111.     if($product_key_details !== null)
  112.     {
  113.         $product_key = $product_key_details['key'];
  114.         $product_key_id = $product_key_details['id'];
  115.        
  116.         $post_data = array(
  117.             'method' => 'limelm.pkey.getDetails',
  118.             'api_key' => $api_key,
  119.             'pkey_id' => $product_key_id,
  120.             'nojsoncallback' => 1,
  121.             'format' => 'json'
  122.         );
  123.         $product_key_expiry = getProductKeyExpiry($post_data);
  124.         $current_date_time = date('Y-m-d H:i:s', time());
  125.        
  126.         if($current_date_time > $product_key_expiry)       
  127.         {
  128.             $new_product_key_expiry = date('Y-m-d H:i:s', strtotime($current_date_time . '+1 year'));
  129.         }
  130.         else
  131.         {
  132.             $new_product_key_expiry = date('Y-m-d H:i:s', strtotime($product_key_expiry . '+1 year'));
  133.         }
  134.        
  135.         $post_data = array(
  136.             'method' => 'limelm.pkey.setDetails',
  137.             'api_key' => $api_key,
  138.             'pkey_id' => $product_key_id,
  139.             'feature_name' => array('subscription_expiry'),
  140.             'feature_value' => array($new_product_key_expiry),
  141.             'nojsoncallback' => 1,
  142.             'format' => 'json'
  143.         );
  144.         setProductKeyDetails($post_data);
  145.         echo $product_key . "\r\n";
  146.     }      
  147. }
  148. catch (Exception $e)
  149. {
  150.     // Uncomment the next line to see the exception.
  151.     //echo 'Failure: '.$e->getMessage();
  152. }
  153. ?>
Add Comment
Please, Sign In to add comment