Viewing file: PaymentGatewayController.php (11.35 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace Modules\PaymentGateway\Http\Controllers;
use App\Events\SendEMail; use App\Models\BillingRequest; use App\Models\Customer; use App\Models\Plan; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Log; use Modules\PaymentGateway\PaymentGatewayProvider\ProcessPayment; use PayPal\Api\Payment;
class PaymentGatewayController extends Controller { public function index() { return view('paymentgateway::index'); }
public function process(Request $request) { $data['plan'] = $plan= Plan::find($request->id); $data['tax']=$tax =auth()->user()->taxs()->first(); $discount=0; if (isset($tax->discount_type) && $tax->discount_type == 'percentage') { $discount = ($plan->price * $tax->discount) / 100; }elseif (isset($tax->discount_type) && $tax->discount_type == 'flat'){ $discount= ($plan->price - $tax->discount); } $data['discount']=isset($discount)?$discount:0; $data['total']=$plan->price +$discount ; return view('paymentgateway::process', $data); }
public function payNow(Request $request) { $plan = Plan::find($request->plan); if (!$plan) return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Plan not found')]);
if ($plan->price > 0) { // $request->validate([ // 'payment_type' => 'required|in:paypal,card,offline,paytm,mollie,paystack' // ]); } $user = auth('customer')->user(); $pre_plan = $user->plan;
if (isset($pre_plan) && $pre_plan->plan_id == $request->id) { return redirect()->route('customer.billing.index')->with('fail', 'You are already subscribed to this plan'); } if ($request->payment_type == 'offline') { $preBilling = BillingRequest::where(['customer_id' => $user->id, 'status' => 'pending'])->first(); if ($preBilling) { return redirect()->route('customer.billing.index')->with('fail', trans('You already have a pending request. Please wait for the admin reply.')); } }
$data['tax']=$tax =auth()->user()->taxs()->first(); $discount=0; if (isset($tax->discount_type) && $tax->discount_type == 'percentage') { $discount = ($plan->price * $tax->discount) / 100; }elseif (isset($tax->discount_type) && $tax->discount_type == 'flat'){ $discount= ($plan->price - $tax->discount); } $total=$plan->price +$discount ; $planReq = new BillingRequest(); $planReq->admin_id = $plan->admin_id; $planReq->customer_id = $user->id; $planReq->transaction_id = $request->transaction_id; $planReq->plan_id = $plan->id; $planReq->other_info = json_encode($request->only('payment_type')); $planReq->save(); setActivity(auth('customer')->user()->id, 'add', 'billing', $planReq->id, 'New Plan Purchase Request');
if ($plan->price <= 0) { $planReq->status = 'accepted'; $planReq->save();
$pre_plan = $user->plan; if ($pre_plan) { $user->plan()->delete(); } $user->plan()->create(['plan_id' => $planReq->plan_id, 'email_limit' => $plan->email_limit, 'available_email' => $plan->email_limit, 'price' => $total,'contact_limit'=>$plan->contact_limit]); return redirect()->route('customer.billing.index')->with('success', trans('Congratulations! Your plan successfully changed')); } // try { try { $emailTemplate = get_email_template('plan_request'); if ($emailTemplate && config('mail.from.address') && config('mail.from.name')) { $regTemp = str_replace('{customer_name}', $user->first_name . ' ' . $user->last_name, $emailTemplate->body); $config=new \stdClass(); $config->value=json_encode([ 'username'=>config('mail.username'), 'password'=>config('mail.password'), 'hostname'=>config('mail.host'), 'port'=>config('mail.port'), ]); SendEMail::dispatch('smtp',config('mail.from.address'),config('mail.from.name'),$user->email,$emailTemplate->title,$regTemp,config('mail.from.address'),null,$config); } } catch (\Exception $ex) { Log::info($ex->getMessage()); }
if (!in_array($request->payment_type,['flutterwave','vogue_pay', 'offline'])) { $processPayment = new ProcessPayment(); $processResult = $processPayment->set_gateway($request->payment_type) ->set_plan($plan, $total) ->plan_request($planReq) ->request($request) ->process(); if ($processResult->error_message) { return redirect()->route('customer.billing.index')->withErrors(['failed' => $processResult->error_message]); } if ($processResult->return_view) { return $processResult->return_view; } elseif ($processResult->will_redirect && $processResult->redirect_url) { return redirect()->to($processResult->redirect_url); } else { return redirect()->route('customer.billing.index')->with('success', trans('Congratulations! Your plan successfully changed')); } }else{ return redirect()->route('customer.billing.index')->with('success', trans('Congratulations! Your plan successfully changed')); } // } catch (\Exception $ex) { // dd($ex->getMessage(),'err'); // Log::error($ex); // return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid Payment')]); // } //end }
public function paymentSuccess(Request $request) { $credentials = json_decode(get_settings('payment_gateway')); if (!isset($credentials) || (!$credentials->paypal_client_id || !$credentials->paypal_client_secret)) { throw new \Exception('Credentials not found. Please contact with the administrator'); } $apiContext = $this->getPaypalApiContext($credentials->paypal_client_id, $credentials->paypal_client_secret); $paymentId = $request->paymentId; $user_plan_id = $request->plan; $user = $request->user; if (!$paymentId || !$user_plan_id || !$user) { return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]); } try { $payment = Payment::get($paymentId, $apiContext); } catch (\Exception $ex) { return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]); }
if (!$payment) return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]);
$url = $payment->getRedirectUrls(); $parsed_url = parse_url($url->getReturnUrl()); $query_string = $parsed_url["query"]; parse_str($query_string, $array_of_query_string);
if ($array_of_query_string["plan"] != $user_plan_id || $array_of_query_string["user"] != $user || $array_of_query_string['paymentId'] != $paymentId) { return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]); } $customer=Customer::find($array_of_query_string["user"]); if(!$customer){ return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]); }
$billingRequest = BillingRequest::where(['id' => $user_plan_id, 'customer_id' => $customer->id])->where(function ($q) use ($paymentId) { $q->whereNotIn('transaction_id', [$paymentId])->orWhereNull('transaction_id'); })->first();
if (!$billingRequest) { return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]); }
$billingRequest->status = 'accepted'; $billingRequest->save();
$pre_plan = $customer->plan; if ($pre_plan) { $customer->plan()->delete(); } $plan=$billingRequest->plan; $customer->plan()->create(['plan_id' => $billingRequest->plan_id, 'email_limit' => $plan->email_limit, 'available_emails' => $plan->email_limit, 'price' => $plan->price,'contact_limit'=>$plan->contact_limit]); // $customer->plan()->create(['plan_id' => $billingRequest->plan_id, 'email_limit' => ->email_limit, 'available_email' => $billingRequest->plan->email_limit, 'price' => $billingRequest->plan->price]); BillingRequest::where(['customer_id' => $user, 'status' => 'pending'])->update(['status' => 'rejected']); return redirect()->route('customer.billing.index')->with('success', trans('Congratulations! Your plan successfully changed')); }
function PayPalPayment($plan, $planReq) { $credentials = json_decode(get_settings('payment_gateway')); if (!isset($credentials) || (!$credentials->paypal_client_id || !$credentials->paypal_client_secret)) { return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Invalid payment')]); } $apiContext = $this->getPayPalApiContext($credentials->paypal_client_id, $credentials->paypal_client_secret); $payer = new \PayPal\Api\Payer(); $payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount(); $amount->setTotal($plan->price); $currency=get_currency(); $amount->setCurrency($currency); //TODO:: get the currency
$transaction = new \PayPal\Api\Transaction(); $transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls(); $redirectUrls->setReturnUrl(route('paymentgateway::payment.process.success', ['plan' => $planReq->id, 'user' => $planReq->customer_id])) ->setCancelUrl(route('paymentgateway::payment.process.cancel'));
$payment = new \PayPal\Api\Payment(); $payment->setIntent('sale') ->setPayer($payer) ->setTransactions(array($transaction)) ->setRedirectUrls($redirectUrls);
try { $payment->create($apiContext); return $payment; } catch (\PayPal\Exception\PayPalConnectionException $ex) { // This will print the detailed information on the exception. //REALLY HELPFUL FOR DEBUGGING Log::error($ex->getData()); } return null; }
function getPayPalApiContext($client_id, $secret_key) {
return new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( $client_id, // ClientID $secret_key // ClientSecret ) ); }
public function paymentCancel() { return redirect()->route('customer.billing.index')->withErrors(['msg' => trans('Payment has been cancelled')]); }
public function checkValidPayment(Request $request){ $plan = Plan::where('id', $request->plan_id)->first(); if ($plan->price==$request->price){ return response()->json(['status'=>'success']); }else{ return abort(404); } } }
|