0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Stripe Elementsを利用したカートシステムで、カートの変更や割引などPayment Intentsの変更を反映する

Posted at

ECサイトやウェブサービスでは、送料や割引の適用などで決済フロー中に請求金額が変わることがあります。

その場合、StripeのPayment IntentsとElementsを利用して決済を提供している場合、Payment Intentsのamountを更新して対応します。

stripe payment_intents update pi_xxx --amount 1090

この変更をPayment Elementsなどに反映させたい場合、useElementのもつelements.fetchUpdatesが利用できます。

const PaymentForm: React.FC = () => {
  const elements = useElements()

  return (
    <form>
      <PaymentElement />
      <button onClick={async () => {
        await elements?.fetchUpdates()
      }}>Sync</button>
    </form>
  )
}

fetchUpdatesを実行することで、Payment Elements内で表示される金額を更新できます。

スクリーンショット 2022-06-24 18.58.47.png

変更後のPaymentIntentsを再取得したい場合は、useStriperetrievePaymentIntentを実行しましょう。

const PaymentForm: React.FC = () => {
  const elements = useElements()
  const stripe = useStripe()

  return (
    <form>
      <PaymentElement />
      <button onClick={async () => {
        await elements?.fetchUpdates()
        await stripe?.retrievePaymentIntent('pi_xxxx_secret_xxxx')
      }}>Sync</button>
    </form>
  )
}

参考ドキュメント

[PR] Stripe開発者向け情報をQiitaにて配信中!

  • [Stripe Updates]:開発者向けStripeアップデート紹介・解説
  • ユースケース別のStripe製品や実装サンプルの紹介
  • Stripeと外部サービス・OSSとの連携方法やTipsの紹介
  • 初心者向けのチュートリアル(予定)

など、Stripeを利用してオンラインビジネスを始める方法について週に2〜3本ペースで更新中です。

-> Stripe Organizationsをフォローして最新情報をQiitaで受け取る

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?