Skip to content

Commit

Permalink
CET-480/feat: Support new fulfilments endpoint in Magento plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkwr committed Oct 22, 2024
1 parent 40d33cc commit 4a4c02d
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 162 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
.direnv/
.envrc

# PHP
.php-cs-fixer.cache
auth.json
vendor/
composer.lock
62 changes: 25 additions & 37 deletions Block/Adminhtml/Order/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Sales\Block\Adminhtml\Order\View as OrderView;
use Two\Gateway\Api\Config\RepositoryInterface as ConfigRepository;
use Two\Gateway\Service\Api\Adapter as Adapter;

/**
* Order View Block
Expand All @@ -20,10 +21,16 @@ class View extends OrderView
*/
public $configRepository;

/**
* @var Adapter
*/
private $apiAdapter;

/**
* View constructor.
*
* @param ConfigRepository $configRepository
* @param Adapter $adapter
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Sales\Model\Config $salesConfig
Expand All @@ -32,60 +39,38 @@ class View extends OrderView
*/
public function __construct(
ConfigRepository $configRepository,
Adapter $apiAdapter,
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Sales\Model\Config $salesConfig,
\Magento\Sales\Helper\Reorder $reorderHelper,
array $data = []
) {
$this->configRepository = $configRepository;
$this->apiAdapter = $apiAdapter;
parent::__construct($context, $registry, $salesConfig, $reorderHelper, $data);
}

/**
* Get payment additional data
*
* @return array
*/
public function getAdditionalData(): array
{
$order = $this->getOrder();
$payment = $order->getPayment();
return $payment->getAdditionalInformation();
}

/**
* Get Two Credit Note Url
* Get Two Fulfillments
*
* @param array $data
*
* @return string
*/
public function getTwoCreditNoteUrl(array $data): string
{
$order = $this->getOrder();
$billingAddress = $order->getBillingAddress();
$langParams = $billingAddress->getCountryId() == 'NO' ? '?lang=nb_NO' : '?lang=en_US';

return isset($data['gateway_data']['credit_note_url'])
? $data['gateway_data']['credit_note_url'] . $langParams
: '';
}

/**
* Get Two Credit Invoice Url
*
* @param array $data
*
* @return string
* @return array
*/
public function getTwoInvoiceUrl(array $data): string
public function getTwoFulfillments(): array
{
$order = $this->getOrder();
$billingAddress = $order->getBillingAddress();
$langParams = $billingAddress->getCountryId() == 'NO' ? '?lang=nb_NO' : '?lang=en_US';
$response = $this->apiAdapter->execute(
"/v1/order/" . $order->getTwoOrderId() . "/fulfillments",
'GET'
);
$error = $order->getPayment()->getMethodInstance()->getErrorFromResponse($response);
if ($error) {
return [];
}

return (isset($data['gateway_data']['invoice_url'])) ? $data['gateway_data']['invoice_url'] . $langParams : '';
return $response;
}

/**
Expand All @@ -95,8 +80,11 @@ public function getTwoInvoiceUrl(array $data): string
*
* @return string
*/
public function getTwoOrderId(array $data): string
public function getTwoOrderId(): string
{
$order = $this->getOrder();
$payment = $order->getPayment();
$data = $payment->getAdditionalInformation();
return (isset($data['gateway_data']['external_order_id'])) ? $data['gateway_data']['external_order_id'] : '';
}

Expand Down
46 changes: 23 additions & 23 deletions Model/Two.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public function capture(InfoInterface $payment, $amount)
}

if ($wholeOrderInvoiced == 1) {
$response = $this->apiAdapter->execute('/v1/order/' . $twoOrderId . '/fulfilled');
$response = $this->apiAdapter->execute('/v1/order/' . $twoOrderId . '/fulfillents');
} else {
$invoices = $order->getInvoiceCollection();
$totalInvoices = count($invoices);
Expand All @@ -441,8 +441,7 @@ public function capture(InfoInterface $payment, $amount)
}
$cnt++;
}
$remainItemInvoice = $payment->getOrder()->prepareInvoice();
$response = $this->partialMainOrder($order, $createdInvoice, $remainItemInvoice);
$response = $this->partialFulfill($order, $createdInvoice);
}

if (!empty($response) && isset($response['id'])) {
Expand Down Expand Up @@ -480,14 +479,15 @@ public function canCapture()
* @return array
* @throws LocalizedException
*/
public function partialMainOrder(Order $order, ?Order\Invoice $invoice, Order\Invoice $remainItemInvoice): array
public function partialFulfill(Order $order, ?Order\Invoice $invoice): array
{
$twoOrderId = $order->getTwoOrderId();
$payload['partially_fulfilled_order'] = $this->composeCapture->execute($invoice);
$payload['remained_order'] = $this->composeCapture->execute($remainItemInvoice);
$payload = [
'partial' => $this->composeCapture->execute($invoice),
];

/* Partially fulfill order*/
return $this->apiAdapter->execute('/v1/order/' . $twoOrderId . '/fulfilled', $payload);
return $this->apiAdapter->execute('/v1/order/' . $twoOrderId . '/fulfillments', $payload);
}

/**
Expand All @@ -506,23 +506,27 @@ private function parseFulfillResponse(array $response, Order $order): void
throw new LocalizedException($error);
}

if (empty($response['invoice_details'] ||
empty($response['invoice_details']['invoice_number']))) {
if (empty($response['fulfilled_order'] ||
empty($response['fulfilled_order']['id']))) {
return;
}

$additionalInformation = $order->getPayment()->getAdditionalInformation();
$additionalInformation['gateway_data']['invoice_number'] = $response['invoice_details']['invoice_number'];
$additionalInformation['gateway_data']['invoice_url'] = $response['invoice_url'];
$additionalInformation['marked_completed'] = true;

$order->getPayment()->setAdditionalInformation($additionalInformation);

$comment = __(
'%1 order marked as completed with invoice number %2',
$this->configRepository::PROVIDER,
$response['invoice_details']['invoice_number'],
);
if (empty($response['remained_order'])) {
$comment = __(
'%1 order marked as completed.',
$this->configRepository::PROVIDER,
);
} else {
$comment = __(
'%1 order marked as partially completed.',
$this->configRepository::PROVIDER,
);
}

$this->addStatusToOrderHistory($order, $comment->render());
}

Expand Down Expand Up @@ -565,7 +569,7 @@ public function refund(InfoInterface $payment, $amount)

$payload = $this->composeRefund->execute(
$payment->getCreditmemo(),
(double)$amount,
(float)$amount,
$order
);
$response = $this->apiAdapter->execute(
Expand All @@ -587,15 +591,11 @@ public function refund(InfoInterface $payment, $amount)
$reason
);
$this->addOrderComment($order, $message);
throw new LocalizedException(
throw new LocalizedException(
$message
);
}

$additionalInformation = $payment->getAdditionalInformation();
$additionalInformation['gateway_data']['credit_note_url'] = $response['credit_note_url'];
$payment->setAdditionalInformation($additionalInformation);

$comment = __(
'Successfully refunded order with %1 for order ID: %2. Refund reference: %3',
$this->configRepository::PROVIDER,
Expand Down
35 changes: 17 additions & 18 deletions Observer/SalesOrderSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,9 @@ public function execute(Observer $observer)
throw new LocalizedException($error);
}

$langParams = '?lang=en_US';
if ($order->getBillingAddress()->getCountryId() == 'NO') {
$langParams = '?lang=nb_NO';
}

//full fulfilment
$response = $this->apiAdapter->execute(
"/v1/order/" . $order->getTwoOrderId() . "/fulfilled" . $langParams
"/v1/order/" . $order->getTwoOrderId() . "/fulfillments"
);

foreach ($order->getInvoiceCollection() as $invoice) {
Expand All @@ -104,7 +99,7 @@ public function execute(Observer $observer)
$invoice->save();
}

$this->parseResponse($response, $order);
$this->parseFulfillResponse($response, $order);
}
}
}
Expand All @@ -131,31 +126,35 @@ private function isWholeOrderShipped(OrderInterface $order): bool
* @return void
* @throws Exception
*/
private function parseResponse(array $response, Order $order): void
private function parseFulfillResponse(array $response, Order $order): void
{
$error = $order->getPayment()->getMethodInstance()->getErrorFromResponse($response);

if ($error) {
throw new LocalizedException($error);
}

if (empty($response['invoice_details'] ||
empty($response['invoice_details']['invoice_number']))) {
if (empty($response['fulfilled_order'] ||
empty($response['fulfilled_order']['id']))) {
return;
}

$additionalInformation = $order->getPayment()->getAdditionalInformation();
$additionalInformation['gateway_data']['invoice_number'] = $response['invoice_details']['invoice_number'];
$additionalInformation['gateway_data']['invoice_url'] = $response['invoice_url'];
$additionalInformation['marked_completed'] = true;

$order->getPayment()->setAdditionalInformation($additionalInformation);

$comment = __(
'%1 order marked as completed with invoice number %2',
$this->configRepository::PROVIDER,
$response['invoice_details']['invoice_number']
);
if (empty($response['remained_order'])) {
$comment = __(
'%1 order marked as completed.',
$this->configRepository::PROVIDER,
);
} else {
$comment = __(
'%1 order marked as partially completed.',
$this->configRepository::PROVIDER,
);
}

$this->addStatusToOrderHistory($order, $comment->render());
}

Expand Down
43 changes: 22 additions & 21 deletions Observer/SalesOrderShipmentAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,21 @@ public function execute(Observer $observer)
if ($this->configRepository->getFulfillTrigger() == 'shipment') {
if (!$this->isWholeOrderShipped($order)) {
$response = $this->partialFulfill($shipment);
$this->parseResponse($response, $order);
$this->parseFulfillResponse($response, $order);
return;
}

$langParams = '?lang=en_US';
if ($order->getBillingAddress()->getCountryId() == 'NO') {
$langParams = '?lang=nb_NO';
}

//full fulfilment
$response = $this->apiAdapter->execute(
"/v1/order/" . $order->getTwoOrderId() . "/fulfilled" . $langParams
"/v1/order/" . $order->getTwoOrderId() . "/fulfillments"
);
foreach ($order->getInvoiceCollection() as $invoice) {
$invoice->pay();
$invoice->setTransactionId($order->getPayment()->getLastTransId());
$invoice->save();
}

$this->parseResponse($response, $order);
$this->parseFulfillResponse($response, $order);
} elseif ($this->configRepository->getFulfillTrigger() == 'complete') {
foreach ($order->getInvoiceCollection() as $invoice) {
$invoice->pay();
Expand Down Expand Up @@ -157,9 +152,11 @@ private function partialFulfill(ShipmentInterface $shipment): array
$order = $shipment->getOrder();
$twoOrderId = $order->getTwoOrderId();

$payload = $this->composeShipment->execute($shipment, $order);
$payload = [
'partial' => $this->composeShipment->execute($shipment, $order),
];

return $this->apiAdapter->execute('/v1/order/' . $twoOrderId . '/fulfilled', $payload);
return $this->apiAdapter->execute('/v1/order/' . $twoOrderId . '/fulfillments', $payload);
}

/**
Expand All @@ -168,31 +165,35 @@ private function partialFulfill(ShipmentInterface $shipment): array
* @return void
* @throws Exception
*/
private function parseResponse(array $response, Order $order): void
private function parseFulfillResponse(array $response, Order $order): void
{
$error = $order->getPayment()->getMethodInstance()->getErrorFromResponse($response);

if ($error) {
throw new LocalizedException($error);
}

if (empty($response['invoice_details'] ||
empty($response['invoice_details']['invoice_number']))) {
if (empty($response['fulfilled_order'] ||
empty($response['fulfilled_order']['id']))) {
return;
}

$additionalInformation = $order->getPayment()->getAdditionalInformation();
$additionalInformation['gateway_data']['invoice_number'] = $response['invoice_details']['invoice_number'];
$additionalInformation['gateway_data']['invoice_url'] = $response['invoice_url'];
$additionalInformation['marked_completed'] = true;

$order->getPayment()->setAdditionalInformation($additionalInformation);

$comment = __(
'%1 order marked as completed with invoice number %2',
$this->configRepository::PROVIDER,
$response['invoice_details']['invoice_number']
);
if (empty($response['remained_order'])) {
$comment = __(
'%1 order marked as completed.',
$this->configRepository::PROVIDER,
);
} else {
$comment = __(
'%1 order marked as partially completed.',
$this->configRepository::PROVIDER,
);
}

$this->addStatusToOrderHistory($order, $comment->render());
}

Expand Down
Loading

0 comments on commit 4a4c02d

Please sign in to comment.