alma / alma-monthlypayments-magento2

Magento 2 Payment Gateway integration for Alma
Other
6 stars 9 forks source link

The order confirmation email was sent, but the field email_sent remains NULL. #192

Closed defenseoftheancients closed 1 month ago

defenseoftheancients commented 2 months ago

Preconditions and environment

Steps to reproduce

Place an order using the Alma payment method,

Expected result

After the order is placed successfully, the order confirmation email is sent, and the field email_sent is set to true. 

Actual result

The order confirmation email was sent, but the field email_sent remains NULL.

Screenshot from 2024-09-06 14-29-40

Additional information

This bug was caused by this line $this->orderHelper->save($order) run after $this->orderHelper->notify($order->getId()); (see \Alma\MonthlyPayments\Helpers\PaymentValidation::processOrder)


public function processOrder(Order $order, AlmaPayment $almaPayment): void
{

      $order->setCanSendNewEmailFlag(true);
      $order->setState(Order::STATE_PROCESSING);
      $newStatus = $order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING);
      $order->setStatus($newStatus);

      // Register successful capture to update order state and generate invoice
      $payment = $order->getPayment();
      $this->addTransactionToPayment($payment, $order, $almaPayment);
      $this->paymentProcessor->registerCaptureNotification($payment, $payment->getBaseAmountAuthorized());
      $this->orderHelper->notify($order->getId());

      // TODO : Paylater / PnX
      $order = $this->addCommentToOrder($order, __('First instalment captured successfully'), $newStatus);

      foreach ($order->getInvoiceCollection() as $invoice) {
          $invoice->setTransactionId($payment->getLastTransId());
      }
      $this->orderHelper->save($order);
      $this->inactiveQuoteById($order->getQuoteId());

}

Needs correcting to:


public function processOrder(Order $order, AlmaPayment $almaPayment): void
{

      $order->setCanSendNewEmailFlag(true);
      $order->setState(Order::STATE_PROCESSING);
      $newStatus = $order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING);
      $order->setStatus($newStatus);

      // Register successful capture to update order state and generate invoice
      $payment = $order->getPayment();
      $this->addTransactionToPayment($payment, $order, $almaPayment);
      $this->paymentProcessor->registerCaptureNotification($payment, $payment->getBaseAmountAuthorized());
      // $this->orderHelper->notify($order->getId());

      // TODO : Paylater / PnX
      $order = $this->addCommentToOrder($order, __('First instalment captured successfully'), $newStatus);

      foreach ($order->getInvoiceCollection() as $invoice) {
          $invoice->setTransactionId($payment->getLastTransId());
      }
      $this->orderHelper->save($order);
      $this->inactiveQuoteById($order->getQuoteId());

      $this->orderHelper->notify($order->getId());
}