简体   繁体   English

PAYPAL PHP SDK不重定向?

[英]PAYPAL PHP SDK doesn't redirect?

I'm trying to do a test transaction with PayPal's PHP SDK, I installed it with composer. 我正在尝试使用PayPal的PHP SDK进行测试交易,并通过composer安装了它。 The problem is when the script is done, I don't get redirected to the PayPal gateway and when I use the header function with $approvalurl I get redirected to the PayPal login but the data doesn't get submitted (I end up being on the PayPal login site of my seller account but no price or product info is transmitted there) 问题是脚本执行完后,我没有重定向到PayPal网关,当我将标头函数与$ approvalurl一起使用时,我重定向到了PayPal登录名,但是数据没有提交(我最终在我卖家帐户的PayPal登录网站,但没有传输任何价格或产品信息)

Any idea what's wrong? 知道有什么问题吗? This is what my script looks like: 这是我的脚本的样子:

<?php 

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
// Autoload SDK package for composer based installations
require 'vendor/autoload.php';

$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
       'api_key',
       'api_secret'
    )
);

$apiContext->setConfig(
    array(
       'log.LogEnabled' => true,
       'log.FileName' => 'PayPal.log',
       'log.LogLevel' => 'DEBUG'
    )
);

use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;

// Create new payer and method
$payer = new Payer();
$payer->setPaymentMethod("paypal");

// Set redirect urls
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('http://localhost:3000/process.php')
->setCancelUrl('http://localhost:3000/cancel.php');

// Set payment amount
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(10);

// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
->setDescription("Payment description");

// Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));

// Create payment with valid API context
try {
    $payment->create($apiContext);
    //echo "Payment Id is " . $payment->getId();
    // Get PayPal redirect URL and redirect user
    $approvalUrl = $payment->getApprovalLink();

    // REDIRECT USER TO $approvalUrl
    header("Location: $approvalUrl");
} catch (PayPal\Exception\PayPalConnectionException $ex) {
    echo 'Exception abgefangen: ', $ex->getData(), "\n";
    echo $ex->getCode();
    echo $ex->getData();
    die($ex);
} catch (Exception $ex) {
    die($ex);
}

$approvalUrl = $payment->getApprovalLink();

return $payment;
?>

paypal.log: paypal.log:

[14-03-2017 01:33:23] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/oauth2/token
[14-03-2017 01:33:26] PayPal\Core\PayPalHttpConnection : INFO: Response Status : 200
[14-03-2017 01:33:26] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/payments/payment
[14-03-2017 01:33:27] PayPal\Core\PayPalHttpConnection : INFO: Response Status : 201
[14-03-2017 01:33:59] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/oauth2/token
[14-03-2017 01:34:02] PayPal\Core\PayPalHttpConnection : INFO: Response Status : 200
[14-03-2017 01:34:02] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/payments/payment
[14-03-2017 01:34:04] PayPal\Core\PayPalHttpConnection : INFO: Response Status : 201

I found the issue. 我发现了问题。 I missed to create a item and itemlist 我错过了创建一个itemitem itemlist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM