Current File : /home/tradevaly/public_html/app/Http/Controllers/SubscriptionController.php |
<?php
namespace App\Http\Controllers;
use App\Models\AdvertiserOrder;
use App\Models\Company;
use App\Models\Country;
use App\Models\Income;
use App\Models\Package;
use App\Models\PackageFeature;
use App\Models\Page;
use App\Models\Subscriber;
use App\Models\Transaction;
use App\Models\User;
use App\Models\Order;
use App\Notifications\OrderSuccess;
use App\Notifications\OrderPaymentSuccess;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;
use Stripe;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Mail;
use App\Notifications\User\Advertisement\AdvertismentPaidInvoice;
use Illuminate\Support\Facades\Notification;
use App\Notifications\UserVerification;
use App\Notifications\UserRegister;
use App\Notifications\SubscriptionSuccess;
use App\Notifications\User\Subscription\SubscriptionInvoice;
use App\Notifications\User\Subscription\SubscriptionPaidInvoice;
use App\Notifications\User\Subscription\RenewSubscription;
use App\Models\Banner;
class SubscriptionController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function buy(Request $request, $slug)
{
$package = Package::where('slug', $slug)->first();
return view('stripe', compact('package'));
}
public function apply(Request $request, $slug)
{
$pages = Page::where('slug', 'apply')->firstOrFail();
$package = Package::where('slug', $slug)->first();
$features = PackageFeature::where('package_id', $package->id)->where('value', 1)->get();
return view('frontend.pages.applypackage', compact('package', 'pages', 'features'));
}
public function register(Request $request, $id)
{
$name = '';
$email = '';
$phone = '';
if (!Auth::check()) {
$request->validate([
'name' => 'required',
'email' => [
'required',
Rule::unique('users', 'email'), // Check uniqueness in the 'users' table for the 'email' column
],
'username' => [
'required',
Rule::unique('users', 'username'), // Check uniqueness in the 'users' table for the 'username' column
],
'company_name' => 'required',
'ccountry_id' => 'required',
'phone' => 'required|min:9',
'password' => 'required',
'company_type' => 'required',
'g-recaptcha-response' => 'required',
]);
$name = $request->name;
$email = $request->email;
$phone = $request->phone;
$username = $request->username;
$type = $request->type;
$country_id = $request->ccountry_id;
$category_id = $request->company_type;
}
if (Auth::check()) {
$name = Auth::user()->name;
$email = Auth::user()->email;
$phone = Auth::user()->company->phone;
$username = Auth::user()->username;
$type = Auth::user()->type;
$country_id = Auth::user()->country_id;
$category_id = Auth::user()->company_type;
$user_id = Auth::id();
$user = Auth::user();
}
$currency = 'usd';
$total_amount = 0;
$package = Package::findOrFail($id);
// extra working
$randomNumber = rand(100000, 999999);
$randomText = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 7);
$order_id = $randomText.$randomNumber;
$main_price = $package->price;
$discount_price = 0;
$main_price_usd = 0;
$disc_percent = $package->discount;
if ($package->discount) {
$discount_price = $package->price / 100 * $package->discount;
// $main_price = $package->price;
// $main_price_usd = $package->price;
$main_price = $package->price - $discount_price;
$main_price_usd = $package->price - $discount_price;
}
if ($request->currency == 'BDT') {
$currency = 'BDT';
$main_price = $main_price * settings()->bdt_rate;
}
if (!Auth::check()) {
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->username = $request->username;
$user->type = $request->type;
$user->image = 'image.jpg';
$user->password = bcrypt($request->password);
$user->country_id = $request->ccountry_id;
$user->category_id = $request->company_type;
$user->package_id = 1;
$user->next_pay = Carbon::now()->addYear(10);
$user->save();
$user_id = $user->id;
$company = new Company();
$company->user_id = $user_id;
$company->name = $name;
$company->type = $type;
$company->main_products = $request->main_products;
$company->image = 'image.jpg';
$company->phone = $phone;
$company->save();
}
$subscriber = new Subscriber();
$subscriber->order_id = $order_id;
$subscriber->user_id = $user_id;
$subscriber->package_id = $id;
$subscriber->date = Carbon::now();
$subscriber->start = Carbon::now();
$subscriber->end = Carbon::now()->addYear();
$subscriber->amount = $main_price;
$subscriber->payment_status = 'unpaid';
$subscriber->currency = 'bdt';
$subscriber->status = 'pending';
// new transaction
$transaction = new Transaction();
$transaction->order_id = $order_id;
$transaction->discount_amount = $discount_price;
$transaction->total_amount = $main_price;
$transaction->user_id = $user_id;
$transaction->payment_type = 'online';
$transaction->order_from = 'subscription';
$transaction->order_status = 'new';
$transaction->save();
if ($subscriber->save()) {
$info = array(
'currency' => 'BDT',
'amount' => intval($main_price),
'order_id' => $order_id,
'discsount_amount' => 0,
'disc_percent' => 0,
'client_ip' => 'N\L',
'customer_name' => $name,
'customer_phone' => $phone,
'email' => $email,
'customer_address' => "N\L",
'customer_city' => "N/L",
'customer_state' => "N/L",
'customer_postcode' => "N/L",
'customer_country' => Country::find($country_id)->name
);
$invoiceData = (object) [
'name' => $name,
'amount' => $main_price,
'payment' => 'pending',
'discount' => 20,
'transaction_id' => $transaction->id,
'package' => $package->name,
'start' => Carbon::now(),
'end' => Carbon::now()->addYear(),
];
Notification::send($user, new UserRegister($user));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new UserRegister($user));
Notification::send($user, new UserVerification($user));
Notification::send($user, new SubscriptionInvoice($invoiceData));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new SubscriptionInvoice($invoiceData));
session(['orderInfo' => $info]);
$shurjopay_service = new ShurjopayController();
return $shurjopay_service->checkout($info);
}
return back();
}
public function package()
{
$data['pages'] = Page::where('slug', 'subscription')->firstOrFail();
$data['packages'] = Package::where('status', 1)->orderBy('id', 'desc')->latest()->get();
return view('frontend.pages.package')->with($data);
}
public function payment_success(Request $request)
{
$tras_id = trim($request->order_id);
$orderInfo = session('orderInfo');
// get payment information
$spayprocess = new SpayPaymentProccess();
$response_data = json_decode($spayprocess->decrypt_and_validate($tras_id));
$payment_info = array_shift($response_data);
$transaction = Transaction::where('order_id',$payment_info->customer_order_id)->firstOrFail();
if ($transaction->order_from == 'subscription'){
$subscriber = Subscriber::where('order_id',$payment_info->customer_order_id)->first();
if ($payment_info->sp_code == 1000){
// transaction
$transaction->transaction_id = $tras_id;
$transaction->payment_method = $payment_info->method;
$transaction->payment_status = 'paid';
// subscriber
$subscriber->payment_status = 'paid';
$subscriber->status = 'active';
$subscriber->save();
// package
$package = Package::findOrFail($subscriber->package_id);
// user
$user = User::find($subscriber->user_id);
if($subscriber->package_id === $user->package_id){
$subscriber->start = Carbon::parse($user->next_pay);
$currentExpireDate = Carbon::parse($user->next_pay);
$newExpireDate = $currentExpireDate->addYear();
}
if($subscriber->package_id != $user->package_id){
$user->package_id = $subscriber->package_id;
$user->pack_start = Carbon::now();
$subscriber->start = Carbon::now();
$currentExpireDate = Carbon::now();
$newExpireDate = $currentExpireDate->addYear();
}
$subscriber->end = $newExpireDate;
$user->pack_end = $newExpireDate;
$user->next_pay = $newExpireDate;
$subscriber->save();
$transaction->save();
$user->save();
$invoiceData = (object) [
'id' => $subscriber->id,
'name' => $user->name,
'amount' => $transaction->total_amount,
'payment' => 'Success',
'discount' => $transaction->discount_amount,
'transaction_id' => $transaction->id,
'package' => $package->name,
'start' => Carbon::parse($user->pack_start),
'end' => Carbon::parse($user->pack_end),
];
if($transaction->order_status == 'new'){
Notification::send($user, new SubscriptionPaidInvoice($invoiceData));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new SubscriptionPaidInvoice($invoiceData));
}else{
Notification::send($user, new RenewSubscription($invoiceData));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new RenewSubscription($invoiceData));
}
Toastr::success('Your subscription has been successfully upgraded', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
Toastr::success('Payment Successful !!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
Notification::send($user, new SubscriptionSuccess($user));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new SubscriptionSuccess($user));
// $redirect_to = route('user.subscription.money_receipt',$payment_info->customer_order_id);
$redirect_to = url('https://www.tradevaly.com.bd/user/dashboard');
}else{
$subscriber->payment_status = 'failed';
$subscriber->save();
$transaction->payment_status = 'failed';
$transaction->save();
$redirect_to = url('https://www.tradevaly.com.bd/user/dashboard');
Toastr::error('Payment Failed', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
}
}
else if($transaction->order_from == 'advertiser'){
$advertiser = AdvertiserOrder::where('order_id',$payment_info->customer_order_id)->first();
if ($payment_info->sp_code == 1000){
// transaction
$transaction->transaction_id = $tras_id;
$transaction->payment_method = $payment_info->method;
$transaction->payment_status = 'paid';
$transaction->save();
// subscriber
$advertiser->payment_status = 'paid';
$advertiser->status = 'pending';
$banner = $this->addAdvertiser($advertiser);
$advertiser->banner_id = $banner->id;
$advertiser->save();
$user = User::find($advertiser->user_id);
$invoiceData = (object) [
'id' => $advertiser->id,
'banner_id' => $banner->id,
'name' => $user->name,
'amount' => $transaction->total_amount,
'order_id' => $transaction->order_id,
'discount' => $transaction->discount_amount,
'payment_status' => 'Paid',
'transaction_id' => $transaction->id,
'advertisment_place' => $advertiser->advertiser_name,
'advertisement_id' => $advertiser->id,
'duration' => $advertiser->duration,
'created_at' => $transaction->created_at,
];
Notification::send($user, new AdvertismentPaidInvoice($invoiceData));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new AdvertismentPaidInvoice($invoiceData));
Toastr::success('Payment Successful !!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
// $redirect_to = route('user.subscription.money_receipt',$payment_info->customer_order_id);
$redirect_to = url('https://www.tradevaly.com.bd/user/dashboard');
}else{
$advertiser->payment_status = 'fail';
$advertiser->save();
$transaction->payment_status = 'fail';
$transaction->save();
Toastr::error('Payment Failed', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
}
}
else if($transaction->order_from == 'product'){
if ($payment_info->sp_code == 1000){
$order = Order::where('order_id',$payment_info->customer_order_id)->firstOrFail();
$order->payment_status="paid";
$order->payment_method=$payment_info->method;
$order->save();
$transaction->transaction_id = $tras_id;
$transaction->payment_method = $payment_info->method;
$transaction->payment_status = 'paid';
$transaction->save();
$invoiceData = [
'name' => auth()->user()->name,
'amount' => $transaction->recived_amount,
'payment' =>"paid",
'order_number' => $transaction->customer_order_id,
'discount' => 0,
'transaction_id' => $transaction->id,
];
// Notification::route('mail', auth()->user()->email)->notify(new OrderSuccess($invoiceData));
// Notification::route('mail', auth()->user()->email)->notify(new OrderPaymentSuccess($invoiceData));
// Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new OrderSuccess($invoiceData));
Toastr::success('Order Successfully Placed');
$redirect_to =route('products');
}else{
$order = Order::where('order_id',$payment_info->customer_order_id)->firstOrFail();
$order->payment_status="Failed";
$order->payment_method=$payment_info->method;
$order->save();
$transaction->transaction_id = $tras_id;
$transaction->payment_method = $payment_info->method;
$transaction->payment_status = 'Failed';
$transaction->save();
$invoiceData = [
'name' => auth()->user()->name,
'amount' => $transaction->recived_amount,
'payment' =>"Failed",
'order_number' => $transaction->customer_order_id,
'discount' => 0,
'transaction_id' => $transaction->id,
];
// Notification::route('mail', auth()->user()->email)->notify(new OrderSuccess($invoiceData));
// Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new OrderSuccess($invoiceData));
Toastr::success('Order Successfully Placed');
$redirect_to = route('products');
}
}
return redirect($redirect_to);
}
public function payment_cancel(Request $request, $id)
{
return $request;
}
public function addAdvertiser($advertiser){
$banner = new Banner();
$banner->image= "default.png";
$banner->position = $advertiser->advertiser_name;
$banner->user_id = $advertiser->user_id;
if($advertiser !== null){
$banner->category_id = $advertiser->category_id;
}
$banner->save();
return $banner;
}
}