From e6f95f6107c1b5c017cf84ee114a9dce5053625d Mon Sep 17 00:00:00 2001 From: Andrii Onufriichuk Date: Wed, 28 Feb 2024 13:22:49 +0200 Subject: [PATCH] Added separate email for pay-by-link outside of order confirmation email --- Plugin/Order/Create/PaymentLink.php | 139 +++++++++++++++++++++++----- etc/adminhtml/di.xml | 5 + 2 files changed, 119 insertions(+), 25 deletions(-) diff --git a/Plugin/Order/Create/PaymentLink.php b/Plugin/Order/Create/PaymentLink.php index e1c9a2f6..a07b37e6 100644 --- a/Plugin/Order/Create/PaymentLink.php +++ b/Plugin/Order/Create/PaymentLink.php @@ -3,19 +3,23 @@ namespace Rvvup\Payments\Plugin\Order\Create; use Laminas\Http\Request; +use Magento\Framework\App\Request\Http; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Serialize\SerializerInterface; -use Magento\Quote\Api\Data\CartInterface; +use Magento\Sales\Api\OrderManagementInterface; +use Magento\Sales\Api\Data\OrderStatusHistoryInterfaceFactory; use Magento\Sales\Model\AdminOrder\Create; +use Magento\Sales\Model\Order; use Magento\Store\Model\ScopeInterface; use Rvvup\Payments\Model\Config; use Rvvup\Payments\Model\RvvupConfigProvider; use Rvvup\Payments\Sdk\Curl; +use Psr\Log\LoggerInterface; class PaymentLink { /** @var Curl */ - private Curl $curl; + private $curl; /** @var Config */ private $config; @@ -23,67 +27,150 @@ class PaymentLink /** @var SerializerInterface */ private $json; + /** @var OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory */ + private $orderStatusHistoryFactory; + + /** @var OrderManagementInterface $orderManagement */ + private $orderManagement; + + /** @var Http */ + private $request; + + /** + * Set via di.xml + * + * @var LoggerInterface + */ + private $logger; + + /** + * @param Curl $curl + * @param Config $config + * @param SerializerInterface $json + * @param OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory + * @param OrderManagementInterface $orderManagement + * @param Http $request + * @param LoggerInterface $logger + */ public function __construct( Curl $curl, Config $config, - SerializerInterface $json + SerializerInterface $json, + OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory, + OrderManagementInterface $orderManagement, + Http $request, + LoggerInterface $logger ) { $this->curl = $curl; $this->config = $config; $this->json = $json; + $this->orderStatusHistoryFactory = $orderStatusHistoryFactory; + $this->orderManagement = $orderManagement; + $this->request = $request; + $this->logger = $logger; } /** * @param Create $subject * @param Create $result * @param array $data - * @return mixed + * @return Create * @throws NoSuchEntityException */ - public function afterImportPostData(Create $subject, Create $result, array $data) + public function afterImportPostData(Create $subject, Create $result, array $data): Create { if ($result->getQuote() && $result->getQuote()->getPayment()->getMethod() == RvvupConfigProvider::CODE) { if (isset($data['comment'])) { - $this->createRvvupPayByLink($result->getQuote(), $subject, $data['comment']); + $quote = $result->getQuote(); + $storeId = (string)$quote->getStore()->getId(); + $amount = (float)$quote->getGrandTotal(); + $orderId = $quote->reserveOrderId()->getReservedOrderId(); + $currencyCode = $quote->getQuoteCurrencyCode(); + $this->createRvvupPayByLink($storeId, $amount, $orderId, $currencyCode, $subject, $data); } } + return $result; + } + + /** Send separate confirmation if merchant is not + * informing customer with order success email + * @param Create $subject + * @param Order $result + * @return Order + * @throws NoSuchEntityException + */ + public function afterCreateOrder(Create $subject, Order $result): Order + { + $order = $this->request->getPost('order'); + if (!(isset($order['send_confirmation']) && $order['send_confirmation'])) { + $this->createRvvupPayByLink( + (string)$result->getStoreId(), + $result->getGrandTotal(), + $result->getId(), + $result->getOrderCurrencyCode(), + $subject, + [] + ); + } return $result; } /** * Create Rvvup pay-by-link and save it to comment - * @param CartInterface $quote + * @param string $storeId + * @param float $amount + * @param string $orderId + * @param string $currencyCode * @param Create $subject * @param array $data * @return void * @throws NoSuchEntityException */ - private function createRvvupPayByLink(CartInterface $quote, Create $subject, array $data): 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, $data['customer_note']); + private function createRvvupPayByLink( + string $storeId, + float $amount, + string $orderId, + string $currencyCode, + Create $subject, + array $data + ): void { + try { + $amount = number_format($amount, 2, '.', ''); + $params = $this->getData($amount, $storeId, $orderId, $currencyCode); + + $request = $this->curl->request(Request::METHOD_POST, $this->getApiUrl($storeId), $params); + $body = $this->json->unserialize($request->body); + $this->processApiResponse($body, $amount, $subject, $data, $orderId); + } catch (\Exception $e) { + $this->logger->error('Rvvup payment link creation failed with error: ' . $e->getMessage()); + } } /** * @param array $body * @param string $amount * @param Create $subject - * @param string $message + * @param array $data + * @param string $orderId * @return void */ - private function processApiResponse(array $body, string $amount, Create $subject, string $message): void + private function processApiResponse(array $body, string $amount, Create $subject, array $data, string $orderId): void { if ($body['status'] == 'ACTIVE') { if ($amount == $body['amount']['amount']) { - $message = 'This order requires payment, please pay using following link:'. PHP_EOL . $body['url'] - . PHP_EOL . $message; - $subject->getQuote()->addData(['customer_note' => $message, 'customer_note_notify' => true]); + $message = 'This order requires payment, please pay using following link:'. PHP_EOL . $body['url']; + if (isset($data['send_confirmation']) && $data['send_confirmation']) { + $message .= PHP_EOL . $data['comment']['customer_note']; + $subject->getQuote()->addData(['customer_note' => $message, 'customer_note_notify' => true]); + } elseif (empty($data)) { + $historyComment = $this->orderStatusHistoryFactory->create(); + $historyComment->setParentId($orderId); + $historyComment->setIsCustomerNotified(true); + $historyComment->setIsVisibleOnFront(true); + $historyComment->setComment($message); + $this->orderManagement->addComment($orderId, $historyComment); + } } } } @@ -105,15 +192,17 @@ private function getApiUrl(string $storeId) /** * @param string $amount * @param string $storeId - * @param CartInterface $cart + * @param string $orderId + * @param string $currencyCode * @return array * @throws NoSuchEntityException */ - private function getData(string $amount, string $storeId, CartInterface $cart): array + private function getData(string $amount, string $storeId, string $orderId, string $currencyCode): array { $postData = [ - 'amount' => ['amount' => $amount, 'currency' => $cart->getQuoteCurrencyCode()], - 'reference' => $cart->getReservedOrderId(), + 'amount' => ['amount' => $amount, 'currency' => $currencyCode], + 'reference' => $orderId, + 'source' => 'MAGENTO_PAYMENT_LINK', 'reusable' => false ]; diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index a64d138d..dd42821a 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -43,4 +43,9 @@ type="Rvvup\Payments\Plugin\Order\Create\PaymentLink" sortOrder="1"/> + + + RvvupLog + +