Paypal 弹出窗口无法使用 php 和 paypal 集成

问题描述 投票:0回答:1

我是新来的,很抱歉问得太早了。我只需要你的帮助,有人告诉我你是回答我问题的最佳人选。

我正在使用我的在线商店进行测试,而我的 PayPal 沙盒在付款时似乎有问题,我不知道要放置什么代码,因为 PayPal 方面没有示例。

这是我的代码,不要介意我的客户ID:

<script src="https://www.paypal.com/sdk/js?client-id=abcdefg&currency=USD"></script>
    <!-- Set up a container element for the button -->
    <script>
        paypal.Buttons({
            createOrder() {
                return fetch("/my-server/create-paypal-order", {
                    method: "POST",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        body: JSON.stringify({
                            cart: [
                            {
                                sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
                                quantity: "YOUR_PRODUCT_QUANTITY",
                            },
                            ],
                       }),
                })
                .then((response) => response.json())
                .then((order) => order.id);
            },
            onApprove(data) {
                return fetch("/my-server/capture-paypal-order", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    body: JSON.stringify({
                    orderID: data.orderID
                })
           })
           .then((response) => response.json())
           .then((orderData) => {
               console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
               const transaction = orderData.purchase_units[0].payments.captures[0];
               alert(`Transaction ${transaction.status}: ${transaction.id}\n\nSee console for all 
               available details`);
           });
      }
  }).render('#paypal-button-container');
</script>`
php paypal
1个回答
0
投票

本质上你需要创建一个后端来实现创建和捕获订单的两条路由,在上面的示例代码中是

/my-server/create-paypal-order
/my-server/capture-paypal-order

路径可以更改,但无论更改为什么,它们都需要是浏览器可以向其发送获取请求并返回 JSON 响应的真实路径(仅 JSON,响应中不得包含其他 HTML 或文本,所以如果使用来自 PHP 的调试信息的“echo”或“print”语句要小心,这将破坏客户端代码解析预期 JSON 有效负载的能力)

你实现 2 条路由的后端可以用任何语言实现,包括 PHP。 standard integration guideintegration builder 中的示例后端以 node.js 为例,因为 JavaScript 是一种每个人都能理解的语言,但它们只是示例——因此您可以实现示例“index.js” " 以您选择的任何服务器端语言执行

© www.soinside.com 2019 - 2024. All rights reserved.