paypal - Paypal 沙盒模式下客户端身份验证失败
问题描述
我正在尝试将 Paypal 的智能按钮添加到我的网站。我按照这里的教程:
https://developer.paypal.com/docs/checkout/integrate/
无论如何,这是我payment.html
文件中的代码:
<!-- End of Body content -->
</body>
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=ILS&locale=he_IL&vault=true"></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '49.99'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
return fetch('../paypal.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
});
}
}).render('#pay');
</script>
这是我的paypal.php
文件:
<?php
namespace Sample;
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
class GetOrder
{
// 2. Set up your server to receive a call from the client
/**
*You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getOrder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
/**
*Enable the following line to print complete response as JSON.
*/
// echo json_encode($response->result);
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
// To print the whole response body, uncomment the following line
// echo json_encode($response->result, JSON_PRETTY_PRINT);
}
}
if (!count(debug_backtrace()))
{
$request_body = file_get_contents('php://input');
$json = json_decode($request_body,true);
$id=$json["orderID"];
GetOrder::getOrder($id, true);
}
这是我的paypal_loader.php
文件:
<?php
namespace Sample;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
class PayPalClient
{
/**
* Returns PayPal HTTP client instance with environment that has access
* credentials context. Use this instance to invoke PayPal APIs, provided the
* credentials have access.
*/
public static function client()
{
return new PayPalHttpClient(self::environment());
}
/**
* Set up and return PayPal PHP SDK environment with PayPal access credentials.
* This sample uses SandboxEnvironment. In production, use LiveEnvironment.
*/
public static function environment()
{
$clientId = getenv("CLIENT_ID") ?: "myclientidhere";
$clientSecret = getenv("CLIENT_SECRET") ?: "mysecrethere";
return new SandboxEnvironment($clientId, $clientSecret);
}
}
使用沙盒帐户付款后,我在 payment.html 上收到 javascript 警报(“交易完成...”)并且付款实际上已完成问题是当我尝试在服务器端验证它时我从 paypal.php 得到的是:
<br />
<b>Fatal error</b>: Uncaught BraintreeHttp\HttpException: {"error":"invalid_client","error_description":"Client Authentication failed"} in C:\xampp\htdocs\firstGear\schoolcontrol\vendor\braintree\braintreehttp\lib\BraintreeHttp\HttpClient.php:185
Stack trace:
#0 C:\xampp\htdocs\firstGear\schoolcontrol\vendor\braintree\braintreehttp\lib\BraintreeHttp\HttpClient.php(97): BraintreeHttp\HttpClient->parseResponse(Object(BraintreeHttp\Curl))
#1 C:\xampp\htdocs\firstGear\schoolcontrol\vendor\paypal\paypal-checkout-sdk\lib\PayPalCheckoutSdk\Core\AuthorizationInjector.php(37): BraintreeHttp\HttpClient->execute(Object(PayPalCheckoutSdk\Core\AccessTokenRequest))
#2 C:\xampp\htdocs\firstGear\schoolcontrol\vendor\paypal\paypal-checkout-sdk\lib\PayPalCheckoutSdk\Core\AuthorizationInjector.php(29): PayPalCheckoutSdk\Core\AuthorizationInjector->fetchAccessToken()
#3 C:\xampp\htdocs\firstGear\schoolcontrol\vendor\braintree\braintreehttp\lib\BraintreeHttp\HttpClient.php(64): PayPalCheckoutSdk\Core\AuthorizationInjector->inject(Object(PayPalChecko in <b>C:\xampp\htdocs\firstGear\schoolcontrol\vendor\braintree\braintreehttp\lib\BraintreeHttp\HttpClient.php</b> on line <b>185</b><br />
我检查了我的 client_id 和 client_secret 的三倍,现在几个小时都无法弄清楚代码中的问题出在哪里,因为它几乎是从 paypal 的网站上复制粘贴的。
非常感谢您的帮助,在此先感谢。
解决方案
推荐阅读
- spring-boot - GRPC 调用失败并出现错误“在来自服务器的 DATA 帧上收到了意外的 EOS”。
- python-3.x - 循环中的Python幂运算符:某个元素后的奇怪输出值
- linux - Centos6.9 目录中的权限被拒绝
- javascript - 将父级添加到多个
- 标签
- react-native - 请求在 react-native 中不适用于 android 9
- sql - 获取最后一个非空值?
- php - PHP函数和赋值:echo和return之间的混淆
- node.js - 使用 pngjs 从缓冲区读取图像
- java - Android canvas.save() 和 canvas.restore() 使我的应用程序崩溃
- r - 在R中读取xlsx文件