Skip to content

Commit

Permalink
Moved pay-by-link email to be send from comment section while making …
Browse files Browse the repository at this point in the history
…an order email through admin
  • Loading branch information
Andrii Onufriichuk committed Feb 27, 2024
1 parent 8ced4d6 commit 15b4a4a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 162 deletions.
130 changes: 130 additions & 0 deletions Plugin/Order/Create/PaymentLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Rvvup\Payments\Plugin\Order\Create;

use Laminas\Http\Request;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\AdminOrder\Create;
use Magento\Store\Model\ScopeInterface;
use Rvvup\Payments\Model\Config;
use Rvvup\Payments\Model\RvvupConfigProvider;
use Rvvup\Payments\Sdk\Curl;

class PaymentLink
{
/** @var Curl */
private Curl $curl;

/** @var Config */
private $config;

/** @var SerializerInterface */
private $json;

public function __construct(
Curl $curl,
Config $config,
SerializerInterface $json
) {
$this->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
];
}
}
159 changes: 0 additions & 159 deletions Plugin/PaymentLink.php

This file was deleted.

6 changes: 3 additions & 3 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
type="Rvvup\Payments\Plugin\Cancel\Payment"
sortOrder="1"/>
</type>
<type name="Magento\Quote\Model\QuoteManagement">
<plugin name="createPaymentLink"
type="Rvvup\Payments\Plugin\PaymentLink"
<type name="Magento\Sales\Model\AdminOrder\Create">
<plugin name="addRvvupPaymentLink"
type="Rvvup\Payments\Plugin\Order\Create\PaymentLink"
sortOrder="1"/>
</type>
</config>

0 comments on commit 15b4a4a

Please sign in to comment.