selimppc

JetPHPAPI

Sep 29th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.74 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SelimReza
  5.  * Date: 9/27/17
  6.  * Time: 5:32 PM
  7.  */
  8.  
  9. namespace App\Http\Helpers;
  10.  
  11.  
  12. class JetPHPAPI
  13. {
  14.  
  15.     #public $api_user_id = 'A8B9D0F42C2479BE19F829CAB43461FC57EFCA64'; // Test
  16.    public $api_user_id = 'AC6E88D6688A778782A531A4302C280DE9064F69'; // Live
  17.     #public $api_secret = 'cTMGxdy7oTyNf2zf6VP21B6yLvwfmli+DVVMTEX9RX+u'; // Test
  18.    public $api_secret = 'CfhFsXuY9kJDvEUA3KqtjBkure+0CcEEy38DeOVxkN+R'; // Live
  19.     public $merchant_id = 'cd0426a0d4ac4680a8ad586746db6942';
  20.     public $api_prefix = 'https://merchant-api.jet.com/api/';
  21.  
  22.     private $api_token = '';
  23.     private $api_token_ts = '';
  24.  
  25.     /**
  26.      * Refresh Jet API token
  27.      **/
  28.     public function getNewToken()
  29.     {
  30.         $ch = curl_init($this->api_prefix . '/Token');
  31.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  32.         //If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem');
  33.         $request = json_encode(array(
  34.             "user" => $this->api_user_id,
  35.             "pass" => $this->api_secret
  36.         ));
  37.         curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  38.         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  39.                 'Content-Type: application/json',
  40.                 'Content-Length: ' . strlen($request))
  41.         );
  42.         $data = curl_exec($ch);
  43.         curl_close($ch);
  44.         if ($data = json_decode($data)) {
  45.             if ($token = $data->id_token) {
  46.                 //SAVE $token SOMEWHERE and save last time you got a token
  47.                 $this->setToken($token);
  48.                 $this->setTokenTs(date('r'));
  49.                 return true;
  50.             }
  51.         }
  52.         return false;
  53.     }
  54.  
  55.     /*
  56.      * Token saving and getting
  57.      */
  58.     private function setToken($token) {
  59.         if(strlen($token) > 0)
  60.             $this->api_token = $token;
  61.  
  62.         return false;
  63.     }
  64.  
  65.     private function setTokenTs($token) {
  66.         if(strlen($token) > 0)
  67.             $this->api_token_ts = $token;
  68.  
  69.         return false;
  70.     }
  71.  
  72.     private function getTokenTs() {
  73.         if($this->api_token_ts == '')
  74.             return $this->api_token_ts;
  75.  
  76.         return false;
  77.     }
  78.  
  79.     private function getToken() {
  80.         if($this->api_token == '')
  81.             return $this->api_token;
  82.  
  83.         return false;
  84.     }
  85.  
  86.     /**
  87.      * Upload bulk file to Jet
  88.      **/
  89.     public function uploadFile($type, $path)
  90.     {
  91.         $data = $this->apiGET('files/uploadToken');
  92.         $url = $data->url;
  93.         $file_id = $data->jet_file_id;
  94.         $this->apiFilePUT($url, $path, $file_id);
  95.         $this->apiGET('files/uploaded', array("url" => $url, "file_type" => $type, "file_name" => basename($path) . ".gz"));
  96.     }
  97.     /**
  98.      * PUT request to $url
  99.      **/
  100.     public function apiPUT($end_point, $request)
  101.     {
  102.         //generate new token
  103.         $this->getNewToken();
  104.  
  105.         if (substr($end_point, 0, 1) === "/") $end_point = substr($end_point, 1);
  106.  
  107.         //get token if it has been over 9 hours since the last token
  108.         //CHANGE THIS TO WHERE YOU SAVED THE TOKEN AND TS
  109.         if (round((strtotime("now") - strtotime($this->getTokenTs())) / 3600, 1) > 9) $this->getNewToken();
  110.  
  111.         $api_call_data = array();
  112.         $api_call_data["request_ts"] = date("r", strtotime("now"));
  113.  
  114.         $ch = curl_init($this->api_prefix . $end_point);
  115.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  116.         //If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem');
  117.         /*$request = json_encode($request);
  118.         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  119.                 'Content-Type: application/json',
  120.                 'Content-Length: ' . strlen($request),
  121.                 //CHANGE THIS TO WHERE YOU SAVED YOUR TOKEN
  122.                 'Authorization: Bearer ' . $this->api_token)
  123.         );
  124.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  125.         curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  126.         $data = curl_exec($ch);*/
  127.  
  128.         $data = $request;
  129.  
  130.         #print_r($data);
  131.        #print "\n";
  132.  
  133.         echo(curl_error($ch));
  134.         $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  135.         curl_close($ch);
  136.         $api_call_data["request_data"] = (string)var_export($request, true);
  137.         $api_call_data["response_ts"] = date("r", strtotime("now"));
  138.         $api_call_data["response_data"] = (string)var_export($data, true);
  139.         $api_call_data["request_url"] = $this->api_prefix . $end_point;
  140.         $api_call_data["service_type"] = "Jet";
  141.         $api_call_data["status_code"] = $httpcode;
  142.         //SAVE $api_call_data SOMEWHERE
  143.  
  144.         print_r($api_call_data);
  145.         print "\n";
  146.  
  147.         #dd($api_call_data);
  148.        #exit;
  149.  
  150.         return json_decode($data);
  151.     }
  152.     /**
  153.      * PUT request to $url
  154.      **/
  155.     public function apiFilePUT($url, $path, $file_id = null)
  156.     {
  157.         //get token if it has been over 9 hours since the last token
  158.         //CHANGE THIS TO WHERE YOU SAVED THE TOKEN AND TS
  159.         if (round((strtotime("now") - strtotime($this->getTokenTs())) / 3600, 1) > 9) $this->getNewToken();
  160.         $api_call_data = array();
  161.         $api_call_data["request_ts"] = date("r", strtotime("now"));
  162.  
  163.         //gzip the data
  164.         $file = file_get_contents($path);
  165.         $gz_file = gzencode($file, 9);
  166.         $g_path = $path . ".gz";
  167.         $gfp = fopen($g_path, 'w+');
  168.         fwrite($gfp, $gz_file);
  169.         rewind($gfp);
  170.  
  171.         $ch = curl_init();
  172.         curl_setopt($ch, CURLOPT_URL, $url);
  173.         curl_setopt($ch, CURLOPT_PUT, 1);
  174.         //If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem');
  175.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-ms-blob-type: BlockBlob'));
  176.         curl_setopt($ch, CURLOPT_INFILE, $gfp);
  177.         curl_setopt($ch, CURLOPT_INFILESIZE, filesize($g_path));
  178.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  179.         $data = curl_exec($ch);
  180.         fclose($gfp);
  181.  
  182.         //delete the file / gzip file
  183.         //unlink($path);
  184.         unlink($g_path);
  185.  
  186.         $api_call_data["response_ts"] = date("r", strtotime("now"));
  187.         $api_call_data["response_data"] = (string)var_export($data, true);
  188.         $api_call_data["request_url"] = $url;
  189.         $api_call_data["service_type"] = "Jet";
  190.         $api_call_data["check_status"] = true;
  191.         if ($file_id) $api_call_data["service_id"] = $file_id;
  192.         //SAVE $api_call_data
  193.         return $data;
  194.     }
  195.     /**
  196.      * Jet API GET
  197.      **/
  198.     public function apiGET($end_point, $request = null)
  199.     {
  200.         if (substr($end_point, 0, 1) === "/") $end_point = substr($end_point, 1);
  201.         //get token if it has been over 9 hours since the last token
  202.         //CHANGE THIS TO WHERE YOU SAVED THE TOKEN AND TS
  203.         if (round((strtotime("now") - strtotime($this->getTokenTs())) / 3600, 1) > 9) $this->getNewToken();
  204.         $api_call_data = array();
  205.         $api_call_data["request_ts"] = date("r", strtotime("now"));
  206.  
  207.         $ch = curl_init($this->api_prefix . $end_point);
  208.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  209.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->getToken()));
  210.         //If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem');
  211.         if ($request) {
  212.             $request = json_encode($request);
  213.             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  214.             $api_call_data["request_data"] = (string)var_export($request, true);
  215.         }
  216.         $data = utf8_encode(curl_exec($ch));
  217.         echo(curl_error($ch));
  218.         $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  219.         curl_close($ch);
  220.  
  221.         $api_call_data["response_ts"] = date("r", strtotime("now"));
  222.         $api_call_data["response_data"] = (string)var_export($data, true);
  223.         $api_call_data["request_url"] = $this->api_prefix . $end_point;
  224.         $api_call_data["service_type"] = "Jet";
  225.         $api_call_data["status_code"] = $httpcode;
  226.         //SAVE $api_call_data
  227.  
  228.         return json_decode($data);
  229.     }
  230.  
  231.     /**
  232.      * Poll for orders
  233.      **/
  234.     public function processOrdersByStatus($status)
  235.     {
  236.         $data = $this->apiGET("orders/$status");
  237.         foreach ($data->order_urls as $end_point) {
  238.             $this->processOrder($end_point);
  239.         }
  240.     }
  241.  
  242.     /**
  243.      * Process Order
  244.      **/
  245.     public function processOrder($end_point)
  246.     {
  247.         $data = $this->apiGET($end_point);
  248.         //STORE AND PROCESS $data
  249.     }
  250.  
  251.     /**
  252.      * Testing Access to the Jet.com API
  253.      **/
  254.     public function testAccess()
  255.     {
  256.         if($this->getNewToken()) {
  257.             echo 'It worked!';
  258.         }
  259.         else {
  260.             echo 'We could not get Jet.com api access!';
  261.         }
  262.     }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment