SHARE
TWEET

Untitled

a guest Apr 4th, 2017 264 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4. use App\Http\Requests;
  5. use Illuminate\Http\Request;
  6. use Validator;
  7. use URL;
  8. use Session;
  9. use Redirect;
  10. Use App\Model\Order;
  11. use App\Model\Invoice;
  12. //use Input;
  13. use Illuminate\Support\Facades\Input;
  14. /** All Paypal Details class **/
  15. use PayPal\Rest\ApiContext;
  16. use PayPal\Auth\OAuthTokenCredential;
  17. use PayPal\Api\Amount;
  18. use PayPal\Api\Details;
  19. use PayPal\Api\Item;
  20. use PayPal\Api\ItemList;
  21. use PayPal\Api\Payer;
  22. use PayPal\Api\PayerInfo;
  23. use PayPal\Api\Payment;
  24. use PayPal\Api\RedirectUrls;
  25. use PayPal\Api\ExecutePayment;
  26. use PayPal\Api\PaymentExecution;
  27. use PayPal\Api\Transaction;
  28.  
  29.  
  30. class AddMoneyController extends HomeController
  31. {
  32.     private $_api_context;
  33.     /**
  34.      * Create a new controller instance.
  35.      *
  36.      * @return void
  37.      */
  38.     public function __construct()
  39.     {
  40.         parent::__construct();
  41.        
  42.         /** setup PayPal api context **/
  43.         $paypal_conf = \Config::get('paypal');
  44.         $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
  45.         $this->_api_context->setConfig($paypal_conf['settings']);
  46.     }
  47.     /**
  48.      * Show the application paywith paypalpage.
  49.      *
  50.      * @return \Illuminate\Http\Response
  51.      */
  52.     public function payWithPaypal()
  53.     {
  54.         return view('paywithpaypal');
  55.     }
  56.     /**
  57.      * Store a details of payment with paypal.
  58.      *
  59.      * @param  \Illuminate\Http\Request  $request
  60.      * @return \Illuminate\Http\Response
  61.      */
  62.     public function postPaymentWithpaypal(Request $request)
  63.     {
  64.         $order = Order::findOrFail($request->get('id'));
  65.         $payer = new Payer();
  66.         $payer->setPaymentMethod('paypal');
  67.  
  68.         $order_discount = $order->invoice->discount;
  69.         $order_subtotal = $order->invoice->sub_total;
  70.         $order_grandtotal = $order->invoice->grand_total;
  71.        
  72.         $i = 1;
  73.         $item_name = array();
  74.         foreach ($order->invoice->products as $product) {
  75.             // Each product information is sent as an item to PayPal
  76.            $item_name[$i] = new Item();
  77.  
  78.             // Set item name, price, price currency and quantity
  79.             // Note that the price is the price for a single product unit
  80.             $item_name[$i]->setName($product->name)
  81.                     ->setCurrency('USD')
  82.                     ->setQuantity($product->qty)
  83.                     ->setPrice($product->price);
  84.  
  85.             $i++;
  86.         }
  87.        
  88.         if ($order->order_discount > 0) {
  89.             $item_name[$i] = new Item();
  90.            $item_name[$i]->setName('Discount')
  91.                     ->setCurrency('USD')
  92.                     ->setQuantity(1)
  93.                     ->setPrice('-' . $order_discount);
  94.         }
  95.  
  96.         $item_list = new ItemList();
  97.         $item_list->setItems($item_name);
  98.        
  99.         $amount_details = new Details();
  100.         // Set order subtotal
  101.         // Subtotal is the sum of each product multiplied by quantity and all that minus the discount  
  102.         $amount_details->setSubtotal($order_subtotal);
  103.        
  104.         $amount = new Amount();
  105.         $amount->setCurrency('USD')
  106.             ->setDetails($amount_details)
  107.     ->setTotal($order_grandtotal);
  108.         $transaction = new Transaction();
  109.         $transaction->setAmount($amount)
  110.             ->setItemList($item_list)
  111.             ->setDescription('Your transaction description')
  112.             ->setInvoiceNumber($order->invoice->invoice_no);
  113.         $redirect_urls = new RedirectUrls();
  114.         $redirect_urls->setReturnUrl(URL::route('payment.status')) /** Specify return URL **/
  115.             ->setCancelUrl(URL::route('payment.status'));
  116.         $payment = new Payment();
  117.         $payment->setIntent('Order')
  118.             ->setPayer($payer)
  119.             ->setRedirectUrls($redirect_urls)
  120.             ->setTransactions(array($transaction));
  121. //          dd($payment->create($this->_api_context));exit;
  122.         try {
  123.             $payment->create($this->_api_context);
  124.         } catch (\PayPal\Exception\PPConnectionException $ex) {
  125.             if (\Config::get('app.debug')) {
  126.                 \Session::put('error','Connection timeout');
  127.                 return Redirect::route('addmoney.paywithpaypal');
  128.                 /** echo "Exception: " . $ex->getMessage() . PHP_EOL; **/
  129.                 /** $err_data = json_decode($ex->getData(), true); **/
  130.                 /** exit; **/
  131.             } else {
  132.                 \Session::put('error','Some error occur, sorry for inconvenient');
  133.                 return Redirect::route('addmoney.paywithpaypal');
  134.                 /** die('Some error occur, sorry for inconvenient'); **/
  135.             }
  136.         }
  137.         foreach($payment->getLinks() as $link) {
  138.             if($link->getRel() == 'approval_url') {
  139.                 $redirect_url = $link->getHref();
  140.                 break;
  141.             }
  142.         }
  143.         /** add payment ID to session **/
  144.         Session::put('paypal_payment_id', $payment->getId());
  145.         if(isset($redirect_url)) {
  146.             /** redirect to paypal **/
  147.             return Redirect::away($redirect_url);
  148.         }
  149.         \Session::put('error','Unknown error occurred');
  150.         return Redirect::route('addmoney.paywithpaypal');
  151.     }
  152.     public function getPaymentStatus()
  153.     {
  154.         /** Get the payment ID before session clear **/
  155.         $payment_id = Session::get('paypal_payment_id');
  156.         /** clear the session payment ID **/
  157.         Session::forget('paypal_payment_id');
  158.         if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
  159.             \Session::put('error','Payment failed');
  160.             return Redirect::route('addmoney.paywithpaypal');
  161.         }
  162.         $payment = Payment::get($payment_id, $this->_api_context);
  163.         /** PaymentExecution object includes information necessary **/
  164.         /** to execute a PayPal account payment. **/
  165.         /** The payer_id is added to the request query parameters **/
  166.         /** when the user is redirected from paypal back to your site **/
  167.         $execution = new PaymentExecution();
  168.         $execution->setPayerId(Input::get('PayerID'));
  169.         /**Execute the payment **/
  170.         $result = $payment->execute($execution, $this->_api_context);
  171.         /** dd($result);exit; /** DEBUG RESULT, remove it later **/
  172.         if ($result->getState() == 'approved') {
  173.            
  174.             /** it's all right **/
  175.             /** Here Write your database logic like that insert record or value in database if you want **/
  176.             \Session::put('success','Payment success');
  177.             return Redirect::route('addmoney.paywithpaypal');
  178.         }
  179.         \Session::put('error','Payment failed');
  180.         return Redirect::route('addmoney.paywithpaypal');
  181.     }
  182.    
  183.    
  184.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top