2016-11-13 3 views
0

Ich denke nur darüber, dass ich eine Zahlungsmethode für meine Website integrieren muss. Meine Website wird in Laravel entwickelt. Deshalb suche ich nach einer Möglichkeit, paypal Integration hinzuzufügen. My site ist dort können Sie besuchen.Paypal Zahlungsmethode in Laravel

Antwort

0

Follow Bellow Wenige Schritt:
1) Laravel Anwendung installieren
2) Datenbankkonfiguration
3) Pakete installieren Required
4) Konfiguration paypal.php Datei
5) Route erstellen
6) erstellen Controller
7) erstellen Ansichtsdatei

Schritt 1: Installieren Laravel Anwendung
wir von Grunde gehen, so benötigen wir frische Laravel Anwendung mit Gebrüll Befehl zu bekommen, so öffnen Sie Terminal oder Eingabeaufforderung und führen brüllen Befehl:

 
composer create-project --prefer-dist laravel/laravel blog 

Schritt 2: Konfiguration der Datenbank
In diesem Schritt müssen Sie zur Konfiguration der Datenbank
folgende Details zu Ihrer .env-Datei hinzufügen.
1.Database Benutzername
1.Database Passwort
1.Database Namen

In .env Datei auch Host und Port Details können Sie alle Details wie in Ihrem System konfigurieren, so können Sie wie als Gebrüll setzen: .env

DB_HOST=localhost 
DB_DATABASE=homestead 
DB_USERNAME=homestead 
DB_PASSWORD=secret 

Schritt 3: Installieren Sie die erforderlichen Pakete
Wir haben folgende 2 Pakete für die Integration paypal Zahlung in unserer Laravel-Anwendung benötigt. Fügen Sie die folgenden zwei Pakete in Ihre composer.json-Datei ein.

"guzzlehttp/guzzle": "~5.4", 
"paypal/rest-api-sdk-php": "*", 

dann nach dem Hochlauf in Ihrem Terminal folgenden Befehl

php artisan vendore:publlish 

nach diesem Befehl, dann nach dem automatischen paypal.php Datei in Ihrem config/paypal.php Pfad erstellen.

Schritt 4: Konfiguration paypal.PHP-Datei

<?php 

return array(
/** set your paypal credential **/ 
'client_id' =>'paypal client_id', 
'secret' => 'paypal secret ID', 
/** 
* SDK configuration 
*/ 
'settings' => array(
    /** 
    * Available option 'sandbox' or 'live' 
    */ 
    'mode' => 'sandbox', 
    /** 
    * Specify the max request time in seconds 
    */ 
    'http.ConnectionTimeOut' => 1000, 
    /** 
    * 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' 
    ), 
); 

Schritt 5: Route erstellen
Dabei ist Schritt, den wir brauchen Weg für PayPal-Zahlung zu erstellen. Öffnen Sie also Ihre routes/web.php Datei und fügen Sie die folgende Route hinzu. Routen/web.php

Route::get('paywithpaypal', array('as' => 'addmoney.paywithpaypal','uses' => '[email protected]',)); 
Route::post('paypal', array('as' => 'addmoney.paypal','uses' => '[email protected]',)); 
Route::get('paypal', array('as' => 'payment.status','uses' => '[email protected]',)); 

Schritt 6: Erstellen-Controller
In diesem Punkt jetzt sollten wir neue Controller als AddMoneyController in diesem Pfad erstellen app/Http/Controller/AddMoneyController.php . Diese Steuerung wird das Layout und die Zahlung Post-Anforderung, verwalten So brüllen Befehl ausführen für neue Controller generieren:

php artisan make:controller AddMoneyController 

Ok, jetzt setzen unten Inhalt in Controller-Datei:
app/Http/Controller/AddMoneyController.php

<?php 

namespace App\Http\Controllers; 

use App\Http\Requests; 
use Illuminate\Http\Request; 
use Validator; 
use URL; 
use Session; 
use Redirect; 
use Input; 

/** All Paypal Details class **/ 
use PayPal\Rest\ApiContext; 
use PayPal\Auth\OAuthTokenCredential; 
use PayPal\Api\Amount; 
use PayPal\Api\Details; 
use PayPal\Api\Item; 
use PayPal\Api\ItemList; 
use PayPal\Api\Payer; 
use PayPal\Api\Payment; 
use PayPal\Api\RedirectUrls; 
use PayPal\Api\ExecutePayment; 
use PayPal\Api\PaymentExecution; 
use PayPal\Api\Transaction; 

class AddMoneyController extends HomeController 
{ 

    private $_api_context; 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 

     /** setup PayPal api context **/ 
     $paypal_conf = \Config::get('paypal'); 
     $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret'])); 
     $this->_api_context->setConfig($paypal_conf['settings']); 
    } 



    /** 
    * Show the application paywith paypalpage. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function payWithPaypal() 
    { 
     return view('paywithpaypal'); 
    } 

    /** 
    * Store a details of payment with paypal. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function postPaymentWithpaypal(Request $request) 
    { 
     $payer = new Payer(); 
     $payer->setPaymentMethod('paypal'); 

     $item_1 = new Item(); 

     $item_1->setName('Item 1') /** item name **/ 
      ->setCurrency('USD') 
      ->setQuantity(1) 
      ->setPrice($request->get('amount')); /** unit price **/ 

     $item_list = new ItemList(); 
     $item_list->setItems(array($item_1)); 

     $amount = new Amount(); 
     $amount->setCurrency('USD') 
      ->setTotal($request->get('amount')); 

     $transaction = new Transaction(); 
     $transaction->setAmount($amount) 
      ->setItemList($item_list) 
      ->setDescription('Your transaction description'); 

     $redirect_urls = new RedirectUrls(); 
     $redirect_urls->setReturnUrl(URL::route('payment.status')) /** Specify return URL **/ 
      ->setCancelUrl(URL::route('payment.status')); 

     $payment = new Payment(); 
     $payment->setIntent('Sale') 
      ->setPayer($payer) 
      ->setRedirectUrls($redirect_urls) 
      ->setTransactions(array($transaction)); 
      /** dd($payment->create($this->_api_context));exit; **/ 
     try { 
      $payment->create($this->_api_context); 
     } catch (\PayPal\Exception\PPConnectionException $ex) { 
      if (\Config::get('app.debug')) { 
       \Session::put('error','Connection timeout'); 
       return Redirect::route('addmoney.paywithpaypal'); 
       /** echo "Exception: " . $ex->getMessage() . PHP_EOL; **/ 
       /** $err_data = json_decode($ex->getData(), true); **/ 
       /** exit; **/ 
      } else { 
       \Session::put('error','Some error occur, sorry for inconvenient'); 
       return Redirect::route('addmoney.paywithpaypal'); 
       /** die('Some error occur, sorry for inconvenient'); **/ 
      } 
     } 

     foreach($payment->getLinks() as $link) { 
      if($link->getRel() == 'approval_url') { 
       $redirect_url = $link->getHref(); 
       break; 
      } 
     } 

     /** add payment ID to session **/ 
     Session::put('paypal_payment_id', $payment->getId()); 

     if(isset($redirect_url)) { 
      /** redirect to paypal **/ 
      return Redirect::away($redirect_url); 
     } 

     \Session::put('error','Unknown error occurred'); 
     return Redirect::route('addmoney.paywithpaypal'); 
    } 

    public function getPaymentStatus() 
    { 
     /** Get the payment ID before session clear **/ 
     $payment_id = Session::get('paypal_payment_id'); 
     /** clear the session payment ID **/ 
     Session::forget('paypal_payment_id'); 
     if (empty(Input::get('PayerID')) || empty(Input::get('token'))) { 
      \Session::put('error','Payment failed'); 
      return Redirect::route('addmoney.paywithpaypal'); 
     } 
     $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(Input::get('PayerID')); 
     /**Execute the payment **/ 
     $result = $payment->execute($execution, $this->_api_context); 
     /** dd($result);exit; /** DEBUG RESULT, remove it later **/ 
     if ($result->getState() == 'approved') { 

      /** it's all right **/ 
      /** Here Write your database logic like that insert record or value in database if you want **/ 

      \Session::put('success','Payment success'); 
      return Redirect::route('addmoney.paywithpaypal'); 
     } 
     \Session::put('error','Payment failed'); 

     return Redirect::route('addmoney.paywithpaypal'); 
    } 
    } 

Schritt 7: erstellen Ansicht
In letzten Schritt wollen wir schaffen paywithpaypal.blade.php (Ressourcen/views/paywithpaypal.blade.php) für layou t und wir werden Design-Code hier schreiben und auch für Pay-Menge durch paypal bilden, also folgenden Code setzen: Ressourcen/views/paywithpaypal.blade.php

@extends('layouts.app') 

@section('content') 
<div class="container"> 
    <div class="row"> 
     <div class="col-md-8 col-md-offset-2"> 
      <div class="panel panel-default"> 
       @if ($message = Session::get('success')) 
       <div class="custom-alerts alert alert-success fade in"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button> 
        {!! $message !!} 
       </div> 
       <?php Session::forget('success');?> 
       @endif 

       @if ($message = Session::get('error')) 
       <div class="custom-alerts alert alert-danger fade in"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button> 
        {!! $message !!} 
       </div> 
       <?php Session::forget('error');?> 
       @endif 
       <div class="panel-heading">Paywith Paypal</div> 
       <div class="panel-body"> 
        <form class="form-horizontal" method="POST" id="payment-form" role="form" action="{!! URL::route('addmoney.paypal') !!}" > 
         {{ csrf_field() }} 

         <div class="form-group{{ $errors->has('amount') ? ' has-error' : '' }}"> 
          <label for="amount" class="col-md-4 control-label">Amount</label> 

          <div class="col-md-6"> 
           <input id="amount" type="text" class="form-control" name="amount" value="{{ old('amount') }}" autofocus> 

           @if ($errors->has('amount')) 
            <span class="help-block"> 
             <strong>{{ $errors->first('amount') }}</strong> 
            </span> 
           @endif 
          </div> 
         </div> 

         <div class="form-group"> 
          <div class="col-md-6 col-md-offset-4"> 
           <button type="submit" class="btn btn-primary"> 
            Paywith Paypal 
           </button> 
          </div> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 
@endsection 

Jetzt sind wir bereit, unser Beispiel zu laufen, so laufen unten Befehl ro Schnelldurchlauf:

php artisan serve 

Jetzt Sie unten URL in Ihrem Browser öffnen:

http://localhost:8000/paywithpaypal 

bitte besuchen Sie diesen toturials Link.

https://www.youtube.com/watch?v=ly2xm_NM46g