Skip to content

Commit

Permalink
Updated call URL as wish destroyed v2 auth URL
Browse files Browse the repository at this point in the history
  • Loading branch information
rkomatz-envisia committed Nov 19, 2019
1 parent 047c732 commit 5d34185
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/Wish/WishAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
149 changes: 82 additions & 67 deletions src/Wish/WishRequest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
<?php
/**
* Copyright 2014 Wish.com, ContextLogic or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
Expand All @@ -14,85 +14,100 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Wish;

use Wish\Exception\ConnectionException;

class WishRequest{
const VERSION = "v2/";
const BASE_PROD_PATH = "https://merchant.wish.com/api/";
const BASE_SANDBOX_PATH = "https://sandbox.merchant.wish.com/api/";
const BASE_STAGE_PATH = "https://merch.corp.contextlogic.com/api/";
private $session;
private $method;
private $path;
private $params;
class WishRequest
{
const VERSION = "v2/";
const BASE_PROD_PATH = "https://merchant.wish.com/api/";
const BASE_SANDBOX_PATH = "https://sandbox.merchant.wish.com/api/";
const BASE_STAGE_PATH = "https://merch.corp.contextlogic.com/api/";
private $session;
private $method;
private $path;
private $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 __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);
}
}

0 comments on commit 5d34185

Please sign in to comment.