Created
April 8, 2019 16:56
-
-
Save malkafly/420e8778502c8ec5f5c7bc95b1b692e6 to your computer and use it in GitHub Desktop.
Integração rápida com a Zoop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Zoop; | |
use Auth; | |
use Carbon\Carbon; | |
use App\Zoop\ZoopGateway; | |
class Seller extends ZoopGateway | |
{ | |
public function createBusiness($data) { | |
$url = $this->endpoint . '/sellers/business'; | |
return $this->request('POST', $url, $data); | |
} | |
public function getBusiness($gateway_id) { | |
$url = $this->endpoint . '/sellers/' . $gateway_id; | |
return $this->request('GET', $url, []); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Zoop; | |
use Auth; | |
use Carbon\Carbon; | |
class ZoopGateway | |
{ | |
public $endpoint; | |
public function __construct() | |
{ | |
$this->endpoint = 'https://api.zoop.ws/v1/marketplaces/' . env('ZOOP_MARKETPLACE_ID'); | |
} | |
public function request($method, $url, $data = []){ | |
global $zoop_last_api_response_code; | |
// if ($this->zoopBase->getMarketplaceId() == null) { | |
// throw new ZoopAuthenticationException("Marketplace id não configurada. Verifique seu arquivo de configurações em 'config/zoop.php'"); | |
// } | |
list($response_body, $response_code) = $this->requestWithCURL($method, $url, $data); | |
$response = json_decode($response_body); | |
// if (json_last_error() != JSON_ERROR_NONE) throw new ZoopObjectNotFound($response_body); | |
// if ($response_code == 404) throw new ZoopObjectNotFound($response_body); | |
if (isset($response->errors)) { | |
if ((gettype($response->errors) != "string") && count(get_object_vars($response->errors)) == 0) { | |
unset($response->errors); | |
} | |
else if ((gettype($response->errors) != "string") && count(get_object_vars($response->errors)) > 0) { | |
$response->errors = (Array) $response->errors; | |
} | |
if (isset($response->errors) && (gettype($response->errors) == "string")) { | |
$response->errors = $response->errors; | |
} | |
} | |
$zoop_last_api_response_code = $response_code; | |
return $response; | |
} | |
private function requestWithCURL($method, $url, $data = Array()){ | |
$curl = curl_init(); | |
$opts = Array(); | |
if(strtolower($method) == 'file'){ | |
$opts[CURLOPT_POST] = 1; | |
$opts[CURLOPT_POSTFIELDS] = $data; | |
} | |
if (strtolower($method) == "post") { | |
$opts[CURLOPT_POST] = 1; | |
$opts[CURLOPT_POSTFIELDS] = http_build_query($data); | |
} | |
if (strtolower($method) == "delete") $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; | |
if (strtolower($method) == "put") { | |
$opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; | |
$opts[CURLOPT_POSTFIELDS] = http_build_query($data); | |
} | |
$pwd = base64_encode(env('ZOOP_PUBLISHABLE_KEY') . ':'); | |
$opts[CURLOPT_URL] = $url; | |
$opts[CURLOPT_USERPWD] = $pwd; | |
$opts[CURLOPT_RETURNTRANSFER] = true; | |
$opts[CURLOPT_CONNECTTIMEOUT] = 30; | |
$opts[CURLOPT_TIMEOUT] = 80; | |
$opts[CURLOPT_HTTPHEADER] = [ | |
'Accept: application/json', | |
'Accept-Charset: utf-8', | |
'Accept-Language: pt-br;q=0.9,pt-BR', | |
'Authorization: Basic ' . $pwd | |
]; | |
$opts[CURLOPT_SSL_VERIFYHOST] = 2; | |
$opts[CURLOPT_SSL_VERIFYPEER] = false; | |
// echo json_encode($opts); | |
// die; | |
curl_setopt_array($curl, $opts); | |
$response_body = curl_exec($curl); | |
$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
curl_close($curl); | |
return Array($response_body, $response_code); | |
} | |
private function encodeParameters($method, $url, $data = []) { | |
$method = strtolower($method); | |
switch($method) { | |
case "get": | |
case "delete": | |
$paramsInURL = $this->arrayToParams($data); | |
$data = null; | |
$url = (strpos($url,"?")) ? $url . "&" . $paramsInURL : $url . "?" . $paramsInURL; | |
break; | |
case "post": | |
case "put": | |
$data = $this->arrayToParams($data); | |
break; | |
} | |
return [ | |
$url, | |
$data | |
]; | |
} | |
public static function arrayToParams($array, $prefix = null){ | |
if (!is_array($array)) return $array; | |
$params = Array(); | |
foreach ($array as $k => $v) { | |
if (is_null($v)) continue; | |
if ($prefix && $k && !is_int($k)) | |
$k = $prefix."[".$k."]"; | |
else if ($prefix) | |
$k = $prefix."[]"; | |
if (is_array($v)) { | |
$params[] = self::arrayToParams($v, $k); | |
} else { | |
$params[] = $k."=".urlencode($v); | |
} | |
} | |
return implode("&", $params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment