diff --git a/Plugin/Order/Create/PaymentLink.php b/Plugin/Order/Create/PaymentLink.php new file mode 100644 index 00000000..bc22077c --- /dev/null +++ b/Plugin/Order/Create/PaymentLink.php @@ -0,0 +1,130 @@ +curl = $curl; + $this->config = $config; + $this->json = $json; + } + + /** + * @param Create $subject + * @param $result + * @param array $data + * @return mixed + * @throws NoSuchEntityException + */ + public function afterImportPostData(Create $subject, $result, array $data) + { + if ($result->getQuote() && $result->getQuote()->getPayment()->getMethod() == RvvupConfigProvider::CODE + && $data['send_confirmation']) { + $this->createRvvupPayByLink($result->getQuote(), $subject); + } + + return $result; + } + + /** + * Create Rvvup pay-by-link and save it to comment + * @param CartInterface $quote + * @param Create $subject + * @return void + * @throws NoSuchEntityException + */ + private function createRvvupPayByLink(CartInterface $quote, Create $subject): void + { + $storeId = (string)$quote->getStore()->getId(); + $amount = number_format((float)$quote->getGrandTotal(), 2, '.', ''); + $params = $this->getData($amount, $storeId, $quote); + + $request = $this->curl->request(Request::METHOD_POST, $this->getApiUrl($storeId), $params); + $body = $this->json->unserialize($request->body); + $this->processApiResponse($body, $amount, $subject); + } + + /** + * @param array $body + * @param string $amount + * @param Create $subject + * @return void + */ + private function processApiResponse(array $body, string $amount, Create $subject): void + { + if ($body['status'] == 'ACTIVE') { + if ($amount == $body['amount']['amount']) { + $message = 'This order requires payment, please pay using following link:'. PHP_EOL . $body['url']; + $subject->getQuote()->addData(['customer_note' => $message, 'customer_note_notify' => true]); + } + } + } + + /** + * @param string $storeId + * @return string + * @throws NoSuchEntityException + */ + private function getApiUrl(string $storeId) + { + $merchantId = $this->config->getMerchantId(ScopeInterface::SCOPE_STORE, $storeId); + $baseUrl = $this->config->getEndpoint(ScopeInterface::SCOPE_STORE, $storeId); + $baseUrl = str_replace('graphql', 'api/v1', $baseUrl); + + return "$baseUrl/$merchantId/payment-links"; + } + + /** + * @param string $amount + * @param string $storeId + * @param OrderInterface $order + * @return array + * @throws NoSuchEntityException + */ + private function getData(string $amount, string $storeId, CartInterface $cart): array + { + $postData = [ + 'amount' => ['amount' => $amount, 'currency' => $cart->getQuoteCurrencyCode()], + 'reference' => $cart->getReservedOrderId(), + 'reusable' => false + ]; + + $token = $this->config->getJwtConfig(ScopeInterface::SCOPE_STORE, $storeId); + + $headers = [ + 'Content-Type: application/json', + 'Accept: application/json', + 'Authorization: Bearer ' . $token + ]; + + return [ + 'headers' => $headers, + 'json' => $postData + ]; + } +} diff --git a/Plugin/PaymentLink.php b/Plugin/PaymentLink.php deleted file mode 100644 index 360ee574..00000000 --- a/Plugin/PaymentLink.php +++ /dev/null @@ -1,159 +0,0 @@ -curl = $curl; - $this->config = $config; - $this->orderStatusHistoryFactory = $orderStatusHistoryFactory; - $this->json = $json; - $this->orderManagement = $orderManagement; - } - - /** - * @param QuoteManagement $subject - * @param AbstractExtensibleModel $result - * @param Quote $quote - * @param array $orderData - * @return AbstractExtensibleModel - * @throws NoSuchEntityException - */ - public function afterSubmit( - QuoteManagement $subject, - AbstractExtensibleModel $result, - Quote $quote, - array $orderData = [] - ): AbstractExtensibleModel { - if ($result->getPayment() && $result->getPayment()->getMethod() == RvvupConfigProvider::CODE) { - $this->createRvvupPayByLink($result); - } - return $result; - } - - /** - * Create Rvvup pay-by-link and save it to comment - * @param OrderInterface $order - * @return void - * @throws NoSuchEntityException - */ - private function createRvvupPayByLink(OrderInterface $order): void - { - $storeId = (string)$order->getStore()->getId(); - $amount = number_format((float)$order->getGrandTotal(), 2, '.', ''); - $params = $this->getData($amount, $storeId, $order); - - $request = $this->curl->request(Request::METHOD_POST, $this->getApiUrl($storeId), $params); - $body = $this->json->unserialize($request->body); - $this->processApiResponse($body, $order, $amount); - } - - /** - * @param array $body - * @param OrderInterface $order - * @param string $amount - * @return void - */ - private function processApiResponse(array $body, OrderInterface $order, string $amount): void - { - if ($body['status'] == 'ACTIVE') { - if ($amount == $body['amount']['amount']) { - $historyComment = $this->orderStatusHistoryFactory->create(); - $historyComment->setParentId($order->getEntityId()); - $historyComment->setIsCustomerNotified(true); - $historyComment->setIsVisibleOnFront(true); - $historyComment->setStatus($order->getStatus()); - $historyComment->setComment('This order requires payment, please pay using following link:'. - PHP_EOL . 'https://checkout.dev.rvvuptech.com/l/' . $body['id']); - $this->orderManagement->addComment($order->getEntityId(), $historyComment); - } - } - } - - /** - * @param string $storeId - * @return string - * @throws NoSuchEntityException - */ - private function getApiUrl(string $storeId) - { - $merchantId = $this->config->getMerchantId(ScopeInterface::SCOPE_STORE, $storeId); - $baseUrl = $this->config->getEndpoint(ScopeInterface::SCOPE_STORE, $storeId); - $baseUrl = str_replace('graphql', 'api/v1', $baseUrl); - - return "$baseUrl/$merchantId/payment-links"; - } - - /** - * @param string $amount - * @param string $storeId - * @param OrderInterface $order - * @return array - * @throws NoSuchEntityException - */ - private function getData(string $amount, string $storeId, OrderInterface $order): array - { - $postData = [ - 'amount' => ['amount' => $amount, 'currency' => $order->getOrderCurrencyCode()], - 'reference' => $order->getId(), - 'reusable' => false - ]; - - $token = $this->config->getJwtConfig(ScopeInterface::SCOPE_STORE, $storeId); - - $headers = [ - 'Content-Type: application/json', - 'Accept: application/json', - 'Authorization: Bearer ' . $token - ]; - - return [ - 'headers' => $headers, - 'json' => $postData - ]; - } -} diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index faa45d17..a64d138d 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -38,9 +38,9 @@ type="Rvvup\Payments\Plugin\Cancel\Payment" sortOrder="1"/> - - +