【发布时间】:2021-11-25 04:42:24
【问题描述】:
我有一个向 Stripe 发送请求的表单,以便我的用户添加付款方式:
<form id="card-form">
<label>Card details</label>
<div id="card-element"></div>
<button wire:target="addPaymentMethod" wire:loading.class="hidden" class="button round" id="card-button">Add payment card</button>
<!-- Loading spinner -->
<svg wire:target="addPaymentMethod" wire:loading class="inline-block animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</form>
(精简后的)JavaScript 如下所示:
const stripe = Stripe('stripe-public-key');
const elements = stripe.elements();
const form = document.getElementById('card-form')
const cardButton = document.getElementById('card-button');
const cardElement = elements.create('card');
cardElement.mount('#card-element');
form.addEventListener('submit', async (e) => {
e.preventDefault();
cardButton.disabled = true;
const {
setupIntent,
error
} = await stripe.confirmCardSetup(
clientSecret, {
payment_method: {
card: cardNumber,
billing_details: {
name: 'Oliver'
}
}
}
);
if (error) {
// Display error.message to the user...
cardButton.disabled = false
console.log(error.message)
} else {
// The card has been verified successfully...
@this.addPaymentMethod(setupIntent.payment_method)
}
});
如果上面的async方法响应成功,会触发Livewire组件下面的方法:
public function addPaymentMethod($paymentMethod)
{
//Create the payment method in Stripe...
auth()->user()->addPaymentMethod($paymentMethod);
//Other validation etc...
}
所有这些的输出都在下面的 gif 中捕获。正如您在此处看到的,用户输入信用卡详细信息并单击“添加支付卡”并被重定向到另一个页面。
问题是,加载状态会开始一段时间,但在实际请求/重定向完成之前就消失了。
在对 Stripe 的实际请求完成之前,如何显示加载微调器?
【问题讨论】:
-
我认为你需要使用队列
-
@Mauro 为此我不能使用队列,因为我还需要在 Stripe 中处理 SCA。
标签: laravel laravel-livewire alpine.js