从信用卡php接受PayPal付款

时间:2014-10-10 14:04:33

标签: php curl paypal

我想接受来自我的商店的付款,并且不想去PayPal我使用curl获得访问令牌,但我怎么能使用下面的代码。我认为这是我应该用于付款的代码。

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {accessToken}' \
-d '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://<return URL here>",
    "cancel_url":"http://<cancel URL here>"
  },
  "payer":{
    "payment_method":"creditcard"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      },
      "description":"This is the payment transaction description."
    }
  ]
}'

我只需要一个技巧就可以在curl中使用它并通过向paypal发送数据并在json中接收付款

1 个答案:

答案 0 :(得分:0)

您可以使用以下php代码进行信用卡付款:

<?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);
}


?>

回应:

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