#cashiersprit_docs

1 messages ยท Page 1 of 1 (latest)

odd leafBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1249732184596680907

๐Ÿ“ Have more to share? Add details, code, screenshots, videos, etc. below.

gray plazaBOT
golden holly
#

Hi there

#

Sounds like you want to collect a new payment method without taking payment at that moment and then set that payment method to be charged in the future for the Subscription, correct?

wild creek
#

everything could happen. the card may be working initially, but later, the card may be expired or lost, so there are cases, customer need to update their payment method

#

that's our thinking ๐Ÿ˜‰

golden holly
#

Sure, but I'm just asking if you are updating while taking a payment or updating in between those times, or both?

wild creek
#

my main concern is what if 1st payment successful, but 2nd payment failed, how customer could update payment method?

golden holly
#

So you still use Payment Element to collect the payment method, just using a SetupIntent to confirm it

#

Then you can update the default_payment_method for the Subscription

wild creek
#

I want to update existing subscription's payment method, not to create a new subscrpition.

golden holly
#

Correct, using a SetupIntent you don't create a new Sub

#

You just collect a new payment method

wild creek
#

oh, i c. I just let customer submit a new payment method, and then I can get the new payment method id, and I just update the payment_method_id to subscription's default_payment_method

#

am I right?

golden holly
#

Yep

wild creek
#

bingo. it should works. thanks very much for your advice!

#

can I ask 2nd question about best pratice?

golden holly
#

Sure

wild creek
#

in our work flow, payment element component is rendered at very first beginning, after render, customer can still change total price by updating quantity. but in https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription#add-the-payment-element-to-your-checkout-page, in render payment element stage, it already request to bind total amount for subscription. at that time, we actually don't know actual amount

Build an integration where you can render the Payment Element prior to creating a PaymentIntent or SetupIntent.

#

the actual amount will be settled when customer clidk submit button, so I just need to update subscription amount later?

golden holly
#

No you should wait to create the Subscription until you know the actual amount

wild creek
golden holly
#

That's not when you create the Subscription

#

See the docs you linked above, you create your Payment Element first and then you only create the Subscription on submission when you call your backend

wild creek
#

I cannot update subscription amount later by updating subscription api?

golden holly
#

Not really -- the initial Invoice will already be cut

#

It will be a mess if you are updating

#

The whole point of the above flow is you shouldn't need to update

wild creek
#

how mess will be? can I create a trial subscription? and then change to charged subscription?

golden holly
#

Why are you creating your Subscription before you know the final amount?

#

There shouldn't be any reason to do that

#

To be clear, you can render Payment Element without creating a Subscription

wild creek
#

interesting. I just followed your example, so

const options = {
  mode: 'subscription',
  amount: 1099,
  currency: 'usd',
  // Fully customizable with appearance API.
  appearance: {/*...*/},
};

// Set up Stripe.js and Elements to use in checkout form
const elements = stripe.elements(options);

// Create and mount the Payment Element
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');

so here mode and amount is optional? I can skip them? and then in

post '/create-subscription' do
  content_type 'application/json'
  data = JSON.parse(request.body.read)
  customer_id = cookies[:customer]
  price_id = data['priceId']

  subscription = Stripe::Subscription.create(
    customer: customer_id,
    items: [{
      price: price_id,
    }],
    payment_behavior: 'default_incomplete',
    payment_settings: {save_default_payment_method: 'on_subscription'},
    expand: ['latest_invoice.payment_intent', 'pending_setup_intent']
  )

  if subscription.pending_setup_intent != null
    { type: 'setup', clientSecret: subscription.pending_setup_intent.client_secret }.to_json
  else
    { type: 'payment', clientSecret: subscription.latest_invoice.payment_intent.client_secret }.to_json
  end
end

pass correct amount to create subscription, right?

golden holly
#

Mode and amount is not optional, no. But you can update that client-side at any point using elements.update()

#

And then yes, you just pass the correct amount (the correct Price really) on your backend when you actually create the Subscription

wild creek
#

haha, so

const options = {
  mode: 'subscription',
  amount: 1099,
  currency: 'usd',
  // Fully customizable with appearance API.
  appearance: {/*...*/},
};

the amount does not matter at all, as long as I pass correct amount in

 subscription = Stripe::Subscription.create(
    customer: customer_id,
    items: [{
      price: price_id,
    }],
    payment_behavior: 'default_incomplete',
    payment_settings: {save_default_payment_method: 'on_subscription'},
    expand: ['latest_invoice.payment_intent', 'pending_setup_intent']
  )

it will override payment element's amount?

golden holly
#

Yes

#

But you do want to use the correct amount client-side so that the correct payment method types are shown as options

#

Also if your customer uses something like a wallet (Google Pay / Apple Pay) then the client-side amount will show up in the UI

wild creek
golden holly
#

You update the Elements object, not the Payment Element

wild creek
#

is it the right place?

wild creek
#

i c , i c. perfectly, perfectly. that's everything I want. thank you so much!

golden holly
#

Sure thing

wild creek
#

that's every thing I want. thank you so much. you are great helpful.

#

I wish you enjoy the rest of your day!