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
700
コミュニティ運営から 中の人に変わって感じたこと
hideokamoto
0
140
Developerが Developer Advocateになった話 / dev-rel-meetup-tokyo-71
hideokamoto
0
380
Jamstack開発者のための App Runner入門
hideokamoto
1
550
WordPressでの webサイト制作2022 / ngk2022s
hideokamoto
0
490
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
共通化で考えるべきは、実装より公開する型だった
codeegg
0
250
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
継続モナドとリアクティブプログラミング
yukikurage
3
610
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
220
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
0
270
act2-costs.pdf
sumedhbala
0
110
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
2
530
act1-costs.pdf
sumedhbala
0
230
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
130
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
580
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
350
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
8
4.4k
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
460
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Claude Code のすすめ
schroneko
67
230k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
64
56k
Side Projects
sachag
455
43k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Google's AI Overviews - The New Search
badams
0
1.1k
Speed Design
sergeychernyshev
33
1.9k
Done Done
chrislema
186
16k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
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