PayPal支付API REQUIRED_SCOPE_MISSING

时间:2018-03-26 03:25:37

标签: php api curl paypal paypal-sandbox

我正在尝试使用CURL实现付款请求。但是我收到了这个错误:" name":" REQUIRED_SCOPE_MISSING"," message":"访问令牌没有必需的范围。" " information_link":" https://developer.paypal.com/docs/api/payments.payouts-batch/#errors"

我已经在开发者信息中心检查了付款范围的应用内设置。

这是代码:

$host = 'https://api.sandbox.paypal.com';
$clientId = 'id';
$secret  = "secret";
$token = '';

function get_access_token($url, $postdata) {
 global $clientId, $clientSecret;
 $curl = curl_init($url); 
 curl_setopt($curl, CURLOPT_POST, true); 
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
 curl_setopt($curl, CURLOPT_HEADER, false); 
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
 $response = curl_exec( $curl );
 if (empty($response)) {
     die(curl_error($curl));
     curl_close($curl);
 } else {
     $info = curl_getinfo($curl);
     echo "Time took: " . $info['total_time']*1000 . "ms\n";
     curl_close($curl);
     if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
        echo "Received error: " . $info['http_code']. "\n";
        echo "Raw response:".$response."\n";
        die();
     }
 }
 $jsonResponse = json_decode( $response );
 return $jsonResponse->access_token;
}

function make_post_call($url, $postdata) {
 global $token;
 $curl = curl_init($url); 
 curl_setopt($curl, CURLOPT_POST, true);
 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 '.$token,
    'Accept: application/json',
    'Content-Type: application/json'
    ));

 curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
 $response = curl_exec( $curl );
 if (empty($response)) {
     die(curl_error($curl));
     curl_close($curl);
 } else {
     $info = curl_getinfo($curl);
     echo "Time took: " . $info['total_time']*1000 . "ms\n";
     curl_close($curl); // close cURL handler
     if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
       echo "Received error: " . $info['http_code']. "\n";
       echo "Raw response:".$response."\n";
       die();
     }
 }
 $jsonResponse = json_decode($response, TRUE);
 print_r($jsonResponse); die;
 return $jsonResponse;
}

echo "\n";
echo "###########################################\n";
echo "Obtaining OAuth2 Access Token.... \n";

$url = $host.'/v1/oauth2/token'; 
$postArgs = 'grant_type=client_credentials';
$token = get_access_token($url,$postArgs);

echo "Got OAuth Token: ".$token;
echo "\n \n";
echo "###########################################\n";
echo "Initiating a Payment with PayPal Account... \n";

$url = $host.'/v1/payments/payouts';
$member_email = "test@test.com";

$data = array(
  "sender_batch_header" => array(
    "sender_batch_id" => '2542'.time(),
    'email_subject' => 'Withdraw',
    'email_message' => 'You have withdraw from asfd! Thanks for using our service!'
  ),
  "items" => array(
    array(
      "recipient_type" => 'EMAIL',
      'amount' => array(
        'value' => 10.99,
        'currency' => 'USD',
      ),
      'note' => 'Thank You',
      'sender_item_id' => 'A25426',
      'receiver' => $member_email,
    ),
  ),
);

$data = json_encode($data);

$json_resp = make_post_call($url, $data);

foreach ($json_resp['links'] as $link) {
 if($link['rel'] == 'execute'){
  $payment_execute_url = $link['href'];
  $payment_execute_method = $link['method'];
 } else  if($link['rel'] == 'approval_url'){
   $payment_approval_url = $link['href'];
   $payment_approval_method = $link['method'];
  }
}
echo "Payment Created successfully: " . $json_resp['id'] ." with state '". $json_resp['state']."'\n\n";
echo "Please goto <a href='".$payment_approval_url."'>link</a> in your browser and approve the payment with a PayPal Account.\n";

请帮忙

谢谢

0 个答案:

没有答案