Make PayPal Credit card payment by PHP Curl with json data

@mit $ingh. September 17, 2015 Comments
<?php

//open connection
$ch = curl_init();

$client="XXXXXXXXXXXXXXXXXXXXX";
$secret="XXXXXXXXXXXXXXXXXXXXX";

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $client.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);

if(empty($result))die("Error: No response.");
else
{
    $json = json_decode($result);
    print_r($json->access_token);
}

// Now doing txn after getting the token 

$ch = curl_init();

$data = '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://<return URL here>",
    "cancel_url":"http://<cancel URL here>"
  },
  "payer": {
    "payment_method": "credit_card",
    "funding_instruments": [
      {
        "credit_card": {
          "number": "5500005555555559",
          "type": "mastercard",
          "expire_month": 12,
          "expire_year": 2018,
          "cvv2": 111,
          "first_name": "Joe",
          "last_name": "Shopper"
        }
      }
    ]
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      },
      "description":"This is the payment transaction description."
    }
  ]
}
';

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: Bearer ".$json->access_token));

$result = curl_exec($ch);


if(empty($result))die("Error: No response.");
else
{
    $json = json_decode($result);
    print_r($json);
}
?>

 

Response : 

A015dVGr.q9WhQyXl-JnJnkJ.xcFkxJmHmUgCXwYqansoL0{  
   "id":"PAY-58R21143N1956774RKQ365CQ",
   "create_time":"2014-10-10T14:34:50Z",
   "update_time":"2014-10-10T14:34:57Z",
   "state":"approved",
   "intent":"sale",
   "payer":{  
      "payment_method":"credit_card",
      "funding_instruments":[  
         {  
            "credit_card":{  
               "type":"mastercard",
               "number":"xxxxxxxxxxxx5559",
               "expire_month":"12",
               "expire_year":"2018",
               "first_name":"Joe",
               "last_name":"Shopper"
            }
         }
      ]
   },
   "transactions":[  
      {  
         "amount":{  
            "total":"7.47",
            "currency":"USD",
            "details":{  
               "subtotal":"7.47"
            }
         },
         "description":"This is the payment transaction description.",
         "related_resources":[  
            {  
               "sale":{  
                  "id":"3J048328N18783546",
                  "create_time":"2014-10-10T14:34:50Z",
                  "update_time":"2014-10-10T14:34:57Z",
                  "amount":{  
                     "total":"7.47",
                     "currency":"USD"
                  },
                  "state":"completed",
                  "parent_payment":"PAY-58R21143N1956774RKQ365CQ",
                  "links":[  
                     {  
                        "href":"https://api.sandbox.paypal.com/v1/payments/sale/3J048328N18783546",
                        "rel":"self",
                        "method":"GET"
                     },
                     {  
                        "href":"https://api.sandbox.paypal.com/v1/payments/sale/3J048328N18783546/refund",
                        "rel":"refund",
                        "method":"POST"
                     },
                     {  
                        "href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-58R21143N1956774RKQ365CQ",
                        "rel":"parent_payment",
                        "method":"GET"
                     }
                  ]
               }
            }
         ]
      }
   ],
   "links":[  
      {  
         "href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-58R21143N1956774RKQ365CQ",
         "rel":"self",
         "method":"GET"
      }
   ]
}1

 

  • paypal
  • json
  • cURL