PayPal API:系统中找不到传入的令牌

问题描述 投票:0回答:1

我正在尝试将 Paypal 与 PHP 集成,但现在我收到以下消息:

“系统中未找到传入的令牌”

这是我的代码。我哪里错了?

这是我获取令牌的代码,我已经成功获取了。

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,
    CURLOPT_POSTFIELDS => "grant_type=client_credentials",
    CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Accept-Language: en_US"
    ),
));
$result= curl_exec($curl);
$array=json_decode($result, true); 
$token=$array['access_token'];

我在这里尝试集成 v1/付款 API:

$ch = curl_init();
$data = '{
"intent": "sale",
    "payer": {
    "payment_method": "credit_card",
    "payer_info": {
    "email": "'.$email.'",
    "shipping_address": {
    "recipient_name": "'.$fname.' '.$lname.'",
    "line1": "'.$address.'",
    "city": "'.$city.'",
    "country_code": "'.$country.'",
    "postal_code": "'.$zip.'",
    "state": "'.$state.'",
    "phone": "'.$phone.'"
},
"billing_address": {
"line1": "'.$address.'",
"city": "'.$city.'",
"state": "'.$state.'",
"postal_code": "'.$zip.'",
"country_code": "'.$country.'",
"phone": "'.$phone.'"
}
},
"funding_instruments": [{
"credit_card": {
"number": "'. $ccnum.'",
"type": "'.$credit_card_type.'",
"expire_month": "'.$ccmo.'",
"expire_year": "'.$ccyr.'",
"cvv2": "'.$cvv2_number.'",
"first_name": "'.$first_name.'",
"last_name": "'.$last_name.'",
"billing_address": {
"line1": "'.$address.'",
"city": "'.$city.'",
"country_code": "'.$country.'",
"postal_code": "'.$zip.'",
"state": "'.$state.'",
"phone": "'.$phone.'"
            }
        }
    }]
},
"transactions": [{
"amount": {
"total": "'.$cost.'",
"currency": "GBP"
},
"description": "This is member subscription payment at Thecodehelpers.",
"item_list": {
"shipping_address": {
"recipient_name": "'.$fname.' '.$lname.'",
"line1": "'.$address.'",
"city": "'.$city.'",
"country_code": "'.$country.'",
"postal_code": "'.$zip.'",
"state": "'.$state.'",
"phone": "'.$phone.'"
}
}
}]
}';

//curl_setopt($ch, CURLOPT_URL, $PAYPAL_API_URL."v1/payments/payment");
curl_setopt($ch, CURLOPT_URL, "https://api.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 ".$token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//curl_close($ch);
$json = json_decode($result);

//$state=$json->state;
echo "<pre>";
print_r($json);
exit;
php paypal
1个回答
4
投票

您使用沙盒 API 客户端 ID 和密钥在沙盒模式下请求 access_token (

https://api.sandbox.paypal.com/v1/oauth2/token
)

然后您尝试使用沙箱访问令牌进行实时/生产 API 调用 (

https://api.paypal.com/v1/payments/payment
)

沙盒令牌(以及所有其他标识符)仅在沙盒环境中工作,而实时令牌仅在实时环境中工作。两个环境完全隔离。


此外,v1/ payment 是一个已弃用的 API,不应该用于任何新的集成。请使用当前的 v2/checkout/orders API 来创建和捕获订单。


对于付款人批准,请使用 https://developer.paypal.com/demo/checkout/#/pattern/server 。但是,您必须从 PHP 中删除所有 echo 和 print 语句才能执行此操作;当调用创建或捕获路由时,只需输出 JSON 字符串。


后期编辑:扩展了标准集成指南集成构建器,以更好地涵盖JS SDK +服务器后端集成。本指南的示例后端代码在 node.js 中给出(因为 javascript 已被广泛理解),但您可以从任何后端环境实现这些服务器路由。

© www.soinside.com 2019 - 2024. All rights reserved.