Why does adding items make Paypal Checkout return an error?

时光毁灭记忆、已成空白 提交于 2020-03-23 12:37:06

问题


I am trying to create a PayPal order using the Javascript SDK. I am unable to add items without PayPal returning a non-descriptive 400 error.

This markup works fine:

return actions.order.create({
  purchase_units: [{
    description: "Stuff",
    amount: {
      value: '57.49',
      currency_code: "CAD",
    },
  }],
  application_context: {
    brand_name: "MyBusiness",
    shipping_preference: 'NO_SHIPPING'
  }
});

This markup, where I add the amount breakdown and items doesn't:

return actions.order.create({
  purchase_units: [{
    description: "Stuff",
    amount: {
      value: '57.49',
      currency_code: "CAD",
      breakdown: {
        item_total: '57.49',
      }
    },
    items: [{
      unit_amount: '57.49',
      quantity: '1',
      name: "item 1",
    }],
  }],
  application_context: {
    brand_name: "MyBusiness",
    shipping_preference: 'NO_SHIPPING'
  }
});

I am following this documentation:

https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit_request

https://developer.paypal.com/docs/api/orders/v2/#definition-item

I'm guessing the way I added the breakdown doesn't work. But the spec implies that it is amount -> breakdown.


回答1:


Full working example... save to an HTML file, or just copy the purchase_units array

<!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=CAD"></script>

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

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


// based on example array from https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
  "purchase_units": [{
      "description": "Stuff",
      "amount": {
        "value": "57.49",
        "currency_code": "CAD",
        "breakdown": {
          "item_total": {
            "currency_code": "CAD",
            "value": "57.49"
          },
        }
      },
      "items": [
        {
          "unit_amount": {
            "currency_code": "CAD",
            "value": "57.49"
          },
          "quantity": "1",
          "name": "item 1",
        },
      ],
    }
  ]

  ,
  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>


来源:https://stackoverflow.com/questions/59384260/why-does-adding-items-make-paypal-checkout-return-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!