Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Re-Order the array keys so that JSON will be an array, not an object #1034

Merged
merged 6 commits into from
Feb 14, 2018

Conversation

benjaminpick
Copy link

@benjaminpick benjaminpick commented Jan 3, 2018

SDK/Library version: 1.13.0
Environment: Sandbox
PayPal-Debug-ID values: 3a5fd0a7adf56

I have create an Item array along these lines:

$items = [ null, new Item() ];
$items = array_filter($items, function($value) { return !is_null($value); })
$itemList->setItems($items);

Which returns this error from the API:

{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request" }

Here is the generated JSON:

{
   "intent":"sale",
   "payer":{
      "payment_method":"paypal"
   },
   "redirect_urls":{
      "return_url":"http://local/?id=67&type=4835&mail_id=8190&success=true",
      "cancel_url":"http://local/?id=67&type=4835&mail_id=8190&success=false"
   },
   "transactions":[
      {
         "amount":{
            "currency":"EUR",
            "total":"400"
         },
         "item_list":{
            "items":{
               "1":{
                  "name":"Label",
                  "currency":"EUR",
                  "price":"400",
                  "quantity":1
               }
            }
         },
         "description":"Ich m\u00f6chte spenden",
         "invoice_number":8190
      }
   ],
   "note_to_payer":"Ich m\u00f6chte spenden",
   "experience_profile_id":"XP-ZZZZ-FPCC-ZCSS-YUZD"
}

Not that "items" is not an array here, but an object - the only difference to working requests.

As the SDK only requires to submit an array, not an "array numbered from 0..N" (e.g. it also could be an associative array), I suggest using array_values as in this pull request. Probably this should be added at other places of the API as well for consistency.

This kind of error could also appear when using "ItemList->removeItem", as array_diff also doesn't re-order the keys (see unit test).

The Unit test failed at the beginning because setItems is given Item instead of array(Item), is this a legacy/compability usage?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants