【发布时间】:2020-07-14 18:12:53
【问题描述】:
我已经为 Paypal Payments 进行了服务器端设置。我从 Html 表单中获取产品价格,然后使用 paypalrestsdk 模块 创建付款。整个系统的重要代码块在这里:-
payment = paypalrestsdk.Payment({
"intent": "sale",
# Set payment method
"payer": {
"payment_method": "paypal"
},
# Set redirect URLs
"redirect_urls": {
"return_url": url_for('after_payment_done_2_sol', hash_id=hash_id, user_id=current_user.user_id, _external=True),
"cancel_url": url_for('topup', _external=True)
},
# Set transaction object
"transactions": [{
"amount": {
"total": query.total_price,
"currency": "USD"
},
"description": f"Payment Method for paying {query.total_price} for Top Up"
}]
})
if payment.create():
print('Payment "{}" created successfully'.format(payment.id))
for link in payment.links:
if link.method == "REDIRECT":
redirect_url = str(link.href)
print('Redirect for approval: {}'.format(redirect_url))
return redirect(redirect_url)
因此,这将创建一个指向“通过 Paypal 支付 X 金额”的链接。但我想像这里一样集成智能按钮 https://developer.paypal.com/demo/checkout/#/pattern/server。现在这里有一个代码块,如:
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// How Does this work?????????????????????
// Set up the transaction
createOrder: function(data, actions) {
return fetch('/demo/checkout/api/paypal/order/create/', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.orderID;
});
},
// ???????????????????????????????????????? How does this Work?????
// Finalize the transaction
onApprove: function(data, actions) {
return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>
所以这里 onAprrove 和 createOrder 是如何工作的?我应该如何在烧瓶中设置我的路线?请帮忙!
【问题讨论】: