Sid Gifari File Manager
🏠 Root
/
home2
/
meumer25
/
api.meumercado.app
/
app
/
Http
/
Controllers
/
Editing: PaypalPaymentController.php
<?php namespace App\Http\Controllers; use App\CentralLogics\Helpers; use App\Model\Currency; use App\Model\Order; use Brian2694\Toastr\Facades\Toastr; use Illuminate\Http\Request; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\URL; use Illuminate\Support\Str; use PayPal\Api\Amount; use PayPal\Api\Item; use PayPal\Api\ItemList; use PayPal\Api\Payer; use PayPal\Api\Payment; use PayPal\Api\PaymentExecution; use PayPal\Api\RedirectUrls; use PayPal\Api\Transaction; use PayPal\Auth\OAuthTokenCredential; use PayPal\Common\PayPalModel; use PayPal\Rest\ApiContext; class PaypalPaymentController extends Controller { public function __construct() { $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']); } public function payWithpaypal(Request $request) { $order = Order::with(['details'])->where(['id' => session('order_id')])->first(); $tr_ref = Str::random(6) . '-' . rand(1, 1000); $payer = new Payer(); $payer->setPaymentMethod('paypal'); $items_array = []; $item = new Item(); $item->setName($order->customer['f_name']) ->setCurrency(Helpers::currency_code()) ->setQuantity(1) ->setPrice($order['order_amount']); array_push($items_array, $item); $item_list = new ItemList(); $item_list->setItems($items_array); $amount = new Amount(); $amount->setCurrency(Helpers::currency_code()) ->setTotal($order['order_amount']); \session()->put('transaction_reference', $tr_ref); $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($item_list) ->setDescription($tr_ref); $redirect_urls = new RedirectUrls(); $redirect_urls->setReturnUrl(URL::route('paypal-status')) ->setCancelUrl(URL::route('payment-fail')); $payment = new Payment(); $payment->setIntent('Sale') ->setPayer($payer) ->setRedirectUrls($redirect_urls) ->setTransactions(array($transaction)); try { $payment->create($this->_api_context); foreach ($payment->getLinks() as $link) { if ($link->getRel() == 'approval_url') { $redirect_url = $link->getHref(); break; } } DB::table('orders') ->where('id', $order->id) ->update([ 'transaction_reference' => $payment->getId(), 'payment_method' => 'paypal', 'order_status' => 'failed', 'updated_at' => now() ]); Session::put('paypal_payment_id', $payment->getId()); if (isset($redirect_url)) { return Redirect::away($redirect_url); } } catch (\Exception $ex) { Toastr::error('Your currency is not supported by PAYPAL.'); return back()->withErrors(['error' => 'Failed']); } Session::put('error', 'Configure your paypal account.'); return back()->withErrors(['error' => 'Failed']); } public function getPaymentStatus(Request $request) { $payment_id = Session::get('paypal_payment_id'); if (empty($request['PayerID']) || empty($request['token'])) { Session::put('error', 'Payment failed'); return Redirect::back(); } $payment = Payment::get($payment_id, $this->_api_context); $execution = new PaymentExecution(); $execution->setPayerId($request['PayerID']); /**Execute the payment **/ $result = $payment->execute($execution, $this->_api_context); if ($result->getState() == 'approved') { DB::table('orders') ->where('transaction_reference', $payment_id) ->update(['order_status' => 'confirmed', 'payment_status' => 'paid', 'transaction_reference' => $payment_id]); $order_id = Order::where('transaction_reference', $payment_id)->first()->id; try { $order = Order::find($order_id); $fcm_token = $order->customer->cm_firebase_token; $value = Helpers::order_status_update_message('confirmed'); if ($value) { $data = [ 'title' => 'Order', 'description' => $value, 'order_id' => $order['id'], 'image' => '', ]; Helpers::send_push_notif_to_device($fcm_token, $data); } } catch (\Exception $e) { } $order = Order::where('transaction_reference', $payment_id)->first(); if ($order->callback != null) { return redirect($order->callback . '/success'); }else{ return \redirect()->route('payment-success'); } } $order = Order::where('transaction_reference', $payment_id)->first(); if ($order->callback != null) { return redirect($order->callback . '/fail'); }else{ return \redirect()->route('payment-fail'); } } }
Save
Cancel