From 5d341855ca5e1f39c2c9954e60b4f84e5d5ba61f Mon Sep 17 00:00:00 2001 From: rkomatz_envg Date: Tue, 19 Nov 2019 17:43:02 -0500 Subject: [PATCH] Updated call URL as wish destroyed v2 auth URL --- src/Wish/WishAuth.php | 4 +- src/Wish/WishRequest.php | 149 +++++++++++++++++++++------------------ 2 files changed, 84 insertions(+), 69 deletions(-) diff --git a/src/Wish/WishAuth.php b/src/Wish/WishAuth.php index 35eb0a6..8263987 100644 --- a/src/Wish/WishAuth.php +++ b/src/Wish/WishAuth.php @@ -38,7 +38,7 @@ public function __construct($client_id,$client_secret,$session_type='prod'){ } public function getToken($code, $redirect_uri){ - $type = 'POST'; + $type = 'GET'; $path = 'oauth/access_token'; $params = array( 'client_id'=>$this->client_id, @@ -64,7 +64,7 @@ public function getToken($code, $redirect_uri){ } public function refreshToken($refresh_token){ - $type = 'POST'; + $type = 'GET'; $path = 'oauth/refresh_token'; $params = array( 'client_id'=>$this->client_id, diff --git a/src/Wish/WishRequest.php b/src/Wish/WishRequest.php index 8eed4b0..01c46c1 100644 --- a/src/Wish/WishRequest.php +++ b/src/Wish/WishRequest.php @@ -1,11 +1,11 @@ -session = $session; - $this->method = $method; - $this->path = $path; - $params['access_token'] = $session->getAccessToken(); - if($session->getMerchantId())$params['merchant_id']=$session->getMerchantId(); - $this->params = $params; - } + public function __construct($session, $method, $path, $params = array()) + { + $this->session = $session; + $this->method = $method; + $this->path = $path; + $params['access_token'] = $session->getAccessToken(); + if ($session->getMerchantId()) $params['merchant_id'] = $session->getMerchantId(); + $this->params = $params; + } - public function getVersion(){ - return static::VERSION; - } + public function getVersion() + { + return static::VERSION; + } - public function getRequestURL(){ - switch($this->session->getSessionType()){ - case WishSession::SESSION_PROD: return static::BASE_PROD_PATH; - case WishSession::SESSION_SANDBOX: return static::BASE_SANDBOX_PATH; - case WishSession::SESSION_STAGE: return static::BASE_STAGE_PATH; - default: throw new InvalidArgumentException("Invalid session type"); + public function getRequestURL() + { + switch ($this->session->getSessionType()) { + case WishSession::SESSION_PROD: + return static::BASE_PROD_PATH; + case WishSession::SESSION_SANDBOX: + return static::BASE_SANDBOX_PATH; + case WishSession::SESSION_STAGE: + return static::BASE_STAGE_PATH; + default: + throw new InvalidArgumentException("Invalid session type"); + } } - } - public function execute(){ - $url = $this->getRequestURL().$this->getVersion().$this->path; - $curl = curl_init(); - $params = $this->params; + public function execute() + { + $url = $this->getRequestURL(); - $options = array( - CURLOPT_CONNECTTIMEOUT => 10, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_USERAGENT => 'wish-php-sdk', - CURLOPT_HEADER => 'true' - ); + // Use v3 for auth, v2 for all other calls, because Wish is INCREDIBLY asinie + if ($this->path == 'oauth/access_token' || $this->path == 'oauth/refresh_token') { + $url .= 'v3/'; + } else { + $url .= 'v2/'; + } - if($this->method === "GET"){ - $url = $url."?".http_build_query($params); - }else { - $options[CURLOPT_POSTFIELDS] = $params; - } - $options[CURLOPT_URL] = $url; - curl_setopt_array($curl, $options); + $url .= $this->path; + $curl = curl_init(); + $params = $this->params; - $result = curl_exec($curl); - $error = curl_errno($curl); - - $error_message = curl_error($curl); + $options = array( + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_USERAGENT => 'wish-php-sdk', + CURLOPT_HEADER => 'true' + ); - if($error){ - throw new ConnectionException($error_message); - } - $httpStatus = curl_getinfo($curl,CURLINFO_HTTP_CODE); - $headerSize = curl_getinfo($curl,CURLINFO_HEADER_SIZE); - curl_close($curl); - - $decoded_result = json_decode($result); - if($decoded_result === null){ - $out = array(); - parse_str($result,$out); - return new WishResponse($this,$out,$result); - } - return new WishResponse($this,$decoded_result,$result); - } + if ($this->method === "GET") { + $url = $url . "?" . http_build_query($params); + } else { + $options[CURLOPT_POSTFIELDS] = $params; + } + $options[CURLOPT_URL] = $url; + curl_setopt_array($curl, $options); + $result = curl_exec($curl); + $error = curl_errno($curl); + $error_message = curl_error($curl); + if ($error) { + throw new ConnectionException($error_message); + } + $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + curl_close($curl); + $decoded_result = json_decode($result); + if ($decoded_result === null) { + $out = array(); + parse_str($result, $out); + return new WishResponse($this, $out, $result); + } + return new WishResponse($this, $decoded_result, $result); + } }