【问题标题】:How do I integrate PayPal Smart Button in Laravel 8?如何在 Laravel 8 中集成 PayPal 智能按钮?
【发布时间】:2021-03-29 14:41:11
【问题描述】:

我正在尝试在我的 Laravel 8 网站中集成 PayPal 智能按钮。这些是我正在使用的文档: 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 文件。

【问题讨论】:

标签: php laravel paypal paypal-sandbox paypal-rest-sdk


【解决方案1】:

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

一旦您对它进行排序并返回一个实际的 id 作为 JSON 对象的一部分,您需要更改前端代码以读取该 id 的密钥。看起来您正在读取密钥orderID,除非您专门设置它,否则不会设置它。

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

这部分前端代码:

<form action="/createOrder" method="POST">

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

您应该拥有一个容器 &lt;div&gt;,其 id 为 paypal-button-container,以便 JS 在其中呈现自己的按钮。该按钮不以任何方式使用或与 HTML 表单一起使用。

【讨论】:

  • 我会试一试的!我将从头开始重建控制器,看看会发生什么。
  • 我确保将其返回为 return response()->json(['id' => $response->result->id]);并且在测试时有效。但现在它给了我一个 419 和错误:Unexpected token
  • 该错误表示您正在输出 JSON 以外的内容。不要那样做。
  • 在浏览器的 Dev Tools 网络选项卡中查看响应正文,或者直接在浏览器中加载 createOrder URL,或者使用 curl 等工具向端点发出请求,并保存整个响应身体输出。似乎那里会有明显的东西,比如 HTML 不是有效的 JSON。但是,如果您完全感到困惑,您可以将您的正文复制并粘贴到 JSON 验证器中,然后观看它大声抱怨您的端点返回的不是 JSON 的错误内容。如果您不知道该怎么做,请研究如何从 Laravel 仅输出 JSON
  • 在后端,您需要设置一个路由,而不是正确处理 POST(或 POST 和 GET),以便它执行预期的 POST。否则,它将与请求不匹配,并将恢复为您的默认值,例如 404 或索引。
猜你喜欢
  • 2020-09-25
  • 2020-12-03
  • 2021-07-14
  • 2020-11-11
  • 2020-10-06
  • 2021-01-28
  • 2020-10-19
  • 2020-06-28
  • 2021-10-31
相关资源
最近更新 更多