VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: rdcustom on April 16, 2025, 13:18:30 PM

Title: Prevent PayPal Webhooks Overwriting Final Order Status (Shipped,Refunded,Cancele
Post by: rdcustom on April 16, 2025, 13:18:30 PM
Message:

Hi everyone,

I want to share a problem I encountered with the PayPal Checkout plugin for VirtueMart, and the fix that solved it.



The Problem:

When PayPal sends a delayed webhook (e.g., PAYMENT.CAPTURE.COMPLETED after a fund release), it automatically updates the order status via the plugin, even if the order is already marked as:
   •   Shipped (S)
   •   Refunded (R)
   •   Cancelled (X)

This can cause serious issues, especially when the order was already processed or closed in your system.



The Solution:

I patched the ppc_webhooks.php file (specifically the updateOrderStatus() method) to prevent overwriting these final statuses.

Here's the modified method:

static function updateOrderStatus ($plugin, $virtuemart_order_id, $orderstatus) {

   $orderModel = VmModel::getModel('orders');
   $order = $orderModel->getOrder($virtuemart_order_id);

   // Prevent updates if current status is final (Shipped, Refunded, Cancelled)
   $finalStatuses = ['S', 'R', 'X'];
   if (!empty($order['details']['BT']) && in_array($order['details']['BT']->order_status, $finalStatuses)) {
      return false;
   }

   if (!empty($order['details']['BT']) && $order['details']['BT']->order_status != $orderstatus) {
      $oData['order_status'] = $orderstatus;
      $orderModel->updateStatusForOneOrder($virtuemart_order_id, $oData, true);
      $plugin->storePayPalData($virtuemart_order_id, $plugin->_currentMethod);
      return true;
   }

   return false;
}

Why this matters:

PayPal doesn't know the current order state in VirtueMart, so it can't avoid overwriting it unless we block it explicitly in the plugin logic. This patch ensures that your finalized orders remain untouched no matter how late a webhook arrives.

Hope this helps someone else facing the same issue.

Cheers!
— Davide