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

如何通过云函数在POST请求中返回Conekta支付生成的客户端编码

问题解决方法

问题根源

conekta.Order.create 是异步执行的操作,你当前采用回调写法获取订单ID时,代码执行到return res.status(200).json(...)这一步的时候,回调函数还没有被Conekta接口触发,customId还是初始的空字符串,所以返回的内容里取不到生成的编码。另外你回调函数里的参数res和路由的响应对象res重名,也会导致作用域冲突。

推荐解决方案(适配async路由)

因为你的路由已经声明了async,直接用await语法改写异步逻辑即可,不需要写回调,代码更简洁易维护:

app.post('/data/processPayment', async(req,res)=>{
  try{
    const product_name = req.body.product_name
    const unit_price = req.body.unit_price
    const quantity = req.body.quantity
    const username = req.body.username
    const email = req.body.email
    const phone = req.body.phone
    const amount = req.body.amount
    const carrier = req.body.carrier
    const street1 = req.body.street1
    const postal_code = req.body.postal_code
    const card_token = req.body.card_token
    
    // 直接await获取订单结果,不需要回调
    const order = await conekta.Order.create({
      "line_items": [{
        "name": product_name,
        "unit_price": unit_price,
        "quantity": quantity
      }],
      "shipping_lines": [{
        "amount": amount,
        "carrier": carrier
      }],
      "currency": "MXN",
      "customer_info": {
        'name': username,
        'email': email,
        'phone': phone
      },
      "shipping_contact":{
        "address": {
          "street1": street1,
          "postal_code": postal_code,
          "country": "MX"
        }
      },
      "metadata": {},
      "charges":[{
        "payment_method": {
          'type': 'card',
          'token_id': card_token // 测试阶段可保留原来的测试token,上线后替换为前端传的card_token
        }
      }]
    })

    // 拿到订单ID直接返回
    const customId = order.toObject().id
    return res.status(200).json({message:"OK ", folio: customId});
  } catch(error) {
    console.log(error)
    return res.status(500).send(error);
  }
})

备选方案(保留回调写法)

如果你不想调整异步逻辑写法,只需要把返回响应的逻辑挪到回调函数内部,同时修改回调参数名避免和路由响应对象重名即可:

app.post('/data/processPayment', async(req,res)=>{
  try{
    const product_name = req.body.product_name
    const unit_price = req.body.unit_price
    const quantity = req.body.quantity
    const username = req.body.username
    const email = req.body.email
    const phone = req.body.phone
    const amount = req.body.amount
    const carrier = req.body.carrier
    const street1 = req.body.street1
    const postal_code = req.body.postal_code
    
    conekta.Order.create({
      "line_items": [{
        "name": product_name,
        "unit_price": unit_price,
        "quantity": quantity
      }],
      "shipping_lines": [{
        "amount": amount,
        "carrier": carrier
      }],
      "currency": "MXN",
      "customer_info": {
        'name': username,
        'email': email,
        'phone': phone
      },
      "shipping_contact":{
        "address": {
          "street1": street1,
          "postal_code": postal_code,
          "country": "MX"
        }
      },
      "metadata": {},
      "charges":[{
        "payment_method": {
          'type': 'card',
          'token_id': 'tok_test_visa_4242'
        }
      }]
    // 回调参数改名为conektaRes避免和路由res冲突
    }, function(err, conektaRes) {
      if(err){
        console.log(err);
        return res.status(500).send(err);
      }
      const customId = conektaRes.toObject().id
      // 响应返回逻辑放在回调内部
      return res.status(200).json({message:"OK ", folio: customId});
    })
  } catch(error) {
    console.log(error)
    return res.status(500).send(error);
  }
})

内容的提问来源于stack exchange,提问作者D.Sanxhez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 04:30:00