diff --git a/.gitignore b/.gitignore index b1714e56..b5fa2f20 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,8 @@ .direnv/ .envrc +# PHP +.php-cs-fixer.cache +auth.json +vendor/ +composer.lock diff --git a/Block/Adminhtml/Order/View.php b/Block/Adminhtml/Order/View.php index c2d01819..a2271c89 100755 --- a/Block/Adminhtml/Order/View.php +++ b/Block/Adminhtml/Order/View.php @@ -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 @@ -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 @@ -32,6 +39,7 @@ 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, @@ -39,53 +47,30 @@ public function __construct( 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; } /** @@ -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'] : ''; } diff --git a/Model/Two.php b/Model/Two.php index 819208f2..8e404b59 100755 --- a/Model/Two.php +++ b/Model/Two.php @@ -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); @@ -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'])) { @@ -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); } /** @@ -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()); } @@ -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( @@ -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, diff --git a/Observer/SalesOrderSaveAfter.php b/Observer/SalesOrderSaveAfter.php index f486d9d8..de53742d 100755 --- a/Observer/SalesOrderSaveAfter.php +++ b/Observer/SalesOrderSaveAfter.php @@ -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) { @@ -104,7 +99,7 @@ public function execute(Observer $observer) $invoice->save(); } - $this->parseResponse($response, $order); + $this->parseFulfillResponse($response, $order); } } } @@ -131,7 +126,7 @@ 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); @@ -139,23 +134,27 @@ private function parseResponse(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()); } diff --git a/Observer/SalesOrderShipmentAfter.php b/Observer/SalesOrderShipmentAfter.php index 826edd00..7db7639e 100755 --- a/Observer/SalesOrderShipmentAfter.php +++ b/Observer/SalesOrderShipmentAfter.php @@ -92,18 +92,13 @@ 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(); @@ -111,7 +106,7 @@ public function execute(Observer $observer) $invoice->save(); } - $this->parseResponse($response, $order); + $this->parseFulfillResponse($response, $order); } elseif ($this->configRepository->getFulfillTrigger() == 'complete') { foreach ($order->getInvoiceCollection() as $invoice) { $invoice->pay(); @@ -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); } /** @@ -168,7 +165,7 @@ 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); @@ -176,23 +173,27 @@ private function parseResponse(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()); } diff --git a/Service/Order/ComposeCapture.php b/Service/Order/ComposeCapture.php index b0b7ac4a..c9c6f53d 100755 --- a/Service/Order/ComposeCapture.php +++ b/Service/Order/ComposeCapture.php @@ -16,7 +16,6 @@ */ class ComposeCapture extends OrderService { - /** * Compose request body for two capture order * @@ -28,30 +27,15 @@ class ComposeCapture extends OrderService public function execute(Order\Invoice $invoice, ?string $twoOriginalOrderId = ''): array { $order = $invoice->getOrder(); + $lineItems = $this->getLineItemsInvoice($invoice, $order); $reqBody = [ - 'billing_address' => $this->getAddress($order, [], 'billing'), - 'shipping_address' => $this->getAddress($order, [], 'shipping'), - 'currency' => $invoice->getOrderCurrencyCode(), 'discount_amount' => $this->roundAmt(abs((float)$invoice->getDiscountAmount())), 'gross_amount' => $this->roundAmt($invoice->getGrandTotal()), + 'line_items' => $lineItems, 'net_amount' => $this->roundAmt($invoice->getGrandTotal() - $invoice->getTaxAmount()), 'tax_amount' => $this->roundAmt($invoice->getTaxAmount()), - 'tax_rate' => $this->roundAmt( - (1.0 * $order->getTaxAmount() / ($order->getGrandTotal() - $order->getTaxAmount())) - ), - 'discount_rate' => '0', - 'invoice_type' => 'FUNDED_INVOICE', - 'line_items' => $this->getLineItemsInvoice($invoice, $order), - 'merchant_order_id' => (string)($order->getIncrementId()), - 'merchant_reference' => '', - 'merchant_additional_info' => '', + 'tax_subtotal' => $this->getTaxSubtotals($lineItems), ]; - if (!$order->getIsVirtual()) { - $reqBody['shipping_details'] = $this->getShippingDetails($order); - } - if ($twoOriginalOrderId) { - $reqBody['original_order_id'] = $twoOriginalOrderId; - } return $reqBody; } diff --git a/Service/Order/ComposeShipment.php b/Service/Order/ComposeShipment.php index bf6ce120..bc13c575 100755 --- a/Service/Order/ComposeShipment.php +++ b/Service/Order/ComposeShipment.php @@ -20,7 +20,6 @@ */ class ComposeShipment extends OrderService { - /** * Compose request body for two ship order * @@ -33,41 +32,14 @@ class ComposeShipment extends OrderService public function execute(Order\Shipment $shipment, Order $order): array { $shipmentItems = $this->getLineItemsShipment($order, $shipment); - $orderItems = $this->getRemainingItems($order); return [ - 'partially_fulfilled_order' => [ - 'billing_address' => $this->getAddress($order, [], 'billing'), - 'shipping_address' => $this->getAddress($order, [], 'shipping'), - 'currency' => $order->getOrderCurrencyCode(), - 'discount_amount' => $this->getSum($shipmentItems, 'discount_amount'), - 'gross_amount' => $this->getSum($shipmentItems, 'gross_amount'), - 'net_amount' => $this->getSum($shipmentItems, 'net_amount'), - 'tax_amount' => $this->getSum($shipmentItems, 'tax_amount'), - 'tax_rate' => reset($shipmentItems)['tax_rate'] ?? 0, - 'discount_rate' => '0', - 'invoice_type' => 'FUNDED_INVOICE', - 'line_items' => array_values($shipmentItems), - 'merchant_additional_info' => $order->getIncrementId(), - 'merchant_order_id' => (string)($order->getIncrementId()), - 'merchant_reference' => $order->getIncrementId(), - ], - 'remained_order' => [ - 'billing_address' => $this->getAddress($order, [], 'billing'), - 'shipping_address' => $this->getAddress($order, [], 'shipping'), - 'currency' => $order->getOrderCurrencyCode(), - 'discount_amount' => $this->getSum($orderItems, 'discount_amount'), - 'gross_amount' => $this->getSum($orderItems, 'gross_amount'), - 'net_amount' => $this->getSum($orderItems, 'net_amount'), - 'tax_amount' => $this->getSum($orderItems, 'tax_amount'), - 'tax_rate' => reset($orderItems)['tax_rate'] ?? 0, - 'discount_rate' => '0', - 'invoice_type' => 'FUNDED_INVOICE', - 'line_items' => array_values($orderItems), - 'merchant_additional_info' => $order->getIncrementId(), - 'merchant_order_id' => (string)($order->getIncrementId()), - 'merchant_reference' => $order->getIncrementId(), - ], + 'discount_amount' => $this->getSum($shipmentItems, 'discount_amount'), + 'gross_amount' => $this->getSum($shipmentItems, 'gross_amount'), + 'line_items' => array_values($shipmentItems), + 'net_amount' => $this->getSum($shipmentItems, 'net_amount'), + 'tax_amount' => $this->getSum($shipmentItems, 'tax_amount'), + 'tax_subtotals' => $this->getTaxSubtotals($shipmentItems), ]; } diff --git a/composer.json b/composer.json index 75398304..0b3e98e7 100755 --- a/composer.json +++ b/composer.json @@ -17,5 +17,16 @@ "psr-4": { "Two\\Gateway\\": "" } + }, + "repositories": [ + { + "type": "composer", + "url": "https://repo.magento.com/" + } + ], + "config": { + "allow-plugins": { + "magento/composer-dependency-version-audit-plugin": true + } } } diff --git a/view/adminhtml/templates/order/view/view.phtml b/view/adminhtml/templates/order/view/view.phtml index 7d5417ca..f063d5c6 100755 --- a/view/adminhtml/templates/order/view/view.phtml +++ b/view/adminhtml/templates/order/view/view.phtml @@ -13,36 +13,36 @@
- getAdditionalData(); ?> - getTwoOrderId($data) != ''): ?> + getTwoOrderId(); ?> + + escapeHtml($twoOrderId); ?> - getTwoInvoiceUrl($data) != ''): ?> + getTwoOrderFulfillments(); ?> + - - getTwoCreditNoteUrl($data) != ''): ?> + + - - +
escapeHtml(__('%1 Order ID', $provider)); ?>: - escapeHtml($block->getTwoOrderId($data)); ?>
escapeHtml(__('%1 Invoice', $provider)); ?>: - escapeHtml(__('Download')); ?>
escapeHtml(__('%1 Credit Note', $provider)); ?>: - escapeHtml(__('Download')); ?>