简体   繁体   中英

Sandbox Paypal Checkout (PHP, Laravel)

I'm trying to implement Paypal's checkout to my Laravel Api (connected to an Ionic app) and it gets stuck when in the app I press the button to checkout and it goes to Paypal (so far so good) in the login screen. I found it weird because it wouldn't let me login with my sandbox account or even my real account, the error is the same: "Some of your info isn't correct. Please try again." By opening developer tools, those are the errors I get (see screenshots). I really couldn't find where I'm making a mistake here. Maybe you can help me. Below are the screenshots and the code that makes takes the checkout to Paypal. Let me know if I should add any extra info here! Thanks a lot!

error 1: , investigating one of the console errors:

Route::middleware('auth:api')->post('/paypal', function (Request $request) {
    $user = $request->user();
    $data = $request->all();
    $list_products_id = $data;

    $products = [];
    $total = 0;

    $titles = '';

    foreach($list_products_id as $key => $value) {
        $product = Product::find($value);
        if($product){
            $products[$key] = $product;
            $total += $product->price;
            $titles .= $product->title." ";
        } 
    }

    if($total){

        $paypal = config('app.paypal', "sandbox");

       if($paypal == "sandbox"){
            $userProvider = 'In my app I have the sandbox business credentials here';
            $pwdProvider = 'In my app I have the sandbox business credentials here';
            $signProvider = 'In my app I have the sandbox business credentials here';
            $url = 'https://api-3t.sandbox.paypal.com/nvp';
            $url2 = 'https://www.sandbox.paypal.com/cgi-bin/webscr?%s';

       } else {
            $userProvider = '';
            $pwdProvider = '';
            $signProvider = '';   
            $url = 'https://api-3t.paypal.com/nvp';
            $url2 = 'https://www.paypal.com/cgi-bin/webscr?%s';
       }

       $data = [];
       $data['USER'] = $userProvider;
       $data['PWD'] = $pwdProvider;
       $data['SIGNATURE'] = $signProvider;
       $data['METHOD'] = 'SetExpressCheckout';
       $data['VERSION'] = '108';
       $data['LOCALECODE'] = 'en_US';

       $data['L_PAYMENTREQUEST_0_NAME0'] = "Products Orders";
       $data['L_PAYMENTREQUEST_0_DESC0'] = $titles;

       $data['PAYMENTREQUEST_0_AMT'] = number_format($total, 2).'';

       $data['PAYMENTREQUEST_0_CURRENCYCODE'] = 'EUR';
       $data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale';

       $data['L_PAYMENTREQUEST_0_QTY0'] = '1'; //number of the same product the user is ordering
       $data['L_PAYMENTREQUEST_0_AMT0'] = number_format($total, 2).'';

       $data['L_BILLINGAGREEMENTDESCRIPTION0'] = $titles;

       $data['CANCELURL'] = url('/');
       $data['RETURNURL'] = url('/');

      // curl

      $data = http_build_query($data);

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

      $response =  curl_exec($curl);
      curl_close($curl);

      $nvp = array();

      if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
          foreach ($matches['name'] as $offset => $name) {
              $nvp[$name] = urldecode($matches['value'][$offset]);
          }
      }

      if(isset($nvp['ACK']) && $nvp['ACK'] == "Success" ){
        $query = array(
                'cmd'    => '_express-checkout',
                'token'  => $nvp['TOKEN']
            );

            $redirectURL = sprintf($url2, http_build_query($query));

            return ['date'=>$redirectURL];
      }else{
        return ['status'=>'error purchasing! - 1'];

      }

    }

    echo "total: " . $total;
    return ['status'=>'error purchasing! - 2'];
});

因此我在我的沙箱帐户上重置了密码,并且可以正常工作!

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