Paypal 使用 php curl 使用支付密钥获取交易详情

Posted

技术标签:

【中文标题】Paypal 使用 php curl 使用支付密钥获取交易详情【英文标题】:Paypal get transaction details with pay key using php curl 【发布时间】:2016-03-17 17:20:05 【问题描述】:

我在成功支付 android paypal 后得到响应。回复如下。


    "response": 
       "state": "approved",
       "id": "PAY-6PU626847B294842SKPEWXHY",
 "create_time": "2014-07-18T18:46:55Z",
        "intent": "sale"
     ,
   "client": 
       "platform": "Android",
       "paypal_sdk_version": "2.11.0",
         "product_name": "PayPal-Android-SDK",
        "environment": "mock"
    ,
    "response_type": "payment"
 
 
   "short_description": "Pay List Payment",
   "amount": "100",
    "intent": "sale",
    "currency_code": "USD"
 

然后我使用 PAY-6PU626847B294842SKPEWXHY 支付密钥并使用 php curl GET 请求和以下代码获取交易详细信息。

$accessToken='<Access Token>';
$curl = curl_init("https://api.sandbox.paypal.com/v1/payments/payment/PAY-6PU626847B294842SKPEWXHY");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $accessToken,
    'Accept: application/json',
    'Content-Type: application/json'
));
$response = curl_exec($curl);

print_r($response);
exit;

点击此按钮后,我得到以下响应,但未获取交易详细信息。

"name":"INVALID_RESOURCE_ID","message":"The requested resource ID was not found","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID","debug_id":"ddc1e3a55f10e"

任何人有想法提前谢谢。

【问题讨论】:

在上述问题之后你得到了什么?你有没有解决办法?我也坚持同样的反应。 【参考方案1】:

以下对我有用。

1.制作 CURL POST 以获取访问令牌

$uri = 'https://api.sandbox.paypal.com/v1/oauth2/token';
    //for live production use $uri = 'https://api.paypal.com/v1/oauth2/token';

$clientId = '<YOUR-CLIENT-ID-HERE>';
$secret = '<YOUR-SECRET-HERE>';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);
$access_token = '';
if(empty($result))die("Error: No response.");
else

    $json = json_decode($result);
    $access_token = $json->access_token;


curl_close($ch);

2。制作 CURL POST 以获取交易详情,如付款状态、金额、日期等。

$url = "https://api.sandbox.paypal.com/v2/payments/captures/<YOUR-PAYMENT-ID-HERE>";
$accessToken=$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $accessToken,
    'Accept: application/json',
    'Content-Type: application/json'
));
$response = curl_exec($curl);

print_r($response);
exit;

3.制作 CURL POST 以获取更多交易详情(这会提供比第 2 步更详细的响应。因此您可以使用此方法而不是第 2 步

$url = "https://api.sandbox.paypal.com/v2/checkout/orders/<YOUR-order-ID-HERE>";
$accessToken=$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $accessToken,
    'Accept: application/json',
    'Content-Type: application/json'
));
$response = curl_exec($curl);

print_r($response);
exit;

【讨论】:

以上是关于Paypal 使用 php curl 使用支付密钥获取交易详情的主要内容,如果未能解决你的问题,请参考以下文章

使用 Curl (SinglePayment) 验证移动支付

PayPal REST API - 使用 PHP 和 curl 进入实时模式

Paypal 链式支付 - 支付密钥和 IPN

从 cURL 支付向 paypal IPN 添加自定义变量

Paypal支付的退款refund之getAccessToken

需要从支付密钥 PayPal 获得交易 ID