user2134
user2134

Reputation: 165

paypal MALFORMED REQUEST error in curl

I am facing MALFORMED_REQUEST in paypal API

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"intent\":\"authorize\",
\"payer\":{
    \"payment_method\":\"credit_card\",
    \"funding_instruments\":[{
        \"credit_card\":{
            \"number\":\"5555555555554444\",
            \"type\":\"mastercard\",
            \"expire_month\":07,
            \"expire_year\":22,
            \"cvv2\":123,
            \"first_name\":\"FName\",
            \"last_name\":\"Lname\",
            \"billing_address\":{
                \"line1\":\"address,\",
                \"city\":\"City\",
                \"state\":\"state\",
                \"postal_code\":\"postal_code\",
                \"country_code\":\"country_code\"
            }
        }
    }]
},
\"transactions\":[{
    \"amount\":{
        \"total\":\"10\",
        \"currency\":\"CAD\",
        \"details\":{
            \"subtotal\":\"10\",
            \"tax\":\"0\",
            \"shipping\":\"0\"
        }
    }
}]}");

curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer myAccessToken";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

Here I am calling curl for authorization with paypal

jsonlint.com shows this json format is ok

But still I am getting response like,

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"9d7454a8637ae"}

Not getting exactly where I am wrong? Does any one know? Thanks in advance!

Upvotes: 1

Views: 2768

Answers (2)

Phil
Phil

Reputation: 165058

Your problem is the 07 in "expire_month": 07. I assume this value is meant to be a string. Not sure what you pasted in to jsonlint but it wasn't the JSON in your question.

As indicated in the comments, don't roll your own JSON. Use the tools available

$data = [
    'intent' => 'authorize',
    'payer' => [
        'payment_method' => 'credit_card',
        'funding_instruments' => [[
            'credit_card' => [
                'number' => '5555555555554444',
                'type' => 'mastercard',
                'expire_month' => '07',
                'expire_year' => '22',
                'hopefully you get the idea' => 'by now'
            ]
        ]]
    ]
];

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Upvotes: 3

Vimalnath
Vimalnath

Reputation: 6463

As the error indicates, your JSON is not formed correctly.You need not pass \ to escape your request parameters . Try removing those or try this sample code which works out of the box.

<?php
$data = 'response_type=token&grant_type=client_credentials';
$headers_arr = array();
$headers_arr[]="Accept-Encoding:application/json;charset=utf-8";
$headers_arr[]="Accept-Language:en_US";
// $headers_arr[]="Authorization:Bearer AZHFuBBAIG1rP98P7Svn2WOVatM5KAllu6KvrTE8GGT4gt9vdfj8TtR_1Cer:EBTMERCurtbf_4auykCeGWYGvwsy147bSb3xCuYpohmouVBGTaYFUIRwWgbx";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// this is used to bypass the SSL protocol, not recommended.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// this is used to bypass the SSL protocol, not recommended.
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "ARLg-BBMeTGLU5IHCsdIKEC_R_sMAkWM-yLsI5W5u9RuhvNhgolhYv-1cWmr:EJdAYxDx29McgMKOWjGK1OLHyaIxQBRuV9sWgfFSeMKGVBe-1jdiNgv5TLtP");
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_arr);

$result = curl_exec($ch);
$res = json_decode($result);
$access_token = $res->access_token;

//now create a paypal payment
$headers_arr = array();
$headers_arr[]="Content-Type:application/json;charset=utf-8";
$headers_arr[] = "Authorization: Bearer $access_token";
// echo '<pre>';
// print_r($headers_arr);
$data_string = '{
  "intent":"authorize",
  "payer":{
    "payment_method":"credit_card",
    "funding_instruments":[
      {
        "credit_card":{
          "number":"4446283285273500",
          "type":"visa",
          "expire_month":11,
          "expire_year":2018,
          "cvv2":"874",
          "first_name":"Betsy",
          "last_name":"Buyer",
          "billing_address":{
            "line1":"111 First Street",
            "city":"Saratoga",
            "state":"CA",
            "postal_code":"95070",
            "country_code":"US"
          }
        }
      }
    ]
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD",
        "details":{
          "subtotal":"7.41",
          "tax":"0.03",
          "shipping":"0.03"
        }
      },
      "description":"This is the payment transaction description."
    }
  ]
}';
// echo data_string;
$chs = curl_init();
curl_setopt($chs, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/payments/payment');
curl_setopt($chs, CURLOPT_POST, true);
curl_setopt($chs, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($chs, CURLOPT_TIMEOUT, 45);
curl_setopt($chs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chs, CURLOPT_POST, 1);

curl_setopt($chs, CURLOPT_HTTPHEADER, $headers_arr);

if(curl_exec($chs) === false)
{
    echo 'Curl error: ' . curl_error($chs);
}

$results = curl_exec($chs);
$res = json_decode($results);
echo 'ress=<pre>';
print_r($results);

Upvotes: 0

Related Questions