如何使用 PayPal Payouts PHP 进行支付
Posted
技术标签:
【中文标题】如何使用 PayPal Payouts PHP 进行支付【英文标题】:How to make a payout with PayPal Payouts PHP 【发布时间】:2020-10-13 18:11:41 【问题描述】:我正在学习 php,我想用 Paypal 付款。我试图运行代码示例 (https://github.com/paypal/Payouts-PHP-SDK),但我无法让它工作。如果有人可以帮助我,我将不胜感激。这是我的代码:
require 'vendor/autoload.php';
use Sample\PayPalClient;
use PaypalPayoutsSDK\Core\PayPalHttpClient;
use PaypalPayoutsSDK\Core\SandboxEnvironment;
// Creating an environment
$clientId = "******************";
$clientSecret = "***************";
$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
use PaypalPayoutsSDK\Payouts\PayoutsPostRequest;
$request = new PayoutsPostRequest();
$body= json_decode(
'
"sender_batch_header":
"email_subject": "SDK payouts test txn"
,
"items": [
"recipient_type": "EMAIL",
"receiver": "payouts2342@paypal.com",
"note": "Your 1$ payout",
"sender_item_id": "Test_txn_12",
"amount":
"currency": "USD",
"value": "1.00"
]
',
true);
$request->body = $body;
$client = PayPalClient::client();
$response = $client->execute($request);
echo 'resuelto<br>';
print "Status Code: $response->statusCode\n";
print "Status: $response->result->batch_header->batch_status\n";
print "Batch ID: $response->result->batch_header->payout_batch_id\n";
print "Links:\n";
foreach($response->result->links as $link)
print "\t$link->rel: $link->href\tCall Type: $link->method\n";
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
这是我运行它时的消息:
致命错误:未捕获的 PayPalHttp\HttpException: "error":"invalid_client","error_description":"客户端认证 失败” /var/www/html/paypal/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php:215
堆栈跟踪:#0 /var/www/html/paypal/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php(100): PayPalHttp\HttpClient->parseResponse() #1 /var/www/html/paypal/vendor/paypal/paypal-payouts-sdk/lib/PaypalPayoutsSDK/Core/AuthorizationInjector.php(37): PayPalHttp\HttpClient->execute() #2 /var/www/html/paypal/vendor/paypal/paypal-payouts-sdk/lib/PaypalPayoutsSDK/Core/AuthorizationInjector.php(29): PaypalPayoutsSDK\Core\AuthorizationInjector->fetchAccessToken() #3 /var/www/html/paypal/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php(64): PaypalPayoutsSDK\Core\AuthorizationInjector->inject() #4 /var/www/html/paypal/index.php(48): PayPalHttp\HttpClient->execute() #5 main 在 /var/www/html/paypal/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php 中抛出 在第 215 行
【问题讨论】:
您对此有何疑问?你读过错误信息吗?如果是:您尝试过什么来解决错误? 检查你的代码,你在第 10 行定义了一个 $client,然后在第 41 行重新定义了 【参考方案1】:您似乎没有输入 PayPal 客户端 ID 和密码。 首先,您必须注册 PayPal 开发人员(如果您已经有 PayPal 帐户,则使用您的 PayPal 帐户访问):https://developer.paypal.com/developer/applications/
然后创建一个应用程序并授予支付权限。
然后在您的应用程序中,您会找到客户 ID 和密码。
最后,您只需将它们复制并粘贴到您的 PHP 代码中,在这两行中:
$clientId = "******************";
$clientSecret = "***************";
例如:
$clientId = "123456";
$clientSecret = "aAbBcC";
【讨论】:
以上是关于如何使用 PayPal Payouts PHP 进行支付的主要内容,如果未能解决你的问题,请参考以下文章
在测试模式下使用 payouts_enabled 创建连接用户?