Skip to content

Commit

Permalink
Added separate email for pay-by-link outside of order confirmation email
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Onufriichuk committed Feb 28, 2024
1 parent 7d609bb commit e6f95f6
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 25 deletions.
139 changes: 114 additions & 25 deletions Plugin/Order/Create/PaymentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,174 @@
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;

/** @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);
}
}
}
}
Expand All @@ -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
];

Expand Down
5 changes: 5 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@
type="Rvvup\Payments\Plugin\Order\Create\PaymentLink"
sortOrder="1"/>
</type>
<type name="Rvvup\Payments\Model\Checks\HasCartRestrictedProduct">
<arguments>
<argument name="logger" xsi:type="object">RvvupLog</argument>
</arguments>
</type>
</config>

0 comments on commit e6f95f6

Please sign in to comment.