繁体   English   中英

为什么 PayPal Smart 按钮无法识别我的 items 数组?

[英]Why will PayPal Smart buttons not recognise my items array?

我已将 PayPal 智能按钮集成到我的 JavaScript 购物车中。 我试图让 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": [{
                        "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 个项目:Item 1 和 Item 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>

贝宝按钮停止工作!

更新

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

<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 控制台给了我这个错误:

在此处输入图片说明

您似乎没有包括所需的细分。 这似乎是多余的,但它需要有一个细分部分。

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM