Paypal Sandbox代码400返回null

So I am messing around with the paypal sandbox but for some reason it doesnt want to return me an array for a purchase. Instead it just returns null. I can retrieve the token through the api using curl , but processing a test card still returns null. Anyone have this problem before?

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

    require 'src/start.php';

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

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

    //Details
    //acutal prices(20 dollars is a test)

    //Amount

    $amount->setCurrency('GBP')
      ->setTotal('22.00')
      ->setDetails($details);

    $details->setShipping('2.00')
    ->setTax('0.00')
    ->setSubtotal('20.00');
    //Transaction
    $transaction->setAmount($amount);
      // ->setDescription('Membership');

    //Payment
    $payment->setIntent('sale')
      ->setPayer($payer)
      ->setTransactions([$transaction]);

    //RedirectUrls
    $redirectUrls->setReturnUrl('http://localhost:8000/paypaletc/pay.php?approved=true');
      // ->setCancelUrl('http://localhost:8000/paypal/pay.php?approved=false');

    $payment->setRedirectUrls($RedirectUrls);


    try{

        $payment->create($api);


    } catch(PayPal\Exception\PayPalConnectionException $e){
      echo $e->getMessage();

      // header('Location: paypaletc/error.php');
    }
    var_dump($payment->getLinks());

My start.php:

<?php

    use PayPal\Rest\ApiContext;
    use PayPal\Auth\OAuthTokenCredential;
    // session_start();

    // $_SESSION['user_id'] = 1;

    // $db = newPDO('mysql:host=localhost;dbname-site', 'homestead', 'secret');

    // $user = $db->prepare("
    //   SELECT * FROM users
    //   WHERE id = :user_id
    //   ");

    // $user-execute(['user_id' => $_SESSION['user_id']]);

    // $user = $user->fetchObject();


    require __DIR__ . '/../vendor/autoload.php';

    // API CREDS

    $api = new ApiContext(
    new OAuthTokenCredential(
      "AfhmDCIi9Gc6cTuMv8Y4wVENTycXAkLfCWJ37v2uvQ0FJO7nqoL_JssmUaYAFBMtQD85xOfk1mHMtMb_",
      "EABb--WImsBgy3Ck_MmsVD-uVa--plWBwxqzaLFt33_DDRz7cV5GkNQbfB_ZH-1z1a9NU4ictM98erdJ"
      )
    );

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

What it returns:

Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.NULL

The culprit is the $redirectUrls. In your setRedirectUrls($RedirectUrls), see that capital R in the $RedirectUrls, that is what is causing the exception.

        $redirectUrls->setReturnUrl('http://localhost:8000/paypaletc/pay.php?approved=true')
     ->setCancelUrl('http://localhost:8000/paypal/pay.php?approved=false');

        $payment->setRedirectUrls($redirectUrls);

This should fix your code. Also, instead of printing $e->getMessage();, try using $e->getData();

Here is how your final code should look like:

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

require 'src/start.php';

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

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

//Details
//acutal prices(20 dollars is a test)

//Amount

$amount->setCurrency('GBP')
    ->setTotal('22.00')
    ->setDetails($details);

$details->setShipping('2.00')
    ->setTax('0.00')
    ->setSubtotal('20.00');
//Transaction
$transaction->setAmount($amount);
// ->setDescription('Membership');

//Payment
$payment->setIntent('sale')
    ->setPayer($payer)
    ->setTransactions([$transaction]);

//RedirectUrls
$redirectUrls->setReturnUrl('http://localhost:8000/paypaletc/pay.php?approved=true')
    ->setCancelUrl('http://localhost:8000/paypal/pay.php?approved=false');

$payment->setRedirectUrls($redirectUrls);


try{

    $payment->create($api);


} catch(PayPal\Exception\PayPalConnectionException $e){
    echo $e->getData();

    // header('Location: paypaletc/error.php');
}
var_dump($payment->getLinks());