From 409cc9397f3e6be54bcfcbc34da6298ff7b62a3f Mon Sep 17 00:00:00 2001 From: magefast Date: Sun, 16 Mar 2025 14:49:47 +0200 Subject: [PATCH] changes for graphql --- .../LiqpayMagento/LiqPay/Block/SubmitForm.php | 29 ++++- .../LiqPay/Controller/Checkout/Form.php | 39 ++++++- .../LiqPay/Controller/Checkout/Thnks.php | 108 +++++++++++++++++ app/code/LiqpayMagento/LiqPay/Helper/Data.php | 46 ++++++-- .../LiqPay/Model/LiqPayCallback.php | 29 +++-- .../LiqpayMagento/LiqPay/Model/Payment.php | 7 +- .../Model/Resolver/PaymentFormRedirect.php | 73 ++++++++++++ app/code/LiqpayMagento/LiqPay/Sdk/LiqPay.php | 60 ++++++++-- .../LiqPay/Service/PaymentFormLiqPay.php | 109 ++++++++++++++++++ .../LiqPay/etc/adminhtml/system.xml | 18 ++- app/code/LiqpayMagento/LiqPay/etc/config.xml | 4 +- app/code/LiqpayMagento/LiqPay/etc/module.xml | 1 + .../LiqpayMagento/LiqPay/etc/schema.graphqls | 9 ++ .../web/template/payment/checkout.html | 2 +- 14 files changed, 488 insertions(+), 46 deletions(-) create mode 100644 app/code/LiqpayMagento/LiqPay/Controller/Checkout/Thnks.php create mode 100644 app/code/LiqpayMagento/LiqPay/Model/Resolver/PaymentFormRedirect.php create mode 100644 app/code/LiqpayMagento/LiqPay/Service/PaymentFormLiqPay.php create mode 100644 app/code/LiqpayMagento/LiqPay/etc/schema.graphqls diff --git a/app/code/LiqpayMagento/LiqPay/Block/SubmitForm.php b/app/code/LiqpayMagento/LiqPay/Block/SubmitForm.php index 95ff63e..adbcf6b 100644 --- a/app/code/LiqpayMagento/LiqPay/Block/SubmitForm.php +++ b/app/code/LiqpayMagento/LiqPay/Block/SubmitForm.php @@ -10,6 +10,7 @@ namespace LiqpayMagento\LiqPay\Block; +use Exception; use Magento\Framework\View\Element\Template; use Magento\Sales\Model\Order; use LiqpayMagento\LiqPay\Sdk\LiqPay; @@ -20,6 +21,8 @@ class SubmitForm extends Template { protected $_order = null; + protected ?string $language; + /* @var $_liqPay LiqPay */ protected $_liqPay; @@ -28,9 +31,9 @@ class SubmitForm extends Template public function __construct( Template\Context $context, - LiqPay $liqPay, - Helper $helper, - array $data = [] + LiqPay $liqPay, + Helper $helper, + array $data = [] ) { parent::__construct($context, $data); @@ -62,13 +65,33 @@ public function setOrder(Order $order) protected function _toHtml() { $order = $this->getOrder(); + $language = $this->getLanguage(); + $html = $this->_liqPay->cnb_form(array( 'action' => 'pay', 'amount' => $order->getGrandTotal(), 'currency' => $order->getOrderCurrencyCode(), 'description' => $this->_helper->getLiqPayDescription($order), 'order_id' => $order->getIncrementId(), + 'language' => $language, + 'result_url'=>$this->_helper->getServerWebsiteUrl() . '/liqpay/checkout/thnks?language='.$language.'&orderNumber=' . $order->getIncrementId(), + 'server_url'=>$this->_helper->getServerWebsiteUrl() . '/V1/liqpay/callback' )); return $html; } + + public function getLanguage(): string + { + return $this->language ?? 'en'; + } + + public function setLanguage(string $language) + { + $this->language = $language; + } + + public function getCacheLifetime() + { + return null; + } } diff --git a/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Form.php b/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Form.php index dc8a4a4..829d4b1 100644 --- a/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Form.php +++ b/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Form.php @@ -10,10 +10,15 @@ namespace LiqpayMagento\LiqPay\Controller\Checkout; +use Exception; +use LiqpayMagento\LiqPay\Block\SubmitForm; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\ResponseInterface; +use Magento\Framework\Controller\Result\Json; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Locale\ResolverInterface; use Magento\Framework\View\LayoutFactory; use Magento\Checkout\Model\Session as CheckoutSession; use LiqpayMagento\LiqPay\Helper\Data as Helper; @@ -36,17 +41,21 @@ class Form extends Action */ protected $_layoutFactory; + private $_localeResolver; + public function __construct( - Context $context, - CheckoutSession $checkoutSession, - Helper $helper, - LayoutFactory $layoutFactory + Context $context, + CheckoutSession $checkoutSession, + Helper $helper, + LayoutFactory $layoutFactory, + ResolverInterface $resolverLocale ) { parent::__construct($context); $this->_checkoutSession = $checkoutSession; $this->_helper = $helper; $this->_layoutFactory = $layoutFactory; + $this->_localeResolver = $resolverLocale; } /** @@ -67,11 +76,14 @@ public function execute() } if ($this->_helper->checkOrderIsLiqPayPayment($order)) { /* @var $formBlock \LiqpayMagento\LiqPay\Block\SubmitForm */ - $formBlock = $this->_layoutFactory->create()->createBlock('LiqpayMagento\LiqPay\Block\SubmitForm'); + $formBlock = $this->_layoutFactory->create()->createBlock(SubmitForm::class); $formBlock->setOrder($order); + $formBlock->setLanguage($this->getLanguageCode()); + $content = $formBlock->toHtml(); + $data = [ 'status' => 'success', - 'content' => $formBlock->toHtml(), + 'content' => $content, ]; } else { throw new \Exception(__('Order payment method is not a LiqPay payment method')); @@ -101,4 +113,19 @@ protected function getCheckoutSession() { return $this->_checkoutSession; } + + /** + * @return string + */ + private function getLanguageCode(): string + { + $locale = $this->_localeResolver->getLocale(); + $code = 'en'; + if ($locale === 'ru_RU') { + $code = 'ru'; + } elseif ($locale === 'uk_UA') { + $code = 'ua'; + } + return $code; + } } diff --git a/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Thnks.php b/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Thnks.php new file mode 100644 index 0000000..a770509 --- /dev/null +++ b/app/code/LiqpayMagento/LiqPay/Controller/Checkout/Thnks.php @@ -0,0 +1,108 @@ +_checkoutSession = $checkoutSession; + $this->_helper = $helper; + $this->_layoutFactory = $layoutFactory; + $this->callback = $callback; + } + + /** + * Dispatch request + * + * @return ResultInterface|ResponseInterface + * @throws NotFoundException + */ + public function execute() + { + $frontendUrl = $this->_helper->getFrontendWebsiteUrl(); +// if ($this->getRequest()->getParam('language', false)) { +// $frontendUrl = $frontendUrl . '/' . $this->getRequest()->getParam('language') . '/'; +// } + + try { + if (!$this->getRequest()->isPost()) { + $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); + $resultRedirect->setUrl($frontendUrl); + return $resultRedirect; + } + + $this->callback->callback(); + } catch (Exception $e) { + + } + + if ($this->getRequest()->getParam('orderNumber', false)) { + $number = $this->getRequest()->getParam('orderNumber'); + $this->_checkoutSession->setLiqPayLastOrder($number); + $frontendUrl = $frontendUrl . \Dragonfly\BootstrapCheckout\Api\BoostrapCheckoutInterface::URL_KEY_ORDER_SUCCESS; + } + + $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); + $resultRedirect->setUrl($frontendUrl); + return $resultRedirect; + } + + + /** + * Return checkout session object + * + * @return CheckoutSession + */ + protected function getCheckoutSession() + { + return $this->_checkoutSession; + } +} diff --git a/app/code/LiqpayMagento/LiqPay/Helper/Data.php b/app/code/LiqpayMagento/LiqPay/Helper/Data.php index cf89f02..315ded1 100644 --- a/app/code/LiqpayMagento/LiqPay/Helper/Data.php +++ b/app/code/LiqpayMagento/LiqPay/Helper/Data.php @@ -26,6 +26,9 @@ class Data extends AbstractHelper const XML_PATH_TEST_ORDER_SURFIX = 'payment/liqpaymagento_liqpay/sandbox_order_surfix'; const XML_PATH_DESCRIPTION = 'payment/liqpaymagento_liqpay/description'; const XML_PATH_CALLBACK_SECURITY_CHECK = 'payment/liqpaymagento_liqpay/security_check'; + const XML_PATH_FRONTEND_WEBSITE_URL = 'payment/liqpaymagento_liqpay/frontend_website_url'; + const XML_PATH_SERVER_WEBSITE_URL = 'payment/liqpaymagento_liqpay/server_website_url'; + /** * @var PaymentHelper */ @@ -73,34 +76,43 @@ public function isSecurityCheck() public function getPublicKey() { - return trim($this->scopeConfig->getValue( + $value = trim($this->scopeConfig->getValue( self::XML_PATH_PUBLIC_KEY, ScopeInterface::SCOPE_STORE )); + + return !empty($value) ? trim($value) : ''; } public function getPrivateKey() { - return trim($this->scopeConfig->getValue( + $value = $this->scopeConfig->getValue( self::XML_PATH_PRIVATE_KEY, ScopeInterface::SCOPE_STORE - )); + ); + + return !empty($value) ? trim($value) : ''; } public function getTestOrderSurfix() { - return trim($this->scopeConfig->getValue( + $value = $this->scopeConfig->getValue( self::XML_PATH_TEST_ORDER_SURFIX, ScopeInterface::SCOPE_STORE - )); + ); + + return !empty($value) ? trim($value) : ''; } public function getLiqPayDescription(\Magento\Sales\Api\Data\OrderInterface $order = null) { - $description = trim($this->scopeConfig->getValue( + $value = $this->scopeConfig->getValue( self::XML_PATH_DESCRIPTION, ScopeInterface::SCOPE_STORE - )); + ); + + $description = !empty($value) ? trim($value) : ''; + $params = [ '{order_id}' => $order->getIncrementId(), ]; @@ -135,4 +147,24 @@ public function getLogger() { return $this->_logger; } + + public function getFrontendWebsiteUrl() + { + $value = $this->scopeConfig->getValue( + self::XML_PATH_FRONTEND_WEBSITE_URL, + ScopeInterface::SCOPE_STORE + ); + + return !empty($value) ? trim($value) : ''; + } + + public function getServerWebsiteUrl() + { + $value = $this->scopeConfig->getValue( + self::XML_PATH_SERVER_WEBSITE_URL, + ScopeInterface::SCOPE_STORE + ); + + return !empty($value) ? trim($value) : ''; + } } diff --git a/app/code/LiqpayMagento/LiqPay/Model/LiqPayCallback.php b/app/code/LiqpayMagento/LiqPay/Model/LiqPayCallback.php index 369b41b..6046766 100644 --- a/app/code/LiqpayMagento/LiqPay/Model/LiqPayCallback.php +++ b/app/code/LiqpayMagento/LiqPay/Model/LiqPayCallback.php @@ -87,6 +87,7 @@ public function callback() $data = $post['data']; $receivedSignature = $post['signature']; + //@todo - can add logic wit liqpay data $decodedData = $this->_liqPay->getDecodedData($data); $orderId = $decodedData['order_id'] ?? null; $receivedPublicKey = $decodedData['public_key'] ?? null; @@ -172,6 +173,9 @@ public function callback() if ($transactionId) { $historyMessage[] = __('LiqPay transaction id %1.', $transactionId); } + + $historyMessage[] = '
'.json_encode($decodedData); + if (count($historyMessage)) { $order->addStatusHistoryComment(implode(' ', $historyMessage)) ->setIsCustomerNotified(true); @@ -190,17 +194,20 @@ public function callback() protected function getRealOrder($status, $orderId) { -// if ($status == LiqPay::STATUS_SANDBOX) { -// $testOrderSurfix = $this->_helper->getTestOrderSurfix(); -// if (!empty($testOrderSurfix)) { -// $testOrderSurfix = LiqPay::TEST_MODE_SURFIX_DELIM . $testOrderSurfix; -// if (strlen($testOrderSurfix) < strlen($orderId) -// && substr($orderId, -strlen($testOrderSurfix)) == $testOrderSurfix -// ) { -// $orderId = substr($orderId, 0, strlen($orderId) - strlen($testOrderSurfix)); -// } -// } -// } + if ($status == LiqPay::STATUS_SANDBOX) { + $testOrderSurfix = $this->_helper->getTestOrderSurfix(); + if (!empty($testOrderSurfix)) { + $testOrderSurfix = LiqPay::TEST_MODE_SURFIX_DELIM . $testOrderSurfix; + if (strlen($testOrderSurfix) < strlen($orderId) + && substr($orderId, -strlen($testOrderSurfix)) == $testOrderSurfix + ) { + $orderId = substr($orderId, 0, strlen($orderId) - strlen($testOrderSurfix)); + } + } + } + + $orderId = str_replace('-test', '', $orderId); + return $this->_order->loadByIncrementId($orderId); } } diff --git a/app/code/LiqpayMagento/LiqPay/Model/Payment.php b/app/code/LiqpayMagento/LiqPay/Model/Payment.php index e858e84..4f2e7a1 100644 --- a/app/code/LiqpayMagento/LiqPay/Model/Payment.php +++ b/app/code/LiqpayMagento/LiqPay/Model/Payment.php @@ -104,4 +104,9 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) } return parent::isAvailable($quote); } -} \ No newline at end of file + + public function getOrderRedirectUrl($order): string + { + return '/liqpay_redirect?id=' . $order->getId(); + } +} diff --git a/app/code/LiqpayMagento/LiqPay/Model/Resolver/PaymentFormRedirect.php b/app/code/LiqpayMagento/LiqPay/Model/Resolver/PaymentFormRedirect.php new file mode 100644 index 0000000..6a58fda --- /dev/null +++ b/app/code/LiqpayMagento/LiqPay/Model/Resolver/PaymentFormRedirect.php @@ -0,0 +1,73 @@ +paymentFormLiqPay = $paymentFormLiqPay; + $this->logger = $logger; + } + + /** + * @param Field $field + * @param $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * @return array + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array + { + if (empty($args['id'])) { + throw new GraphQlInputException(__('Required parameter "id" is missing')); + } + + $orderId = $args['id']; + $formData = $this->paymentFormLiqPay->execute($orderId); + + try { + return [ + 'action' => $formData['action'], + 'data' => $formData['data'], + 'signature' => $formData['signature'], + 'language' => $formData['language'] + ]; + } catch (NoSuchEntityException|LocalizedException $exception) { + throw new GraphQlNoSuchEntityException(__($exception->getMessage())); + } + } +} diff --git a/app/code/LiqpayMagento/LiqPay/Sdk/LiqPay.php b/app/code/LiqpayMagento/LiqPay/Sdk/LiqPay.php index 5d0b9b9..87d5355 100644 --- a/app/code/LiqpayMagento/LiqPay/Sdk/LiqPay.php +++ b/app/code/LiqpayMagento/LiqPay/Sdk/LiqPay.php @@ -28,6 +28,8 @@ class LiqPay extends \LiqPay const STATUS_FAILURE = 'failure'; const STATUS_ERROR = 'error'; + const DEFAULT_CHECKOUT_URL = 'https://www.liqpay.ua/api/3/checkout'; + // wait const STATUS_WAIT_SECURE = 'wait_secure'; const STATUS_WAIT_ACCEPT = 'wait_accept'; @@ -54,20 +56,20 @@ public function __construct( } } - protected function prepareParams($params) + public function prepareParams($params) { -// if (!isset($params['sandbox'])) { -// $params['sandbox'] = (int)$this->_helper->isTestMode(); -// } + if (!isset($params['sandbox'])) { + $params['sandbox'] = (int)$this->_helper->isTestMode(); + } if (!isset($params['version'])) { $params['version'] = static::VERSION; } -// if (isset($params['order_id']) && $this->_helper->isTestMode()) { -// $surfix = $this->_helper->getTestOrderSurfix(); -// if (!empty($surfix)) { -// $params['order_id'] .= self::TEST_MODE_SURFIX_DELIM . $surfix; -// } -// } + if (isset($params['order_id']) && $this->_helper->isTestMode()) { + $surfix = $this->_helper->getTestOrderSurfix(); + if (!empty($surfix)) { + $params['order_id'] .= self::TEST_MODE_SURFIX_DELIM . $surfix; + } + } return $params; } @@ -93,6 +95,12 @@ public function cnb_form($params) return parent::cnb_form($params); } + public function cnb_form_raw($params) + { + $params = $this->prepareParams($params); + return parent::cnb_form_raw($params); + } + public function getDecodedData($data) { return json_decode(base64_decode($data), true, 1024); @@ -105,4 +113,36 @@ public function checkSignature($signature, $data) return $signature == $generatedSignature; } + + + public function encode_params($params) + { + return base64_encode(json_encode($params)); + } + + public function cnb_params($params) + { + $params['public_key'] = $this->_helper->getPublicKey(); + + if (!isset($params['version'])) { + throw new \InvalidArgumentException('version is null'); + } + if (!isset($params['amount'])) { + throw new \InvalidArgumentException('amount is null'); + } + if (!isset($params['currency'])) { + throw new \InvalidArgumentException('currency is null'); + } + if (!in_array($params['currency'], $this->_supportedCurrencies)) { + throw new \InvalidArgumentException('currency is not supported'); + } + if ($params['currency'] == self::CURRENCY_RUR) { + $params['currency'] = self::CURRENCY_RUB; + } + if (!isset($params['description'])) { + throw new \InvalidArgumentException('description is null'); + } + + return $params; + } } diff --git a/app/code/LiqpayMagento/LiqPay/Service/PaymentFormLiqPay.php b/app/code/LiqpayMagento/LiqPay/Service/PaymentFormLiqPay.php new file mode 100644 index 0000000..fb8ac88 --- /dev/null +++ b/app/code/LiqpayMagento/LiqPay/Service/PaymentFormLiqPay.php @@ -0,0 +1,109 @@ +liqPay = $liqPay; + $this->helper = $helper; + $this->orderFactory = $orderFactory; + } + + /** + * @param $orderId + * @return array + */ + public function execute($orderId, $language = 'ua'): array + { + $formData = [ + 'action' => $this->liqPay::DEFAULT_CHECKOUT_URL, + 'data' => '', + 'signature' => '', + 'language' => $language + ]; + + $order = $this->getOrder($orderId); + if ($order && $order->getId()) { + $paramsLiqPay = [ + 'action' => 'pay', + 'amount' => $order->getGrandTotal(), + 'currency' => $order->getOrderCurrencyCode(), + 'description' => $this->helper->getLiqPayDescription($order), + 'order_id' => $order->getIncrementId(), + ]; + + $paramsLiqPay = $this->liqPay->prepareParams($paramsLiqPay); + + $paramsLiqPay = $this->liqPay->cnb_params($paramsLiqPay); + + $paramsLiqPay['result_url'] = $this->helper->getServerWebsiteUrl() . '/liqpay/checkout/thnks?language='.$language.'&orderNumber=' . $order->getIncrementId(); + + $paramsLiqPay['server_url'] = $this->helper->getServerWebsiteUrl() . '/V1/liqpay/callback'; + + $data = $this->liqPay->encode_params($paramsLiqPay); + $signature = $this->liqPay->cnb_signature($paramsLiqPay); + + $formData = [ + 'action' => $this->liqPay::DEFAULT_CHECKOUT_URL, + 'data' => $data, + 'signature' => $signature, + 'language' => $language + ]; + } + + return $formData; + } + + /** + * @param $incrementId + * @return OrderInterface|null + */ + private function getOrder($incrementId): ?OrderInterface + { + try { + $orderModel = $this->orderFactory->create(); + $order = $orderModel->load($incrementId); + $orderId = $order->getId(); + + if ($orderId) { + return $order; + } + } catch (LocalizedException $exception) { + + } + + return null; + } +} diff --git a/app/code/LiqpayMagento/LiqPay/etc/adminhtml/system.xml b/app/code/LiqpayMagento/LiqPay/etc/adminhtml/system.xml index 63af6d4..4166777 100644 --- a/app/code/LiqpayMagento/LiqPay/etc/adminhtml/system.xml +++ b/app/code/LiqpayMagento/LiqPay/etc/adminhtml/system.xml @@ -25,11 +25,11 @@ Magento\Config\Model\Config\Source\Yesno - - - - - + + + test]]> + @@ -63,6 +63,14 @@ Magento\Config\Model\Config\Source\Yesno + + + + + + diff --git a/app/code/LiqpayMagento/LiqPay/etc/config.xml b/app/code/LiqpayMagento/LiqPay/etc/config.xml index fadbca7..ae240a2 100644 --- a/app/code/LiqpayMagento/LiqPay/etc/config.xml +++ b/app/code/LiqpayMagento/LiqPay/etc/config.xml @@ -15,7 +15,7 @@ 0 0 - + test LiqpayMagento\LiqPay\Model\Payment authorize_capture LiqPay @@ -26,4 +26,4 @@ - + \ No newline at end of file diff --git a/app/code/LiqpayMagento/LiqPay/etc/module.xml b/app/code/LiqpayMagento/LiqPay/etc/module.xml index 45967e9..48b2c7a 100644 --- a/app/code/LiqpayMagento/LiqPay/etc/module.xml +++ b/app/code/LiqpayMagento/LiqPay/etc/module.xml @@ -16,6 +16,7 @@ + \ No newline at end of file diff --git a/app/code/LiqpayMagento/LiqPay/etc/schema.graphqls b/app/code/LiqpayMagento/LiqPay/etc/schema.graphqls new file mode 100644 index 0000000..dcdab5d --- /dev/null +++ b/app/code/LiqpayMagento/LiqPay/etc/schema.graphqls @@ -0,0 +1,9 @@ +type Query { + LiqPayPaymentForm(id: String @doc(description: "The ID of the Order.")): PaymentFormRedirect @resolver(class:"LiqpayMagento\\LiqPay\\Model\\Resolver\\PaymentFormRedirect") @doc(description:"Return form details for LiqPay payment") @cache(cacheable: false) +} +type PaymentFormRedirect @doc(description: "Payment form Redirect LiqPay details") { + action: String + data: String + signature: String + language: String +} diff --git a/app/code/LiqpayMagento/LiqPay/view/frontend/web/template/payment/checkout.html b/app/code/LiqpayMagento/LiqPay/view/frontend/web/template/payment/checkout.html index e70387c..2b52543 100644 --- a/app/code/LiqpayMagento/LiqPay/view/frontend/web/template/payment/checkout.html +++ b/app/code/LiqpayMagento/LiqPay/view/frontend/web/template/payment/checkout.html @@ -42,7 +42,7 @@ enable: (getCode() == isChecked()) " disabled> - +