Current File : //home/tradevaly/new.tradevaly.com.bd/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 Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;
use Stripe;
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',
'username' => 'required',
'company_name' => 'required',
'ccountry_id' => 'required',
'phone' => 'required',
'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();
}
$currency = 'usd';
$total_amount = 0;
if (User::where('email', $request->email)->first() != null) {
Toastr::error('Email Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
return redirect()->back();
}
if (User::where('username', $request->username)->first() != null) {
Toastr::error('Username Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
return redirect()->back();
}
$package = Package::where('id', $id)->first();
// extra working
$order_id = $request->company_type . rand(99999, 9999);
$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 = $id;
$user->next_pay = date('Y-m-d', strtotime("+25000 days"));
$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();
}
// if($user->save()){
$subscriber = new Subscriber();
$subscriber->order_id = $order_id;
$subscriber->user_id = $user_id;
$subscriber->package_id = $id;
$subscriber->date = date('Y-m-d');;
$subscriber->amount = $main_price_usd;
$subscriber->payment_status = 'unpaid';
$subscriber->currency = $currency;
$subscriber->status = 'pending';
if ($subscriber->save()) {
// $income = Income::where('id', $income->id)->first();
// dd($currency,number_format($main_price));
// dd(number_format($main_price));
// dd($main_price);
$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
);
$order = [
'order_id' => $subscriber->order_id,
'order_for' => 'subscription',
'package_id' => $id,
'user_id' => $user_id,
];
session(['orderInfo' => $order]);
$shurjopay_service = new ShurjopayController();
return $shurjopay_service->checkout($info);
}
// }
return back();
}
public function stripePost(Request $request, $slug)
{
$amount = 0;
$package = Income::where('id', $slug)->firstorFail();
if ($package != null) {
$amount = round($package->amount * 100);
}
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [
[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => "Payment"
],
'unit_amount' => $amount,
],
'quantity' => 1,
]
],
'mode' => 'payment',
'success_url' => '' . route('stripe.success', $package->id) . '?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('packages'),
]);
$package->session = $session->id;
if ($package->save()) {
return response()->json(['id' => $session->id, 'status' => 200]);
}
}
public function advertiserstripePost(Request $request, $slug)
{
$amount = 0;
$advertiser = AdvertiserOrder::where('id', $slug)->firstorFail();
$amount = $advertiser->amount;
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [
[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => "Payment"
],
'unit_amount' => $amount,
],
'quantity' => 1,
]
],
'mode' => 'payment',
'success_url' => '' . route('advertiser.stripe.success', $advertiser->id) . '?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('packages'),
]);
$advertiser->session = $session->ID;
if ($advertiser->save()) {
return response()->json(['id' => $session->id, 'status' => 200]);
}
}
public function stripe_success(Request $request, $id)
{
//Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
//$session = \Stripe\Checkout\Session::retrieve($request->session_id);
//$paymentIntent = \Stripe\PaymentIntent::retrieve($session->payment_intent);
//$customer = \Stripe\Customer::retrieve($session->customer);
// return $paymentIntent;
$income = Income::where('id', $id)->first();
$income->status = 1;
if ($income->save()) {
$user = User::find($income->user->id);
$user->next_pay = date('Y-m-d', strtotime("+365 days"));
if ($user->save()) {
Auth::loginUsingId($user->id);
Toastr::success('Registration Successfull', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
return redirect()->route('user.dashboard');
}
}
return view('frontend.pages.subscribe', compact('income'));
}
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 = array_shift($response_data);
$transaction = new Transaction();
$transaction->order_id = $orderInfo['order_id'];
$transaction->discount_amount = $payment->discsount_amount;
$transaction->total_amount = $payment->received_amount;
$transaction->transaction_id = $tras_id;
$transaction->payment_method = $payment->method;
$transaction->payment_status = 'paid';
$transaction->payment_type = 'online';
if ($orderInfo['order_for'] == 'advertiser'){
$order = AdvertiserOrder::where('order_id', $orderInfo['order_id'])->first();
if ($payment->sp_code == 1000){
$order->status = 'success';
$order->payment_status = 'paid';
$transaction->order_from = 'advertiser';
Toastr::success('Payment Successful', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
$redirect_to = route('user.my.buy.advertiser.money_receipt',$order->id);
}else{
$order->status = 'pending';
$order->payment_status = 'failed';
Toastr::error('Payment Failed', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
}
$order->save();
$transaction->save();
}else if ($orderInfo['order_for'] == 'subscription'){
if (!Auth::check()){
$user = User::find($orderInfo['user_id']);
$redirect_to = route('submit.register');
}else{
$user = Auth::guard('web')->user();
$redirect_to = route('user.subs');
}
$user->package_id = $orderInfo['package_id'];
$user->save();
$subscription = Subscriber::where('order_id',$orderInfo['order_id'])->first();
$subscription->status = 'success';
$subscription->payment_status = 'paid';
$subscription->save();
$transaction->order_from = $orderInfo['order_for'];
$transaction->save();
Toastr::success('Your subscription has been successfully upgraded', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
}
return redirect($redirect_to);
}
public function payment_cancel(Request $request, $id)
{
return $request;
}
}