Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Stripe & Next.js + AWS Amplify で会員 + 定期課金機能 / ...
Search
Hidetaka Okamoto
September 03, 2021
Programming
3.3k
7
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Stripe & Next.js + AWS Amplify で会員 + 定期課金機能 / JP_Stripes20210903
Hidetaka Okamoto
September 03, 2021
More Decks by Hidetaka Okamoto
See All by Hidetaka Okamoto
OpenAI APIで API Changelogを要約してみた話 / chatgpt-osaka-1
hideokamoto
0
710
コミュニティ運営から 中の人に変わって感じたこと
hideokamoto
0
150
Developerが Developer Advocateになった話 / dev-rel-meetup-tokyo-71
hideokamoto
0
390
Jamstack開発者のための App Runner入門
hideokamoto
1
560
WordPressでの webサイト制作2022 / ngk2022s
hideokamoto
0
500
JavaScript(TypeScript)で メディアサイトを インフラから構築する方法 / jsconf-jp-2021
hideokamoto
2
4.4k
AWS上でStripeを利用したアプリをより安全にデプロイする方法 /jaws-pankration-2021
hideokamoto
1
250
Shifter Headlessと Headless WordPressの紹介
hideokamoto
0
2.2k
後付けで 従量課金プランの 提供を開始した話 / 20210609-jp_stripes
hideokamoto
0
250
Other Decks in Programming
See All in Programming
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1k
VibeCodingからAgenticWorkflowへ
starfish719
0
560
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.9k
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
180
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
290
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
220
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
630
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
380
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
160
テーブルをDELETEした
yuzneri
0
140
「人を評価する AI」の設計と実装
ryoyanara
0
210
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
180
Featured
See All Featured
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
350
Thoughts on Productivity
jonyablonski
76
5.3k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
340
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
530
Designing Powerful Visuals for Engaging Learning
tmiket
1
490
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
380
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
25k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
RailsConf 2023
tenderlove
30
1.5k
Transcript
Stripe & Next.js + AWS Amplify Ͱձһ + ఆظ՝ۚػೳ JP_Stripes
Online Hidetaka Okamoto #JP_Stripes
H i d e t a k a O k
a m o t o • Digitalcube Co. Ltd. • JavaScript Developer • WordPress 4.7 / 5.0 / 5.3 Core contributor
H i d e t a k a O k
a m o t o • Digitalcube Co. Ltd. • JavaScript Developer • WordPress 4.7 / 5.0 / 5.3 Core contributor https://zenn.dev/hideokamoto/books/e961b4bad92429
Agenda • ࠷খݶͷ։ൃͰ༗ྉձһϝσΟΞΛ࡞Δ • Next.js / Stripe / AWS AmplifyͰ࣮͢Δ
• ઃܭɾ։ൃ࣌ͷҙ • ʮਖ਼͘͠SaaSʹґଘ͢Δʯ #JP_Stripes
Agenda • ࠷খݶͷ։ൃͰ༗ྉձһϝσΟΞΛ࡞Δ • Next.js / Stripe / AWS AmplifyͰ࣮͢Δ
• ઃܭɾ։ൃ࣌ͷҙ • ʮਖ਼͘͠SaaSʹґଘ͢Δʯ #JP_Stripes
ؾʹͳΔπʔϧ ৽ػೳͰ༡Ϳॴ͕ཉ͍͠
ݸਓαΠτʹ શ෦ϒνࠐΜͩ #JP_Stripes https://wp-kyoto.net Ͱ 9݄தެ։༧ఆ
• AWS Amplify: Authentication • AWS CDK: IaC • Stripe:
Subscription • Algolia: Advanced Search • Next.js: Framework • Ionic: UI library • WordPress: Headless CMS • Capacitor / Sentry / etc… #JP_Stripes ٕज़ελοΫ
༗ྉձһαΠτ։ൃͷγϣʔτΧοτίʔε • ෳࡶͳػೳͯ͢SaaS / FrameworkʹͤΔ • ΞϓϦʮAPIΛΑͼͩ͢ʯʮAPIͷ݁ՌΛදࣔ͢ΔʯʹಛԽ • ʮܾࡁʯͱʮೝূೝՄʯΛͲΕ͚ͩ࠷ڑͰ࡞ΕΔ͔͕伴 •
ೝূೝՄ: Auth0 / Cognito / Firebase / Superbase / etc… • ܾࡁ: Stripe / pay.jp / PayPal / Paidy / etc… #JP_Stripes
Stripeͷ Low codeܥػೳΛ׆༻ #JP_Stripes
#JP_Stripes SpeakerDeck
SaaS APIΛ׆༻ͯ͠ɺίʔυͷྔΛ͑Δ • ܾࡁͱܾࡁཧ΄΅ඞͣඞཁʹͳΔػೳཁ݅ • StripeͰܾࡁ͢Δ߹ɺҎԼͷ2͕Low CodeԽͰ͖Δ • จϑΥʔϜ(Χʔυ /
ߪೖऀใೖྗ) -> Checkout • ఆظ՝ۚใཧ -> Customer Portal • ʮࣗ༝ͱޮʯͲͬͪΛॏࢹ͢Δ͔ͰಓΛબ΅͏ #JP_Stripes
Agenda • ࠷খݶͷ։ൃͰ༗ྉձһϝσΟΞΛ࡞Δ • Next.js / Stripe / AWS AmplifyͰ࣮͢Δ
• ઃܭɾ։ൃ࣌ͷҙ • ʮਖ਼͘͠SaaSʹґଘ͢Δʯ #JP_Stripes
Next.js & AWS AmplifyʹStripeΛΈࠐΉ • React AppଆʹStripe JS SDK •
REST APIଆʹStripe Nodejs SDK • CheckoutͱCustomer Portal͚ͩͳΒReact SDKෆཁ • React SDKStripe ElementΛ͏ͨΊͷSDK • APIΩʔͷऔΓѻ͍ʹҙʢޙड़ʣ #JP_Stripes
SSM·ͨSecret Manager͔ΒAPIΩʔΛऔಘ export const createStripeClient = async () => {
const data = await new SSM({ region: “us-east-1"ɹ}) .getParameter({ Name: !!process.env.AWS_LAMBDA_FUNCTION_NAME ? "STRIPE_LIVE_SECRET_KEY": "STRIPE_TEST_SECRET_KEY", WithDecryption: true, }).promise(); const stripe = new Stripe(data.Parameter?.Value;, { apiVersion: "2020-08-27", maxNetworkRetries: 3, }); return stripe; }; #JP_Stripes
Stripe CustomerͱCognito User poolͷ࿈ܞ // Authorization HeaderͳͲͰऔಘͨ͠tokenͰCognito UserΛGet const user
= await cognito.getUser({ AccessToken: req.headers.authorization || “" }).promise() // User attributes͔ΒEmailΛऔಘ const emailAttribute = user.UserAttributes.find((data) => data.Name === "email"); const email = emailAttribute?.Value || “"; // EmailΛ͔ͭͬͯStripe CustomerΛ࡞ const customer = await stripe.customers.create({ email }); #JP_Stripes
Stripe CustomerͱCognito User poolͷ࿈ܞ // ࡞ͨ͠Stripe Customer const customer =
await stripe.customers.create({ email }); // User Attributeʹอଘ͢Δ await cognito .updateUserAttributes({ AccessToken: req.headers.authorization, UserAttributes: [{ Name: "custom:stripeCustomerId", Value: customer.id, }], }).promise(); #JP_Stripes
Customer idΛηοτͯ͠checkout.sessions.create const session = await stripe.checkout.sessions.create({ customer: customerId, allow_promotion_codes:
true, line_items: [{ price: price.id, quantity: 1, }], mode: "subscription", success_url: `${appUrl.replace(/\/$/, "")}/mypages/subscriptions`, cancel_url: `${appUrl.replace(/\/$/, "")}/mypages/plans`, payment_method_types: ["card"], }); res.status(200).json({ session_id: session.id }); #JP_Stripes
Customer PortalCustomer IDΛ͏ // Cognito͔ΒϢʔβʔΛऔಘ const user = await cognito.getUser({
AccessToken: req.headers.authorization }).promise() const attribute = user.UserAttributes.find((data) => { return data.Name === “custom:stripeCustomerId" }); // Customer Poral SessionΛ࡞ const session = await stripe.billingPortal.sessions.create({ customer: attribute?.Value, return_url: `${appUrl.replace(/\/$/, "")}/mypages/subscriptions`, }); res.status(200).json({ url: session.url }); #JP_Stripes
#JP_Stripes ໘ͳ ٻཧܥը໘͕ ͨͬͨ4ߦͰ࡞ΕΔ
APIͰϙʔλϧͷURLΛੜͯ͠ϦμΠϨΫτ const { push } = useRouter(); return (<button onClick={async
() => { const session = await Auth.currentSession(); const token = session.getAccessToken().getJwtToken(); const data = await fetch("/api/portal", { method: "POST", headers: { "Content-Type": "application/json", Authorization: token, }, }).then(data => data.json()); push(data.url); }}>Button</button>) #JP_Stripes
࣮࣌ͷϙΠϯτ 1. StripeͷCustomer IDΛϢʔβʔใͱඥ͚ͮΔඞཁ͋Γ • Customer Portal Linkͷੜαʔόʔଆॲཧ 2. Customer
Portalʹग़͢ίϯςϯπͷ੍ޚݪଇDashboard • API͔ΒͳΒઃఆෳ࡞ΕΔ༷ʢະݕূʣ 3. Stripe Secret APIΩʔͷѻ͍͕ෆ҆ͳΒ੍ݶ͖ΩʔΛ͓͏ #JP_Stripes
Agenda • ࠷খݶͷ։ൃͰ༗ྉձһϝσΟΞΛ࡞Δ • Next.js / Stripe / AWS AmplifyͰ࣮͢Δ
• ઃܭɾ։ൃ࣌ͷҙ • ʮਖ਼͘͠SaaSʹґଘ͢Δʯ #JP_Stripes
ར༻نϖʔδͱ ϓϥΠόγʔϙϦγʔϖʔδ ొඞਢ #JP_Stripes
#JP_Stripes AWS Amplify Next.jsΛLambda@edge Ͱ࣮ߦ͢Δ
#JP_Stripes ͍͍ͩͨ ͜Εͩͱࢥͬͯ ѻ͓͏
ΞϓϦ֎ͰStripeͷσʔλ͕มΘΔ͜ͱʹҙ • ϓϥϯมߋɾղɾϢʔβʔใͷมߋͳͲͷૢ࡞ • Customer PortalͰى͖ͨมߋΛγεςϜʹөͤ͞Δඞཁ͕͋Δ • Stripe WebhookͰΠϕϯτΛड͚ͯσʔλΛมߋ͠Α͏ •
Customer PortalʹʮCustomerআʯ͕ݱঢ়ͳ͍ 1. Subscriptions.delete WebhookͰσʔλΛফ͢ 2. CognitoͳͲͷϢʔβʔআ࣌ʹফ͢Α͏ʹγεςϜΛߏங #JP_Stripes
ඞཁͳAPIΛݺͼग़ͯ͠ ػೳΛ։ൃ͢Δ ↓ ඞཁͳWebhookΠϕϯτΛ Subscribeͯ͠։ൃ͢Δ
Agenda • ࠷খݶͷ։ൃͰ༗ྉձһϝσΟΞΛ࡞Δ • Next.js / Stripe / AWS AmplifyͰ࣮͢Δ
• ઃܭɾ։ൃ࣌ͷҙ • ʮਖ਼͘͠SaaSʹґଘ͢Δʯ #JP_Stripes
ઃఆ߲ʹʹ૿Ճ͍ͯ͠Δ
Customer Portalͱ ಉͷػೳΛ ࣗલͰ։ൃɾఏڙͰ͖Δ͔ʁ
ίΞϏδωεྖҬ֎ͷػೳΛ୭͕࡞Δ͔ʁ A. ࣗྗ։ൃ͢Δ͜ͱͰɺσβΠϯ༷ͷࣗ༝Λ֬อͰ͖Δ • ػೳ͕૿͑Δͱอकɾӡ༻ͷൣғ૿͑Δ • ͦͷࣗ༝Λ׆༻Ͱ͖Δ͚ͩͷ։ൃϦιʔε͕͋Δ͔൱͔ B. SaaS /
Low Codeʹҕৡͯ͠ɺॏཁͳػೳʹूத͢Δ • ϕϯμʔ͕࠷దͱ͢ΔUIػೳΛৗʹड͚औΕΔ • υϝΠϯσβΠϯɾػೳͷ੍Λڐ༰Ͱ͖Δ͔൱͔ #JP_Stripes
ΠϕϯτۦಈͳγεςϜ͔ͩΒͰ͖Δ͜ͱ #JP_Stripes • Checkout / Customer PortalWebhookͰγεςϜͱ࿈ܞ͢ΔΈ • ϓϥϯมߋͳͲͷΠϕϯτΛड͚ͯಈ࡞͢ΔγεςϜʹͳΔ •
ʮಉ͡ΠϕϯτΛൃՐͰ͖Εʯ͍ͭͰஔ͖͑Ͱ͖Δ • ্ཱͪ͛࣌྆ػೳΛͬͯίΞ։ൃʹϦιʔεΛूத͢Δ • Ҡߦ͢Δ࣌ɺΠϕϯτ୯ҐͰஈ֊తʹΓସ͍͑ͯ͘ • ʮΠϕϯτۦಈΞʔΩςΫνϟʯͷୈҰาʹ
More info
ϝʔϧͰߋ৽ใΛ ͬ͟ͱݟͰ͖Δ Developer Digest #JP_Stripes https://stripe.dev/
https://www.youtube.com/stripedevelopers
Thanks! • ࠷খݶͷ։ൃͰ༗ྉձһϝσΟΞΛ࡞Δ • Next.js / Stripe / AWS AmplifyͰ࣮͢Δ
• ઃܭɾ։ൃ࣌ͷҙ • ʮਖ਼͘͠SaaSʹґଘ͢Δʯ #JP_Stripes