PayPal ReturnUrl不传递参数

时间:2015-07-28 02:23:45

标签: php paypal paypal-sandbox

我正在尝试使用PayPal PHP SDK将paypal整合到我的网站中。 Eveything似乎正在工作,除非在成功或失败后,返回网址既不包括我定义的参数也不包括paypal返回的参数,例如paymentId,PayerID&令牌。这是我的php文件:

use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Exception\PayPalConnectionException;

require './paypal.php';

$payer = new Payer();
$details= new Details();
$amount= new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls=new RedirectUrls();

//payer
$payer->setPaymentMethod('paypal');

//details
$details->setShipping('2.00')
        ->setTax('0.00')
        ->setSubtotal('20.00');
 //amount
 $amount->setCurrency('USD')
        ->setTotal('22.00')
        ->setDetails($details);
 //transcation
 $transaction->setAmount($amount)
            ->setDescription('Membership');
 //payment
 $payment->setIntent('sale')
         ->setPayer($payer)
         ->setTransactions([$transaction]);

 //redirect
 $redirectUrls->setReturnUrl('http://localhost/test/add-funds.php?approved=true')
 ->setCancelUrl('http://localhost/test/add-funds.php?approved=false');

$payment->setRedirectUrls($redirectUrls);

    try{
    $payment->create($api);
    //generate and store hash
    //prepare and execute transaction storage

}catch(PayPalConnectionException  $e){
echo $e->getData();
header('Location:../test/add-funds.php');
}
$approvalUrl = $payment->getApprovalLink();

header('Location:'.$approvalUrl);

这是我的paypal.php文件

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;


$client='client';
$secret='secret';

$api= new ApiContext(new OAuthTokenCredential($client,$secret));
$api->setConfig([
'mode' => 'sandbox',
'http.ConnectionTimeOut'=> 30,
'log.LogEnabled' => false,
'log.FileName' => '',
'log.LogLevel' =>'FINE',
'validation.level'=>'log'
]);

编辑:$ payment->后创建($ api);我还检查$ payment-> getId()它是否具有正确的值。

1 个答案:

答案 0 :(得分:0)

事实证明问题出在add-funds.php中,它将用户重定向到另一个页面,并且所有参数都在此过程中丢失。我发布的代码工作正常。