@unlok-co/nuxt-stripe

@unlok-co/nuxt-stripe
這個 Nuxt 模組提供了一個簡單的方法來整合 Stripe 到你的 Nuxt 應用程式中,無論是在客戶端還是伺服器端。它在伺服器端使用官方的 stripe 套件,在客戶端使用 @stripe/stripe-js。
Stripe 的 Nuxt 模組
用於使用 stripe 的應用程式的 Nuxt 模組。
功能
這個 Nuxt 模組提供了一個簡單的方法來整合 Stripe 到你的 Nuxt 應用程式中,無論是在客戶端還是伺服器端。它在伺服器端使用官方的 stripe 套件,在客戶端使用 @stripe/stripe-js。
安裝
- 將
@unlok-co/nuxt-stripe
依賴項新增到你的專案中
npx nuxi@latest module add stripe-next
- 將
@unlok-co/nuxt-stripe
新增到nuxt.config.ts
的modules
區段
export default defineNuxtConfig({
modules: ["@unlok-co/nuxt-stripe"],
});
設定
關於所有可用的 serverConfig
選項,請查看官方儲存庫 README。而關於 clientConfig
選項,請查看官方文件。
使用選項
export default defineNuxtConfig({
modules: ["@unlok-co/nuxt-stripe"],
stripe: {
// Server
server: {
key: process.env.STRIPE_SECRET_KEY,
options: {
// your api options override for stripe server side
// https://github.com/stripe/stripe-node?tab=readme-ov-file#configuration
},
// CLIENT
},
client: {
key: process.env.STRIPE_PUBLIC_KEY,
// manualClientLoad: true, // if you want to have control where you are going to load the client
// your api options override for stripe client side https://stripe.com/docs/js/initializing#init_stripe_js-options
options: {},
},
},
});
或者使用 執行時設定
export default defineNuxtConfig({
modules: ["@unlok-co/nuxt-stripe"],
runtimeConfig: {
// Server
stripe: {
key: process.env.STRIPE_SECRET_KEY,
options: {},
},
// Client
public: {
stripe: {
key: process.env.STRIPE_PUBLIC_KEY,
options: {},
},
},
},
});
使用方法
伺服器端
此模組提供一個 useServerStripe
函式,用於在伺服器端建立 Stripe 實例。此實例可用於與 Stripe API 互動。
最小範例
import { defineEventHandler } from "h3";
import { useServerStripe } from "#stripe/server";
export default defineEventHandler(async (event) => {
const stripe = await useServerStripe(event);
console.info("Stripe instance:", stripe);
return {
version: stripe.VERSION,
};
});
為 stripe elements 產生付款意圖
您可以隨時在 playground/server/api/create-payment-intent.get.ts
中找到伺服器端的完整程式碼,在 playground/components/OtherComponent.vue
中找到客戶端的完整程式碼。
伺服器端範例
export default defineEventHandler(async (event) => {
const stripe = await useServerStripe(event);
const orderAmount = 1400;
let paymentIntent;
try {
paymentIntent = await stripe.paymentIntents.create({
currency: "usd",
amount: orderAmount,
automatic_payment_methods: { enabled: true },
});
return {
clientSecret: paymentIntent.client_secret,
error: null,
};
} catch (e) {
return {
clientSecret: null,
error: e,
};
}
});
客戶端範例
const { stripe } = useClientStripe();
watch(
stripe,
async () => {
if (stripe.value) {
// https://github.com/stripe-samples/accept-a-payment/blob/main/payment-element/client/vue-cva/src/components/SrCheckoutForm.vue
const { clientSecret, error } = await $fetch(
"/api/create-payment-intent"
);
if (error) {
console.error(error);
return;
}
const elements = stripe.value.elements({
clientSecret: clientSecret as string,
});
const linkAuthenticationElement = elements.create("linkAuthentication");
linkAuthenticationElement.mount("#linkAuthenticationElement");
}
},
{
immediate: true,
}
);
客戶端使用方法
在客戶端,您可以使用 useClientStripe
。這將會向您公開一個物件,其中包含
{
stripe, // This composable is a wrap around the [`loadStripe`](https://github.com/stripe/stripe-js#loadstripe) and can be used in pages or plugins.
isLoading, // You don't really need this in practice but we did expose it
loadStipe; // you can also manually loadStripe if you have disabled auto load for stripe
}
您可以在 playground/app.vue
檔案中看到實際使用的程式碼。
自動載入客戶端 stripe
<template>
<h1>Nuxt Stripe instance</h1>
<div>
{{ stripe ? stripe : "Loading..." }}
</div>
</template>
<script setup lang="ts">
import { watch } from "vue";
const { stripe, isLoading } = await useClientStripe();
</script>
手動載入客戶端 stripe
nuxt.config.ts
stripe: {
client: {
// ...
manualClientLoad: true, // this is the part you want
},
// ...
},
App.vue
import { useNuxtApp, useClientStripe } from "#imports";
const { loadStripe, stripe } = useClientStripe();
const nuxtApp = useNuxtApp();
// you can leave it empty if you already have defined the keys in the config or override like in this example
stripe.value = await loadStripe(nuxtApp.$config.public.stripe.key);
開發
初始步驟:複製此儲存庫
# Install dependencies
yarn install
npm install
# Generate type stubs
yarn dev:prepare
npm run dev:prepare
# Develop with the playground
yarn dev
npm run dev
# Build the playground
yarn dev:build
npm run dev:build
# Run ESLint
yarn lint
npm run lint
# Run Vitest
yarn test
yarn test:watch
npm run test
npm run test:watch
# Release new version
yarn release
npm run release