#ubi
1 messages · Page 1 of 1 (latest)
%Stripe.Error{
source: :stripe,
code: :invalid_request_error,
request_id: {"Request-Id", "req_IeeqsrVPG4l0Sm"},
extra: %{
http_status: 400,
raw_error: %{
"message" => "The provided PaymentMethod cannot be attached. To reuse a PaymentMethod, you must attach it to a Customer first.",
"request_log_url" => "https://dashboard.stripe.com/test/logs/req_IeeqsrVPG4l0Sm?t=1684436898",
"type" => "invalid_request_error"
}
},
message: "The provided PaymentMethod cannot be attached. To reuse a PaymentMethod, you must attach it to a Customer first.",
user_message: nil
}
I have the following in the FrontEnd code:
const createPaymentMethodResp = await stripe.createPaymentMethod({ elements });
if (createPaymentMethodResp.error) {
handleError(createPaymentMethodResp.error);
return;
}
Do I suppose to use another function?
Good question. That depends on what you are trying to do. Is the intended flow:
- Create a payment method client side
- Create and confirm a payment intent server-side with the payment method from step 1
?
I do not confirm the payments in the client
I am doing a recurring payment (installments, but without using Stripe components, implementing it from scratch
Right, you are confirming them server-side
I think I have a good idea of what you are trying to do now.
yep
Checking in to the best time to attach the PM and will get back to you
I think the best process for you would be:
- (client) Create PaymentMethod, send ID to server
- (server) Create a Customer
- (server) Create and confirm PaymentIntent, pass in both the PaymentMethod ID and Customer ID to the PaymentIntent creation request
You are getting that error because PaymentMethods are only single use unless they are attached to a customer first. If you attach the payment method while confirming that first payment, that will allow the PM to be re-used
I used the off_session stting but I still need the customer, right?!
Correct, you need both
I see, I am in event-driven land, so I could also create the customer upon IdentityRegistered event
Should I always create a PaymentMethod or should look existing ones?
If you already have a PaymentMethod saved, you can re-use the same object. You only need to create PaymentMethods when the user is adding new card details
How do I deal with an address being enter every time?
Is this a shipping address or billing address?
You can update either but I need to check in to how you can do that.
Gotcha. Checking in to how to update the billing address on an existing payment method.
Are you using the Stripe.js Address Element to collect addresses? Or are these just normal address fields on your page?
You have two options:
- Use that same form each time, in which case you will be creating a new PaymentMethod for each payment.
- If you only want them to re-enter address info, you can use the Address Element, which will present just the address part of that form. https://stripe.com/docs/elements/address-element/collect-addresses?platform=web
Both of them are valid ways to build your page. You can choose to do either. The second option would take some more complex logic but is definitely possible if you want to do that.
Gonna create it everytime, but I am guessing I need the Stripe Customer to move forward, thank you so much
Sounds like a plan. You can keep attaching payment methods to the same customer if you want. You would just look up the existing customer ID and then pass that in to the payment intent create call
hey, when creating the Customer, how could I attach my internal Identity ID?
I am using that, thus far
defmodule CourseCareers.Iam.StripeCustomerCreator do
alias CourseCareers.Iam.Identity
alias CourseCareers.Repo
use Oban.Pro.Worker,
queue: :iam_stripe_customer_creator,
unique: [keys: [:identity_id], period: :infinity]
@impl Oban.Pro.Worker
def process(%Oban.Job{} = job) do
identity = Repo.get!(Identity, job.args["identity_id"])
Stripe.Customer.create(%{
email: identity.email,
metadata: %{
iam_identity_id: identity.id,
},
name: identity.traits["first_name"],
})
|> OnePiece.Result.when_ok(:ok)
end
end
Now, the question is, how could I retrieve the customer by the metadata key?
Or do I have to save the customer ID in my side as well?
You likely want to record the customer ID in your own system too, for easy retrieval/use
But you can search by metadata if needed:
I see, thanks
NP!