javascript - 为什么 PayPal 智能按钮无法识别我的商品数组?

标签 javascript arrays paypal

我已将 PayPal 智能按钮集成到我的 JavaScript 购物车中。我试图让 PayPal 在结帐时告诉用户他们在购物车中的商品。例如; enter image description here

我知道要做到这一点,我需要使用以下代码:

<script>
        paypal.Buttons({
            createOrder: function(data, actions) {
                // This function sets up the details of the transaction, including the amount and line item details.
                return actions.order.create({
                    "purchase_units": [{
                        "description": "Stuff",
                        "amount": {
                            "value": "20.00",
                            "currency_code": "USD",
                            "breakdown": {
                                "item_total": {
                                    "currency_code": "USD",
                                    "value": "20.00"
                                },
                            }
                        },
                        "items": [
                            {
                                "unit_amount": {
                                    "currency_code": "USD",
                                    "value": "10.00"
                                },
                                "quantity": "1",
                                "name": "Item 1",
                            },
                            {
                                "unit_amount": {
                                    "currency_code": "USD",
                                    "value": "10.00"
                                },
                                "quantity": "1",
                                "name": "Item 2",
                            },
                        ],
                    }
                    ]
                });
            },
            onApprove: function(data, actions) {
                // This function captures the funds from the transaction.
                return actions.order.capture().then(function(details) {
                    // This function shows a transaction success message to your buyer.
                    window.location.href = "orderConfirmed.php"
                    clearCart()
                });
            }
        }).render('#paypal-button-container');
        //This function displays Smart Payment Buttons on your web page.
    </script>

在此示例中,下拉选项卡中有 2 个项目:项目 1 和项目 2,但我需要这些来代表用户购物车中的商品。我得到了她的答复,并说我需要创建 amn 数组来保存购物车商品名称、价格和数量。

我想出了这个代码,我想要做的是对于购物车中的每件商品,我想返回产品名称、产品价格和产品数量。我想出了下面的代码:

function arrayOfItems() {

        cart.forEach((cartItem, index) => {
            let currency = cartItem.price;
            let quantity = cartItem.quantity;
            let itemName = cartItem.name;

            let items = [{"unit_amount": {"currency_code": "USD","value": currency},"quantity": quantity,"name": itemName,}];

            return items;

        });

    }

但是当我像这样运行新的 PayPal 脚本时:

<script src="cart.js"></script>

    <script>
        paypal.Buttons({
            createOrder: function(data, actions) {
                // This function sets up the details of the transaction, including the amount and line item details.
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: countCartTotal()
                        },
                        items: [
                            {
                                arrayOfItems()
                            },
                        ],
                    }
                    ]
                });
            },
            onApprove: function(data, actions) {
                // This function captures the funds from the transaction.
                return actions.order.capture().then(function(details) {
                    // This function shows a transaction success message to your buyer.
                    window.location.href = "orderConfirmed.php"
                    clearCart()
                });
            }
        }).render('#paypal-button-container');
        //This function displays Smart Payment Buttons on your web page.
    </script>

PayPal 按钮停止工作!

更新

进行更改后,代码现在如下所示:

<script>
        paypal.Buttons({
            createOrder: function(data, actions) {
                // This function sets up the details of the transaction, including the amount and line item details.
                return actions.order.create({
                    "purchase_units": [{
                        "amount": {
                            "value": countCartTotal(),
                            "currency_code": "USD",
                        "breakdown": {
                            "item_total": {
                                "currency_code": "USD",
                                "value": countCartTotal()
                            },
                        },
                        "items": arrayOfItems()
                    }
                    ]
                });
            },
            onApprove: function(data, actions) {
                // This function captures the funds from the transaction.
                return actions.order.capture().then(function(details) {
                    // This function shows a transaction success message to your buyer.
                    window.location.href = "orderConfirmed.php"
                    clearCart()
                });
            }
        }).render('#paypal-button-container');
        //This function displays Smart Payment Buttons on your web page.
    </script>

并且在我的 IDE 中没有产生任何错误,但是当我运行代码时,JavaScript 控制台给出了这个错误:

enter image description here

最佳答案

您似乎没有包含所需的分割。这可能看起来多余,但需要有一个分割部分。

                        "breakdown": {
                            "item_total": {
                                "value": countCartTotal()
                            },
                        }

此外,您似乎正在生成一个项目数组,因此您需要像这样使用它:

                    amount: {
                        value: countCartTotal()
                    },
                    items: arrayOfItems(),

在传递订单项信息时,所有 currency_code 字段似乎也是必需的,而不是可选的,因此您需要重新添加这些字段。

这是您需要解决的三个问题。

如果您仍然需要帮助,请发布一个运行时示例,说明所有内容的评估结果,即函数的输出,以便我们可以告诉您出了什么问题

关于javascript - 为什么 PayPal 智能按钮无法识别我的商品数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60973630/

相关文章:

javascript - 提交时如何知道 JSP 页面中正在执行的页面或调用

perl - PayPal Express 客户结账交易 ID

android - Calabash 使用 Paypal 进行自动化测试

没有jquery的Javascript动画

javascript - 圆不会在 Canvas 上绘制

javascript - 如何根据 Angular 确定绝对顶部和左侧位置? Javascript

c# - 奇怪的数组问题

java - 我应该使用什么数据结构来存储java中的二进制代码?

actionscript-3 - 在as3中写入16位字节数组

react-native - 在 native webview 中跟踪支付状态