php - Paypal PHP SDK 付款状态
问题描述
我正在使用贝宝 php sdk。我有以下代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . '/vendor/PayPal-PHP-SDK/autoload.php';
$dev_ClientID = 'xxxxxxxxxx';
$dev_ClientSecret = 'xxxxxxxxxx';
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
$dev_ClientID
,$dev_ClientSecret
)
);
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal('1.00');
$amount->setCurrency('USD');
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://www.somedomain.com/paypal_success.php")
->setCancelUrl("https://www.somedomain.com/paypal_cancel.php");
// Create the WebProfile
$presentation = new \PayPal\Api\Presentation();
$presentation->setLogoImage("http://www.yeowza.com/favico.ico")
->setBrandName("YeowZa! Paypal")
->setLocaleCode("US");
$inputfields = new \PayPal\Api\InputFields();
$inputfields ->getNoShipping(1);
$webProfile = new \PayPal\Api\WebProfile();
$webProfile
->setName('somename' . uniqid())
->setInputFields($inputfields)
->setTemporary(true);
$webProfileId = $webProfile->create($apiContext)->getId();
$payment = new \PayPal\Api\Payment();
$payment->setExperienceProfileId($webProfileId);
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
try {
$payment->create($apiContext);
echo "\n\nRedirect user to approval_url: " . $payment->getApprovalLink() . "\n";
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
// This will print the detailed information on the exception.
echo $ex->getData();
}
?>
我得到了付款链接,但我仍然看到运输信息。
我已将 getNoShipping 定义为 true,可能挂钩的网络配置文件错误。所以这是一回事。我不确定的第二件事是这个。
我通过了付款并被重定向回 paypal_success.php,并带有 paymentId、token 和 PayerID。我将如何使用这些来接收有关交易的信息。我希望确保它确实发生了。
我尝试了以下方法:
$paymentId = $_GET["paymentId"];
$token = $_GET["token"];
$PayerID = $_GET["PayerID"];
echo "paymentId: " . $paymentId . "<br>";
echo "token: " .$token . "<br>";
echo "PayerID: " .$PayerID . "<br>";
$curl = curl_init("https://api.sandbox.paypal.com/v1/checkout/orders/$paymentId");
curl_setopt($curl, CURLOPT_POST, false);
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'
));
$response = curl_exec($curl);
$result = json_decode($response);
var_dump($result);
结果如下:
{ ["error"]=> string(13) "invalid_token" ["error_description"]=> string(35) "令牌签名验证失败" }
将感谢提供的任何帮助。谢谢
解决方案
您可以使用以下电话获取付款详情
$payment_details = PayPal\Api\Payment::get($paymentId, $apiContext);
这$apiContext
是您用于进行交易的 api 凭据。
此外,如果您想使用 curl,则以下端点
$curl = curl_init("https://api.sandbox.paypal.com/v1/checkout/orders/$paymentId");
无法正确获取付款详细信息,您应该使用此端点$curl = curl_init("https://api.sandbox.paypal.com/v1/payments/payment/$paymentId");
推荐阅读
- javascript - 如何在传单中获取 cql_filtered wms 层的 bbox?
- wordpress - Laravel Forge 数据库未与 Sequel Pro 连接
- r - 如何根据多个条件滞后日期并在滞后发生后重置滞后
- linux - 从 Fedora Linux 主机到 Windows 目标的交叉编译 Rust 找不到依赖项
- angular - 材料小吃店可观测数据是否完整?
- r - 动态创建数据框
- python - 如何在 python 中使用 getter/setter 方法来操作列表?
- swift - 如何在 Swift Xcode 中编码 NSSet?
- java - 带有代理的 Java 11 HttpClient,标头解析器未收到任何字节
- python - 如何避免 ValueError 解包元组?