user3788675
user3788675

Reputation: 47

PayPal REST API shipping address not working upon a cURL request

I guess this is a two part question, so basically I'm working with PayPal's REST api and I want to set up a payment. I made this array that I used json_encode on it and it translates it into the json code below it, so basically there's something wrong with the shipping address object because as soon as I remove it everything works smoothly. I can't really find a problem when I'm looking through the documentation, I've placed it into the item_list object like it says I should, filled out all of the required fields so I'm guessing that something might be wrong with the syntax

$json_object = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "localhost/oauth2/src/OAuth2/success.php",
        "cancel_url" => "localhost"
    ),
    "payer" => array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(
            0 => array(
            "amount" => array(
                "total" => "12.00",
                "currency" => "USD"
            ),
            "description" => "payment description",
            "item_list" => array(
                "items" => array(
                    0 => array(
                        "quantity" => "1",
                        "name" => "jacket",
                        "price" => "12.00",
                        "sku" => "dasd",
                        "currency" => "USD",
                        "description" => "blue"
                    )
                ),
                   "shipping_address" => array(
                    "recipient_name" => "John Johnson",
                    "line1" => "Whatever street 2",
                    "line2" => "Another street 2",
                    "city" => "London",
                    "country_code" => "UK",
                    "postal_code" => "NR30 1LY",
                    "state" => "England",
                    "phone" => "3123123123"
                )
            )
        )
    )
);

encodes to

{
  "intent": "sale",
  "redirect_urls": {
    "return_url": "localhost/oauth2/src/OAuth2/success.php",
    "cancel_url": "localhost"
  },
  "payer": {
    "payment_method": "paypal"
  },
  "transactions": [
    {
      "amount": {
        "total": "12.00",
        "currency": "USD"
      },
      "description": "payment description",
      "item_list": {
        "items": [
          {
            "quantity": "1",
            "name": "jacket",
            "price": "12.00",
            "sku": "dasd",
            "currency": "USD",
            "description": "blue"
          }
        ],
        "shipping_address": {
          "recipient_name": "John Johnson",
          "line1": "Whatever street 2",
          "line2": "Another street 2",
          "city": "London",
          "country_code": "UK",
          "postal_code": "NR30 1LY",
          "state": "England",
          "phone": "3123123123"
        }
      }
    }
  ]
}

The other question i wanted to ask is how can I find out where the exactly is the error, after I var_dump() the response of the curl request it either returns an empty string in which case it doesn't work or it returns the desired request which contains the JSON object, I never get an error or anything

Here's the code I use to submit the JSON object above

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_HEADER, $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", $token));
$response = curl_exec($ch);
curl_close($ch);

Upvotes: 0

Views: 358

Answers (1)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

You've go your objects a bit out of order, try like this:

$json_object = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "localhost/oauth2/src/OAuth2/success.php",
        "cancel_url" => "localhost"
    ),
    "payer" => array(
        "payment_method" => "paypal",
        "payer_info" => array(
            "shipping_address" => array(
                "recipient_name" => "John Johnson",
                "line1" => "Whatever street 2",
                "line2" => "Another street 2",
                "city" => "London",
                "country_code" => "UK",
                "postal_code" => "NR30 1LY",
                "state" => "England",
                "phone" => "3123123123"
            )
        )
    ),
    "transactions" => array(
            0 => array(
            "amount" => array(
                "total" => "12.00",
                "currency" => "USD"
            ),
            "description" => "payment description",
            "item_list" => array(
                "items" => array(
                    0 => array(
                        "quantity" => "1",
                        "name" => "jacket",
                        "price" => "12.00",
                        "sku" => "dasd",
                        "currency" => "USD",
                        "description" => "blue"
                    )
                )
            )
        )
    )
);

Upvotes: 1

Related Questions