Paypal PHP SDK付款状态

时间:2019-03-16 15:44:08

标签: php paypal paypal-sandbox payment

我正在使用Paypal 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,可能是将Web配置文件挂错了。这就是一件事。我不确定的第二件事就是这个。

我通过了一笔付款,并使用PaymentId,令牌和PayerID重定向回了paypal_success.php。我将如何使用这些接收有关交易的信息。我希望确保它确实发生了。

我尝试了以下操作:

$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”] =>字符串(13)“ invalid_token” [“ error_description”] =>字符串(35)“令牌签名验证失败”}

将感谢您提供的任何帮助。 谢谢

1 个答案:

答案 0 :(得分:1)

您可以通过以下电话获取付款明细

$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");