php - 如何使用 Laravel 将 PayPal 数组响应详细信息发布到数据库
问题描述
如何将此 Paypal 响应详细信息发布到数据库
我已使用智能按钮贝宝成功付款。
我可以使用控制台日志捕获响应详细信息。
现在如何使用 laravel 将响应数组发布到数据库。
批准后
onApprove: function(data, actions) {
let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
if(details.status == 'COMPLETED'){
return fetch('/pages/save', {
method: 'post',
headers: {
'content-type': 'application/json',
"Accept": "application/json, text-plain, */*",
"X-Requested-With": "XMLHttpRequest",
"X-CSRF-TOKEN": token
},
body: JSON.stringify({
orderId : data.orderID,
id : details.id,
status: details.status,
payerEmail: details.payer.email_address,
})
})
.then(status)
.then(function(response){
// redirect to the completed page if paid
console.log(details)
// window.location.href = '/pages/sucess';
})
.catch(function(error) {
// redirect to failed page if internal error occurs
window.location.href = '/pages/fail?reason=internalFailure';
});
}else{
window.location.href = '/pages/fail?reason=failedToCapture';
}
});
},
Laravel 路线
Route::get('pages/sucess', [App\Http\Controllers\OrderController::class, 'sucess'])->name('pages.sucess');
输出
{
"id": "9S369747UW261581G",
"intent": "CAPTURE",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "USD",
"value": "0.50"
},
"shipping": {
"name": {
"full_name": "John Doe"
},
},
"payments": {
"captures": [
{
"id": "5DS42883V93959154",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "0.50"
},
}
]
}
}
],
}
如何捕获 PayPal 响应详细信息并将相同的详细信息转储到 Laravel 控制器,以便将其保存到数据库中?
解决方案
不要在客户端使用actions.order.create() / .capture()
,然后将信息发布到数据库。
相反,改为适当的服务器端集成。在您的服务器上创建两条路由,一条用于“创建订单”,一条用于“捕获订单”,记录在此处。这些路由应该只返回 JSON 数据(没有 HTML 或文本)。当捕获响应成功时,将其生成的付款详细信息存储在您的数据库中(特别purchase_units[0].payments.captures[0].id
是 PayPal 交易 ID)并在发送返回 JSON 之前执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)。
将这两条路线与以下批准流程配对:https ://developer.paypal.com/demo/checkout/#/pattern/server
推荐阅读
- solr - 如何使用 solr 在距离我的用户位置(Lat、Long)一定距离内找到位置(Lat、Long)?
- activiti - Activiti 5.15.1 流程定义在 activiti 7.0.0.SR1 版本上工作,没有任何改变,如模式和其他?
- reactjs - this.setState() 之后的 setState 回调函数上的 this.state
- python - pip virtualenv 重新激活后重置路径
- java - 暂停后,如果加密文件很大(例如 10Gb),恢复下载文件在 IE 中不起作用,它在 Chrome 中工作正常
- java - DBPedia 实时端点给出 504 或 503 异常
- spring-boot - 春季安全实施后,它会阻止我的静态文件夹中的所有资源吗?
- node.js - 如何在监视模式下运行 grunt 文件并启用实时重新加载?
- c# - 自定义打印的保存文件对话框
- xamarin.forms - Xamarin 表单 - 可以部署到 iPhone 但不能部署到 iPad(错误代码 0xe8008015)