Current File : /home/tradevaly/public_html/app/Http/Controllers/User/SubscriptionController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Subscriber;
use App\Models\Package;
use App\Models\Transaction;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;
use App\Notifications\User\Subscription\RenewSubscription;
use Illuminate\Support\Facades\Notification;
use App\Notifications\User\Subscription\SubscriptionInvoice;
use App\Models\Country;


class SubscriptionController extends Controller
{
    
    public function renew()
    {
        $user = Auth::user();
        if(empty($user->company->phone)){
            Toastr::error('Error!', 'You Have To Provide Your Number go to Company Setting and update!');
            return back();
        }
        $package = Package::find($user->package_id);
        $subscriber = new Subscriber();
        
        $currency = 'bdt';
        $total_amount = $package->price;
        $discount_price = 0;
        $main_price_usd = 0;

        $disc_percent = $package->discount;
        if ($package->discount) {
            $discount_price = $package->price / 100 * $package->discount;
            $total_amount = $package->price - $discount_price;
        }
        
        $randomNumber = rand(100000, 999999);
        $randomText = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 7);
        $order_id = $randomText.$randomNumber;
        
        
        $subscriber->package_id = $package->id;
        $subscriber->order_id = $order_id;
        $subscriber->user_id = $user->id;
        $subscriber->date = Carbon::now();
        $subscriber->amount = $total_amount;
        $subscriber->payment_status = 'unpaid';
        $subscriber->currency = $currency;
        $subscriber->status = 'pending';
        
        
         // new transaction
        
         $transaction = new Transaction();
        $transaction->order_id = $order_id;
        $transaction->discount_amount = $discount_price;
        $transaction->total_amount = $total_amount;
        $transaction->user_id = $user->id;
        $transaction->payment_type = 'online';
        $transaction->order_from = 'subscription';
        $transaction->order_status = 'renew';
        
        
        if ($transaction->save()) {
           $info = array(
                'currency' => 'bdt',
                'order_for' => 'renew_subscription',
                'amount' => $total_amount,
                'order_id' => $order_id, 
                'discsount_amount' => $discount_price,
                'disc_percent' => 0,
                'client_ip' => "N/L",
                'customer_name' => $user->name,
                'customer_phone' => $user->company->phone,
                'email' => Auth::user()->email,
                'customer_address' => ($user->address? $user->address:'N\L'),
                'customer_city' => "N/L",
                'customer_state' => "N/L",
                'customer_postcode' => "N/L",
                'customer_country' => $user->country->name
            );
            $invoiceData = (object) [
                'id' => $subscriber->id,
                'name' => $user->name,
                'amount' => $total_amount,
                'payment' => 'pending',
                'discount' => $discount_price,
                'transaction_id' => $transaction->id,
                'package' => $package->name,
                'start' => Carbon::now(),
                'end' => Carbon::now()->addYear(),
            ];
            
             $subscriber->save();
            $transaction->save();
             Notification::send($user, new RenewSubscription($invoiceData));
             Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new RenewSubscription($invoiceData));
             
$shurjopay_service = new ShurjopayController();return $shurjopay_service->checkout($info);
        }
    }
public function subs()
    {
        $package = Package::find(Auth::user()->package_id);
        return view('user.pages.subs', compact("package"));
    }

    public function changeplan()
    {
        $packages = Package::where('status', 1)->get();
        return view('user.pages.changeplan', compact("packages"));
    }

    public function choose($id, Request $request)
    {
       
        
        $package = Package::findOrFail($id);
        $user = Auth::user();
        if(empty($user->company->phone)){
            Toastr::error('Error!', 'You Have To Provide Your Number go to Company Setting and update!');
            return back();
        }
        $subscriber = Subscriber::where('user_id',$user->id)->first();

        $order_id = rand(100000, 999999);
        
        $main_price = $package->price;
        $discount_price = 0;
        $disc_percent = $package->discount;
        if ($package->discount) {
            $discount_price = $package->price / 100 * $package->discount;
            $main_price = $package->price - $discount_price;
        }
        
        
        
        if(!$subscriber){
            $subscriber = new Subscriber();
            $subscriber->order_id = $order_id;
            $subscriber->user_id = $user->id;
            $subscriber->package_id = $id;
            $subscriber->date = Carbon::now();
            $subscriber->amount = $main_price;
            $subscriber->payment_status = 'unpaid';
            $subscriber->currency = 'bdt';
            $subscriber->status = 'pending'; 
        $subscriber->save(); 
        }
      
        
        // 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();
        
        $order_id = 'tv-'.rand(1000, 9999).$transaction->id;
       
        $subscriber->order_id = $order_id;
         $subscriber->save();
         
         $transaction->order_id = $order_id;
         $transaction->save();
         
            $info = array(
                'currency' => 'BDT',
                'amount' => 1,
                // 'amount' => intval($main_price),
                'order_id' => $order_id,
                'discsount_amount' => $discount_price,
                'disc_percent' => 0,
                'client_ip' => "N/L",
                'customer_name' => $user->name,
                'customer_phone' => $user->company->phone,
                'email' => $user->email,
                'customer_address' => ($user->address? $user->address:'N\L'),
                'customer_city' => "N/L",
                'customer_state' => "N/L",
                'customer_postcode' => "N/L",
                'customer_country' => $user->country->name
            );
            
            
            $invoiceData = (object) [
                'id' => $subscriber->id,
                'name' => $user->name,
                'amount' => $main_price,
                'payment' => 'pending',
                'discount' => $discount_price,
                'transaction_id' => $transaction->id,
                'package' => $package->name,
                'start' => Carbon::now(),
                'end' => Carbon::now()->addYear(),
            ];

             Notification::send($user, new SubscriptionInvoice($invoiceData));
             Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new SubscriptionInvoice($invoiceData));
             
            $shurjopay_service = new ShurjopayController();
            return $shurjopay_service->checkout($info);
       
    }

    public function subscriptionInvoice()
    {
        $invoice = Subscriber::where('user_id',Auth::user()->id)->first();
        return view('user.subscription_invoice.invoice', compact('invoice'));
    }
    public function subscriptionMoneyReceipt()
    {
        $money_receipt = Subscriber::where('user_id',Auth::user()->id)->first();
        return view('user.subscription_invoice.money_receipt', compact('money_receipt'));
    }
    
}