2016-05-02 10 views
0

私はlaravel 5.2で初めてpaypalを統合しています。私はapiとしてPayPal SDKを使用していますが、私は固まっているところに達しています。支払いフォームを提出すると、私は次のエラーが発生します。laravelへのPaypal Rest API SDKの統合5.2

"PayPalHttpConnection.php行のPayPalConnectionException 1732: https://api.sandbox.paypal.com/v1/payments/paymentにアクセスすると、HTTP応答コード400が表示されます。

は私がthis websiteからチュートリアルを持って、ここで私のコントローラ

​​

からのコードである私は、PayPalのウェブサイトにredirecitingときに問題が来ると思いますが、私は間違って起こっている正確に何を把握することはできません。

+0

'Session :: put( 'paypal_payment_id'、$ payment-> getId());'の前に 'dd($ payment-> getId());'を実行し、その応答を確認してください。 – Abbasi

+0

もう一つは、すべてのデータをpaypalに提供することです。そうでなければ、同じ例外がスローされます。 – Abbasi

+0

こんにちはムハンマドは返事をありがとう。私はPayPalHttpConnection.phpの176: https://api.sandbox.paypal.com/v1/payments/paymentにアクセスすると、HTTP応答コード400が表示されます。 。私はpaypalにすべてのデータを提供しているかどうかを確認する方法を確認することができます。 –

答えて

2

私もこの問題に直面しました。私のケースでは、実際には偽のデータをpaypalに送っていました。最初のステップで

は、例外をキャッチし、actualyにErrorMessage

// For testing purpose use the general exception (failed to catch with paypal for me) 
catch (Exception $ex) { 
    if (\Config::get('app.debug')) { 
    echo "Exception: " . $ex->getMessage() . PHP_EOL; 
    $err_data = json_decode($ex->getData(), true); 
    exit; 
    } else { 
    die('Some error occur, sorry for inconvenient'); 
    } 
} 

結果のメッセージはあなたが間違って得たものを解決するのに十分な情報を与える必要がありますを取得しよう。

以下私はpaypal REST APIを使用して私のために働いている自分のコードを貼り付けます。あなたは(ペイパルのハンドルキャンセルを)キャンセル/

  • /支払い/成功(支払いを検証&がペイパルからのリダイレクトに成功した)
  • /支払(支払の作成)を作成/支払い/
    • 3ルートが必要になります

    また、ペイパル設定を追加し、コントローラで初期化する必要があります。 paypal設定ファイルがない場合は、クライアントIDとシークレットを直接関数に設定することができます。設定は、私はすべてを持って期待しルート

    // create a payment 
        public function create(Request $request) 
        { 
         $payer = new Payer(); 
         $payer->setPaymentMethod('paypal'); 
    
         $price = '10.00'; // 10 € for example 
    
         if($price == 0) { // ensure a price above 0 
          return Redirect::to('/'); 
         } 
    
         // Set Item 
         $item_1 = new Item(); 
         $item_1->setName('My Item') 
          ->setCurrency('EUR') 
          ->setQuantity(1) 
          ->setPrice($price); 
    
         // add item to list 
         $item_list = new ItemList(); 
         $item_list->setItems(array($item_1)); 
    
         $amount = new Amount(); 
         $amount->setCurrency('EUR') 
          ->setTotal($price); // price of all items together 
    
         $transaction = new Transaction(); 
         $transaction->setAmount($amount) 
          ->setItemList($item_list) 
          ->setDescription('Fitondo Fitnessplan'); 
    
         $redirect_urls = new RedirectUrls(); 
         $redirect_urls->setReturnUrl(URL::to('/payment/status')) 
          ->setCancelUrl(URL::to('/payments/cancel')); 
    
         $payment = new Payment(); 
         $payment->setIntent('Sale') 
          ->setPayer($payer) 
          ->setRedirectUrls($redirect_urls) 
          ->setTransactions(array($transaction)); 
    
         try { 
          $payment->create($this->_api_context); 
         } catch (\PayPal\Exception\PayPalConnectionException $ex) { 
          if (config('app.debug')) { 
           echo "Exception: " . $ex->getMessage() . PHP_EOL; 
           $err_data = json_decode($ex->getData(), true); 
           exit; 
          } else { 
           die('Error.'); 
          } 
         } 
    
         foreach($payment->getLinks() as $link) { 
          if($link->getRel() == 'approval_url') { 
           $redirect_url = $link->getHref(); 
           break; 
          } 
         } 
    
         /* here you could already add a database entry that a person started buying stuff (not finished of course) */ 
    
         if(isset($redirect_url)) { 
          // redirect to paypal 
          return Redirect::away($redirect_url); 
         } 
    
         die('Error.'); 
        } 
    

    成功ルート

    public function get(Request $request) 
    { 
        // Get the payment ID before session clear 
        $payment_id = $request->paymentId; 
    
        if (empty($request->PayerID) || empty($request->token)) { 
         die('error'); 
        } 
    
        $payment = Payment::get($payment_id, $this->_api_context); 
    
        // PaymentExecution object includes information necessary 
        // to execute a PayPal account payment. 
        // The payer_id is added to the request query parameters 
        // when the user is redirected from paypal back to your site 
        $execution = new PaymentExecution(); 
        $execution->setPayerId($request->PayerID); 
    
        //Execute the payment 
        $result = $payment->execute($execution, $this->_api_context); 
    
        if ($result->getState() == 'approved') { // payment made 
    
         /* here you should update your db that the payment was succesful */ 
    
         return Redirect::to('/this-is-what-you-bought') 
          ->with(['success' => 'Payment success']); 
        } 
    
        return Redirect::to('/') 
         ->with(['error' => 'Payment failed']); 
    } 
    

    を作成コントローラー

    $paypal_conf = config('paypal'); 
        $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret'])); 
        $this->_api_context->setConfig($paypal_conf['settings']); 
    

    のこの

    'settings' => array(
        /** 
        * Available option 'sandbox' or 'live' 
        */ 
        'mode' => 'sandbox', 
    
        /** 
        * Specify the max request time in seconds 
        */ 
        'http.ConnectionTimeOut' => 30, 
    
        /** 
        * Whether want to log to a file 
        */ 
        'log.LogEnabled' => true, 
    
        /** 
        * Specify the file that want to write on 
        */ 
        'log.FileName' => storage_path() . '/logs/paypal.log', 
    
        /** 
        * Available option 'FINE', 'INFO', 'WARN' or 'ERROR' 
        * 
        * Logging is most verbose in the 'FINE' level and decreases as you 
        * proceed towards ERROR 
        */ 
        'log.LogLevel' => 'FINE' 
    ) 
    

    コンストラクタのようにする必要があります - 私が持っていました私をきれいにするそれを単純化するために少しコードを書いてください。