PayPal Webhook无法获取Custom ID及订单创建失败求助
解决PayPal订单custom_id无法在Webhook中获取的问题
问题根源分析
你当前使用的是PayPal REST API v1的Payment资源(旧版接口),这个接口的字段规则和你尝试的写法不匹配:
- 第一种写法里,你在
transactions节点下用了custom_id,但v1 Payment的transaction对象只支持custom字段,custom_id是v2 Orders API专属字段,因此PayPal会忽略这个无效字段,导致Webhook里无法获取对应值。 - 第二种写法把
custom_id放在根节点,这是v2 Orders API的用法,但v1 Payment接口根本没有这个根层级字段,所以支付创建直接失败。
修复方案(基于你当前的v1 Payment接口)
将transactions里的custom_id替换为custom字段,代码修改如下:
payment = paypalrestsdk.Payment({ "intent":"sale", "payer":{ "payment_method":"paypal" }, "redirect_urls":{ "return_url":"http://localhost:3000/order/success", "cancel_url":"http://localhost:3000/order/cancel" }, "transactions":[{ "item_list":{ "items":items }, "amount":{ "total":price, "currency":"EUR" }, "description":"This is the payment transaction description.", "custom": order_data["products"][0]['order'] # 替换为custom字段 }] }) if payment.create(): return JsonResponse({'status': 'success', "url": payment.links[1].href}) else: # 打印错误信息便于排查问题 print(payment.error) return JsonResponse({'status': 'error', 'message': payment.error})
修改后,当Webhook触发PAYMENT.SALE.COMPLETED等事件时,你可以在返回数据的transaction.custom字段中找到传入的订单ID。
重要调试建议
以后遇到支付创建失败时,一定要打印payment.error内容,它会返回PayPal官方给出的具体错误原因(比如字段不存在、格式错误等),不要只返回模糊的error状态。
长期优化建议
PayPal v1 Payments接口已被官方弃用,建议迁移到v2 Orders API,新版本接口支持根节点的custom_id字段,Webhook事件中也会直接返回该字段,且功能更完善、后续维护更有保障。
内容的提问来源于stack exchange,提问作者david backx
相关产品推荐
相关产品推荐

