2025-07-21 11:34:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simplified IOP Client for WordPress plugin
|
|
|
|
|
* Based on AliExpress SDK but without namespaces
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class SimpleIopClient
|
|
|
|
|
{
|
|
|
|
|
public $appKey;
|
|
|
|
|
public $secretKey;
|
|
|
|
|
public $gatewayUrl;
|
|
|
|
|
protected $signMethod = "sha256";
|
|
|
|
|
protected $sdkVersion = "iop-sdk-php-20220608";
|
|
|
|
|
public $last_error = '';
|
|
|
|
|
|
|
|
|
|
public function __construct($url = "", $appKey = "", $secretKey = "")
|
|
|
|
|
{
|
|
|
|
|
if (strlen($url) === 0) {
|
|
|
|
|
throw new InvalidArgumentException("url is empty", 0);
|
|
|
|
|
}
|
|
|
|
|
$this->gatewayUrl = $url;
|
|
|
|
|
$this->appKey = $appKey;
|
|
|
|
|
$this->secretKey = $secretKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function generateSign($apiName, $params)
|
|
|
|
|
{
|
|
|
|
|
ksort($params);
|
|
|
|
|
$stringToBeSigned = $apiName;
|
|
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
|
if (is_array($v) || is_object($v)) {
|
|
|
|
|
$v = json_encode($v);
|
|
|
|
|
}
|
|
|
|
|
$stringToBeSigned .= $k . $v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strtoupper(hash_hmac($this->signMethod, $stringToBeSigned, $this->secretKey));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute($request, $accessToken = null)
|
|
|
|
|
{
|
|
|
|
|
$sysParams = [
|
|
|
|
|
"app_key" => $this->appKey,
|
|
|
|
|
"sign_method" => $this->signMethod,
|
|
|
|
|
"timestamp" => time() * 1000,
|
|
|
|
|
"partner_id" => $this->sdkVersion,
|
|
|
|
|
"method" => $request->getApiName(),
|
|
|
|
|
"v" => "1.0",
|
|
|
|
|
"format" => "json"
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($accessToken) {
|
2025-07-21 12:03:49 +02:00
|
|
|
// Personal tokens use access_token parameter instead of session
|
|
|
|
|
$sysParams["access_token"] = $accessToken;
|
2025-07-21 11:34:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$apiParams = $request->getApiParams();
|
|
|
|
|
$totalParams = array_merge($apiParams, $sysParams);
|
|
|
|
|
$sign = $this->generateSign($request->getApiName(), $totalParams);
|
|
|
|
|
$totalParams["sign"] = $sign;
|
|
|
|
|
|
|
|
|
|
$requestUrl = $this->gatewayUrl;
|
|
|
|
|
|
|
|
|
|
if(class_exists('LOG')) {
|
|
|
|
|
LOG::add("DEBUG SimpleSDK: Making request to: " . $requestUrl);
|
|
|
|
|
LOG::add("DEBUG SimpleSDK: Method: " . $request->getApiName());
|
|
|
|
|
LOG::add("DEBUG SimpleSDK: Params: " . json_encode($totalParams));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response = $this->curl($requestUrl, $totalParams);
|
|
|
|
|
|
|
|
|
|
if(class_exists('LOG')) {
|
|
|
|
|
LOG::add("DEBUG SimpleSDK: Response: " . $response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json_decode($response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function curl($url, $postFields = null)
|
|
|
|
|
{
|
|
|
|
|
$ch = curl_init();
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
|
|
|
|
|
if (is_array($postFields) && 0 < count($postFields)) {
|
|
|
|
|
$postBodyString = "";
|
|
|
|
|
foreach ($postFields as $k => $v) {
|
|
|
|
|
$postBodyString .= "$k=" . urlencode($v) . "&";
|
|
|
|
|
}
|
|
|
|
|
unset($k, $v);
|
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
|
|
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
|
$this->last_error = curl_error($ch);
|
|
|
|
|
if(class_exists('LOG')) {
|
|
|
|
|
LOG::add("DEBUG SimpleSDK: CURL Error: " . $this->last_error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_close($ch);
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SimpleIopRequest
|
|
|
|
|
{
|
|
|
|
|
private $apiName;
|
|
|
|
|
private $apiParams = [];
|
|
|
|
|
|
|
|
|
|
public function __construct($apiName)
|
|
|
|
|
{
|
|
|
|
|
$this->apiName = $apiName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addApiParam($key, $value)
|
|
|
|
|
{
|
|
|
|
|
$this->apiParams[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getApiName()
|
|
|
|
|
{
|
|
|
|
|
return $this->apiName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getApiParams()
|
|
|
|
|
{
|
|
|
|
|
return $this->apiParams;
|
|
|
|
|
}
|
|
|
|
|
}
|