Created
April 19, 2021 16:01
-
-
Save malkafly/af9eb58f014ae21ddc97f33198eb22cb to your computer and use it in GitHub Desktop.
Zoop PHP com Curl
This file contains 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 Buyer extends ZoopGateway | |
{ | |
public function create($data) { | |
$url = $this->endpoint . '/buyers'; | |
return $this->request('POST', $url, $data); | |
} | |
public function update($id, $data) { | |
$url = $this->endpoint . '/buyers/' . $id; | |
return $this->request('PUT', $url, $data); | |
} | |
public function get($id) { | |
$url = $this->endpoint . '/buyers/' . $id; | |
return $this->request('GET', $url); | |
} | |
public function delete($id) { | |
$url = $this->endpoint . '/buyers/' . $id; | |
return $this->request('DELETE', $url); | |
} | |
} |
This file contains 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/businesses'; | |
return $this->request('POST', $url, $data); | |
} | |
public function uptadeBusiness($id, $data) { | |
$url = $this->endpoint . '/sellers/businesses/' . $id; | |
return $this->request('PUT', $url, $data); | |
} | |
public function createIndividual($data) { | |
$url = $this->endpoint . '/sellers/individuals'; | |
return $this->request('POST', $url, $data); | |
} | |
public function updateIndividual($id, $data) { | |
$url = $this->endpoint . '/sellers/individuals/' . $id; | |
return $this->request('PUT', $url, $data); | |
} | |
public function get($gateway_id) { | |
$url = $this->endpoint . '/sellers/' . $gateway_id; | |
return $this->request('GET', $url, []); | |
} | |
public function getByDocument($document, $type) { | |
$url = $this->endpoint . '/sellers/search?' . (($type == 'PF') ? 'taxpayer_id=' : 'ein=') . $document; | |
return $this->request('GET', $url, []); | |
} | |
public function sendDocument($gateway_id, $data) { | |
$url = $this->endpoint . '/sellers/' . $gateway_id . '/documents'; | |
return $this->request('POST', $url, $data); | |
} | |
public function updateDocument($gateway_id, $document_id, $data) { | |
$url = $this->endpoint . '/sellers/' . $gateway_id . '/documents/' . $document_id; | |
return $this->request('PUT', $url, $data); | |
} | |
} |
This file contains 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 Transaction extends ZoopGateway | |
{ | |
public function post($data) { | |
$url = $this->endpoint . '/transactions'; | |
return $this->request('POST', $url, $data); | |
} | |
// public function update($id, $data) { | |
// $url = $this->endpoint . '/transactions/' . $id; | |
// return $this->request('PUT', $url, $data); | |
// } | |
public function get($id) { | |
$url = $this->endpoint . '/transactions/' . $id; | |
return $this->request('GET', $url); | |
} | |
public function delete($id) { | |
$url = $this->endpoint . '/transactions/' . $id; | |
return $this->request('DELETE', $url); | |
} | |
} |
This file contains 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'"); | |
// } | |
// echo $url; | |
// echo $method; | |
// echo json_encode($data); | |
// die; | |
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