#ubi

1 messages · Page 1 of 1 (latest)

elder jettyBOT
opal surge
#
%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?

elfin skiff
#

Good question. That depends on what you are trying to do. Is the intended flow:

  1. Create a payment method client side
  2. Create and confirm a payment intent server-side with the payment method from step 1
    ?
opal surge
#

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

elfin skiff
#

Right, you are confirming them server-side

#

I think I have a good idea of what you are trying to do now.

opal surge
#

YEP yep

elfin skiff
#

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:

  1. (client) Create PaymentMethod, send ID to server
  2. (server) Create a Customer
  3. (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

opal surge
#

I used the off_session stting but I still need the customer, right?!

elfin skiff
#

Correct, you need both

opal surge
#

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?

elfin skiff
#

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

opal surge
#

How do I deal with an address being enter every time?

elfin skiff
#

Is this a shipping address or billing address?

#

You can update either but I need to check in to how you can do that.

opal surge
#

Billing

#

I need the billing address to be entered to calculate taxes at that point

elfin skiff
#

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?

opal surge
#

React using Elements

elfin skiff
#

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.

opal surge
#

Gonna create it everytime, but I am guessing I need the Stripe Customer to move forward, thank you so much

elder jettyBOT
elfin skiff
#

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

opal surge
#

hey, when creating the Customer, how could I attach my internal Identity ID?

west quarry
#

You can use metadata for that

opal surge
#

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?

west quarry
#

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:

opal surge
#

I see, thanks

west quarry
#

NP!