沙盒Paypal结帐(PHP,Laravel)

时间:2019-05-02 18:03:07

标签: php laravel paypal

我正在尝试对我的Laravel Api(连接到Ionic应用程序)实施Paypal的结帐,当我在应用程序中按下按钮结帐时,它卡住了,并且登录时转到Paypal(到目前为止非常好)屏幕。我发现它很奇怪,因为它不允许我使用沙箱帐户甚至我的真实帐户登录,错误是相同的:“您的某些信息不正确。请重试。”通过打开开发人员工具,这些就是我得到的错误(请参见屏幕截图)。我真的找不到在这里犯错的地方。也许你可以帮我。以下是屏幕截图和使结帐到Paypal的代码。让我知道是否应在此处添加任何其他信息!非常感谢!

错误1:, 正在调查控制台错误之一:

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'];
});

1 个答案:

答案 0 :(得分:1)

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