subscription with referral updated
This commit is contained in:
@@ -97,7 +97,7 @@ class SubscriptionController extends Controller
|
||||
public function createStripeProduct(Request $request)
|
||||
{
|
||||
|
||||
|
||||
|
||||
try {
|
||||
|
||||
DB::beginTransaction();
|
||||
@@ -260,98 +260,83 @@ class SubscriptionController extends Controller
|
||||
public function cancelSubscription(Request $request)
|
||||
{
|
||||
try {
|
||||
Log::info("Razorpay Cancel subscriptions Begin: ");
|
||||
DB::beginTransaction();
|
||||
$stripeSecret = (config('constants.subscription.stripe_secret_key'));
|
||||
// $stripeSecret = env('STRIPE_SECRET');
|
||||
|
||||
$validator = $this->validateCancelSubscriptionForm($request);
|
||||
$stripe = new \Stripe\StripeClient($stripeSecret);
|
||||
$userId = $request->iam_principal_xid;
|
||||
|
||||
if ($validator->fails()) {
|
||||
$validationErrors = $validator->errors()->all();
|
||||
Log::error("Razorpay Cancel subscriptions validation error: " . implode(", ", $validationErrors));
|
||||
return jsonResponseWithErrorMessageApi($validationErrors, 203);
|
||||
}
|
||||
// dd($request->all(),$stripeSecret);
|
||||
|
||||
$iamPrincipalId = $request->iam_principal_xid;
|
||||
$razorpaySubscriptionId = $request->subscription_id;
|
||||
$subscriptionXid = $request->subscription_xid;
|
||||
$getSubscriptionData = Subscriptions::where('iam_principal_xid', $userId)->where('subscription_status', 'active')->first();
|
||||
|
||||
$getSubscriptionData = RazorpaySubscriptions::where(['id' => $subscriptionXid, 'iam_principal_xid' => $iamPrincipalId, 'subscription_id' => $razorpaySubscriptionId, 'isCancelledSubscription' => 0])->first();
|
||||
$subscriptionId = $getSubscriptionData->subscription_id;
|
||||
|
||||
$cancelledSubscription = $stripe->subscriptions->update(
|
||||
$subscriptionId,
|
||||
['cancel_at_period_end' => true]
|
||||
);
|
||||
|
||||
|
||||
if (!$getSubscriptionData) {
|
||||
return response()->json(['status' => 502, 'message' => 'Something went wrong while cancelling Subscription']);
|
||||
}
|
||||
|
||||
$api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret'));
|
||||
|
||||
$options = ['cancel_at_cycle_end' => 1];
|
||||
// Call the subscription create method with request parameters
|
||||
$response = $api->subscription->fetch($razorpaySubscriptionId)->cancel($options);
|
||||
$subscriptionFromDatabase = Subscriptions::where('subscription_id', $subscriptionId)->first();
|
||||
$subscriptionFromDatabase->cancelled_at = date('Y-m-d H:i:s', $cancelledSubscription->canceled_at);
|
||||
|
||||
$subscriptionFromDatabase->subscription_status = $cancelledSubscription->status;
|
||||
$subscriptionFromDatabase->is_cancelled_subscription = 1; //2 for cancelled
|
||||
$subscriptionFromDatabase->status = "cancelled";
|
||||
|
||||
$subscriptionFromDatabase->save();
|
||||
|
||||
|
||||
$getSubscription = $stripe->subscriptions->retrieve($subscriptionFromDatabase->subscription_id, []);
|
||||
|
||||
|
||||
$getSubscribeCustomer = $stripe->customers->retrieve(
|
||||
$subscriptionFromDatabase->stripe_customer_id,
|
||||
[]
|
||||
);
|
||||
|
||||
$endAt = date('Y-m-d H:i:s', $response->end_at);
|
||||
$dateTime = now();
|
||||
$currentDateTime = $dateTime->format('Y-m-d H:i:s');
|
||||
$getSubscriptionData->isCancelledSubscription = 1;
|
||||
$getSubscriptionData->cancelled_at = $currentDateTime;
|
||||
$getSubscriptionData->save();
|
||||
|
||||
DB::commit();
|
||||
Log::info("Razorpay Cancel subscriptions end: ");
|
||||
return redirect()->back()->with(['success' => "Your Subscription Cancelled Successfully!"]);
|
||||
|
||||
return response()->json(['status' => 200, 'end_date' => $endAt]);
|
||||
// return response()->json(['status' => 200, 'message' => 'Your Subscription has been cancelled Sucessfully']);
|
||||
|
||||
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
||||
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
||||
|
||||
return redirect()->back()->with(['error' => "Something went wrong while cancelling subscription!" . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
public function validateCancelSubscriptionForm(Request $request)
|
||||
|
||||
/**
|
||||
* Created By : Hritik
|
||||
* Created at : 05 Jult 2024
|
||||
* Use : To Apply Referral Code and return the User Id.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
public function applyReferralCode(Request $request)
|
||||
{
|
||||
return Validator::make(
|
||||
$request->all(),
|
||||
[
|
||||
'iam_principal_xid' => 'required',
|
||||
'subscription_id' => 'required',
|
||||
'subscription_xid' => 'required',
|
||||
]
|
||||
);
|
||||
try {
|
||||
|
||||
$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();
|
||||
|
||||
if ($code) {
|
||||
return response()->json(['success' => true, 'message' => 'Referral code applied successfully.', 'referralUserId' => $code->id]);
|
||||
} else {
|
||||
return response()->json(['success' => false, 'message' => 'Invalid referral code.']);
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
||||
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// public function subscriptionUpgrade(Request $request)
|
||||
// {
|
||||
// // dd($request->all());
|
||||
|
||||
// $razorPaysubscriptionProductXid = $request->razorpay_plan_id;
|
||||
// $productTypeId = $request->product_type_xid;
|
||||
// $productTierId = $request->product_tier_xid;
|
||||
// $iamId = $request->iam_principal_xid;
|
||||
|
||||
// $user_id = 12; // Change this to dynamic user ID
|
||||
|
||||
// // Retrieve the current monthly subscription
|
||||
// $currentSubscription = RazorpaySubscriptions::where('iam_principal_xid', $user_id)->first();
|
||||
|
||||
// if (!$currentSubscription) {
|
||||
// return response()->json(['status' => 502, 'message' => 'Something went wrong current user Subscription not found']);
|
||||
// }
|
||||
|
||||
// $products = Products::where('product_type_xid', $productTypeId)
|
||||
// ->where('product_tier_xid', $productTierId)
|
||||
// ->where('razorpay_plan_id', $razorPaysubscriptionProductXid)
|
||||
// ->where('is_active', '1')->first();
|
||||
|
||||
// if (!$products) {
|
||||
// return response()->json(['status' => 502, 'message' => 'Something went wrong current product not found']);
|
||||
// }
|
||||
|
||||
// $api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret'));
|
||||
// $plan_id = $products->razorpay_plan_id;
|
||||
|
||||
// // Call the subscription create method with request parameters
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user