This commit is contained in:
sayliraut
2024-07-08 14:52:37 +05:30
5 changed files with 261 additions and 132 deletions

View File

@@ -1,5 +1,4 @@
<?php
<?php
namespace App\Exports;
use App\Models\IamPrincipal;
@@ -30,7 +29,7 @@ class ReportExport implements FromView
$data = collect();
if ($this->reportType === 'Total Subscribed') {
$query = Subscriptions::with(['iamPrincipal.state']);
$query = Subscriptions::query();
if (!empty($this->states)) {
$query->whereHas('iamPrincipal', function ($q) {
@@ -96,9 +95,49 @@ class ReportExport implements FromView
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
} elseif ($this->reportType === 'Subscriptions Cancelled') {
$query = Subscriptions::query()->where('is_cancelled_subscription', 1);
if (!empty($this->states)) {
$query->whereHas('iamPrincipal', function ($q) {
$q->whereIn('state_xid', $this->states);
});
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
}
elseif ($this->reportType === 'Subscriptions Cancelled') {
$query = Subscriptions::with('iamPrincipal')->where('is_cancelled_subscription', 1);
if (!empty($this->states)) {
$query->whereHas('iamPrincipal', function ($q) {
$q->whereIn('state_xid', $this->states);
});
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
}
return view('exports.report', [
'data' => $data,
'reportType' => $this->reportType

View File

@@ -15,13 +15,15 @@ use Stripe\Stripe;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use DateTime;
class StripeWebhookController extends Controller
{
//
public function getWebhook(Request $request){
public function getWebhook(Request $request)
{
// dd("ssssss",$request);
Log::info('Stripe Webhook Received= in getWebhook ');
Log::info('Stripe Webhook Received: ' . $request->getContent());
@@ -81,6 +83,47 @@ class StripeWebhookController extends Controller
$userEmail = $metadata->userEmail;
$subscriptionProductId = $metadata->subscriptionProductXid;
$isReferralSubscription = $metadata->is_referral_subscription;
$referralUserId = $metadata->referral_user_id;
if ($isReferralSubscription == 1) {
//who has refer the Subscription to Current user
Log::info("Referral User Subscription Updating Function Starts");
$referralUserSubscriptionData = Subscriptions::where('iam_principal_xid', $referralUserId)->where('subscription_status', '=', 'active')->first();
if ($referralUserSubscriptionData && $referralUserSubscriptionData->is_cancelled_subscription == 0) {
//update subscription add 30 days trial period to this subscription
$nextPaymentDate = $referralUserSubscriptionData->next_payment_date;
$date = new DateTime($nextPaymentDate);
$date->modify('+30 days');
$newDate = $date->format('Y-m-d H:i:s');
Log::info("new Date = ");
Log::info($newDate);
$timestampForReferralUser = $date->getTimestamp();
$updateSubscription = $stripe->subscriptions->update(
$referralUserSubscriptionData->subscription_id,
['trial_end' => $timestampForReferralUser]
);
$SubscriptionObject = $stripe->subscriptions->retrieve($referralUserSubscriptionData->subscription_id, []);
$upcoming_invoice = $stripe->invoices->upcoming([
'subscription' => $referralUserSubscriptionData->subscription_id, // use retrieved id from subscription
]);
$referralUserSubscriptionData->current_period_start = date('Y-m-d H:i:s', $SubscriptionObject->current_period_start);
$referralUserSubscriptionData->current_period_end = date('Y-m-d H:i:s', $SubscriptionObject->current_period_end);
$referralUserSubscriptionData->next_payment_date = date('Y-m-d H:i:s', $upcoming_invoice->next_payment_attempt);
$referralUserSubscriptionData->save();
Log::info("Referral User Subscription Updated with Referral");
//updated the Referral Users Subscription.
}
}
$subscriptionProductData = SubscriptionProducts::where('id', $subscriptionProductId)->first();
//checkout store in db
@@ -115,6 +158,41 @@ class StripeWebhookController extends Controller
]
);
if ($isReferralSubscription == 1) {
Log::info("new User Subscription Updating Function Starts");
//update Current User subscription to add 30 days additional to their subscription end
$SubscriptionObject = $stripe->subscriptions->retrieve($subscriptionData->subscription, []);
$newUserSubscriptionDetails = Subscriptions::where('iam_principal_xid', $userId)
->where('subscription_product_xid', $subscriptionProductId)->first();
$nextPaymentDate = $newUserSubscriptionDetails->next_payment_date;
$date = new DateTime($nextPaymentDate);
$date->modify('+30 days');
$newDate = $date->format('Y-m-d H:i:s');
Log::info("new Date = ");
Log::info($newDate);
$timestampForReferralUser = $date->getTimestamp();
$updateSubscription = $stripe->subscriptions->update(
$newUserSubscriptionDetails->subscription_id,
['trial_end' => $timestampForReferralUser]
);
$SubscriptionObject = $stripe->subscriptions->retrieve($newUserSubscriptionDetails->subscription_id, []);
$upcoming_invoice = $stripe->invoices->upcoming([
'subscription' => $newUserSubscriptionDetails->subscription_id, // use retrieved id from subscription
]);
$newUserSubscriptionDetails->current_period_start = date('Y-m-d H:i:s', $SubscriptionObject->current_period_start);
$newUserSubscriptionDetails->current_period_end = date('Y-m-d H:i:s', $SubscriptionObject->current_period_end);
$newUserSubscriptionDetails->next_payment_date = date('Y-m-d H:i:s', $upcoming_invoice->next_payment_attempt);
$newUserSubscriptionDetails->save();
Log::info("new User Subscription Updated with Referral");
}
$getUserData = IamPrincipal::where('id', $userId)->first();
$title = "Congratulations you subscription is now active";
@@ -140,10 +218,6 @@ class StripeWebhookController extends Controller
}
return response('Webhook received', 200);
}
}

View File

@@ -183,6 +183,13 @@ class SubscriptionController extends Controller
// dd($request->all());
try {
$isReferralSubscription = 0;
$referralUserId = $request->referral_user_id;
if( $referralUserId && $referralUserId != null){
$isReferralSubscription = 1;
}else{
$isReferralSubscription = 0;
}
$userId = $request->user_id;
$subscriptionProductXid = $request->subscription_product_xid;
@@ -214,9 +221,12 @@ class SubscriptionController extends Controller
// 'tax_id_collection' => ['enabled' => true],
'metadata' => [
'userId' => $userData->id,
'userEmail' => $userData->email_address,
'subscriptionProductXid' => $subscriptionProductXid
'subscriptionProductXid' => $subscriptionProductXid ,
'is_referral_subscription'=>$isReferralSubscription,
'referral_user_id'=>$referralUserId,
],
'success_url' => route('thankyou'),

View File

@@ -18,30 +18,6 @@ class Subscriptions extends Model
}
protected $fillable = [
'id',
'subscription_product_xid',
'iam_principal_xid',
'amount',
'stripe_customer_id',
'payment_intent_id',
'payment_intent_client_secret',
'subscription_id',
'subscription_status',
'current_period_start',
'current_period_end',
'status',
'next_payment_date',
'is_cancelled_subscription',
'cancelled_at',
'is_active',
'deleted_at',
'created_by',
'modified_by',
'created_at',
'updated_at'
];
public function iamPrincipal()
{

View File

@@ -1,111 +1,141 @@
<!DOCTYPE html>
<html>
<head>
<title>Report</title>
</head>
<body>
<h1>{{ $reportType }} Report</h1>
@if ($reportType === 'Total Subscribed')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Subscription ID</th>
<th>User First Name</th>
<th>User Last Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $subscription)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $subscription->id }}</td>
<td>{{ $subscription->iamPrincipal->first_name ?? 'N/A' }}</td>
<td>{{ $subscription->iamPrincipal->last_name ?? 'N/A' }}</td>
<td>{{ $subscription->iamPrincipal->state->name ?? 'N/A' }}</td>
<td>{{ $subscription->created_at ?? 'N/A' }}</td>
</tr>
@endforeach
</tbody>
</table>
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Subscription ID</th>
<th>User First Name</th>
<th>User Last Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $subscription)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $subscription->id }}</td>
<td>{{ $subscription->iamPrincipal->first_name ?? 'N/A' }}</td>
<td>{{ $subscription->iamPrincipal->last_name ?? 'N/A' }}</td>
<td>{{ $subscription->iamPrincipal->state->name ?? 'N/A' }}</td>
<td>{{ $subscription->created_at ?? 'N/A' }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Total Users')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>User ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $user)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $user->id }}</td>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
<td>{{ $user->state->name }}</td>
<td>{{ $user->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>User ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $user)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $user->id }}</td>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
<td>{{ $user->state->name }}</td>
<td>{{ $user->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Redemptions')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Redemption ID</th>
<th>Restaurant Name</th>
<th>Customer Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $redemption)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $redemption->id }}</td>
<td>{{ $redemption->restaurant->name }}</td>
<td>{{ $redemption->customer->first_name }} {{ $redemption->customer->last_name }}</td>
<td>{{ $redemption->customer->state->name }}</td>
<td>{{ $redemption->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Redemption ID</th>
<th>Restaurant Name</th>
<th>Customer Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $redemption)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $redemption->id }}</td>
<td>{{ $redemption->restaurant->name }}</td>
<td>{{ $redemption->customer->first_name }} {{ $redemption->customer->last_name }}</td>
<td>{{ $redemption->customer->state->name }}</td>
<td>{{ $redemption->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Redemptions for Specific Restaurants')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Redemption ID</th>
<th>Restaurant Name</th>
<th>Customer Name</th>
<th>State</th>
<th>Created At</th>
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Redemption ID</th>
<th>Restaurant Name</th>
<th>Customer Name</th>
<th>State</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $redemption)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $redemption->id }}</td>
<td>{{ $redemption->restaurant->name }}</td>
<td>{{ $redemption->customer->first_name }} {{ $redemption->customer->last_name }}</td>
<td>{{ $redemption->customer->state->name }}</td>
<td>{{ $redemption->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Subscriptions Cancelled')
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>State</th>
<th>Subscription Status</th>
<th>Cancelled At</th>
</tr>
</thead>
<tbody>
@foreach($data as $item)
<tr>
<td>{{ $item->iamPrincipal->first_name }}</td>
<td>{{ $item->iamPrincipal->last_name }}</td>
<td>{{ $item->iamPrincipal->email_address }}</td>
<td>{{ $item->iamPrincipal->state->name }}</td>
<td>{{ $item->subscription_status }}</td>
<td>{{ \Carbon\Carbon::parse($item->cancelled_at)->format('m-d-Y') }}</td>
</tr>
</thead>
<tbody>
@foreach($data as $redemption)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $redemption->id }}</td>
<td>{{ $redemption->restaurant->name }}</td>
<td>{{ $redemption->customer->first_name }} {{ $redemption->customer->last_name }}</td>
<td>{{ $redemption->customer->state->name }}</td>
<td>{{ $redemption->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
@endforeach
</tbody>
</table>
@endif
</body>
</html>
</html>