我正在尝试使用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-> create($ api)之后; 我还检查$ payment-> getId()它是否具有正确的值。
I'm trying to use PayPal PHP SDK to integerate paypal into my website. Eveything seems to be working except after Success or Fail the return url doesn't include neither the parameters defined by me nor ones returned by paypal such as paymentId, PayerID & token. Here is my php file:
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);this is my paypal.php file
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' ]);Edit: After $payment->create($api); I also check $payment->getId() it has the proper value.
最满意答案
事实证明问题出在add-funds.php中,它将用户重定向到另一个页面,并且所有参数都在此过程中丢失。 我发布的代码工作正常。
It turned out the problem was in add-funds.php which was redirecting user to another page and all parameters were getting lost in the process. The code I posted works properly.
更多推荐
发布评论