<?php
namespace App\Http\Controllers\User;
use Exception;
use App\Models\UserWallet;
use App\Models\Transaction;
use Illuminate\Http\Request;
use App\Models\TemporaryData;
use App\Models\UserNotification;
use Illuminate\Support\Facades\DB;
use App\Models\Admin\BasicSettings;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use App\Constants\PaymentGatewayConst;
use App\Traits\ControlDynamicInputFields;
use Illuminate\Support\Facades\Validator;
use App\Models\Admin\PaymentGatewayCurrency;
use Illuminate\Support\Facades\Notification;
use App\Traits\PaymentGateway\PaystackGateway;
use App\Notifications\User\AddMoneyNotification;
use App\Http\Helpers\PaymentGateway as PaymentGatewayHelper;
class AddMoneyController extends Controller
{
use ControlDynamicInputFields,PaystackGateway;
/**
* Method for view index page
* @return view
*/
public function index(){
$page_title = "Add Money";
$payment_gateways = PaymentGatewayCurrency::whereHas('gateway', function ($gateway) {
$gateway->where('slug', PaymentGatewayConst::add_money_slug());
$gateway->where('status', 1);
})->get();
$transactions = Transaction::auth()->with(['payment_gateway'])->where('type',PaymentGatewayConst::ADDMONEY)
->orderBy('id','desc')->latest()->take(3)->get();
return view('user.sections.add-money.index',compact(
'page_title',
'transactions',
'payment_gateways'
));
}
/**
* Method for confirm add money
* @param Illuminate\Http\Request $request
*/
public function confirm(Request $request){
$validator = Validator::make($request->all(),[
'amount' => 'required|numeric',
'payment_gateway' => 'required|string',
]);
if($validator->fails()) return back()->withErrors($validator)->withInput($request->all());
$validated = $validator->validated();
$request->merge(['currency' => $validated['payment_gateway']]);
try{
$instance = PaymentGatewayHelper::init($request->all())->type(PaymentGatewayConst::TYPEADDMONEY)->gateway()->render();
if($instance instanceof RedirectResponse === false && isset($instance['gateway_type']) && $instance['gateway_type'] == PaymentGatewayConst::MANUAL) {
$manual_handler = $instance['distribute'];
return $this->$manual_handler($instance);
}
}catch(Exception $e) {
return back()->with(['error' => [$e->getMessage()]]);
}
return $instance;
}
/**
* Method for add money success method
*/
public function success(Request $request, $gateway){
try{
$token = PaymentGatewayHelper::getToken($request->all(),$gateway);
$temp_data = TemporaryData::where("identifier",$token)->first();
if(Transaction::where('callback_ref', $token)->exists()) {
if(!$temp_data) return redirect()->route('user.add.money.index')->with(['success' => ['Transaction request sended successfully!']]);;
}else {
if(!$temp_data) return redirect()->route('user.add.money.index')->with(['error' => ['Transaction failed. Record didn\'t saved properly. Please try again.']]);
}
$update_temp_data = json_decode(json_encode($temp_data->data),true);
$update_temp_data['callback_data'] = $request->all();
$temp_data->update([
'data' => $update_temp_data,
]);
$temp_data = $temp_data->toArray();
$instance = PaymentGatewayHelper::init($temp_data)->type(PaymentGatewayConst::TYPEADDMONEY)->responseReceive();
if($instance instanceof RedirectResponse) return $instance;
}catch(Exception $e) {
return back()->with(['error' => [$e->getMessage()]]);
}
return redirect()->route("user.add.money.index")->with(['success' => ['Successfully added money.']]);
}
public function cancel(Request $request, $gateway) {
$token = PaymentGatewayHelper::getToken($request->all(),$gateway);
if($temp_data = TemporaryData::where("type",PaymentGatewayConst::TYPEADDMONEY)->where("identifier",$token)->first()) {
$temp_data->delete();
}
return redirect()->route('user.add.money.index')->with(['error' => ['Transaction failed.']]);
}
public function postSuccess(Request $request, $gateway){
try{
$token = PaymentGatewayHelper::getToken($request->all(),$gateway);
$temp_data = TemporaryData::where("identifier",$token)->first();
Auth::guard($temp_data->data->creator_guard)->loginUsingId($temp_data->data->creator_id);
}catch(Exception $e) {
return redirect()->route('frontend.index');
}
return $this->success($request, $gateway);
}
public function postCancel(Request $request, $gateway){
try{
$token = PaymentGatewayHelper::getToken($request->all(),$gateway);
$temp_data = TemporaryData::where("type",PaymentGatewayConst::TYPEADDMONEY)->where("identifier",$token)->first();
Auth::guard($temp_data->data->creator_guard)->loginUsingId($temp_data->data->creator_id);
}catch(Exception $e) {
return redirect()->route('frontend.index');
}
return $this->cancel($request, $gateway);
}
public function callback(Request $request,$gateway) {
$callback_token = $request->get('token');
$callback_data = $request->all();
try{
PaymentGatewayHelper::init([])->type(PaymentGatewayConst::TYPEADDMONEY)->setProjectCurrency(PaymentGatewayConst::PROJECT_CURRENCY_SINGLE)->handleCallback($callback_token,$callback_data,$gateway);
}catch(Exception $e) {
// handle Error
logger($e);
}
}
public function handleManualPayment($payment_info) {
// Insert temp data
$data = [
'type' => PaymentGatewayConst::TYPEADDMONEY,
'identifier' => generate_unique_string("temporary_datas","identifier",16),
'data' => [
'gateway_currency_id' => $payment_info['currency']->id,
'amount' => $payment_info['amount'],
'wallet_id' => $payment_info['wallet']->id,
],
];
try{
TemporaryData::create($data);
}catch(Exception $e) {
return redirect()->route('user.add.money.index')->with(['error' => ['Failed to save data. Please try again']]);
}
return redirect()->route('user.add.money.manual.form',$data['identifier']);
}
public function showManualForm($token) {
$tempData = TemporaryData::search($token)->first();
if(!$tempData || $tempData->data == null || !isset($tempData->data->gateway_currency_id)) return redirect()->route('user.add.money.index')->with(['error' => ['Invalid request']]);
$gateway_currency = PaymentGatewayCurrency::find($tempData->data->gateway_currency_id);
if(!$gateway_currency || !$gateway_currency->gateway->isManual()) return redirect()->route('user.add.money.index')->with(['error' => ['Selected gateway is invalid']]);
$gateway = $gateway_currency->gateway;
if(!$gateway->input_fields || !is_array($gateway->input_fields)) return redirect()->route('user.add.money.index')->with(['error' => ['This payment gateway is under constructions. Please try with another payment gateway']]);
$amount = $tempData->data->amount;
$page_title = "Payment Instructions";
return view('user.sections.add-money.manual.instruction',compact("gateway","page_title","token","amount"));
}
public function manualSubmit(Request $request,$token) {
$request->merge(['identifier' => $token]);
$tempDataValidate = Validator::make($request->all(),[
'identifier' => "required|string|exists:temporary_datas",
])->validate();
$tempData = TemporaryData::search($tempDataValidate['identifier'])->first();
if(!$tempData || $tempData->data == null || !isset($tempData->data->gateway_currency_id)) return redirect()->route('user.add.money.index')->with(['error' => ['Invalid request']]);
$gateway_currency = PaymentGatewayCurrency::find($tempData->data->gateway_currency_id);
if(!$gateway_currency || !$gateway_currency->gateway->isManual()) return redirect()->route('user.add.money.index')->with(['error' => ['Selected gateway is invalid']]);
$gateway = $gateway_currency->gateway;
$amount = $tempData->data->amount ?? null;
if(!$amount) return redirect()->route('user.add.money.index')->with(['error' => ['Transaction Failed. Failed to save information. Please try again']]);
$wallet = UserWallet::find($tempData->data->wallet_id ?? null);
if(!$wallet) return redirect()->route('user.add.money.index')->with(['error' => ['Your wallet is invalid!']]);
$this->file_store_location = "transaction";
$dy_validation_rules = $this->generateValidationRules($gateway->input_fields);
$validated = Validator::make($request->all(),$dy_validation_rules)->validate();
$get_values = $this->placeValueWithFields($gateway->input_fields,$validated);
$trx_id = generateTrxString("transactions","trx_id","AM",8);
// Make Transaction
DB::beginTransaction();
try{
$id = DB::table("transactions")->insertGetId([
'type' => PaymentGatewayConst::TYPEADDMONEY,
'trx_id' => $trx_id,
'user_id' => $wallet->user->id,
'user_wallet_id' => $wallet->id,
'payment_gateway_currency_id' => $gateway_currency->id,
'request_amount' => $amount->requested_amount,
'fixed_charge' => $amount->fixed_charge,
'percent_charge' => $amount->percent_charge,
'total_charge' => $amount->total_charge,
'total_payable' => $amount->total_amount,
'request_currency' => get_default_currency_code(),
'available_balance' => $wallet->balance,
'payment_currency' => $gateway_currency->currency_code,
'remark' => PaymentGatewayConst::MANUAL,
'details' => json_encode(['input_values' => $get_values]),
'attribute' => PaymentGatewayConst::RECEIVED,
'status' => PaymentGatewayConst::STATUSPENDING,
'created_at' => now(),
]);
$basic_settings = BasicSettings::first();
if($basic_settings->email_notification == true){
try{
Notification::route('mail',auth()->user()->email)->notify(new AddMoneyNotification($id));
}catch(Exception $e){}
}
$transaction = Transaction::with(['payment_gateway'])->where('trx_id',$trx_id)->first();
UserNotification::create([
'user_id' => auth()->user()->id,
'transaction_id' => $id,
'details' => [
'title' => 'Add Money using ' . $transaction->payment_gateway->name,
'receiver' => "",
'amount' => get_amount($transaction->request_amount),
'currency' => $transaction->request_currency,
'message' => "Successfully Received."
],
]);
DB::table("temporary_datas")->where("identifier",$token)->delete();
DB::commit();
}catch(Exception $e) {
DB::rollBack();
return redirect()->route('user.add.money.manual.form',$token)->with(['error' => ['Something went wrong! Please try again']]);
}
return redirect()->route('user.add.money.index')->with(['success' => ['Transaction Success. Please wait for admin confirmation.']]);
}
public function redirectUsingHTMLForm(Request $request, $gateway)
{
$temp_data = TemporaryData::where('identifier', $request->token)->first();
if(!$temp_data || $temp_data->data->action_type != PaymentGatewayConst::REDIRECT_USING_HTML_FORM) return back()->with(['error' => ['Request token is invalid!']]);
$redirect_form_data = $temp_data->data->redirect_form_data;
$action_url = $temp_data->data->action_url;
$form_method = $temp_data->data->form_method;
return view('payment-gateway.redirect-form', compact('redirect_form_data', 'action_url', 'form_method'));
}
/**
* Redirect Users for collecting payment via Button Pay (JS Checkout)
*/
public function redirectBtnPay(Request $request, $gateway)
{
try{
return PaymentGatewayHelper::init([])->type(PaymentGatewayConst::ADDMONEY)->handleBtnPay($gateway, $request->all());
}catch(Exception $e) {
return redirect()->route('user.add.money.index')->with(['error' => [$e->getMessage()]]);
}
}
/**
* Method for paystack pay callback
*/
public function paystackPayCallBack(Request $request){
$instance = $this->paystackSuccess($request->all());
return redirect()->route('user.add.money.index')->with(['success' => ['Successfully added money.']]);
}
}
Service Section
Discover how our services are designed to enhance your NFC Pay experience with convenience, security, and innovative solutions. From managing transactions to secure payments, we are dedicated to providing seamless support every step of the way.
Easily save your credit and debit card details within our app for quick and secure transactions. This feature ensures that your payment information is protected with advanced encryption and can be used for future purchases with just a tap.
Transfer funds quickly and securely between users with our streamlined money transfer service. Simply select the recipient, enter the amount, and authorize the transaction for instant, hassle-free transfers.
Activate your merchant account effortlessly to start receiving payments. Our intuitive setup process ensures that you can begin accepting transactions smoothly, helping your business thrive with minimal setup time.
Keep track of all your transactions in real time through our app. Monitor payment statuses, view transaction history, and manage your account efficiently, ensuring complete control over your financial activities.
Our dedicated support team is available to assist you with any queries or issues. Whether you need help with setting up your account or resolving transaction-related questions, we’re here to provide prompt and reliable assistance.