doumu8217 2015-12-11 17:50
浏览 439

Paypal使用php curl通过pay键获取交易详情

I get response after successful android paypal payment. The response was below.

{
    "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"
 }

then I use the PAY-6PU626847B294842SKPEWXHY pay key and get the transaction details using PHP curl GET request with the following code.

$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;

After hit this i get the following response not get the transaction detail.

{"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"}

anybody have idea thanks in advance.

  • 写回答

1条回答 默认 最新

  • douyue5856 2019-05-23 20:32
    关注

    The following works for me.

    1. MAKE CURL POST TO GET ACCESS TOKEN

    $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. MAKE CURL POST TO GET TRANSACTION DETAILS LIKE payment status,amount, date etc.

    $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. MAKE CURL POST TO GET MORE TRANSACTION DETAILS (This give more detailed response than step 2. so you can us this instead of step 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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办