REPO STORY / リポジトリ紹介
ectoflow/vue-stripe-js
VueアプリケーションにStripe支払いコンポーネントを統合するライブラリですが、開発は停止しています。
01 / 何ができる?
どんな作業に
役立つ?
vue-stripe-jsはVueアプリケーション内でStripeの支払い機能をコンポーネント化するNPMライブラリです。StripeElementsとStripeElementコンポーネントを提供し、カード入力、支払い処理、エクスプレスチェックアウトなど複数の支払い要素をサポートしています。ただし開発者がメンテナンスを停止しており、npmパッケージは廃止予定で今後の更新は受け取りません。使用するにはStripeアカウントとpublishable keyが必要です。
- StripeElementsとStripeElementの2つのコンポーネント
- payment・card・IBAN・住所・銀行口座など複数の支払い要素タイプをサポート
- Vue Composition APIとTypeScriptで実装、オプションがリアクティブに更新される
Provider Patternで Stripe.js のすべてのメソッドへアクセス可能であり要素が自動で作成・破棄されますが、開発はメンテナンス中止のため新規採用は推奨されません。
02 / 自分に合う?
使いどころと、
選ぶ前の確認。
こんなときに
- Webストアで商品購入時にクレジットカード決済を受け付ける
- SaaSアプリケーションで定期請求を管理する
先に確認すること
以下の前提条件を確認し、必要に応じて環境を準備してください。
- Node.jsとnpm(またはyarn)
- Stripeアカウントとpublishable key
- Vue 3プロジェクト
ギット飯の日本語要約から、利用例と前提条件を紹介しています。
03 / 始め方
最初の一歩を、
順番に。
準備するもの
- Node.jsとnpm(またはyarn)
- Stripeアカウントとpublishable key
- Vue 3プロジェクト
- 依存パッケージをインストールします。
npm i vue-stripe-js @stripe/stripe-js
- Vue スクリプト内で Stripe.js をロードします。
import { onBeforeMount, ref } from "vue"
import { loadStripe, type Stripe } from "@stripe/stripe-js"
const publishableKey = '' // ご自身のpublishable keyに置き換え
const stripe = ref<Stripe | null>(null)
onBeforeMount(async() => {
stripe.value = await loadStripe(publishableKey)
})
別の方法としてスクリプトタグで読み込むこともできます。
<script src="https://js.stripe.com/v3/"></script>
- StripeElements と StripeElement コンポーネントで支払い要素を実装します。
<template>
<form
v-if="stripeLoaded"
@submit.prevent="handleSubmit"
>
<StripeElements
:stripe-key="stripeKey"
:elements-options="elementsOptions"
ref="elementsComponent"
>
<StripeElement
type="payment"
ref="paymentComponent"
/>
</StripeElements>
<button type="submit">送信</button>
</form>
</template>
<script setup lang="ts">
import { onBeforeMount, ref } from "vue"
import { loadStripe } from "@stripe/stripe-js"
import { StripeElements, StripeElement } from "vue-stripe-js"
const stripeKey = "pk_test_..." // ご自身のpublishable keyに置き換え
const elementsOptions = ref({
mode: "payment",
amount: 1099,
currency: "usd"
})
const stripeLoaded = ref(false)
onBeforeMount(() => {
loadStripe(stripeKey).then(() => {
stripeLoaded.value = true
})
})
async function handleSubmit() {
const stripeInstance = elementsComponent.value?.instance
const elements = elementsComponent.value?.elements
if (stripeInstance) {
const { error } = await stripeInstance.confirmPayment({
elements,
clientSecret: "client_secret_from_backend",
confirmParams: {
return_url: "https://example.com/order/complete"
}
})
}
}
</script>
注:PaymentIntentのclient_secretはバックエンド側で作成し、フロントエンドに送信する必要があります。
コマンドは要約からの引用です。実行やインストールの成否は検証していません。公式の所要時間は記載されていません。
04 / 関連リポジトリ
どんなリポジトリとつながる?
比較グラフのスナップショットから関係の種類と根拠を紹介します。現在公開中のリポジトリだけを表示します。
vue-stripe/vue-stripe ↗
共通するGitHubトピック:stripe-elements,stripe-checkout
repos.topicsの共通項(stripe-elements,stripe-checkout)
グラフの元データ ↗