Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Http\Requests;
- use Illuminate\Http\Request;
- use Validator;
- use URL;
- use Session;
- use Redirect;
- Use App\Model\Order;
- use App\Model\Invoice;
- //use Input;
- use Illuminate\Support\Facades\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\PayerInfo;
- 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)
- {
- $order = Order::findOrFail($request->get('id'));
- $payer = new Payer();
- $payer->setPaymentMethod('paypal');
- $order_discount = $order->invoice->discount;
- $order_subtotal = $order->invoice->sub_total;
- $order_grandtotal = $order->invoice->grand_total;
- $i = 1;
- $item_name = array();
- foreach ($order->invoice->products as $product) {
- // Each product information is sent as an item to PayPal
- $item_name[$i] = new Item();
- // Set item name, price, price currency and quantity
- // Note that the price is the price for a single product unit
- $item_name[$i]->setName($product->name)
- ->setCurrency('USD')
- ->setQuantity($product->qty)
- ->setPrice($product->price);
- $i++;
- }
- if ($order->order_discount > 0) {
- $item_name[$i] = new Item();
- $item_name[$i]->setName('Discount')
- ->setCurrency('USD')
- ->setQuantity(1)
- ->setPrice('-' . $order_discount);
- }
- $item_list = new ItemList();
- $item_list->setItems($item_name);
- $amount_details = new Details();
- // Set order subtotal
- // Subtotal is the sum of each product multiplied by quantity and all that minus the discount
- $amount_details->setSubtotal($order_subtotal);
- $amount = new Amount();
- $amount->setCurrency('USD')
- ->setDetails($amount_details)
- ->setTotal($order_grandtotal);
- $transaction = new Transaction();
- $transaction->setAmount($amount)
- ->setItemList($item_list)
- ->setDescription('Your transaction description')
- ->setInvoiceNumber($order->invoice->invoice_no);
- $redirect_urls = new RedirectUrls();
- $redirect_urls->setReturnUrl(URL::route('payment.status')) /** Specify return URL **/
- ->setCancelUrl(URL::route('payment.status'));
- $payment = new Payment();
- $payment->setIntent('Order')
- ->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');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement