简体   繁体   中英

PayPal Smart buttons adding items description to button

Hi I want to add a description to my smart button but my code for the items array is not working. Please refer to this question: Why will PayPal Smart buttons not recognise my items array?

I am having basically the same problem, this person's question was never resolved. Whenever I put my function for the Items value it returns a really long error. I am just not sure what I am doing wrong.

function arrayOfItems(){
   var arrayItems = []
   var items = document.querySelectorAll(".cp-info");
   items.forEach(function(item){
    // console.log(item);
    // console.log(item.children[1].children[1].children[1].textContent)
     let currency = item.children[1].children[1].children[1].textContent;
     let quantity = "1";
     //console.log(currency);
     //console.log(item.children[0].textContent);
     let name = item.children[0].textContent;

     let size = item.children[1].children[0].textContent;
     //console.log(size);

     name = name;

    //console.log(name);
    var itemInfo = {"unit_amount": {"currency_code": "USD", "value": currency},"quantity": "1", "name": name,};
    //console.log(arrayItems);

    arrayItems.push(itemInfo);
    //console.log(arrayItems);


  })

  return arrayItems;
 };

The array returned from the function in the console looks like this. ( the first by the 0 is just the break down

(4) [{…}, {…}, {…}, {…}]
0:
name: "GB Stacked"
quantity: "1"
unit_amount: {currency_code: "USD", value: "49.99"}
__proto__: Object
1: {unit_amount: {…}, quantity: "1", name: "Stacked Jeans"}
2: {unit_amount: {…}, quantity: "1", name: "GB Stacked"}
3: {unit_amount: {…}, quantity: "1", name: "Blue Stacked Leggings"}
length: 4
__proto__: Array(0)

UPDATE TO QUESTION

this is what I am pass in on the PayPal side

  createOrder: function(data, actions) {
                return actions.order.create({
                  "purchase_units": [{
                          "amount": {
                              "value": showTotals(),
                              "currency_code": "USD",
                          "breakdown": {
                              "item_total": {
                                  "currency_code": "USD",
                                  "value": showTotals(),
                              },
                          },
                          "items": arrayOfItems()
                        }
                    }],
                  });
              },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            }


        }).render('#paypal-button-container');

I am not sure what I changed but it doesn't give a console error anymore and allows the purchase to happen but the order details are still empty.

JSON stringily of what I am passing in

  var x = arrayItems;
    console.log(JSON.stringify(x,4));
  return arrayItems;
---------------------------
this returns to the console =
[{"unit_amount":{"currency_code":"USD","value":"49.99"},"quantity":"1","name":"GB Stacked"},{"unit_amount":{"currency_code":"USD","value":"79.99"},"quantity":"1","name":"Stacked Jeans"},{"unit_amount":{"currency_code":"USD","value":"49.99"},"quantity":"1","name":"GB Stacked"},{"unit_amount":{"currency_code":"USD","value":"79.99"},"quantity":"1","name":"Stacked Jeans"}]

this is a picture of the PayPal transaction screen, as you can see the order details is blank

We'll be able to give a more precise answer when you show exactly what you are doing in your code or passing, but it appears you purchase_units array is not valid.

It should be an array with a single (0 index) item, and that item should be a purchase_unit object with at least two keys, amount and items .

amount should be an object containing the required breakdown key with a breakdown object, and items should be an array of item objects


Code for debugging:

            createOrder: function(data, actions) {
                var req = {
                    purchase_units: [{
                        amount: {
                            value: showTotals(),
                            currency_code: "USD",
                            breakdown: {
                                item_total: {
                                    currency_code: "USD",
                                    value: showTotals()
                                }
                            }
                        },
                        items: arrayOfItems()
                    }]
                }

                console.log(JSON.stringify(req,null,4));
                return actions.order.create(req);
            },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            }


        }).render('#paypal-button-container');

Woking JSON example ( tested here ):

在此处输入图像描述

                    purchase_units: [{
                        amount: {
                            value: 259.96,
                            currency_code: "USD",
                            breakdown: {
                                item_total: {
                                    currency_code: "USD",
                                    value: 259.96
                                }
                            }
                        },
                        items: [{"unit_amount":{"currency_code":"USD","value":"49.99"},"quantity":"1","name":"GB Stacked"},
                        {"unit_amount":{"currency_code":"USD","value":"79.99"},"quantity":"1","name":"Stacked Jeans"},
                        {"unit_amount":{"currency_code":"USD","value":"49.99"},"quantity":"1","name":"GB Stacked"},
                        {"unit_amount":{"currency_code":"USD","value":"79.99"},"quantity":"1","name":"Stacked Jeans"}]
                    }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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