简体   繁体   中英

PAYPAL PHP SDK doesn't redirect?

I'm trying to do a test transaction with PayPal's PHP SDK, I installed it with 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)

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:

[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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