You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 1.9.2.4使用PayUmoney支付触发Observer.php致命错误求助

Fixing "Call to a member function getTelephone() on a non-object" in Magento SMS Observer

Hey there, let's break down and fix this error you're hitting with your SMS module when using PayUmoney. The root issue is that $order->getBillingAddress() is returning null (a non-object) when the order is placed via this payment gateway, so calling getTelephone() on it throws that fatal error. Here's how to resolve it:

Why This Happens

When using third-party payment gateways like PayUmoney, the order creation flow might differ from standard checkout. Your current code relies on loading the order from the checkout session (getLastOrderId()), which can be unreliable in payment callback scenarios—sometimes the session isn't properly maintained, or the order is created before the billing address is fully attached.

Step-by-Step Fixes

1. Add Null Checks for Billing Address

First, we'll add a safety check to avoid calling methods on a non-object. Modify the part where you fetch the telephone number:

// Replace this line
$mobilenumbers = $order->getBillingAddress()->getTelephone();

// With this:
$billingAddress = $order->getBillingAddress();
if ($billingAddress) {
    $mobilenumbers = $billingAddress->getTelephone();
    // Rest of your SMS logic here
} else {
    Mage::log('Billing address missing for order #' . $order->getIncrementId(), Zend_Log::WARNING);
}

2. Load the Order Directly from the Observer Event

Instead of relying on the checkout session to get the order, use the order object passed directly in the observer event. This is far more reliable for payment gateway scenarios:

// Replace your order loading code:
$order = Mage::getModel('sales/order');
$incrementId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);

// With this single line:
$order = $observer->getEvent()->getOrder();

3. Full Modified Observer Code

Here's your updated salesOrderPlace method with both fixes applied:

public function salesOrderPlace(Varien_Event_Observer $observer) {
    try{
        Mage::log("New Order Placed");
        // Fetch order directly from the observer event (no session dependency)
        $order = $observer->getEvent()->getOrder();
        
        if ($order instanceof Mage_Sales_Model_Order) {
            $incrementId = $order->getIncrementId();
            Mage::log("Order ID:".$incrementId);
            
            if ($this->getHelper()->issalesOrderPlace()) {
                $billingAddress = $order->getBillingAddress();
                if ($billingAddress) {
                    $mobilenumbers = $billingAddress->getTelephone();
                    $message = $this->getHelper()->getOrderMessage($order);
                    $customerName = $order->getCustomerName();
                    $retunValue = $this->getHelper()->curlApiCall($message,$mobilenumbers,$customerName,"New Order");
                } else {
                    Mage::log('Billing address missing for order #' . $incrementId, Zend_Log::WARNING);
                }
            }
            
            if ($this->getHelper()->issalesOrderPlaceForAdmin()) {
                $mobilenumbers = $this->getHelper()->getAdminMobileNumber();
                $message = $this->getHelper()->getOrderMessageForAdmin($order);
                $customerName = $order->getCustomerName();
                $retunValue = $this->getHelper()->curlApiCall($message,$mobilenumbers,$customerName,"New Order");
            }
        } else {
            Mage::log('Invalid order object received in salesOrderPlace observer', Zend_Log::ERR);
        }
    }catch(Exception $e) {
        Mage::log('Error in salesOrderPlace observer: ' . $e->getMessage(), Zend_Log::ERR);
        Mage::log($e->getTraceAsString(), Zend_Log::ERR);
    }
}

Additional Troubleshooting Tips

  • After applying the fix, test a PayUmoney order to see if the error is resolved. Check your Magento logs (var/log/system.log) for any warning messages about missing billing addresses.
  • If billing addresses are still missing for PayUmoney orders, inspect your PayUmoney module's callback logic. Ensure it's properly copying the customer's billing address to the order when processing the payment confirmation.
  • You can also verify the order directly in the Magento admin: open the problematic order and check if the billing address is populated. If not, the issue lies with how the PayUmoney module creates orders.

内容的提问来源于stack exchange,提问作者user8769728

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:59:37