Search code examples
phppaypallaravel-8laravel-package

PayPal createOrder() How to include multiple items in a purchase_item


This question has been (not fully) addressed in other posts such as PayPal Orders API schema. The API seems to indicate that multiple purchase_items can exist in an order, which act as 'contracts' between payer and sellor. The docs also seem indicate a procession like this: [purchase_items][purchase_item_request][items][item]. I have looked at the example on the API page, and altered it to include three items, and it worked, BUT it makes three charges against the credit card, where it should be just one charge for the total. So this works ... sorta.

This works for me, but 3 charges are made as opposed to one.

So I tried this to see if I could get Paypal to only make one charge:

This does not work

And, Trying to follow the documentation, I went with the following:

This does not work either

The last attempt produces an error like the others the purchase_units section is mal-formed, which is partially seen in this graphic:

This error is very similar to the error for two of the above attempts.

Now the error message includes a handy link to read about the error:

"href": "https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON"

But this just sends you, or changes to https://developer.paypal.com/api/orders/v2/ which is where I have been reading and reading.

I think, I hope I am close with the last effort above, yet I have tried more than just the two example above.

Your suggestions would be welcome. This is Orders, v2, paypal api.

I am also using laravel with https://github.com/srmklive/laravel-paypal

I tried this as well. Did not work. HELP!

This does not work as well  HELP


Solution

  • purchase_units takes an array with no key names, i.e. purchase_units[0] must exist (and is usually the only one, unless paying multiple sellers at once)

    The key name purchase_units_request is nonsense, there is none called this so it will be ignored....

    The key name orders_application_context is also nonsense and will be ignored, there is no key called that either (but there is an application_context key)

    Don't confuse object names in the document with the actual key name such an object goes under; look at the parent object's description for the correct key name of its members, starting with the request body and building your hierarchy out that way.

    For a working example of a purchase_units with an item in JSON format (not a PHP object array, but can be converted to one), see the Integrate Checkout guide, bullet #6 in 'Add and modify the code'.