BxDolAIProviderShopifyAdmin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /*
  10. * Login for Owner/Collaborator account:
  11. * https://admin.shopify.com/store/your_shop/
  12. *
  13. * Integration:
  14. * 0. Custom apps creation guide (for a shop Owner/Collaborator, not a Partner):
  15. * https://help.shopify.com/en/manual/apps/app-types/custom-apps
  16. *
  17. * 1. Make authenticated requests to the REST Admin API:
  18. * https://shopify.dev/docs/apps/auth/admin-app-access-tokens
  19. *
  20. * 2. REST Admin API reference:
  21. * https://shopify.dev/docs/api/admin-rest
  22. *
  23. * 3. REST Admin API -> Product
  24. * https://shopify.dev/docs/api/admin-rest/2023-10/resources/product
  25. *
  26. * 4. REST Admin API -> Order
  27. * https://shopify.dev/docs/api/admin-rest/2023-10/resources/order
  28. *
  29. */
  30. class BxDolAIProviderShopifyAdmin extends BxDolAIProvider
  31. {
  32. public static $NAME = 'shopify_admin';
  33. protected $_sShopDomain;
  34. protected $_sAccessToken;
  35. protected $_sEndpoint;
  36. protected $_sStorefront;
  37. public function __construct($aProvider)
  38. {
  39. $this->_sName = self::$NAME;
  40. parent::__construct($aProvider);
  41. $this->_sShopDomain = $this->getOption('shop_domain');
  42. $this->_sAccessToken = $this->getOption('access_token');
  43. $this->_sEndpoint = "https://{$this->_sShopDomain}/admin/api/2023-10/";
  44. $this->_sStorefront = "https://{$this->_sShopDomain}/";
  45. }
  46. public function getEntry($sId)
  47. {
  48. $sProduct = $this->call('products/' . $sId . '.json', [
  49. 'fields' => 'id,title,handle,body_html,tags,variants',
  50. ], 'get');
  51. if(empty($sProduct))
  52. return [];
  53. $aProduct = json_decode($sProduct, true);
  54. if(empty($aProduct) || !is_array($aProduct) || empty($aProduct['product']))
  55. return [];
  56. return $aProduct['product'];
  57. }
  58. public function getOptionWebhookUrl()
  59. {
  60. return bx_append_url_params(BX_DOL_URL_ROOT . 'agents.php', [
  61. 'p' => $this->_iId,
  62. 'a' => 'webhook'
  63. ]);
  64. }
  65. public function processActionWebhook()
  66. {
  67. $sHmacHeader = isset($_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']) ? $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'] : '';
  68. $sData = @file_get_contents('php://input');
  69. if(!$this->_verifyWebhook($sData, $sHmacHeader)) {
  70. http_response_code(401);
  71. return;
  72. }
  73. $oAi = BxDolAI::getInstance();
  74. $aAutomators = $oAi->getAutomatorsWebhook($this->_iId);
  75. foreach($aAutomators as $aAutomator)
  76. $oAi->callAutomator(BX_DOL_AI_AUTOMATOR_WEBHOOK, [
  77. 'automator' => $aAutomator
  78. ]);
  79. http_response_code(200);
  80. }
  81. public function call($sRequest, $aParams, $sMethod = 'post-json', $aHeaders = [])
  82. {
  83. $aHeaders[] = 'Content-Type: application/json';
  84. if(!empty($this->_sAccessToken))
  85. $aHeaders[] = 'X-Shopify-Access-Token: ' . $this->_sAccessToken;
  86. else
  87. $aHeaders[] = 'Authorization: Basic ' . base64_encode($this->_sApiKey . ':' . $this->_sApiSecretKey);
  88. $sResponse = bx_file_get_contents($this->_sEndpoint . $sRequest, $aParams, $sMethod, $aHeaders);
  89. if(empty($sResponse))
  90. return false;
  91. $aResponse = json_decode($sResponse, true);
  92. if(empty($aResponse) || !is_array($aResponse))
  93. return false;
  94. return $aResponse;
  95. }
  96. /**
  97. * Internal methods.
  98. */
  99. protected function _dateI2S($iTimestamp)
  100. {
  101. return date("Y-m-d", $iTimestamp);
  102. }
  103. protected function _verifyWebhook($sData, $sHmacHeader)
  104. {
  105. $sHmacCalc = base64_encode(hash_hmac('sha256', $sData, $this->getOption('secret_key'), true));
  106. return hash_equals($sHmacCalc, $sHmacHeader);
  107. }
  108. }