Skip to content

Enhancement: Improve BillableMiddleware for SPAs | revive PR #167 #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rvibit
Copy link
Contributor

@rvibit rvibit commented Jun 21, 2025

Note: This is not my original work, its a copy of #167 by @ggelashvili

Overview

This PR enhances the Billable middleware to provide better support for SPAs by allowing AJAX requests to receive a JSON response with a redirect URL. Currently, Billable middleware and the billing_enabled configuration have no use for SPAs, & causes confusion which this PR aims to rectify.

Changes

The updated Billable middleware now checks if the incoming request is an AJAX request and, if billing is enabled, returns a 403 status code alongside a JSON response containing a forceRedirectUrl field. This field provides a URL to which the SPA can redirect the user. The updated middleware assumes that the host parameter is included in the request, which can be easily implemented using an axios interceptor (if using axios on front-end).

By returning a JSON response and a 403 status code, the front-end can now hook into the response interceptor, catch 403 errors, extract the redirect URL from the response, and manage redirection on the front-end. This makes the Billable middleware more useful for SPAs.

I added my own middleware in a project I'm working on with React front-end & worked perfectly, so thought it might be useful to improve the original Billable middleware.

Note that the wiki would need to be updated if this gets approved/merged.

This should not be a breaking change since Billable middleware should not be used anyways for traditional SPAs.

@Kyon147
Copy link
Owner

Kyon147 commented Jul 16, 2025

Hi @rvibit thanks for the PR.

Can you just fix the merge conflict, it is because we changed up how we handle the template engine to make the keys more generic i.e. SPA rather than REACT and MPA rather than Blade

I'll re-review it and get it into the next release.

@rvibit rvibit force-pushed the billable-middleware-SPA-support branch from f60e95d to 8e87d37 Compare July 18, 2025 11:28
@rvibit
Copy link
Contributor Author

rvibit commented Jul 18, 2025

Hi @rvibit thanks for the PR.

Can you just fix the merge conflict, it is because we changed up how we handle the template engine to make the keys more generic i.e. SPA rather than REACT and MPA rather than Blade

I'll re-review it and get it into the next release.

updated the PR, please check

@SDGPeteBatin
Copy link

@rvibit did you do the Laracasts tutorial too?

@rvibit
Copy link
Contributor Author

rvibit commented Aug 5, 2025

@rvibit did you do the Laracasts tutorial too?

No I don't

@SDGPeteBatin
Copy link

@rvibit did you do the Laracasts tutorial too?

No I don't

Oh, I've just realised! @ggelashvili from #167 is the guy that published the course on Laracasts! That's why I recognised it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants