#Mr.Medi
1 messages ยท Page 1 of 1 (latest)
do you have more context like an exact error message and request ID req_xxxx?
yes, req_jXu1A2LlXP4ZkA
that is my request error
The error is "Invalid token id: pm_1NR97MJKetCvGTPaMxVMmyNI",
that request makes no sense unfortunately, you already have a PaymentMethod object, you should just use that directly.
for whatever reason you are acting as though you have a Token (tok_xxx)and need toconvert it to a PaymentMehtod, but you already have the PaymentMethod(with the ID pm_xxx)
you can see this from looking at https://dashboard.stripe.com/test/logs/req_jXu1A2LlXP4ZkA
Oh I see, is there any way in django to createOrgetCustomer function like in laravel?
I don;'t know anything about Django or the library you're using
so, the problem is that the customer already have a payment method?
no, the problem is that you created a PaymentMethod pm_xxx and for some reason you are passing it to code that is expecting to have a tok_xxx and needs to convert it to a pm_xxx
what is tok used for?
I guess the problem lies in ```python
customer = stripe.Customer.create(
email=request.user.email
)
payment_method = stripe.PaymentMethod.create(
type="card",
card={
"token": token,
},
)
stripe.PaymentMethod.attach(payment_method.id, customer=customer.id, )
yes
token is the pm that you mention
shouldn't be used for anything, it's a legacy way to integrate
overall you should be using a SetupIntent here and not attaching a PaymentMethod in backend code. https://stripe.com/docs/payments/save-and-reuse?platform=web
i have a setupintent as well in stripe elements in the frontend
ok. Then either way, you need to not be doing this call to stripe.PaymentMethod.create if your frontend code is already creating PaymentMethods and not tokens
presumably you have this code from some old legacy thing you're upgrading that used to use tokens
i will try, but the customer dont have any payment method
I mean of course not?
the code that would do the attachment crashed with an error, which is what we're talking about here ๐
Sorry for asking a lot of questions, but I remove that lines i get this error This customer has no attached payment source or default payment method. Please consider adding a default payment method. For more information, visit https://stripe.com/docs/billing/subscriptions/payment-methods-setting#payment-method-priority. with req_id req_9HROJA6iQ1xt2t
Is the front supposed to create it?
var stripe = Stripe('{{stripe_publishable_key}}');
var style = {
base: {
color: "#32325d",
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: "antialiased",
fontSize: "16px",
"::placeholder": {
color: "#aab7c4"
}
},
invalid: {
color: "#fa755a",
iconColor: "#fa755a"
}
};
const elements = stripe.elements()
const cardElement = elements.create('card', {style:style});
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', 'token')
token.setAttribute('value', setupIntent.payment_method)
form.appendChild(token)
form.submit();
}
})
this is my front, I just add the token in the form when the user click the button to purchase it
that code will only attach a card to a customer if you passed a customer ID when creating the SetupIntent
or you need to attach it later after the SI succeeds (which your code you showed earlier tries to do perhaps but fails due to the issue explained)
and also you need to pass https://stripe.com/docs/api/subscriptions/create#create_subscription-default_payment_method when creating the subscription so the card would actually be used
but really none of this is the correct way to build it, you should be using the guide https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements (you create the Subscription first, then use the Invoice's PaymentIntent on the frontend, which accepts the card, saves it, charges it and activates the subscription all in one step).
Thank you!
Im following the guide, but in the frontend says that Your code called confirmPayment() but you passed a client_secret associated with a SetupIntent. Did you mean to call confirmSetup() instead? Do I have to call it or is that error because the customer already exists?
no, it means you're not really following the guide correctly
you wouldn't use a SetupIntent at all here ,you'd use the Invoice's PaymentIntent
I see, when the user makes the GET request to the plan im passing the setupintent
there is no way to create the customer and subscription once the user buys it?
not really following
but yes if you want to do it the other way where you create the Subscription after the PaymentMethod is created, you 'can' but it's highly recommended against. The recommended approach is https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements (you create the Subscription first when the customer visits the "sign up" page, then use the Invoice's PaymentIntent on the frontend, which accepts the card, saves it, charges it and activates the subscription all in one step)
Im starting to get it. Now a get request to https://merchant-ui-api.stripe.com/elements/sessions? is being made to create the session
that's an internal part of the integration but sure, that's normal
Yeah, in the stripe js, right? Thank you very much for your help ๐ i will follow with the guide