Nov 7, 2020
0
Level 7
Livewire + Alpine equivalent of axios call + do something with the return
I recently started playing with Livewire. There's one scenario where I can not find the right solution at the documentation and I need some advice from a more experienced Livewire + Alpinejs user.
For example, when working with Stripe, what I usually do is post an ajax request that will generate the session id at the backend and then return to the front where the Stripe code redirects the user to the payment page.
How would you implement this with Livewire and Alpine?
I've tried this but it doesn't work:
<div>
<button wire:click="$wire.checkout().then(result => console.log(result))">
<span>{{ $pricing->name }} ({{ $pricing->type }})</span>
<span>
{{ $pricing->price }} {{ $pricing->currency }}
</span>
</button>
</div>
Finally used this approach but, is there any better approach?
// Livewire method
$this->dispatchBrowserEvent('checkout-created', $session->id);
// Front
window.addEventListener('checkout-created', event => {
goToCheckout(event.detail)
})
Please or to participate in this conversation.