ltsecurityinc安防产品在线商城代码
This commit is contained in:
Executable
+269
@@ -0,0 +1,269 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Lib;
|
||||
|
||||
use JR\NetSuite\Helper\Data;
|
||||
use JR\NetSuite\Logger\Logger;
|
||||
use JR\NetSuite\Lib\Oauth;
|
||||
use Magento\Framework\HTTP\Client\Curl;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
|
||||
class NetSuiteAPI
|
||||
{
|
||||
const CUSTOMER = 'customer';
|
||||
const CUSTOMER_WITH_FILTER = 'customerWithFilter';
|
||||
const RETURN_AUTHORIZATION = 'returnAuthorization';
|
||||
const INVENTORY_ITEM = 'inventoryItem';
|
||||
const INVENTORY_ITEM_WITH_LOCATION = 'inventoryItemWithLocation';
|
||||
const LOCATION = 'location';
|
||||
const SALES_ORDER = 'salesOrder';
|
||||
const CREATE_LEAD_SCRIPT = '351';
|
||||
const CREATE_SALES_ORDER_SCRIPT = '468';
|
||||
|
||||
const DEFAULT_PAGE_SIZE = 150;
|
||||
const DEFAULT_PAGE = 1;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Helper\Data
|
||||
*/
|
||||
protected $helper;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Logger\Logger
|
||||
*/
|
||||
protected $_logger;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Lib\Oauth
|
||||
*/
|
||||
protected $oauth;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\HTTP\Client\Curl
|
||||
*/
|
||||
public $curl;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\Serializer\Json
|
||||
*/
|
||||
public $json;
|
||||
|
||||
/**
|
||||
* @param Data $helper
|
||||
* @param Logger $_logger
|
||||
*/
|
||||
public function __construct(
|
||||
Data $helper,
|
||||
Logger $logger,
|
||||
Oauth $oauth,
|
||||
Curl $curl,
|
||||
Json $json
|
||||
) {
|
||||
$this->helper = $helper;
|
||||
$this->_logger = $logger;
|
||||
$this->oauth = $oauth;
|
||||
$this->curl = $curl;
|
||||
$this->json = $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch customer data from NetSuite
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @param $addtionalParams
|
||||
* @return array
|
||||
*/
|
||||
public function fetchCustomers($pageSize = self::DEFAULT_PAGE_SIZE, $page = 1, $addtionalParams = [])
|
||||
{
|
||||
return $this->fetchData(self::CUSTOMER, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch customer data from NetSuite by email
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @param $addtionalParams
|
||||
* @return array
|
||||
*/
|
||||
public function fetchCustomersWithFilter($pageSize = self::DEFAULT_PAGE_SIZE, $page = 1, $addtionalParams = [])
|
||||
{
|
||||
// TODO: filter not right for new lead
|
||||
return $this->fetchData(self::CUSTOMER_WITH_FILTER, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch return authorization data from NetSuite
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @return array
|
||||
*/
|
||||
public function fetchReturnAuthorizations($pageSize = self::DEFAULT_PAGE_SIZE, $page = self::DEFAULT_PAGE, $addtionalParams = [])
|
||||
{
|
||||
return $this->fetchData(self::RETURN_AUTHORIZATION, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch location data from NetSuite
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @return array
|
||||
*/
|
||||
public function fetchLocations($pageSize = self::DEFAULT_PAGE_SIZE, $page = self::DEFAULT_PAGE, $addtionalParams = [])
|
||||
{
|
||||
return $this->fetchData(self::LOCATION, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch product data from NetSuite
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @return array
|
||||
*/
|
||||
public function fetchProducts($pageSize = self::DEFAULT_PAGE_SIZE, $page = self::DEFAULT_PAGE, $addtionalParams = [])
|
||||
{
|
||||
return $this->fetchData(self::INVENTORY_ITEM, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch product data from NetSuite
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @return array
|
||||
*/
|
||||
public function fetchProductsWithLocation($pageSize = self::DEFAULT_PAGE_SIZE, $page = self::DEFAULT_PAGE, $addtionalParams = [])
|
||||
{
|
||||
return $this->fetchData(self::INVENTORY_ITEM_WITH_LOCATION, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch sales order data from NetSuite
|
||||
*
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @return array
|
||||
*/
|
||||
public function fetchOrders($pageSize = self::DEFAULT_PAGE_SIZE, $page = self::DEFAULT_PAGE, $addtionalParams = [])
|
||||
{
|
||||
return $this->fetchData(self::SALES_ORDER, $pageSize, $page, $addtionalParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch data from NetSuite
|
||||
*
|
||||
* @param $recordType
|
||||
* @param $pageSize
|
||||
* @param $page
|
||||
* @return array
|
||||
*/
|
||||
private function fetchData($recordType, $pageSize = self::DEFAULT_PAGE_SIZE, $page = self::DEFAULT_PAGE, $addtionalParams = [])
|
||||
{
|
||||
$params = [
|
||||
'recordType' => $recordType,
|
||||
'page' => $page,
|
||||
'pageSize' => $pageSize
|
||||
];
|
||||
if (!empty($addtionalParams)) {
|
||||
$params = array_merge($params, $addtionalParams);
|
||||
}
|
||||
$response = $this->postData($params);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* update customer data to NetSuite
|
||||
*/
|
||||
public function updateCustomer($data)
|
||||
{
|
||||
$this->oauth->setScript(self::CREATE_LEAD_SCRIPT);
|
||||
$response = $this->postData($data);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* update order data to NetSuite
|
||||
*/
|
||||
public function updateOrder($data)
|
||||
{
|
||||
$this->oauth->setScript(self::CREATE_SALES_ORDER_SCRIPT);
|
||||
$response = $this->postData($data);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* create return authorization in NetSuite
|
||||
*/
|
||||
public function createReturnAuthorization($data)
|
||||
{
|
||||
$params = [
|
||||
'action' => 'create',
|
||||
'recordType' => self::RETURN_AUTHORIZATION,
|
||||
'data' => $data
|
||||
];
|
||||
$response = $this->postData($params);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* update data to NetSuite
|
||||
*/
|
||||
public function updateData($recordType, $data)
|
||||
{
|
||||
$params = [
|
||||
'recordType' => $recordType,
|
||||
'data' => $data
|
||||
];
|
||||
$response = $this->postData($params);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function postData(array $data)
|
||||
{
|
||||
$url = $this->oauth->getApiUrl() . '?script=' . $this->oauth->getScript() . '&deploy=1';
|
||||
$oauthCredentials = $this->oauth->getOauthParams('POST');
|
||||
$jsonData = $this->json->serialize($data);
|
||||
$this->_logger->info('Request: ' . $jsonData);
|
||||
|
||||
try {
|
||||
$this->curl->addHeader("Accept", "application/json");
|
||||
$this->curl->addHeader("Content-Type", "application/json");
|
||||
$this->curl->addHeader("Authorization", $oauthCredentials);
|
||||
// $this->curl->addHeader("Cookie", "NS_ROUTING_VERSION=LAGGING");
|
||||
$this->curl->post($url, $jsonData);
|
||||
// check the status code returned by NetSuite
|
||||
$statusCode = $this->curl->getStatus();
|
||||
$this->_logger->info('Status Code: ' . $statusCode);
|
||||
$this->_logger->info('NetSuite API data: ', ['data' => $data, 'url' => $url]);
|
||||
if ($statusCode != 200) {
|
||||
$this->_logger->error("Error: " . $this->curl->getBody());
|
||||
return $response = [
|
||||
'error' => true,
|
||||
'message' => $this->curl->getBody()
|
||||
];
|
||||
}
|
||||
$response = $this->curl->getBody();
|
||||
$this->_logger->info('Response: ' . $response);
|
||||
$response = $this->json->unserialize($response);
|
||||
# if response is not an array, unserialize again
|
||||
if (!is_array($response)) {
|
||||
$response = $this->json->unserialize($response);
|
||||
}
|
||||
return $response;
|
||||
} catch (\Exception $e) {
|
||||
$this->_logger->error("error: " . $e->getMessage());
|
||||
return $response = [
|
||||
'error' => true,
|
||||
'message' => $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+215
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
namespace JR\NetSuite\Lib;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Oauth
|
||||
{
|
||||
const SIGNATURE_METHOD_HMACSHA256 = 'HMAC-SHA256';
|
||||
const OAUTH_SCRIPT = 'jr_netsuite/apidetail/script';
|
||||
const OAUTH_VERSION = '1.0';
|
||||
const NONCE_STRING = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
const XML_PATH_OAUTH_URL = 'jr_netsuite/apidetail/api_url';
|
||||
const XML_PATH_OAUTH_REALM = 'jr_netsuite/apidetail/oauth_realm';
|
||||
const XML_PATH_OAUTH_CONSUMER_KEY = 'jr_netsuite/apidetail/oauth_consumer_key';
|
||||
const XML_PATH_OAUTH_CONSUMER_SECRET = 'jr_netsuite/apidetail/oauth_consumer_secret';
|
||||
const XML_PATH_OAUTH_TOKEN = 'jr_netsuite/apidetail/oauth_token';
|
||||
const XML_PATH_OAUTH_TOKEN_SECRET = 'jr_netsuite/apidetail/oauth_token_secret';
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $storeManager;
|
||||
|
||||
/**
|
||||
* @var ScopeConfigInterface
|
||||
*/
|
||||
protected $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $script;
|
||||
|
||||
/**
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param ScopeConfigInterface $scopeConfig
|
||||
**/
|
||||
|
||||
public function __construct(
|
||||
StoreManagerInterface $storeManager,
|
||||
ScopeConfigInterface $scopeConfig
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* consumer key
|
||||
* @return string
|
||||
*/
|
||||
public function getConsumerKey()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_CONSUMER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* consumer Secret
|
||||
* @return string
|
||||
*/
|
||||
public function getConsumerSecret()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_CONSUMER_SECRET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Token access key
|
||||
* @return string
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_TOKEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Token Secret
|
||||
* @return string
|
||||
*/
|
||||
public function getTokenSecret()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_TOKEN_SECRET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Token Secreat
|
||||
* @return string
|
||||
*/
|
||||
public function getRealm()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_REALM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Token Script
|
||||
* @return string
|
||||
*/
|
||||
public function setScript($script)
|
||||
{
|
||||
$this->script = $script;
|
||||
}
|
||||
|
||||
/**
|
||||
* Token script
|
||||
* @return string
|
||||
*/
|
||||
public function getScript()
|
||||
{
|
||||
if ($this->script) {
|
||||
return $this->script;
|
||||
}
|
||||
return $this->getConfigValue(self::OAUTH_SCRIPT);
|
||||
}
|
||||
/**
|
||||
* Token Secreat
|
||||
* @return string
|
||||
*/
|
||||
public function getApiUrl()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_URL);
|
||||
}
|
||||
/**
|
||||
* Calculate header for request
|
||||
* @return string
|
||||
*/
|
||||
public function getOauthParams($method = 'POST')
|
||||
{
|
||||
$time = time();
|
||||
$nonce = rawurlencode($this->generateNonceString(11));
|
||||
$oauthparams = $this->getParamObject($nonce, $time);
|
||||
$baseString = $this->createBaseString($method, $oauthparams);
|
||||
$signature = $this->getSignature('sha256', $baseString);
|
||||
return $this->buildAuthorizationHeader($nonce, $time, $signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate signature for request
|
||||
* @return string
|
||||
*/
|
||||
public function getSignature($algo, $baseString)
|
||||
{
|
||||
$key = rawurlencode($this->getConsumerSecret()) . '&' . rawurlencode($this->getTokenSecret());
|
||||
return rawurlencode(base64_encode(hash_hmac($algo, $baseString, $key, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the Authorization header for a request
|
||||
* @return string
|
||||
*/
|
||||
private function buildAuthorizationHeader($nonce, $time, $signature)
|
||||
{
|
||||
return 'OAuth realm="' . $this->getRealm() . '",oauth_consumer_key="' . $this->getConsumerKey() . '",oauth_token="' . $this->getToken() . '",oauth_signature_method="HMAC-SHA256",oauth_timestamp="' . $time . '",oauth_nonce="' . $nonce . '",oauth_version="1.0",oauth_signature="' . $signature . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the Signature Base String.
|
||||
* The Signature Base String is a consistent reproducible concatenation of
|
||||
* the request elements into a single string. The string is used as an
|
||||
* input in hashing or signing algorithms.
|
||||
* @return string Returns the base string
|
||||
*/
|
||||
|
||||
private function createBaseString($method, $params)
|
||||
{
|
||||
$url = $this->getApiUrl();
|
||||
$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
|
||||
|
||||
return strtoupper($method)
|
||||
. '&' . rawurlencode($url)
|
||||
. '&' . rawurlencode($query);
|
||||
}
|
||||
/**
|
||||
* generating nonce value
|
||||
* @return string
|
||||
*/
|
||||
public function generateNonceString($length = 11)
|
||||
{
|
||||
$characters = self::NONCE_STRING;
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
/**
|
||||
* generating param value
|
||||
* @return array
|
||||
*/
|
||||
public function getParamObject($nonce, $time)
|
||||
{
|
||||
return [
|
||||
'deploy' => 1,
|
||||
"oauth_consumer_key" => $this->getConsumerKey(),
|
||||
"oauth_nonce" => $nonce,
|
||||
"oauth_signature_method" => self::SIGNATURE_METHOD_HMACSHA256,
|
||||
"oauth_timestamp" => $time,
|
||||
"oauth_token" => $this->getToken(),
|
||||
"oauth_version" => self::OAUTH_VERSION,
|
||||
'script' => $this->getScript()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* get config value
|
||||
* @return string
|
||||
*/
|
||||
public function getConfigValue($path)
|
||||
{
|
||||
$store = $this->storeManager->getStore()->getStoreId();
|
||||
$config_data = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $store);
|
||||
return $config_data;
|
||||
}
|
||||
}
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Lib\Restful;
|
||||
|
||||
use JR\NetSuite\Cron\OrderSyncWorker;
|
||||
use JR\NetSuite\Helper\Data;
|
||||
use JR\NetSuite\Logger\Logger;
|
||||
use Lts\Sales\Cron\OrderNetsuiteSync;
|
||||
use Magento\Framework\Exception\InvalidArgumentException;
|
||||
use Magento\Framework\HTTP\Client\Curl;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Subscriber\Oauth\Oauth1;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
|
||||
class NetSuiteAPI
|
||||
{
|
||||
const CUSTOMER = 'customer';
|
||||
const LOCATION = 'location';
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Helper\Data
|
||||
*/
|
||||
protected $helper;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Logger\Logger
|
||||
*/
|
||||
protected $_logger;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Lib\Restful\Oauth
|
||||
*/
|
||||
protected \JR\NetSuite\Lib\Restful\Oauth $oauth;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\HTTP\Client\Curl
|
||||
*/
|
||||
public $curl;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\Serializer\Json
|
||||
*/
|
||||
public $json;
|
||||
|
||||
public array $responseHeaders;
|
||||
|
||||
/**
|
||||
* @param Data $helper
|
||||
* @param Logger $_logger
|
||||
*/
|
||||
public function __construct(
|
||||
Data $helper,
|
||||
Logger $logger,
|
||||
Oauth $oauth,
|
||||
Curl $curl,
|
||||
Json $json
|
||||
) {
|
||||
$this->helper = $helper;
|
||||
$this->_logger = $logger;
|
||||
$this->oauth = $oauth;
|
||||
$this->curl = $curl;
|
||||
$this->json = $json;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function postData(string $service, array $data)
|
||||
{
|
||||
$url = $this->oauth->getEndpoint($service);
|
||||
|
||||
$this->responseHeaders = [];
|
||||
$oauthCredentials = $this->oauth->getOauthParams($service);
|
||||
$jsonData = $this->json->serialize($data);
|
||||
// $this->_logger->info('Restful Request data: ' . $jsonData);
|
||||
// $this->_logger->info('Restful Request oauthCredentials: ' . $oauthCredentials);
|
||||
|
||||
try {
|
||||
$this->curl->addHeader("Accept", "application/json");
|
||||
$this->curl->addHeader("Content-Type", "application/json");
|
||||
$this->curl->addHeader("Authorization", $oauthCredentials);
|
||||
// $this->curl->addHeader("Cookie", "NS_ROUTING_VERSION=LAGGING");
|
||||
$this->curl->post($url, $jsonData);
|
||||
// check the status code returned by NetSuite
|
||||
$statusCode = $this->curl->getStatus();
|
||||
// $this->_logger->info('Restful Status Code: ' . $statusCode);
|
||||
// $this->_logger->info('Restful NetSuite API data: ', ['data' => $data, 'url' => $url]);
|
||||
$header = $this->curl->getHeaders();
|
||||
|
||||
$this->responseHeaders = $header;
|
||||
if (!str_starts_with($statusCode, '2')) {
|
||||
// $this->_logger->error("Error: " . $this->curl->getBody());
|
||||
return [
|
||||
'error' => true,
|
||||
'message' => $this->curl->getBody()
|
||||
];
|
||||
}
|
||||
|
||||
if ($statusCode==204) {
|
||||
return [
|
||||
'error' => false,
|
||||
];
|
||||
}
|
||||
$response = $this->curl->getBody();
|
||||
|
||||
$response = $this->json->unserialize($response);
|
||||
# if response is not an array, unserialize again
|
||||
if (!is_array($response)) {
|
||||
$response = $this->json->unserialize($response);
|
||||
}
|
||||
return $response;
|
||||
} catch (\Exception $e) {
|
||||
$this->_logger->error("Restful error: " . $e->getMessage(), $e->getTrace());
|
||||
return $response = [
|
||||
'error' => true,
|
||||
'message' => $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResponseHeaders()
|
||||
{
|
||||
return $this->responseHeaders;
|
||||
}
|
||||
/**
|
||||
* @param string $service
|
||||
* @param array $params
|
||||
* @return array
|
||||
* @throws \Random\RandomException
|
||||
*/
|
||||
public function get(string $service, array $params = [])
|
||||
{
|
||||
$consumerKey = $this->oauth->getConsumerKey();
|
||||
$consumerSecret = $this->oauth->getConsumerSecret();
|
||||
$token = $this->oauth->getToken();
|
||||
$token_secret = $this->oauth->getTokenSecret();
|
||||
|
||||
$realm = $this->oauth->getAccountId();
|
||||
// OAuth parameters
|
||||
$oauth = [
|
||||
'oauth_consumer_key' => $consumerKey,
|
||||
'oauth_token' => $token,
|
||||
'oauth_nonce' => bin2hex(random_bytes(16)),
|
||||
'oauth_timestamp' => time(),
|
||||
'oauth_signature_method' => Oauth::SIGNATURE_METHOD_HMACSHA256,
|
||||
'oauth_version' => '1.0'
|
||||
];
|
||||
|
||||
$url = $this->oauth->getEndpoint($service);
|
||||
// Combine and sort parameters for signature
|
||||
$all_params = array_merge($params, $oauth);
|
||||
ksort($all_params);
|
||||
|
||||
// Build base string
|
||||
$base_info = 'GET&' . rawurlencode($url) . '&' . rawurlencode(http_build_query($all_params, '', '&', PHP_QUERY_RFC3986));
|
||||
|
||||
// Create signature key
|
||||
$composite_key = rawurlencode($consumerSecret) . '&' . rawurlencode($token_secret);
|
||||
|
||||
// Generate the signature
|
||||
$oauth_signature = base64_encode(hash_hmac('sha256', $base_info, $composite_key, true));
|
||||
|
||||
// Add the signature to the OAuth parameters
|
||||
$oauth['oauth_signature'] = $oauth_signature;
|
||||
|
||||
// Build the Authorization header
|
||||
$header = 'OAuth realm="' . rawurlencode($realm) . '"';
|
||||
foreach ($oauth as $key => $value) {
|
||||
$header .= ', ' . rawurlencode($key) . '="' . rawurlencode($value) . '"';
|
||||
}
|
||||
|
||||
$fullUrl = $url . '?' . http_build_query($params, '', null, PHP_QUERY_RFC3986);
|
||||
|
||||
// Send the GET request with cURL
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $fullUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: $header"]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($status != 200) {
|
||||
$this->_logger->error("netsuite get request fail: ", ['url' => $fullUrl, 'response' => $response, 'http_code' => $status]);
|
||||
}
|
||||
return [
|
||||
'error' => $status != 200,
|
||||
'response' => json_decode($response, true)
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
Executable
+220
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
namespace JR\NetSuite\Lib\Restful;
|
||||
|
||||
use JR\NetSuite\Logger\Logger;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Exception\InvalidArgumentException;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Oauth
|
||||
{
|
||||
const SIGNATURE_METHOD_HMACSHA256 = 'HMAC-SHA256';
|
||||
const OAUTH_VERSION = '1.0';
|
||||
const NONCE_STRING = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
const XML_PATH_RESTFUL_ENDPOINT = 'jr_netsuite/restful/service_endpoint';
|
||||
const XML_PATH_ACCOUNT_ID = 'jr_netsuite/restful/account_id';
|
||||
const XML_PATH_OAUTH_CONSUMER_KEY = 'jr_netsuite/apidetail/oauth_consumer_key';
|
||||
const XML_PATH_OAUTH_CONSUMER_SECRET = 'jr_netsuite/apidetail/oauth_consumer_secret';
|
||||
const XML_PATH_OAUTH_TOKEN = 'jr_netsuite/apidetail/oauth_token';
|
||||
const XML_PATH_OAUTH_TOKEN_SECRET = 'jr_netsuite/apidetail/oauth_token_secret';
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $storeManager;
|
||||
|
||||
/**
|
||||
* @var ScopeConfigInterface
|
||||
*/
|
||||
protected $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $script;
|
||||
|
||||
private Logger $logger;
|
||||
|
||||
/**
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param ScopeConfigInterface $scopeConfig
|
||||
**/
|
||||
|
||||
public function __construct(
|
||||
StoreManagerInterface $storeManager,
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
Logger $logger
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->storeManager = $storeManager;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* consumer key
|
||||
* @return string
|
||||
*/
|
||||
public function getConsumerKey()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_CONSUMER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* consumer Secret
|
||||
* @return string
|
||||
*/
|
||||
public function getConsumerSecret()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_CONSUMER_SECRET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Token access key
|
||||
* @return string
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_TOKEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Token Secret
|
||||
* @return string
|
||||
*/
|
||||
public function getTokenSecret()
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_OAUTH_TOKEN_SECRET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAccountId(): string
|
||||
{
|
||||
return $this->getConfigValue(self::XML_PATH_ACCOUNT_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $service
|
||||
* @return string
|
||||
*/
|
||||
public function getEndpoint(string $service = ''): string
|
||||
{
|
||||
$endpoint = $this->getConfigValue(self::XML_PATH_RESTFUL_ENDPOINT);
|
||||
if ($service) {
|
||||
$endpoint = $endpoint .'/'.ltrim($service, '/');
|
||||
}
|
||||
return $endpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $service, for example, if you want to sync customer to netsuite, this param is record/v1/customer
|
||||
* @param string $method
|
||||
* @return string
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getOauthParams(string $service, string $method = 'POST')
|
||||
{
|
||||
if (!$service) {
|
||||
throw new InvalidArgumentException(__('Parameter service name is required.'));
|
||||
}
|
||||
$time = time();
|
||||
$nonce = rawurlencode($this->generateNonceString(11));
|
||||
$oauthparams = $this->getParamObject($nonce, $time);
|
||||
|
||||
$baseString = $this->createBaseString($service, $method, $oauthparams );
|
||||
|
||||
$signature = $this->getSignature('sha256', $baseString);
|
||||
|
||||
return $this->buildAuthorizationHeader($nonce, $time, $signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate signature for request
|
||||
* @return string
|
||||
*/
|
||||
public function getSignature($algo, $baseString)
|
||||
{
|
||||
$key = rawurlencode($this->getConsumerSecret()) . '&' . rawurlencode($this->getTokenSecret());
|
||||
return rawurlencode(base64_encode(hash_hmac($algo, $baseString, $key, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the Authorization header for a request
|
||||
* @return string
|
||||
*/
|
||||
private function buildAuthorizationHeader($nonce, $time, $signature)
|
||||
{
|
||||
return 'OAuth realm="' . $this->getAccountId() . '",oauth_consumer_key="' . $this->getConsumerKey() .
|
||||
'",oauth_token="' . $this->getToken() . '",oauth_signature_method="'.self::SIGNATURE_METHOD_HMACSHA256.
|
||||
'",oauth_timestamp="' . $time . '",oauth_nonce="' . $nonce . '",oauth_version="1.0",oauth_signature="'
|
||||
. $signature . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the Signature Base String.
|
||||
* The Signature Base String is a consistent reproducible concatenation of
|
||||
* the request elements into a single string. The string is used as an
|
||||
* input in hashing or signing algorithms.
|
||||
* @param string $service
|
||||
* @param string $method
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
private function createBaseString(string $service, string $method, array $params = [])
|
||||
{
|
||||
$url = $this->getEndpoint().'/'.$service;
|
||||
$rawString = strtoupper($method). '&' . rawurlencode($url);
|
||||
if ($params) {
|
||||
$rawString .= '&';
|
||||
ksort($params);
|
||||
$rawString .= rawurlencode(http_build_query($params, '', '&', PHP_QUERY_RFC3986));
|
||||
}
|
||||
|
||||
return $rawString;
|
||||
}
|
||||
/**
|
||||
* generating nonce value
|
||||
* @return string
|
||||
*/
|
||||
public function generateNonceString($length = 11)
|
||||
{
|
||||
$characters = self::NONCE_STRING;
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate param value
|
||||
* @param $nonce
|
||||
* @param $time
|
||||
* @return array
|
||||
*/
|
||||
public function getParamObject(string $nonce, string $time)
|
||||
{
|
||||
return [
|
||||
"oauth_consumer_key" => $this->getConsumerKey(),
|
||||
"oauth_nonce" => $nonce,
|
||||
"oauth_signature_method" => self::SIGNATURE_METHOD_HMACSHA256,
|
||||
"oauth_timestamp" => $time,
|
||||
"oauth_token" => $this->getToken(),
|
||||
"oauth_version" => self::OAUTH_VERSION,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* get config value
|
||||
* @return string
|
||||
*/
|
||||
public function getConfigValue($path)
|
||||
{
|
||||
$store = $this->storeManager->getStore()->getStoreId();
|
||||
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $store);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user