Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: partial-captures #804

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Model/Client/Orders/Processors/SuccessfulPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Mollie\Payment\Model\Client\ProcessTransactionResponse;
use Mollie\Payment\Model\Client\ProcessTransactionResponseFactory;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\ProcessCaptures;
use Mollie\Payment\Service\Order\SendOrderEmails;
use Mollie\Payment\Service\Order\TransactionProcessor;

Expand Down Expand Up @@ -78,6 +79,10 @@ class SuccessfulPayment implements OrderProcessorInterface
* @var SendOrderEmails
*/
private $sendOrderEmails;
/**
* @var ProcessCaptures
*/
private $processCaptures;

public function __construct(
ProcessTransactionResponseFactory $processTransactionResponseFactory,
Expand All @@ -89,7 +94,8 @@ public function __construct(
TransactionProcessor $transactionProcessor,
OrderCommentHistory $orderCommentHistory,
OrderRepositoryInterface $orderRepository,
SendOrderEmails $sendOrderEmails
SendOrderEmails $sendOrderEmails,
ProcessCaptures $processCaptures
) {
$this->processTransactionResponseFactory = $processTransactionResponseFactory;
$this->request = $request;
Expand All @@ -101,6 +107,7 @@ public function __construct(
$this->orderCommentHistory = $orderCommentHistory;
$this->orderRepository = $orderRepository;
$this->sendOrderEmails = $sendOrderEmails;
$this->processCaptures = $processCaptures;
}

/**
Expand Down Expand Up @@ -142,6 +149,10 @@ public function process(OrderInterface $order, Order $mollieOrder, string $type,
$this->sendOrderEmails($order);
}

if ($payment && $mollieOrder->amountCaptured !== null && $mollieOrder->amountCaptured->value != '0.00') {
$this->processCaptures->execute($order, $payment->captures());
}

$result = ['success' => true, 'status' => $mollieOrder->status, 'order_id' => $orderId, 'type' => $type];
$this->mollieHelper->addTolog('success', $result);
$this->checkCheckoutSession($order, $mollieOrder, $type);
Expand Down
13 changes: 12 additions & 1 deletion Model/Client/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Mollie\Payment\Service\Order\CancelOrder;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\ExpiredOrderToTransaction;
use Mollie\Payment\Service\Order\ProcessCaptures;
use Mollie\Payment\Service\Order\SaveAdditionalInformationDetails;
use Mollie\Payment\Service\Order\SendOrderEmails;
use Mollie\Payment\Service\Order\Transaction;
Expand Down Expand Up @@ -143,6 +144,10 @@ class Payments extends AbstractModel
* @var MethodCode
*/
private $methodCode;
/**
* @var ProcessCaptures
*/
private $processCaptures;

public function __construct(
OrderRepository $orderRepository,
Expand All @@ -165,7 +170,8 @@ public function __construct(
SaveAdditionalInformationDetails $saveAdditionalInformationDetails,
ExpiredOrderToTransaction $expiredOrderToTransaction,
CanRegisterCaptureNotification $canRegisterCaptureNotification,
MethodCode $methodCode
MethodCode $methodCode,
ProcessCaptures $processCaptures
) {
$this->orderRepository = $orderRepository;
$this->checkoutSession = $checkoutSession;
Expand All @@ -188,6 +194,7 @@ public function __construct(
$this->expiredOrderToTransaction = $expiredOrderToTransaction;
$this->canRegisterCaptureNotification = $canRegisterCaptureNotification;
$this->methodCode = $methodCode;
$this->processCaptures = $processCaptures;
}

/**
Expand Down Expand Up @@ -423,6 +430,10 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',
}
}

if ($paymentData->amountCaptured !== null && $paymentData->amountCaptured->value != '0.00') {
$this->processCaptures->execute($order, $paymentData->captures());
}

if (!$order->getIsVirtual()) {
$defaultStatusProcessing = $this->mollieHelper->getStatusProcessing($storeId);
if ($defaultStatusProcessing && ($defaultStatusProcessing != $order->getStatus())) {
Expand Down
48 changes: 42 additions & 6 deletions Model/Client/Payments/CapturePayment.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Model\Client\Payments;

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Mollie\Api\Resources\Payment;
use Mollie\Payment\Helper\General;
use Mollie\Payment\Service\Mollie\MollieApiClient;
use Mollie\Payment\Service\Mollie\Order\UsedMollieApi;
use Mollie\Payment\Service\Order\Invoice\ShouldEmailInvoice;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\PartialInvoice;
Expand All @@ -22,6 +29,11 @@ class CapturePayment
*/
private $mollieApiClient;

/**
* @var UsedMollieApi
*/
private $usedMollieApi;

/**
* @var PartialInvoice
*/
Expand All @@ -41,7 +53,6 @@ class CapturePayment
* @var InvoiceSender
*/
private $invoiceSender;

/**
* @var OrderCommentHistory
*/
Expand All @@ -54,49 +65,59 @@ class CapturePayment
* @var PriceCurrencyInterface
*/
private $price;
/**
* @var InvoiceRepositoryInterface
*/
private $invoiceRepository;

public function __construct(
MollieApiClient $mollieApiClient,
UsedMollieApi $usedMollieApi,
PartialInvoice $partialInvoice,
General $mollieHelper,
OrderRepositoryInterface $orderRepository,
InvoiceSender $invoiceSender,
OrderCommentHistory $orderCommentHistory,
ShouldEmailInvoice $shouldEmailInvoice,
PriceCurrencyInterface $price
PriceCurrencyInterface $price,
InvoiceRepositoryInterface $invoiceRepository
) {
$this->mollieApiClient = $mollieApiClient;
$this->usedMollieApi = $usedMollieApi;
$this->partialInvoice = $partialInvoice;
$this->mollieHelper = $mollieHelper;
$this->orderRepository = $orderRepository;
$this->invoiceSender = $invoiceSender;
$this->orderCommentHistory = $orderCommentHistory;
$this->shouldEmailInvoice = $shouldEmailInvoice;
$this->price = $price;
$this->invoiceRepository = $invoiceRepository;
}

public function execute(InvoiceInterface $invoice): void
{
// Otherwise the ID isn't available yet
$this->invoiceRepository->save($invoice);

$order = $invoice->getOrder();
$payment = $order->getPayment();

$order->setState(Order::STATE_PAYMENT_REVIEW);
$status = $order->getConfig()->getStateDefaultStatus(Order::STATE_PAYMENT_REVIEW);

$captureAmount = $invoice->getBaseGrandTotal();
$captureAmount = $invoice->getGrandTotal();

$mollieTransactionId = $order->getMollieTransactionId();
$mollieApi = $this->mollieApiClient->loadByStore($order->getStoreId());

$data = [];
$data = ['metadata' => ['invoice_id' => $invoice->getEntityId()]];
if ($captureAmount != $order->getBaseGrandTotal()) {
$data['amount'] = $this->mollieHelper->getAmountArray(
$order->getOrderCurrencyCode(),
$captureAmount
);
}

$capture = $mollieApi->paymentCaptures->createForId($mollieTransactionId, $data);
$capture = $this->createCapture($mollieApi, $order, $data);
$payment->setTransactionId($capture->id);

$order->addCommentToStatusHistory(
Expand All @@ -108,4 +129,19 @@ public function execute(InvoiceInterface $invoice): void
$status
);
}

private function createCapture(\Mollie\Api\MollieApiClient $mollieApi, OrderInterface $order, array $data): \Mollie\Api\Resources\Capture
{
$mollieTransactionId = $order->getMollieTransactionId();
if ($this->usedMollieApi->execute($order) == UsedMollieApi::TYPE_PAYMENTS) {
return $mollieApi->paymentCaptures->createForId($mollieTransactionId, $data);
}

$mollieOrder = $mollieApi->orders->get($mollieTransactionId);
$payments = $mollieOrder->payments();
/** @var Payment $last */
$last = end($payments);

return $mollieApi->paymentCaptures->createForId($last->id, $data);
}
}
13 changes: 12 additions & 1 deletion Model/Client/Payments/Processors/SuccessfulPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Mollie\Payment\Service\Mollie\Order\CanRegisterCaptureNotification;
use Mollie\Payment\Service\Order\OrderAmount;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\ProcessCaptures;
use Mollie\Payment\Service\Order\SendOrderEmails;
use Mollie\Payment\Service\Order\TransactionProcessor;
use Mollie\Payment\Service\Order\Uncancel;
Expand Down Expand Up @@ -69,6 +70,10 @@ class SuccessfulPayment implements PaymentProcessorInterface
* @var CanRegisterCaptureNotification
*/
private $canRegisterCaptureNotification;
/**
* @var ProcessCaptures
*/
private $processCaptures;

public function __construct(
ProcessTransactionResponseFactory $processTransactionResponseFactory,
Expand All @@ -79,7 +84,8 @@ public function __construct(
General $mollieHelper,
OrderRepositoryInterface $orderRepository,
SendOrderEmails $sendOrderEmails,
CanRegisterCaptureNotification $canRegisterCaptureNotification
CanRegisterCaptureNotification $canRegisterCaptureNotification,
ProcessCaptures $processCaptures
) {
$this->processTransactionResponseFactory = $processTransactionResponseFactory;
$this->orderAmount = $orderAmount;
Expand All @@ -90,6 +96,7 @@ public function __construct(
$this->orderRepository = $orderRepository;
$this->sendOrderEmails = $sendOrderEmails;
$this->canRegisterCaptureNotification = $canRegisterCaptureNotification;
$this->processCaptures = $processCaptures;
}

public function process(
Expand Down Expand Up @@ -200,6 +207,10 @@ private function handlePayment(OrderInterface $magentoOrder, Payment $molliePaym
}
}

if ($molliePayment->amountCaptured !== null && $molliePayment->amountCaptured->value != '0.00') {
$this->processCaptures->execute($magentoOrder, $molliePayment->captures());
}

if (!$magentoOrder->getIsVirtual()) {
$defaultStatusProcessing = $this->mollieHelper->getStatusProcessing($magentoOrder->getStoreId());
if ($defaultStatusProcessing && ($defaultStatusProcessing != $magentoOrder->getStatus())) {
Expand Down
20 changes: 2 additions & 18 deletions Observer/SalesOrderInvoiceRegister/CaptureInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Client\Payments\CapturePayment;
use Mollie\Payment\Service\Mollie\Order\CanUseManualCapture;
use Mollie\Payment\Service\Mollie\Order\UsedMollieApi;

class CaptureInvoice implements ObserverInterface
{
/**
* @var Config
*/
private $config;
/**
* @var CapturePayment
*/
Expand All @@ -31,30 +25,20 @@ class CaptureInvoice implements ObserverInterface
* @var CanUseManualCapture
*/
private $canUseManualCapture;
/**
* @var UsedMollieApi
*/
private $usedMollieApi;

public function __construct(
Config $config,
CapturePayment $capturePayment,
CanUseManualCapture $canUseManualCapture,
UsedMollieApi $usedMollieApi
CanUseManualCapture $canUseManualCapture
) {
$this->capturePayment = $capturePayment;
$this->config = $config;
$this->canUseManualCapture = $canUseManualCapture;
$this->usedMollieApi = $usedMollieApi;
}

public function execute(Observer $observer)
{
/** @var OrderInterface $order */
$order = $observer->getData('order');
if ($this->usedMollieApi->execute($order) == UsedMollieApi::TYPE_ORDERS ||
!$this->canUseManualCapture->execute($order)
) {
if (!$this->canUseManualCapture->execute($order)) {
return;
}

Expand Down
Loading
Loading