Skip to content

Commit

Permalink
Update cancel button
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Mar 25, 2024
1 parent ac98ead commit 4a841ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
25 changes: 21 additions & 4 deletions resources/views/themes/tailwind/partials/cancel-modal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
</div>
<div class="mt-5 sm:mt-6 sm:flex sm:flex-row-reverse">
<span class="flex flex-1 w-full rounded-md shadow-sm sm:ml-3 sm:w-full">
<div data-url="{{ auth()->user()->subscription->cancel_url }}" @click="$store.confirmCancel.open=false" class="inline-flex justify-center w-full px-4 py-2 text-base font-medium leading-6 text-white transition duration-150 ease-in-out bg-red-600 border border-transparent rounded-md shadow-sm cursor-pointer checkout-cancel hover:bg-red-500 focus:outline-none focus:border-red-700 focus:shadow-outline-red sm:text-sm sm:leading-5">
Cancel Subscription
</div>

<div data-url="{{ auth()->user()->subscription->cancel_url }}" id="cancelSubscriptionButton" class="inline-flex justify-center w-full px-4 py-2 text-base font-medium leading-6 text-white transition duration-150 ease-in-out bg-red-600 border border-transparent rounded-md shadow-sm cursor-pointer checkout-cancel hover:bg-red-500 focus:outline-none focus:border-red-700 focus:shadow-outline-red sm:text-sm sm:leading-5">
Cancel Subscription
</div>
</span>
<span class="flex flex-1 w-full mt-3 rounded-md shadow-sm sm:mt-0 sm:w-full">
<button onclick="closeCancelModal()" type="button" class="inline-flex justify-center w-full px-4 py-2 text-base font-medium leading-6 text-gray-700 transition duration-150 ease-in-out bg-white border border-gray-300 rounded-md shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue sm:text-sm sm:leading-5">
Expand All @@ -50,4 +49,22 @@
window.closeCancelModal = function(){
Alpine.store('confirmCancel').close();
}
document.getElementById('cancelSubscriptionButton').addEventListener('click', function() {
fetch('/cancel', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
'Content-Type': 'application/json'
},
body: JSON.stringify({ subscription_id: this.getAttribute('data-url') })
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
closeCancelModal();
})
.catch(error => {
console.error('Error:', error);
});
});
</script>
2 changes: 1 addition & 1 deletion wave/resources/views/checkout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function waveUpdate(){
}
function waveCancel(){
axios.post('/cancel', { _token: csrf, id: subscriptionId })
axios.post('/cancel', { _token: csrf })
.then(function (response) {
if(parseInt(response.data.status) == 1){
window.location = '/settings/subscription';
Expand Down
21 changes: 12 additions & 9 deletions wave/src/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,46 @@ public function cancel(Request $request){
return response()->json(['status' => 1]);
}

private function cancelSubscription($subscription_id){
private function cancelSubscription(){
// Ensure user is authenticated
if (!auth()->check()) {
return redirect('/login')->with(['message' => 'Please log in to continue.', 'message_type' => 'danger']);
}

// Auth user get subscription id
$subscription_id = auth()->user()->subscription->subscription_id;

// Ensure the provided subscription ID matches the user's subscription ID
$localSubscription = PaddleSubscription::where('subscription_id', $subscription_id)->first();

if (!$localSubscription || auth()->user()->subscription->subscription_id != $subscription_id) {
return back()->with(['message' => 'Invalid subscription ID.', 'message_type' => 'danger']);
}

$response = Http::withToken($this->vendor_auth_code)
->post($this->paddle_url . '/subscriptions/' . $subscription_id . '/cancel', [
'effective_from' => 'next_billing_period'
'effective_from' => 'immediately'
]);

dd($response->json());
\Illuminate\Support\Facades\Log::info($response->body());

// Check if the request was successful
if ($response->successful()) {
$body = $response->json();
if (isset($body['success']) && $body['success']) {

if (isset($body['data']) && isset($body['data']['status']) && $body['data']['status'] == 'canceled') {

// Update subscription in local database
$localSubscription->cancelled_at = Carbon::now();
$localSubscription->cancelled_at = Carbon::parse($body['data']['canceled_at']);
$localSubscription->status = 'cancelled';
$localSubscription->save();

// Update user's role to "cancelled"
$user = User::find($localSubscription->user_id);
$cancelledRole = Role::where('name', '=', 'cancelled')->first();
$user->role_id = $cancelledRole->id;
$user->save();

return back()->with(['message' => 'Your subscription has been successfully canceled.', 'message_type' => 'success']);
} else {
// Handle any errors that were returned in the response body
Expand All @@ -79,7 +83,6 @@ private function cancelSubscription($subscription_id){
// Handle failed HTTP requests
return back()->with(['message' => 'Failed to cancel the subscription. Please try again later.', 'message_type' => 'danger']);
}

}

public function checkout(Request $request){
Expand Down

0 comments on commit 4a841ba

Please sign in to comment.