如何获取通过贝宝交易发送的变量(NODEJS)
Posted
技术标签:
【中文标题】如何获取通过贝宝交易发送的变量(NODEJS)【英文标题】:How to get variables that were sent with paypal transaction (NODEJS) 【发布时间】:2020-06-27 12:09:25 【问题描述】:我正在创建将 Angular 8 和 NodeJS 集成为 paypal 订购者的应用程序。我想知道如何获取我发送到交易的所有项目。例如,这是我的 createOrder 对象:
request.requestBody(
intent: 'CAPTURE',
purchase_units: [
amount:
value: '7',
currency_code: 'USD',
breakdown:
item_total: value: '7', currency_code: 'USD'
,
invoice_id: 'muesli_invoice_id',
items: [
name: 'Hafer',
unit_amount: value: '3', currency_code: 'USD',
quantity: '1',
sku: 'haf001'
,
name: 'Discount',
unit_amount: value: '4', currency_code: 'USD',
quantity: '1',
sku: 'dsc002'
]
]
);
但是当我试图在我的服务器上捕获这个事务时:
const paymentId = req.body['paymentID'];
const request = new paypal.orders.OrdersCaptureRequest(paymentId);
request.requestBody();
// Call API with your client and get a response for your call
let response = await payPalClient.execute(request);
res.send(response);
我没有得到项目列表,只是来自贝宝的基本信息(送货地址等)
这里有什么问题?当我想取回这个项目列表时,我需要改变什么?
编辑:
statusCode: 201
headers:
cache-control: "max-age=0, no-cache, no-store, must-revalidate"
content-length: "1500"
content-type: "application/json"
date: "Mon, 16 Mar 2020 12:10:51 GMT"
paypal-debug-id: "c82e286689b67"
connection: "close"
__proto__: Object
result:
id: "64L22722PN8615907"
purchase_units: Array(1)
0:
reference_id: "default"
shipping:
name:
full_name: "John Doe"
__proto__: Object
address:
address_line_1: "Bajkowa 2 / 5"
address_line_2: "Case postale 12"
admin_area_2: "Warszawa"
admin_area_1: "PL_zip = 00800"
postal_code: "00800"
country_code: "PL"
__proto__: Object
__proto__: Object
payments:
captures: Array(1)
0:
id: "1XE85118J8824304V"
status: "COMPLETED"
amount:
currency_code: "USD"
value: "7.00"
__proto__: Object
final_capture: true
seller_protection:
status: "ELIGIBLE"
dispute_categories: (2) ["ITEM_NOT_RECEIVED", "UNAUTHORIZED_TRANSACTION"]
__proto__: Object
seller_receivable_breakdown:
gross_amount: currency_code: "USD", value: "7.00"
paypal_fee: currency_code: "USD", value: "0.50"
net_amount: currency_code: "USD", value: "6.50"
__proto__: Object
invoice_id: "muesli_invoice_id"
links: (3) […, …, …]
create_time: "2020-03-16T12:10:50Z"
update_time: "2020-03-16T12:10:50Z"
__proto__: Object
length: 1
__proto__: Array(0)
__proto__: Object
__proto__: Object
length: 1
__proto__: Array(0)
payer:
name:
given_name: "John"
surname: "Doe"
__proto__: Object
email_address: "sb-o0n47b1188456@personal.example.com"
payer_id: "Z95Z7FJLRFVBS"
address: country_code: "PL"
__proto__: Object
links: Array(1)
0:
href: "https://api.sandbox.paypal.com/v2/checkout/orders/64L22722PN8615907"
rel: "self"
method: "GET"
__proto__: Object
length: 1
__proto__: Array(0)
status: "COMPLETED"
这是我的 json 格式的全部回复
【问题讨论】:
显示您获得的实际信息。如果不存在,您将需要对付款对象的获取请求。 我已经编辑了我的帖子。也许我应该以其他方式或其他方式将项目添加到付款中? 【参考方案1】:没有办法。捕获响应不包含订单项详细信息。
据我所知,使用 v2/payments API 到 get the details of the captured payment(在您的示例中为 1XE85118J8824304V)也可能不会返回订单项详细信息 -- 尽管您可以测试和确认。
除非您在捕获 API 调用中将订单项详细信息发送到靠近您实际尝试在问题中获取它们的位置。也就是在抓包的时候在这里重新添加发送:
const request = new paypal.orders.OrdersCaptureRequest(paymentId);
request.requestBody();
然后,也许,可以在稍后的付款详细信息电话中取回它们。
但基本上你需要自己跟踪这些信息。
在不相关的笔记上,
invoice_id: 'muesli_invoice_id',
invoice_id 的值应始终是唯一生成的值,以前从未在该帐户上用于成功捕获的付款。在这里,您似乎在重复使用相同的字符串,这是一个禁忌。
【讨论】:
【参考方案2】:好的,解决方法很简单。服务器捕获路由应如下所示:
app.route('/success').post(async (req,res) =>
const paymentId = req.body['paymentID'];
let requestDetails = new paypal.orders.OrdersGetRequest(paymentId);
let responseDetails = await payPalClient.execute(requestDetails);
res.send(responseDetails);
);
我们应该使用let requestDetails = new paypal.orders.OrdersGetRequest(paymentId);
而不是const request = new paypal.orders.OrdersCaptureRequest(paymentId);
请记住在查看请求的所有详细信息后捕获请求。整个请求应该是这样的:
let requestDetails = new paypal.orders.OrdersGetRequest(paymentId);
let responseDetails = await payPalClient.execute(requestDetails);
const request = new paypal.orders.OrdersCaptureRequest(paymentId);
await payPalClient.execute(request);
res.send(responseDetails);
【讨论】:
有趣,据我所知,这对结果应该没有影响。以上是关于如何获取通过贝宝交易发送的变量(NODEJS)的主要内容,如果未能解决你的问题,请参考以下文章