Reputation: 171
I need to make a user subscription Stripe. I use the payment page:
public function paymentForm()
{
$id = \request()->get('id');
$plan = Plan::find(1);
$intent = auth()->user()->createSetupIntent();
return view("user.subscription", compact("plan", "intent", "id"));
}
// Post action
public function subscription(Request $request)
{
$plan = Plan::where('slug', 'standard')->first();
$subscription = $request->user()
->newSubscription($plan->slug, $plan->stripe_plan)
->create($request->paymentMethod);
$subscription->ends_at = Carbon::now()->addDays(6);
$subscription->save();
return redirect()->to('user/payment');
}
After sending the form, I get the following error: Attempt to read property "default_payment_method" on null.
dd($request->paymentMethod); //pm_1OWfTrIDQRzGQKp4IDkRK2Gi
JS code:
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('{{ env('STRIPE_KEY') }}')
const elements = stripe.elements()
const cardElement = elements.create('card');
cardElement.mount('#card-element')
const form = document.getElementById('payment-form')
const cardBtn = document.getElementById('card-button')
const cardHolderName = document.getElementById('card-holder-name')
form.addEventListener('submit', async (e) => {
e.preventDefault()
cardBtn.disabled = true
const { setupIntent, error } = await stripe.confirmCardSetup(
cardBtn.dataset.secret, {
payment_method: {
card: cardElement,
billing_details: {
name: cardHolderName.value
}
}
}
)
if(error) {
cardBtn.disable = false
} else {
let token = document.createElement('input')
token.setAttribute('type', 'hidden')
token.setAttribute('name', 'paymentMethod')
token.setAttribute('value', setupIntent.payment_method)
form.appendChild(token)
form.submit();
}
})
</script>
The data token is generated on the client side, but for some reason an error occurs.
Upvotes: 0
Views: 181
Reputation: 171
t worked.It was necessary to clear the fields(stripe_id, pm_type,pm_last_four) in the user table
Upvotes: 0