PayPal API:系统中未发现的令牌

发布于 2025-02-04 04:49:16 字数 2873 浏览 2 评论 0原文

我正在尝试将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/Payments 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;

I am trying to integrate Paypal with PHP, but right now I am getting the following message:

"The token passed in was not found in the system"

Here is my code. Where I am wrong?

Here is my code for getting the token, which I am getting successfully.

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

And here I am trying to integrate v1/payments 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-02-11 04:49:16

您使用Sandbox API客户端ID和秘密在沙盒模式下请求access_token(https://api.sandbox.paypal.com/v1/oauth2/token2/token

然后您尝试使用Sandbox Access用于实时/生产API调用的令牌(https://api.paypal.com/v1/payments/payments/payment/payment

sandbox tokens(以及所有其他标识符)仅在沙盒环境中工作,并且现场直播只有现场工作。这两个环境完全分开。


此外,v1/付款是一种弃用的API,不应用于任何新集成。使用当前 v2/chechbout/orders api api api 改为订单。


要获得付款人批准,请使用 https://developer.paypeveloper.paypleveper.paypal.com/demo/demo/demo/checkout/checkout/checkout/checkout/checkout/checkout/checkout-checkout/checkout/checkout- /#/模式/服务器。但是,您必须从PHP中删除所有回声和打印语句才能这样做。当调用创建或捕获路由时,只能输出一个JSON字符串。


晚期编辑:标准集成指南 Integration Builder 进行了扩展,以更好地覆盖JS SDK +服务器后端集成。该指南的样本后端代码在Node.js中给出(因为JavaScript已广泛理解),但是您可以从任何后端环境中实现这些服务器路由。

You used a sandbox API client ID and secret to request an access_token in sandbox mode (https://api.sandbox.paypal.com/v1/oauth2/token)

Then you attempted to use a sandbox access token for a live/production API call (https://api.paypal.com/v1/payments/payment)

Sandbox tokens (as well as all other identifiers) only work in the sandbox environment, and live ones only work in live. The two environments are completely separated.


Moreover, v1/payments is a deprecated API, and should not be used for any new integrations. Use the current v2/checkout/orders API to create and capture an order instead.


For payer approval, use https://developer.paypal.com/demo/checkout/#/pattern/server . However, you must remove all echo and print statements from your PHP to do so; only a JSON string must be outputted when your create or capture routes are called.


Late edit: The standard integration guide and integration builder were expanded to better cover a JS SDK + server backend integration. The guide's sample backend code is given in node.js (since javascript is widely understood) but you can implement those server routes from any backend environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文