duandazhen7306 2015-03-13 20:54
浏览 39
已采纳

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

  • 写回答

1条回答 默认 最新

  • doudinghan8319 2015-03-13 23:57
    关注

    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());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线