2024-06-28 19:30:31 +05:30
< ? php
2024-07-01 12:01:35 +05:30
namespace App\Http\Controllers\APIs\Customer_API ;
2024-06-28 19:30:31 +05:30
use App\Http\Controllers\Controller ;
2024-08-02 14:52:42 +05:30
use App\Models\CustomReferralCode ;
2024-07-11 17:59:09 +05:30
use App\Models\Faq ;
2024-07-02 19:20:18 +05:30
use App\Models\IamPrincipal ;
use App\Models\SubscriptionProducts ;
use App\Models\Subscriptions ;
2024-07-03 18:01:09 +05:30
use App\Services\APIs\CustomerAPIs\RulesApiServices ;
use Illuminate\Support\Facades\Log ;
2024-06-28 19:30:31 +05:30
use Illuminate\Http\Request ;
2024-07-03 18:01:09 +05:30
use Illuminate\Support\Facades\DB ;
2024-06-28 19:30:31 +05:30
2024-07-02 19:20:18 +05:30
2024-07-03 18:01:09 +05:30
use Illuminate\Support\Facades\Validator ;
use Illuminate\Support\Facades\Redirect ;
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
class SubscriptionController extends Controller
2024-06-28 19:30:31 +05:30
{
2024-07-03 18:01:09 +05:30
//created by; Hritik
//On - 28th June ,2024
//use - to get Data of User in Webview and show list of product
public function mySubscription ( Request $request )
2024-07-03 17:59:03 +05:30
{
2024-07-03 18:01:09 +05:30
try {
2024-07-26 11:21:09 +05:30
$token = readHeaderToken ();
2024-08-02 14:52:42 +05:30
$getCustomToken = CustomReferralCode :: where ( 'is_active' , 1 ) -> first ();
2024-07-26 11:21:09 +05:30
// $token = true;
2024-07-03 18:01:09 +05:30
// dd($token, Session::get('vendorToken'));
if ( $token ) {
2024-08-02 14:52:42 +05:30
// $user_id = 104;
2024-07-26 11:21:09 +05:30
$user_id = $token [ 'sub' ];
2024-07-03 18:01:09 +05:30
$dateTime = now ();
$formattedDateTime = $dateTime -> format ( 'Y-m-d H:i:s' );
$isSubscribedUser = Subscriptions :: where ( 'iam_principal_xid' , $user_id )
-> where ( 'next_payment_date' , '>=' , $formattedDateTime )
-> first ();
$userData = IamPrincipal :: where ( 'id' , $user_id ) -> first ();
// $request['iam_principal_id'] = $user_id;
$products = SubscriptionProducts :: where ( 'is_active' , '1' ) -> get ();
if ( $isSubscribedUser ) {
return view ( " Admin.pages.subscriptions.my-subscription " , compact ( 'user_id' , 'userData' , 'isSubscribedUser' , 'products' ));
}
$productList = SubscriptionProducts :: where ( 'is_active' , 1 ) -> first ();
2024-07-11 17:59:09 +05:30
$faqs = Faq :: where ( 'faq_category_id' , 3 ) -> where ( 'is_active' , '1' ) -> get (); //getting only subscription faqS
2024-07-03 18:01:09 +05:30
2024-08-02 14:52:42 +05:30
return view ( 'Admin.pages.subscriptions.list-of-products' , compact ( 'productList' , 'userData' , 'faqs' , 'getCustomToken' ));
2024-07-03 18:01:09 +05:30
} else {
return jsonResponseWithErrorMessageApi ( __ ( 'auth.user_deleted' ), 409 );
}
} catch ( \Exception $e ) {
2024-07-10 16:50:45 +05:30
Log :: error ( " An error occurred in " . __METHOD__ . " : " . $e -> getMessage (), [ 'exception' => $e ]);
2024-07-03 18:01:09 +05:30
return jsonResponseWithErrorMessage ( __ ( 'auth.something_went_wrong' ));
}
}
2024-07-03 16:24:22 +05:30
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
//created by; Hritik
//On - 28th June ,2024
//use - to get Data of User in Webview and show list of product
public function listOfProduct ( Request $request )
{
2024-06-28 19:30:31 +05:30
try {
2024-07-03 18:01:09 +05:30
$productList = SubscriptionProducts :: where ( 'is_active' , 1 ) -> first ();
return view ( 'Admin.pages.subscriptions.list-of-products' , compact ( 'productList' ));
} catch ( \Exception $e ) {
2024-07-10 16:50:45 +05:30
Log :: error ( " An error occurred in " . __METHOD__ . " : " . $e -> getMessage (), [ 'exception' => $e ]);
2024-07-12 19:17:19 +05:30
return jsonResponseWithErrorMessage ( __ ( 'auth.something_went_wrong' ));
2024-06-28 19:30:31 +05:30
}
2024-07-03 18:01:09 +05:30
}
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
//created by; Hritik
//On - 02th July ,2024
//use - to Create Subscription Product & price for Monthly
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
public function createStripeProduct ( Request $request )
{
2024-07-05 19:32:58 +05:30
2024-07-03 18:01:09 +05:30
try {
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
DB :: beginTransaction ();
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$stripeSecret = ( config ( 'constants.subscription.stripe_secret_key' ));
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
// $stripeSecret = env('STRIPE_SECRET');
$stripe = new \Stripe\StripeClient ( $stripeSecret );
// dd($stripeSecret,$stripe,$request->all());
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$validator = $this -> validateSubscriptionsproductForm ( $request );
if ( $validator -> fails ()) {
$validationErrors = $validator -> errors () -> all ();
Log :: error ( " Stripe subscriptions plan validation error: " . implode ( " , " , $validationErrors ));
return jsonResponseWithErrorMessageApi ( $validationErrors , 203 );
}
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$id = SubscriptionProducts :: create ([
'product_name' => $request -> product_name ,
'product_value' => $request -> product_value ,
'product_details' => $request -> product_details ,
]);
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$productCreate = $stripe -> products -> create ([
'name' => $request -> product_name ,
]);
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$updateCreatedPlan = SubscriptionProducts :: where ( 'id' , $id -> id ) -> update ([
'stripe_product_id' => $productCreate -> id
]);
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$totalPrice = $request -> product_value ;
if ( $totalPrice && $totalPrice != 0 && $totalPrice != null ) {
$productPrice1 = $stripe -> prices -> create ([
'unit_amount' => $totalPrice * 100 ,
// amount entered by Admin (in cents)
'currency' => 'usd' ,
'recurring' => [ 'interval' => 'month' ],
'product' => $productCreate -> id ,
'nickname' => 'Monthly Price' ,
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
]);
2024-07-02 19:20:18 +05:30
2024-07-03 18:01:09 +05:30
$updateCreatedPlan = SubscriptionProducts :: where ( 'id' , $id -> id ) -> update ([
'stripe_price_id' => $productPrice1 -> id
2024-07-02 19:20:18 +05:30
]);
2024-07-03 18:01:09 +05:30
}
DB :: commit ();
return jsonResponseWithSuccessMessage ( __ ( 'success.save_data' ), 200 );
} catch ( \Exception $ex ) {
DB :: rollBack ();
Log :: error ( 'Favourite Restaurant service failed: ' . $ex -> getMessage ());
return jsonResponseWithErrorMessageApi ( __ ( 'auth.something_went_wrong' ), 500 );
}
}
2024-07-10 16:39:32 +05:30
//update trial Period
public function updateTrialPeriod ( Request $request )
{
try {
DB :: beginTransaction ();
$stripeSecret = config ( 'constants.subscription.stripe_secret_key' );
$validator = Validator :: make (
$request -> all (),
[
'stripe_id' => 'required' ,
'timestamp_to_update' => 'required' ,
]
);
if ( $validator -> fails ()) {
$validationErrors = $validator -> errors () -> all ();
Log :: error ( " Stripe subscriptions plan validation error: " . implode ( " , " , $validationErrors ));
return jsonResponseWithErrorMessageApi ( $validationErrors , 203 );
}
// $stripeSecret = env('STRIPE_SECRET');
$stripe = new \Stripe\StripeClient ( $stripeSecret );
// dd($stripeSecret,$stripe,$request->all());
$stripeId = $request -> stripe_id ;
$timestamp = $request -> timestamp_to_update ;
2024-07-10 16:50:45 +05:30
2024-07-10 16:39:32 +05:30
$updateSubscription = $stripe -> subscriptions -> update (
$stripeId ,
[ 'trial_end' => $timestamp ] //8 RD aUG
);
//8 sept
// 13th aug
DB :: commit ();
return jsonResponseWithSuccessMessage ( 'success.save_data' , $updateSubscription );
} catch ( \Exception $ex ) {
DB :: rollBack ();
Log :: error ( 'Favourite Restaurant service failed: ' . $ex -> getMessage ());
return jsonResponseWithErrorMessageApi ( __ ( 'auth.something_went_wrong' ), 500 );
}
}
2024-07-03 18:01:09 +05:30
public function validateSubscriptionsproductForm ( Request $request )
{
return Validator :: make (
$request -> all (),
[
'product_name' => 'required' ,
'product_value' => 'required' ,
'product_details' => 'required' ,
]
);
}
//created by; Hritik
//On - 02th July ,2024
//use - to Create Subscription
public function subscriptionToPlan ( Request $request )
{
2024-08-02 16:35:51 +05:30
2024-07-03 18:01:09 +05:30
try {
2024-08-02 14:52:42 +05:30
$isCustomReferralCodeApplied = 0 ;
2024-08-06 11:20:06 +05:30
$customReferralCode = CustomReferralCode :: where ( 'referral_code' , $request -> referral_code_to_check ) -> where ( 'is_active' , 1 ) -> first ();
2024-08-02 14:52:42 +05:30
if ( $customReferralCode && $customReferralCode -> referral_code &&
$customReferralCode -> referral_code == $request -> referral_code_to_check ){
$isCustomReferralCodeApplied = 1 ;
} else {
$isCustomReferralCodeApplied = 0 ;
}
2024-07-08 13:05:55 +05:30
$isReferralSubscription = 0 ;
$referralUserId = $request -> referral_user_id ;
2024-07-10 16:39:32 +05:30
if ( $referralUserId && $referralUserId != null ) {
2024-07-08 13:05:55 +05:30
$isReferralSubscription = 1 ;
2024-07-10 16:39:32 +05:30
} else {
2024-07-08 13:05:55 +05:30
$isReferralSubscription = 0 ;
}
2024-07-03 18:01:09 +05:30
$userId = $request -> user_id ;
$subscriptionProductXid = $request -> subscription_product_xid ;
$stripeSecret = ( config ( 'constants.subscription.stripe_secret_key' ));
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$stripe = new \Stripe\StripeClient ( $stripeSecret );
// $taxId = (config('services.stripe.tax_id'));
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$userData = IamPrincipal :: where ( 'id' , $userId ) -> first ();
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
if ( $userData ) {
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
$subscriptionPlan = SubscriptionProducts :: where ( 'id' , $subscriptionProductXid ) -> first ();
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
if ( $subscriptionPlan && $subscriptionPlan -> stripe_price_id && $subscriptionPlan -> stripe_price_id != null ) {
$checkout = $stripe -> checkout -> sessions -> create ([
'customer_email' => $userData -> email_address ,
'line_items' => [
[
'price' => $subscriptionPlan -> stripe_price_id ,
'quantity' => 1 ,
// 'tax_rates'=>[$taxId],
],
],
'mode' => 'subscription' ,
// 'allow_promotion_codes' => true,
// 'tax_id_collection' => ['enabled' => true],
'metadata' => [
'userId' => $userData -> id ,
'userEmail' => $userData -> email_address ,
2024-07-10 16:39:32 +05:30
'subscriptionProductXid' => $subscriptionProductXid ,
2024-07-08 13:05:55 +05:30
2024-07-10 16:39:32 +05:30
'is_referral_subscription' => $isReferralSubscription ,
'referral_user_id' => $referralUserId ,
2024-08-02 14:52:42 +05:30
'isCustomReferralCodeApplied' => $isCustomReferralCodeApplied
2024-07-08 13:05:55 +05:30
2024-07-03 18:01:09 +05:30
],
'success_url' => route ( 'thankyou' ),
2024-07-03 17:08:20 +05:30
2024-07-03 18:01:09 +05:30
// 'cancel_url' => "http://localhost/cheerstothe_season_2_o/my-subscription-page",
]);
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
return Redirect :: away ( $checkout -> url );
//note:-for now i am just sending the cveonly=0 and lmacve=1 for reference .'&lmacve=1&cveonly=0'
} else {
return redirect () -> back () -> with ([ 'error' => " Something went wrong while subscription! " ]);
2024-06-28 19:30:31 +05:30
2024-07-03 18:01:09 +05:30
}
2024-06-28 19:30:31 +05:30
2024-07-02 19:20:18 +05:30
}
2024-07-03 18:01:09 +05:30
} catch ( \Exception $e ) {
2024-07-10 16:50:45 +05:30
Log :: error ( " An error occurred in " . __METHOD__ . " : " . $e -> getMessage (), [ 'exception' => $e ]);
2024-07-03 18:01:09 +05:30
return jsonResponseWithErrorMessage ( __ ( 'auth.something_went_wrong' ));
// return redirect()->back()->with(['error' => "Something went wrong while subscription!" . $e->getMessage()]);
2024-06-28 19:30:31 +05:30
}
2024-07-03 18:01:09 +05:30
}
public function thankyou ( Request $request )
{
return view ( 'Admin.pages.subscriptions.thank-you' );
}
//created by; Hritik
//On - 28th May ,2024
//use - To cancel the subscription of user on end of subscription end period
public function cancelSubscription ( Request $request )
{
try {
DB :: beginTransaction ();
2024-07-05 19:32:58 +05:30
$stripeSecret = ( config ( 'constants.subscription.stripe_secret_key' ));
// $stripeSecret = env('STRIPE_SECRET');
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
$stripe = new \Stripe\StripeClient ( $stripeSecret );
$userId = $request -> iam_principal_xid ;
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
// dd($request->all(),$stripeSecret);
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
$getSubscriptionData = Subscriptions :: where ( 'iam_principal_xid' , $userId ) -> where ( 'subscription_status' , 'active' ) -> first ();
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
$subscriptionId = $getSubscriptionData -> subscription_id ;
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
$cancelledSubscription = $stripe -> subscriptions -> update (
$subscriptionId ,
[ 'cancel_at_period_end' => true ]
);
2024-06-28 19:30:31 +05:30
2024-07-05 19:32:58 +05:30
$subscriptionFromDatabase = Subscriptions :: where ( 'subscription_id' , $subscriptionId ) -> first ();
$subscriptionFromDatabase -> cancelled_at = date ( 'Y-m-d H:i:s' , $cancelledSubscription -> canceled_at );
2024-06-28 19:30:31 +05:30
2024-07-05 19:32:58 +05:30
$subscriptionFromDatabase -> subscription_status = $cancelledSubscription -> status ;
$subscriptionFromDatabase -> is_cancelled_subscription = 1 ; //2 for cancelled
$subscriptionFromDatabase -> status = " cancelled " ;
$subscriptionFromDatabase -> save ();
2024-06-28 19:30:31 +05:30
2024-07-05 19:32:58 +05:30
$getSubscription = $stripe -> subscriptions -> retrieve ( $subscriptionFromDatabase -> subscription_id , []);
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
$getSubscribeCustomer = $stripe -> customers -> retrieve (
$subscriptionFromDatabase -> stripe_customer_id ,
[]
);
DB :: commit ();
2024-07-11 11:24:51 +05:30
return redirect () -> route ( 'cancel-subscription-thank-you' );
2024-07-05 19:32:58 +05:30
} catch ( \Exception $e ) {
2024-07-03 18:01:09 +05:30
DB :: rollBack ();
2024-07-10 16:50:45 +05:30
Log :: error ( " An error occurred in " . __METHOD__ . " : " . $e -> getMessage (), [ 'exception' => $e ]);
2024-07-05 19:32:58 +05:30
return redirect () -> back () -> with ([ 'error' => " Something went wrong while cancelling subscription! " . $e -> getMessage ()]);
2024-07-05 19:35:49 +05:30
// return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
2024-07-03 18:01:09 +05:30
}
}
2024-07-11 11:24:51 +05:30
/**
* Created By : Hritik
* Created at : 05 Jult 2024
* Use : To get Cancel Subscription Success page .
*/
public function cancelSubscriptionThankYou ( Request $request )
{
return view ( 'Admin.pages.subscriptions.cancel-thank-you' );
}
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
/**
* Created By : Hritik
* Created at : 05 Jult 2024
* Use : To Apply Referral Code and return the User Id .
*/
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
public function applyReferralCode ( Request $request )
{
try {
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
$referralCode = $request -> input ( 'referral_code' );
$currentUserId = $request -> input ( 'current_iam_principal_xid' );
$code = IamPrincipal :: where ( 'referral_code' , $referralCode ) -> where ( 'id' , '!=' , $currentUserId ) -> where ( 'principal_type_xid' , 3 ) -> first ();
2024-08-02 14:52:42 +05:30
$customCode = CustomReferralCode :: where ( 'is_active' , 1 ) -> where ( 'referral_code' , $referralCode ) -> first ();
if ( $code ) {
return response () -> json ([ 'success' => true , 'message' => 'Successfully applied referral code!' , 'referralUserId' => $code -> id , 'referral_code' => $referralCode ]);
} else if ( $customCode ) {
2024-08-02 16:32:30 +05:30
return response () -> json ([ 'success' => true , 'message' => 'Successfully applied referral code!' , 'referralUserId' => 2 , 'referral_code' => $referralCode ]); //2 is admin Id //need to change when principal typr_xid = 1
2024-08-02 14:52:42 +05:30
} else {
2024-07-05 19:32:58 +05:30
return response () -> json ([ 'success' => false , 'message' => 'Invalid referral code.' ]);
}
2024-07-03 18:01:09 +05:30
2024-07-05 19:32:58 +05:30
} catch ( \Exception $e ) {
Log :: error ( " An error occurred in " . __METHOD__ . " : " . $e -> getMessage (), [ 'exception' => $e ]);
return jsonResponseWithErrorMessage ( __ ( 'auth.something_went_wrong' ));
}
}
2024-07-03 18:01:09 +05:30
}