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

PayPal Checkout按钮无法向IPN消息传递custom字段求助

解决新版PayPal Checkout的IPN中custom字段为空的问题

嘿,我之前也踩过这个坑!新版PayPal Checkout用的是REST API,和旧版按钮的NVP API逻辑完全不一样——直接在purchase_units里加custom字段,是不会自动同步到IPN的$_POST['custom']里的,核心原因是IPN本质上是为旧版Classic API设计的,和REST API的字段映射规则不兼容。

下面给你两种可行的解决方案:

方案一:改用invoice_id字段传递自定义数据

REST API里的invoice_id会直接映射到IPN的invoice字段,完全可以替代原来的custom字段来传递你的自定义ID:

createOrder: function(data, actions) {
  return actions.order.create({
    purchase_units: [{
      amount: {
        value: '1'
      },
      invoice_id: '35627187' // 把你的自定义ID放在这里
    }]
  });
}

之后在你的IPN处理脚本里,直接读取$_POST['invoice']就能拿到这个值,效果和原来用custom字段完全一致。

方案二:通过REST API查询订单详情获取自定义数据

如果你一定要用custom字段(或者需要传递更复杂的自定义数据),可以在创建订单时把数据存在订单的metadata或custom属性里,然后在IPN脚本中通过txn_id调用PayPal的REST API查询订单详情,从而取出自定义数据:

第一步:创建订单时添加自定义数据

createOrder: function(data, actions) {
  return actions.order.create({
    purchase_units: [{
      amount: {
        value: '1'
      },
      custom: '35627187', // 直接存简单ID
      metadata: {
        user_id: '35627187',
        order_type: 'premium' // 也可以存更复杂的键值对
      }
    }]
  });
}

第二步:IPN脚本中调用API查询订单

在你的IPN处理脚本(比如PHP)里,先验证IPN的合法性,再用收到的txn_id调用PayPal接口查询订单详情:

// 假设已经完成IPN合法性验证
$txn_id = $_POST['txn_id'];

// 先获取PayPal访问令牌(用你的Client ID和Secret)
$token_ch = curl_init();
curl_setopt($token_ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($token_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($token_ch, CURLOPT_POST, 1);
curl_setopt($token_ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($token_ch, CURLOPT_USERPWD, "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET");
$token_response = curl_exec($token_ch);
curl_close($token_ch);
$access_token = json_decode($token_response, true)['access_token'];

// 用令牌查询订单详情
$order_ch = curl_init();
curl_setopt($order_ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders/$txn_id");
curl_setopt($order_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($order_ch, CURLOPT_HTTPHEADER, array(
  "Authorization: Bearer $access_token",
  "Content-Type: application/json"
));
$order_response = curl_exec($order_ch);
curl_close($order_ch);

$order_details = json_decode($order_response, true);
// 取出custom字段值
$custom_value = $order_details['purchase_units'][0]['custom'];
// 取出metadata里的复杂数据
$user_id = $order_details['purchase_units'][0]['metadata']['user_id'];

额外提醒

还要确认你的PayPal沙箱商家账户已经正确启用了IPN,并且通知URL设置无误——沙箱环境的IPN设置是独立于生产环境的,别搞混了!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 19:02:32