Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
32 changes: 20 additions & 12 deletions Helper/AdyenOrderPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,30 +192,38 @@ public function isFullAmountAuthorized(Order $order): bool
}

/**
* Create an entry in the adyen_order_payment table based on the passed notification
* Create an entry in the adyen_order_payment table based on the provided details
*
* @param Order $order
* @param Notification $notification
* @param bool $autoCapture
* @param string $pspReference
* @param string $paymentMethod
* @param int $amountValue
* @param string $amountCurrency
* @return Payment|null
*/
public function createAdyenOrderPayment(Order $order, Notification $notification, bool $autoCapture): ?Payment
{
public function createAdyenOrderPayment(
Order $order,
bool $autoCapture,
string $pspReference,
string $paymentMethod,
int $amountValue,
string $amountCurrency
): ?Payment {
$adyenOrderPayment = null;
$payment = $order->getPayment();
$amount = $this->adyenDataHelper->originalAmount($notification->getAmountValue(),
$notification->getAmountCurrency());
$captureStatus = $autoCapture ? Payment::CAPTURE_STATUS_AUTO_CAPTURE : Payment::CAPTURE_STATUS_NO_CAPTURE;
$merchantReference = $notification->getMerchantReference();
$pspReference = $notification->getPspreference();
$amount = $this->adyenDataHelper->originalAmount($amountValue, $amountCurrency);
$captureStatus = $autoCapture ?
OrderPaymentInterface::CAPTURE_STATUS_AUTO_CAPTURE :
OrderPaymentInterface::CAPTURE_STATUS_NO_CAPTURE;

try {
$date = new \DateTime();
$adyenOrderPayment = $this->adyenOrderPaymentFactory->create();
$adyenOrderPayment->setPspreference($pspReference);
$adyenOrderPayment->setMerchantReference($merchantReference);
$adyenOrderPayment->setMerchantReference($order->getIncrementId());
$adyenOrderPayment->setPaymentId($payment->getId());
$adyenOrderPayment->setPaymentMethod($notification->getPaymentMethod());
$adyenOrderPayment->setPaymentMethod($paymentMethod);
$adyenOrderPayment->setCaptureStatus($captureStatus);
$adyenOrderPayment->setAmount($amount);
$adyenOrderPayment->setTotalRefunded(0);
Expand All @@ -228,7 +236,7 @@ public function createAdyenOrderPayment(Order $order, Notification $notification
'adyen_order_payment table but something went wrong later. Please check your logs for potential ' .
'error messages regarding the merchant reference (order id): %s and PSP reference: %s. ' .
'Exception message: %s',
$merchantReference,
$order->getIncrementId(),
$pspReference,
$e->getMessage()
));
Expand Down
34 changes: 19 additions & 15 deletions Helper/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,23 @@ public function __construct(

/**
* @param Order $order
* @param Notification $notification
* @param bool $isAutoCapture
* @param string $pspReference
* @param int $amountValue
* @return InvoiceModel|null
* @throws LocalizedException
*/
public function createInvoice(Order $order, Notification $notification, bool $isAutoCapture): ?InvoiceModel
{
public function createInvoice(
Order $order,
bool $isAutoCapture,
string $pspReference,
int $amountValue
): ?InvoiceModel {
$this->adyenLogger->addAdyenNotification(
'Creating invoice for order',
[
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
'pspReference' => $pspReference,
'merchantReference' => $order->getIncrementId()
]
);

Expand All @@ -102,12 +107,12 @@ public function createInvoice(Order $order, Notification $notification, bool $is
$invoice->getOrder()->setIsInProcess(true);

// set transaction id so you can do a online refund from credit memo
$invoice->setTransactionId($notification->getPspreference());
$invoice->setTransactionId($pspReference);


if ((!$isAutoCapture)) {
// if amount is zero create a offline invoice
$value = (int)$notification->getAmountValue();
$value = $amountValue;
if ($value == 0) {
$invoice->setRequestedCaptureCase(InvoiceModel::CAPTURE_OFFLINE);
} else {
Expand All @@ -121,19 +126,18 @@ public function createInvoice(Order $order, Notification $notification, bool $is

$this->invoiceRepository->save($invoice);
$this->adyenLogger->addAdyenNotification(sprintf(
'Notification %s created an invoice for order with pspReference %s and merchantReference %s',
$notification->getEntityId(),
$notification->getPspreference(),
$notification->getMerchantReference()
'An invoice was generated for order with pspReference %s and merchantReference %s',
$pspReference,
$order->getIncrementId()
),
$this->adyenLogger->getInvoiceContext($invoice)
);
} catch (Exception $e) {
$this->adyenLogger->addAdyenNotification(
'Error saving invoice: ' . $e->getMessage(),
[
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
'pspReference' => $pspReference,
'merchantReference' => $order->getIncrementId()
]

);
Expand All @@ -153,9 +157,9 @@ public function createInvoice(Order $order, Notification $notification, bool $is
return $invoice;
} else {
$this->adyenLogger->addAdyenNotification(
sprintf('Unable to create invoice when handling Notification %s', $notification->getEntityId()),
__('Unable to create invoice when handling notification'),
array_merge($this->adyenLogger->getOrderContext($order), [
'pspReference' => $notification->getPspReference(),
'pspReference' => $pspReference,
'canUnhold' => $order->canUnhold(),
'isPaymentReview' => $order->isPaymentReview(),
'isCancelled' => $order->isCanceled(),
Expand Down
39 changes: 18 additions & 21 deletions Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function __construct(

/**
* @param MagentoOrder $order
* @param Notification $notification
* @param string $pspReference
* @return TransactionInterface|null
* @throws Exception
*/
public function updatePaymentDetails(MagentoOrder $order, Notification $notification): ?TransactionInterface
public function updatePaymentDetails(MagentoOrder $order, string $pspReference): ?TransactionInterface
{
//Set order state to new because with order state payment_review it is not possible to create an invoice
if (strcmp($order->getState(), MagentoOrder::STATE_PAYMENT_REVIEW) == 0) {
Expand All @@ -100,15 +100,15 @@ public function updatePaymentDetails(MagentoOrder $order, Notification $notifica
$paymentObj = $order->getPayment();

// set pspReference as transactionId
$paymentObj->setCcTransId($notification->getPspreference());
$paymentObj->setLastTransId($notification->getPspreference());
$paymentObj->setCcTransId($pspReference);
$paymentObj->setLastTransId($pspReference);

// set transaction
$paymentObj->setTransactionId($notification->getPspreference());
$paymentObj->setTransactionId($pspReference);
// Prepare transaction
$transaction = $this->transactionBuilder->setPayment($paymentObj)
->setOrder($order)
->setTransactionId($notification->getPspreference())
->setTransactionId($pspReference)
->build(TransactionInterface::TYPE_AUTH);

$transaction->setIsClosed(false);
Expand Down Expand Up @@ -198,12 +198,15 @@ public function createShipment(MagentoOrder $order): MagentoOrder
* Full order will only NOT be finalized if the full amount has not been captured/authorized.
*
* @param MagentoOrder $order
* @param Notification $notification
* @param string $pspReference
* @param int $amount
* @return MagentoOrder
*/
public function finalizeOrder(MagentoOrder $order, Notification $notification): MagentoOrder
{
$amount = $notification->getAmountValue();
public function finalizeOrder(
MagentoOrder $order,
string $pspReference,
int $amount
): MagentoOrder {
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order, false);
$formattedOrderAmount = $this->dataHelper->formatAmount($orderAmountCurrency->getAmount(), $orderAmountCurrency->getCurrencyCode());
$fullAmountFinalized = $this->adyenOrderPaymentHelper->isFullAmountFinalized($order);
Expand All @@ -221,12 +224,6 @@ public function finalizeOrder(MagentoOrder $order, Notification $notification):
$status = $this->getVirtualStatus($order, $status);
}

Comment thread
candemiralp marked this conversation as resolved.
// check for boleto if payment is totally paid
if ($order->getPayment()->getMethod() == "adyen_boleto") {
$status = $this->paymentMethodsHelper->getBoletoStatus($order, $notification, $status);
}

$order = $this->addProcessedStatusHistoryComment($order, $notification);
if ($fullAmountFinalized) {
$this->adyenLogger->addAdyenNotification(sprintf(
'Notification w/amount %s has completed the capturing of order %s w/amount %s',
Expand All @@ -235,8 +232,8 @@ public function finalizeOrder(MagentoOrder $order, Notification $notification):
$formattedOrderAmount
),
[
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
'pspReference' => $pspReference,
'merchantReference' => $order->getIncrementId()
]);
$comment = "Adyen Payment Successfully completed";
// If a status is set, add comment, set status and update the state based on the status
Expand All @@ -248,7 +245,7 @@ public function finalizeOrder(MagentoOrder $order, Notification $notification):
'Order status was changed to authorised status: ' . $status,
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $notification->getPspreference()]
['pspReference' => $pspReference]
)
);
} else {
Expand All @@ -258,8 +255,8 @@ public function finalizeOrder(MagentoOrder $order, Notification $notification):
$order->getIncrementId()
),
[
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
'pspReference' => $pspReference,
'merchantReference' => $order->getIncrementId()
]);
}
} else {
Expand Down
53 changes: 0 additions & 53 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,59 +943,6 @@ public function isBankTransfer(string $paymentMethod): bool
return $isBankTransfer;
}

/**
* @param Order $order
* @param Notification $notification
* @param string $status
* @return string|null
*/
public function getBoletoStatus(Order $order, Notification $notification, string $status): ?string
{
$additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize(
$notification->getAdditionalData()
) : "";

$boletobancario = $additionalData['boletobancario'] ?? null;
if ($boletobancario && is_array($boletobancario)) {
// check if paid amount is the same as orginal amount
$originalAmount =
isset($boletobancario['originalAmount']) ?
trim((string) $boletobancario['originalAmount']) :
"";
$paidAmount = isset($boletobancario['paidAmount']) ? trim((string) $boletobancario['paidAmount']) : "";

if ($originalAmount != $paidAmount) {
// not the full amount is paid. Check if it is underpaid or overpaid
// strip the BRL of the string
$originalAmount = str_replace("BRL", "", $originalAmount);
$originalAmount = floatval(trim($originalAmount));

$paidAmount = str_replace("BRL", "", $paidAmount);
$paidAmount = floatval(trim($paidAmount));

if ($paidAmount > $originalAmount) {
$overpaidStatus = $this->configHelper->getConfigData(
'order_overpaid_status',
'adyen_boleto',
$order->getStoreId()
);
// check if there is selected a status if not fall back to the default
$status = (!empty($overpaidStatus)) ? $overpaidStatus : $status;
} else {
$underpaidStatus = $this->configHelper->getConfigData(
'order_underpaid_status',
'adyen_boleto',
$order->getStoreId()
);
// check if there is selected a status if not fall back to the default
$status = (!empty($underpaidStatus)) ? $underpaidStatus : $status;
}
}
}

return $status;
}

/**
* @param string $paymentMethodCode
* @param array $params
Expand Down
15 changes: 15 additions & 0 deletions Helper/PaymentResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Adyen\Payment\Helper;

use Adyen\Payment\Helper\Order as AdyenOrderHelper;
use Adyen\Payment\Model\AuthorizationHandler;
use Adyen\Payment\Logger\AdyenLogger;
use Exception;
use Magento\Framework\Exception\AlreadyExistsException;
Expand Down Expand Up @@ -59,6 +60,7 @@ class PaymentResponseHandler
* @param PaymentMethods $paymentMethodsHelper
* @param OrderStatusHistory $orderStatusHistoryHelper
* @param OrdersApi $ordersApiHelper
* @param AuthorizationHandler $authorizationHandler
*/
public function __construct(
private readonly AdyenLogger $adyenLogger,
Expand All @@ -70,6 +72,7 @@ public function __construct(
private readonly PaymentMethods $paymentMethodsHelper,
private readonly OrderStatusHistory $orderStatusHistoryHelper,
private readonly OrdersApi $ordersApiHelper,
private readonly AuthorizationHandler $authorizationHandler,
) { }

public function formatPaymentResponse(
Expand Down Expand Up @@ -319,6 +322,18 @@ public function handlePaymentsDetailsResponse(
break;
}

// Authorize the payment based on the result code
if ($resultCode === self::AUTHORISED) {
$order = $this->authorizationHandler->execute(
$order,
$paymentMethod,
$paymentsDetailsResponse['pspReference'],
intval($paymentsDetailsResponse['amount']['value']),
$paymentsDetailsResponse['amount']['currency'],
$paymentsDetailsResponse['additionalData'] ?? []
);
}

// needed because then we need to save $order objects
$order->setAdyenResulturlEventCode($authResult);
$this->orderRepository->save($order);
Expand Down
Loading