Viewing file: PaystackPaymentController.php (15.52 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\Coupon; use App\Models\Customer; use App\Models\Invoice; use App\Models\InvoicePayment; use App\Models\Order; use App\Models\Plan; use App\Models\User; use App\Models\UserCoupon; use App\Models\Utility; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB;
class PaystackPaymentController extends Controller { // public $secret_key; public $public_key; public $is_enabled; protected $invoiceData;
public function paymentConfig() {
$payment_setting = Utility::getAdminPaymentSetting();
$this->secret_key = isset($payment_setting['paystack_secret_key']) ? $payment_setting['paystack_secret_key'] : ''; $this->public_key = isset($payment_setting['paystack_public_key']) ? $payment_setting['paystack_public_key'] : ''; $this->is_enabled = isset($payment_setting['is_paystack_enabled']) ? $payment_setting['is_paystack_enabled'] : 'off'; $this->currency = isset($payment_setting['currency']) ? $payment_setting['currency'] : 'off';
} public function companyPaymentConfig() {
$payment_setting = Utility::getCompanyPaymentSetting($this->invoiceData->created_by); $setting = Utility::settingsById($this->invoiceData->created_by);
$this->secret_key = isset($payment_setting['paystack_secret_key']) ? $payment_setting['paystack_secret_key'] : ''; $this->public_key = isset($payment_setting['paystack_public_key']) ? $payment_setting['paystack_public_key'] : ''; $this->is_enabled = isset($payment_setting['is_paystack_enabled']) ? $payment_setting['is_paystack_enabled'] : 'off'; $this->currency = isset($setting['site_currency']) ? $setting['site_currency'] : 'off';
}
public function planPayWithPaystack(Request $request) { $payment = $this->paymentConfig();
$planID = \Illuminate\Support\Facades\Crypt::decrypt($request->plan_id); $plan = Plan::find($planID); $authuser = \Auth::user(); $coupon_id = ''; if($plan) { $price = $plan->price; if(isset($request->coupon) && !empty($request->coupon)) { $coupons = Coupon::where('code', strtoupper($request->coupon))->where('is_active', '1')->first(); if(!empty($coupons)) { $usedCoupun = $coupons->used_coupon(); $discount_value = ($plan->price / 100) * $coupons->discount; $price = $plan->price - $discount_value; if($coupons->limit == $usedCoupun) { return redirect()->back()->with('error', __('This coupon code has expired.')); } $coupon_id = $coupons->id; } else { return redirect()->back()->with('error', __('This coupon code is invalid or has expired.')); } }
if($price <= 0) { $authuser->plan = $plan->id; $authuser->save();
$assignPlan = $authuser->assignPlan($plan->id);
if($assignPlan['is_success'] == true && !empty($plan)) {
$orderID = strtoupper(str_replace('.', '', uniqid('', true))); Order::create( [ 'order_id' => $orderID, 'name' => null, 'email' => null, 'card_number' => null, 'card_exp_month' => null, 'card_exp_year' => null, 'plan_name' => $plan->name, 'plan_id' => $plan->id, 'price' => $price, 'price_currency' => $this->currency, 'txn_id' => '', 'payment_type' => 'Paystack', 'payment_status' => 'success', 'receipt' => null, 'user_id' => $authuser->id, ] ); $res['msg'] = __("Plan successfully upgraded."); $res['flag'] = 2;
return $res; } else { return redirect()->route('plans.index')->with('error', __('Plan fail to upgrade.'));
} } $res_data['email'] = \Auth::user()->email; $res_data['total_price'] = $price; $res_data['currency'] = $this->currency; $res_data['flag'] = 1; $res_data['coupon'] = $coupon_id;
return $res_data; } else { return redirect()->route('plans.index')->with('error', __('Plan is deleted.'));
}
}
public function getPaymentStatus(Request $request, $pay_id, $plan) { $payment = $this->paymentConfig(); $planID = \Illuminate\Support\Facades\Crypt::decrypt($plan); $plan = Plan::find($planID); $user = \Auth::user(); $result = array();
if($plan) { try { $orderID = strtoupper(str_replace('.', '', uniqid('', true)));
//The parameter after verify/ is the transaction reference to be verified $url = "https://api.paystack.co/transaction/verify/$pay_id"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $this->secret_key, ] ); $responce = curl_exec($ch); curl_close($ch); if($responce) { $result = json_decode($responce, true); }
if(isset($result['status']) && $result['status'] == true) { $status = $result['data']['status']; if($request->has('coupon_id') && $request->coupon_id != '') { $coupons = Coupon::find($request->coupon_id); if(!empty($coupons)) { $userCoupon = new UserCoupon(); $userCoupon->user = $user->id; $userCoupon->coupon = $coupons->id; $userCoupon->order = $orderID; $userCoupon->save(); $usedCoupun = $coupons->used_coupon(); if($coupons->limit <= $usedCoupun) { $coupons->is_active = 0; $coupons->save(); } } }
$order = new Order(); $order->order_id = $orderID; $order->name = $user->name; $order->card_number = ''; $order->card_exp_month = ''; $order->card_exp_year = ''; $order->plan_name = $plan->name; $order->plan_id = $plan->id; $order->price = $result['data']['amount'] / 100; $order->price_currency = $this->currency; $order->txn_id = $pay_id; $order->payment_type = __('Paystack'); $order->payment_status = $result['data']['status']; $order->receipt = ''; $order->user_id = $user->id; $order->save();
$assignPlan = $user->assignPlan($plan->id); if($assignPlan['is_success']) { return redirect()->route('plans.index')->with('success', __('Plan activated Successfully.')); } else { return redirect()->route('plans.index')->with('error', $assignPlan['error']); } } else { return redirect()->back()->with('error', __('Transaction Unsuccesfull')); } } catch(\Exception $e) { return redirect()->route('plans.index')->with('error', __('Transaction has been failed.')); } } else { return redirect()->route('plans.index')->with('error', __('Plan is deleted.')); } }
public function customerPayWithPaystack(Request $request) {
$invoiceID = \Illuminate\Support\Facades\Crypt::decrypt($request->invoice_id); $invoice = Invoice::find($invoiceID); $user = User::find($invoice->created_by); $this->invoiceData = $invoice; $payment = $this->companyPaymentConfig();
$settings=Utility::settingsById($invoice->created_by);
if($invoice) { $price = $request->amount; if($price > 0) { $res_data['email'] = $user->email; $res_data['total_price'] = $price; $res_data['currency'] = isset($settings['site_currency'])?$settings['site_currency']:'USD'; $res_data['flag'] = 1;
return $res_data;
} else { $res['msg'] = __("Enter valid amount."); $res['flag'] = 2;
return $res; }
} else { return redirect()->back()->with('error', __('Invoice is deleted.'));
}
}
public function getInvoicePaymentStatus(Request $request, $pay_id, $invoice_id) { $invoiceID = \Illuminate\Support\Facades\Crypt::decrypt($invoice_id); $invoice = Invoice::find($invoiceID); $this->invoiceData = $invoice; $payment = $this->companyPaymentConfig(); $orderID = strtoupper(str_replace('.', '', uniqid('', true))); $settings = DB::table('settings')->where('created_by', '=', $invoice->created_by)->get()->pluck('value', 'name');
$result = array();
if($invoice) { try {
//The parameter after verify/ is the transaction reference to be verified $url = "https://api.paystack.co/transaction/verify/$pay_id"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $this->secret_key, ] ); $responce = curl_exec($ch); curl_close($ch); if($responce) { $result = json_decode($responce, true); }
if(isset($result['status']) && $result['status'] == true) {
$payments = InvoicePayment::create( [ 'invoice_id' => $invoice->id, 'date' => date('Y-m-d'), 'amount' => $request->amount, 'payment_method' => 1, 'order_id' => $orderID, 'payment_type' => __('Paystack'), 'receipt' => '', 'description' => __('Invoice') . ' ' . Utility::invoiceNumberFormat($settings, $invoice->invoice_id), ] );
$invoice = Invoice::find($invoice->id);
if($invoice->getDue() <= 0) { Invoice::change_status($invoice->id, 4); } else { Invoice::change_status($invoice->id, 3); }
//for customer balance update Utility::updateUserBalance('customer', $invoice->customer_id, $request->amount, 'debit');
//For Notification $setting = Utility::settingsById($invoice->created_by); $customer = Customer::find($invoice->customer_id); $notificationArr = [ 'payment_price' => $request->amount, 'invoice_payment_type' => 'Paystack', 'customer_name' => $customer->name, ]; //Slack Notification if(isset($setting['payment_notification']) && $setting['payment_notification'] ==1) { Utility::send_slack_msg('new_invoice_payment', $notificationArr,$invoice->created_by); } //Telegram Notification if(isset($setting['telegram_payment_notification']) && $setting['telegram_payment_notification'] == 1) { Utility::send_telegram_msg('new_invoice_payment', $notificationArr,$invoice->created_by); }
//Twilio Notification if(isset($setting['twilio_payment_notification']) && $setting['twilio_payment_notification'] ==1) { Utility::send_twilio_msg($customer->contact,'new_invoice_payment', $notificationArr,$invoice->created_by); } //webhook $module ='New Invoice Payment'; $webhook= Utility::webhookSetting($module,$invoice->created_by); if($webhook) { $parameter = json_encode($invoice); $status = Utility::WebhookCall($webhook['url'],$parameter,$webhook['method']); if($status == true) { return redirect()->back()->with('success', __('Payment successfully added!')); } else { return redirect()->back()->with('error', __('Webhook call failed.')); } }
return redirect()->route('invoice.link.copy', Crypt::encrypt($invoice->id))->with('success', __(' Payment successfully added.'));
} else {
return redirect()->route('invoice.link.copy', Crypt::encrypt($invoice->id))->with('error', __('Transaction Unsuccesfull')); } } catch(\Exception $e) {
return redirect()->route('invoice.link.copy', Crypt::encrypt($invoice->id))->with('error', __('Transaction has been failed.')); } } else { return redirect()->route('invoice.link.copy', Crypt::encrypt($invoice->id))->with('error', __('Invoice is deleted.')); } } }
|