I'm working on a Drupal 10 offsite payment module. I'm trying to show Drupal message whenever order gets placed, declined, or failed. Please see code snippet:
module file: Drupal\my_module\Plugin\Commerce\PaymentGateway\LatpayRedirect.php
public function onReturn(OrderInterface $order, Request $request) {
//\Drupal::logger('commerce_payment')->notice('onReturn function called.');
$amount = $request->get('amount');
$status_code = $request->get('Status');
$merchant_ref_number = $request->get('merchant_ref_number');
$currencydesc = $request->get('currencydesc');
$transaction_id = $request->get('Transactionid');
$Requestid = $request->get('Requestid');
$CardLast4 = $request->get('CardLast4');
$CardType = $request->get('CardType');
// Concatenate the merchant_ref_number and the last 4 digits
$remote_id = $merchant_ref_number . '.' . $CardType . ' Card ending in ' . $CardLast4;
// Create and save payment entity.
$payment_storage = $this->entityTypeManager->getStorage('commerce_payment');
$payment = $payment_storage->create([
'state' => 'completed',
'amount' => $order->getBalance(),
'payment_gateway' => $this->parentEntity->id(),
'order_id' => $order->id(),
'remote_id' => $remote_id,
'remote_state' => $status_code,
]);
$payment->save();
// Check the status code returned from the payment gateway.
if ($status_code == '00') {
// Payment was successful.
\Drupal::messenger()->addMessage($this->t('Your payment was successful. Status Code - @status / Reference number - @merchant_ref_number / Transaction ID - @transaction_id.', ['@merchant_ref_number' => $merchant_ref_number, '@transaction_id' => $transaction_id, '@status' => $status_code]), 'status');
}
elseif ($status_code == '05') {
// Transaction Failed / Declined.
\Drupal::messenger()->addMessage($this->t('Payment declined. Please try again with correct card details. Status Code - @status / Reference number - @merchant_ref_number / Transaction ID - @transaction_id.', ['@status' => $status_code, '@transaction_id' => $transaction_id, '@merchant_ref_number' => $merchant_ref_number]), 'error');
$order->set('state', 'pending');
$order->save();
}
}
public function onCancel(OrderInterface $order, Request $request) {
$status_code = $request->get('Status');
//$merchant_ref_number = $request->get('Merchant_ref_number');
\Drupal::messenger()->addMessage($this->t('Status Code : @status / You cancelled before making payment but you may resume the checkout process here whenever you are ready.', [
'@status' => $status_code,
//'@merchant_ref_number' => $merchant_ref_number,
]), 'error');
}
The problem is - it logs out user everytime the order is placed or transaction failed. While performing a test, I commented out those drupal messenger, and it started working fine. It didn't logged me out then. So I knew who the culprit was.
I have been stuck for days. Can someone please help me why this happening and what I could do to resolve or any alternate solution? Thanks a lot.
Update: Here's the thing, for me it's very urgent and I genuinely need help to resolve this issue asap no matter if I have to pay few bucks. So Whoever helps me will get paid with bounty. Please guys!! Help.
\Drupal::...