为什么贝宝智能按钮无法识别我的物品阵列? [英] Why will PayPal Smart buttons not recognise my items array?

查看:56
本文介绍了为什么贝宝智能按钮无法识别我的物品阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have integrated the PayPal smart buttons into my JavaScript shopping cart. I am trying to allow PayPal to tell the user the items they had in the cart at checkout. For example;

我知道要执行此操作,我需要使用以下代码:

I know that to do this I need to use the following code:

<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,但我需要这些项目来表示用户在他们的购物车中拥有的物品.我得到了她的答案,而不是说我需要创建一个用于保存购物车商品名称,价格和数量的数组.

In this example, there are 2 items in the dropdown tab: Item 1 and Item 2 but I need these to represent what the user has in their cart. I got an answer on her than said I needed to create amn array that held the cart item name, price and quantity.

我想出了这段代码,我想做的是针对购物车中的每个商品,我想返回商品名称,商品价格和商品数量.我想出了以下代码:

I came up with this code, what I am trying to do is for every item in the cart, I want to return the product name, product price and product quantity. I came up with the code below:

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脚本时:

But when I run the new PayPal script like so:

<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)按钮停止工作!

The PayPal buttons stop working!

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

After making the changes the code now looks like this:

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

And is not producing any errors in my IDE, however when I run the code the JavaScript Console gives me this error:

推荐答案

您似乎没有包括所需的细目分类.这似乎是多余的,但是必须有一个细分部分.

You don't seem to be including the required breakdown. This may seem redundant, but it is required to have a breakdown section.

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

看起来您正在生成一个项目数组,因此您需要像这样使用它:

Also it looks like you are generating an items array, so you need to use it like so:

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

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

All of the currency_code fields also seem to be required, and not optional when passing line item information, so you will need to add those back in.

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

That's three issues you need to fix.

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

If you still need help, post a runtime example of what everything is evaluating to, i.e. the output of the functions so we can tell you what's wrong

这篇关于为什么贝宝智能按钮无法识别我的物品阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆