PAYPAL PHP SDK不重定向?

时间:2017-03-15 14:59:57

标签: php paypal paypal-rest-sdk

我正在尝试使用PayPal的PHP SDK进行测试交易,我用composer安装它。问题是当脚本完成后,我没有被重定向到PayPal网关,当我使用带有$ approvalurl的头功能时,我被重定向到PayPal登录但是数据没有被提交(我最终在我的卖家帐户的PayPal登录网站,但没有传输价格或产品信息)

知道什么是错的吗?这就是我的脚本:

<?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:

[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

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。我错过了创建itemitemlist