Paypal Pass item sku and order reference id

I am trying to send order to paypal using javascript api purchase_units, but when the paypal redirects to success page I get error unknown purchase_units. When I check the api calls in console I get exclamation mark near the call with purchase_units

Here is my code

 paypal.Buttons({
        env: 'sendbox',
        style: {
            layout: 'horizontal',
            size:   'responsive',  
            shape:  'pill',        
            color:  'gold',        
            fundingicons: false,    
            tagline: false          
        },
        createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [ {
                reference_id: "PUHF",
                description: "Some description",
                custom_id: "Something7364",
                soft_descriptor: "Great description 1",
                amount: {
                    currency_code: "USD",
                    value: "200.00",
                    breakdown: {
                        item_total: {
                            currency_code: "USD",
                            value: "200.00"
                        }
                    }
                }, items: [{
                        name: "Item 1",
                        description: "The best item ever",
                        sku: "xyz-2654",
                        unit_amount: {
                            currency_code: "USD",
                            value: "100.00"
                        },
                        quantity: "1"
                    }, {
                        name: "Item 2",
                        description: "Not bad too",
                        sku: "zdc-3942",
                        unit_amount: {
                            currency_code: "USD",
                            value: "50.00"
                        }, quantity: "2"
                    }
                ],
            }
        ]
    })}, onApprove: function(data, actions) {
            return fetch('<?= $rootPath.URL['services']['orderGet'] ?>', {
                    method: 'GET'
                }
            ).then(function(res) {
                return res.json();
            }).then(function(res) {
                window.location.href = 'pages/success.php';
            });
        }

    }).render('#paypalCheckoutContainer');

Answers:

Answer

I copied your purchase_units array into a full working HTML example (below, don't try to run it within StackOverflow) and did not have any issues.

Looking at the rest of your code, env: sendbox is a typo. The onApprove section gave me trouble, I see there's PHP in there, but your code worked when removing the whole onApprove section -- so maybe try testing without that and then fixing it.

<!DOCTYPE html>
<!-- example from https://developer.paypal.com/demo/checkout/#/pattern/client -->

<head>
    <!-- Add meta tags for mobile and IE -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>

<body>
    <!-- Set up a container element for the button -->
    <div id="paypal-button-container"></div>

    <!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD"></script>

    <script>
        // Render the PayPal button into #paypal-button-container
        paypal.Buttons({

            // Set up the transaction
            createOrder: function(data, actions) {
                return actions.order.create({


// You can find a working example purchase_units at https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
  purchase_units: [ {
                reference_id: "PUHF",
                description: "Some description",
                custom_id: "Something7364",
                soft_descriptor: "Great description 1",
                amount: {
                    currency_code: "USD",
                    value: "200.00",
                    breakdown: {
                        item_total: {
                            currency_code: "USD",
                            value: "200.00"
                        }
                    }
                }, items: [{
                        name: "Item 1",
                        description: "The best item ever",
                        sku: "xyz-2654",
                        unit_amount: {
                            currency_code: "USD",
                            value: "100.00"
                        },
                        quantity: "1"
                    }, {
                        name: "Item 2",
                        description: "Not bad too",
                        sku: "zdc-3942",
                        unit_amount: {
                            currency_code: "USD",
                            value: "50.00"
                        }, quantity: "2"
                    }
                ],
            }
        ]

  ,
  application_context: {
    brand_name: "MyBusiness",
    /*shipping_preference: 'NO_SHIPPING'*/
  }


                }/*end of parameters to actions.order.create*/);
            },

            // 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');
    </script>
</body>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.