首页 > 解决方案 > 如何使用 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 控制器,以便将其保存到数据库中?

标签: phpajaxlaravelpaypal

解决方案


不要客户端使用actions.order.create() / .capture(),然后将信息发布到数据库。

相反,改为适当的服务器端集成。在您的服务器上创建两条路由,一条用于“创建订单”,一条用于“捕获订单”,记录在此处。这些路由应该只返回 JSON 数据(没有 HTML 或文本)。当捕获响应成功时,将其生成的付款详细信息存储在您的数据库中(特别purchase_units[0].payments.captures[0].id是 PayPal 交易 ID)并在发送返回 JSON 之前执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)。

将这两条路线与以下批准流程配对:https ://developer.paypal.com/demo/checkout/#/pattern/server


推荐阅读