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

PrestaShop1.7.8.8支付模块:验证后将cart-id替换为order-id存库问题

PrestaShop 1.7.8.8支付模块:OrderID传入merchant_ref_number及存储问题

问题背景

开发PrestaShop 1.7.8.8支付模块时,需要将orderid传入支付页面请求URL的merchant_ref_number参数并存储,但订单验证完成前无法获取orderid,目前只能传入cartid,且尝试多种方法后无法将orderid存入数据库的merchant_ref_number字段。

需确认两个方案可行性:

  • 是否可先传入cartid,待订单验证完成后替换为orderid?(已尝试hookActionValidateOrder钩子但失败)
  • 若上述方案不可行,能否通过重写类修改默认订单编号,使其与自定义的merchant_ref_number匹配?

现有尝试代码及问题

1. 未生效的hookActionValidateOrder钩子代码

public function hookActionValidateOrder($params)
{
  // Retrieve the generated order ID
  $orderId = $params['order']->reference;

  // Retrieve the cart ID from the database
  $cartId = $this->getCartIdFromDatabase($orderId);

  // Update the merchant_Ref_number field with the order ID in the database
  $this->updateMerchantRefNumber($cartId, $orderId);

}

public function getCartIdFromDatabase($orderId)
{
  $query = new DbQuery();
  $query->select('id_cart');
  $query->from('orders');
  $query->where('id_order = ' . (int)$orderId);

  $cartId = Db::getInstance()->getValue($query);

  return $cartId;
}

public function updateMerchantRefNumber($cartId, $orderId)
{
  // Assuming the merchant_Ref_number field is in a table named 'cart'
  $sql = 'UPDATE `' . _DB_PREFIX_ . 'cart` SET `merchant_ref_number` = \'' . pSQL($orderId) . '\' WHERE `id_cart` = ' . (int)$cartId;
  Db::getInstance()->execute($sql);
}

2. PaymentOptions及latpayInput核心代码

public function hookPaymentOptions($params) {
   // Check if this module is enabled if (!$this->active) { return; } 
  $action = 'https://lateralpayments.com/hps/hps_Payment.aspx';
  $inputs = $this->latpayInput();

  // Set the payment option details
  $externalOption = new PaymentOption();
  $externalOption->setCallToActionText($this->l('Pay with Latpay'))
      ->setAction($action)
      ->setInputs($inputs)
      ->setAdditionalInformation($this->context->smarty->fetch('module:latpayredirect/views/templates/front/payment_infos.tpl'));
      
  return [$externalOption];
}

protected function latpayInput() {
  global $smarty, $cart;
  $Merchant_User_Id = Configuration::get('latpayredirect_Merchant_User_Id');
  $merchantpwd = Configuration::get('latpayredirect_merchantpwd');
  $Purchase_summary = Configuration::get('latpayredirect_Purchase_summary');
  $SecretKey = Configuration::get('latpayredirect_salt');
  $processurl = Configuration::get('latpayredirect_process_url');
  $order_id = (int) $this->context->cart->id;
  $merchant_ref_number = trim( (string) $order_id );
  //......some codes.....

  //Merchant Data
  $merchant_data = array();
  $merchant_data['Merchant_User_Id']      = $Merchant_User_Id;
  $merchant_data['merchantpwd']         = $merchantpwd;
  $merchant_data['Purchase_summary']         = $Purchase_summary;
  $merchant_data['currencydesc']           = $currencydesc;
  $merchant_data['merchant_ref_number']     = $merchant_ref_number;

  //.........some more codes......
}

3. 无效代码尝试

$order_id = (int) $this->context->cart->id;
$merchant_ref_number = trim( (string) $order_id );

问题分析与解决方案

方案1:验证后替换cartid为orderid(修复钩子代码)

你的hookActionValidateOrder未生效,可从以下几点修复:

  • 确认钩子已注册:在模块安装方法中添加钩子注册逻辑:
    public function install()
    {
        return parent::install() && $this->registerHook('actionValidateOrder');
    }
    
  • 修正orderId取值:$params['order']->reference是带前缀的订单参考号(如#123),若需要纯数字订单ID,应改为:
    $orderId = $params['order']->id;
    
  • 简化cartId获取:无需额外查询数据库,$params['order']自带id_cart属性:
    public function hookActionValidateOrder($params)
    {
        $order = $params['order'];
        $orderId = $order->id; // 或$order->reference,根据支付平台需求选择
        $cartId = $order->id_cart;
    
        $this->updateMerchantRefNumber($cartId, $orderId);
    }
    
  • 优化数据库更新语句:使用PrestaShop官方推荐的安全更新写法:
    public function updateMerchantRefNumber($cartId, $orderId)
    {
        return Db::getInstance()->update(
            'cart',
            ['merchant_ref_number' => pSQL($orderId)],
            ['id_cart' => (int)$cartId]
        );
    }
    
  • 检查表字段合法性:确认cart表中存在merchant_ref_number字段,且字段长度、类型符合存储需求。

方案2:自定义订单编号匹配merchant_ref_number

若方案1不可行,可通过重写Order类自定义订单编号规则,使其与你设置的merchant_ref_number一致:

  1. 在模块目录下创建override/classes/Order.php文件:
class Order extends OrderCore
{
    public static function generateReference()
    {
        // 自定义生成规则,与latpayInput中merchant_ref_number的逻辑一致
        // 示例:直接使用cart ID作为订单参考号(需确保业务逻辑中无重复风险)
        $cart = Context::getContext()->cart;
        return (string)$cart->id;
        // 或生成唯一自定义编号:
        // return 'ORD-' . time() . '-' . $cart->id;
    }
}
  1. 清除PrestaShop缓存(删除var/cache目录下的所有文件),使重写生效。
  2. 此时在latpayInput中传入的merchant_ref_number(如cart ID)会与后续生成的订单编号完全匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 12:22:07