如何在 Laravel 8 中集成 PayPal 智能按钮?

问题描述

我正在尝试将 PayPal Smart 按钮集成到我的 Laravel 8 网站中。这些是我正在使用的文档: https://developer.paypal.com/docs/checkout/integrate/

https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/

https://github.com/paypal/Checkout-PHP-SDK

问题:我收到两个错误

这个错误强调了我前端的提取

cart:278 POST http://127.0.0.1:8000/createOrder 500 (Internal Server Error)
{err: "Error: Expected an order id to be passed↵ 

这是我的路由文件

use App\Http\Controllers\PayPalController;

Route::post('/createOrder',[PayPalController::class,'createOrder']);

这是我的 PayPalController 方法

<?PHP

namespace App\Http\Controllers;

// To capture payment for paypal
namespace Sample\CaptureIntentExamples;

// Grabs the environment and request to be used for paypal
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;

// Request/Response
use Illuminate\Http\Request;
use Illuminate\Http\Response;


class PayPalController extends Controller
{
    public function createOrder() {

        $request = new OrdersCreateRequest();

        $request->prefer('return=representation');

        $request->body = [
            "intent" => "CAPTURE","purchase_units" => [[
                "reference_id" => "test_ref_id1","amount" => [
                    "value" => "100.00","currency_code" => "USD"
                    ]
            ]],"application_context" => [
                "cancel_url" => "https://127.0.0.1:8000/cart","return_url" => "https://127.0.0.1:8000/"
            ]         
        ];           

        // 3. Call PayPal to set up a transaction
        $client = PayPalClient::client();

        $response = $client->execute($request);

        // 4. Return a successful response to the client.
        return $response;
    }
}

这是我的前端 .blade.view 文件

<script src="https://www.paypal.com/sdk/js?client-id=*******************"></script>
                                
<form action="/createOrder" method="POST">
@csrf   
<div id="paypal-button-container"></div>
                                
<script>
  paypal.Buttons({
    createOrder: function() {
      return fetch('/createOrder',{
        method: 'POST',headers: {
          'content-type': 'application/json','Accept': 'application/json','url': '/createOrder',"X-CSRF-Token": document.querySelector('input[name=_token]').value
         },}).then(function(res) {
    return res.json();
  }).then(function(data) {
    return data.orderID; // Use the same key name for order ID on the client and server
  });
  }
  }).render('#paypal-button-container');
</script>
</form>

我已经用我的凭据编辑了 PayPalClient.PHP 文件

解决方法

/createOrder 路由中的 500 错误是您需要在内部诊断/调试的内容。尝试在浏览器中加载 URL 并查看 Laravel 是否有更多错误输出。如果不这样做,请编辑相应的 PHP 以输出有关正在发生的事情的更多有用信息。

一旦您解决了这个问题并返回一个实际的 alias mygrep grep -i mykeyword myfile.txt 作为 JSON 对象的一部分,您需要更改前端代码以读取该 id 的键。看起来您正在读取密钥 id,除非您专门设置它,否则不会对其进行设置。

以下前端演示代码更好,并且有错误处理:https://developer.paypal.com/demo/checkout/#/pattern/server

这部分前端代码:

orderID

完全没有意义,除非它只是为了测试目的,但仍然没有意义。

您应该拥有一个容器 <form action="/createOrder" method="POST"> ,id 为 <div>,以便 JS 呈现自己的按钮。该按钮不以任何方式使用或处理 HTML 表单。