Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stripe CLIでWebhook APIを効率的に開発する/jp_stripes_deepdive_vol5

Stripe CLIでWebhook APIを効率的に開発する/jp_stripes_deepdive_vol5

75486cbfd37125f121cf4a6c5614601c?s=128

Hidetaka Okamoto
PRO

April 22, 2021
Tweet

Transcript

  1. Stripe CLIͰ Webhook APIΛ ޮ཰తʹ։ൃ͢Δ JP_Stripes Deep Dive vol.5 2021/04/22

    Hidetaka Okamoto
  2. TL;DR • Stripe Webhookͷಈ࡞ςετʹ͸ɺPublicͳAPI͕ඞཁ • Stripe CLIΛ࢖͏͜ͱͰɺlocalhostͷAPIΛ઀ଓͰ͖Δ • trigger/listenͳͲɺWebhookͷςετ/σόοά޲͚ίϚϯυ͕๛෋ •

    Stripe΍ΔͳΒWebhook -> Webhook΍ΔͳΒStripe CLI -> Stripe΍ΔͳΒStripe CLIʂ
  3. Agenda • Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe

    CLIͰWebhook APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  4. Agenda •Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe CLIͰWebhook

    APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  5. ΦϯϥΠϯܾࡁ͸Πϕϯτۦಈ • ʮܾࡁʹ੒ޭͨ͠৔߹ʯಛఆͷϝʔϧ΍ૢ࡞Λ࣮ߦ͢Δ • ʮఆظ՝ۚͷ੥ٻ͕ࣦഊͨ͠ͷͰʯαʔϏε੍ݶͱϢʔβʔ௨஌ • ʮܾࡁֹۚ߹ܭ / ܖ໿ظ͕ؒҰఆ஋Λ௒͑ͨͷͰʯಛయΛ෇༩ •

    ʮෆ৹ͳܾࡁ͕ݟ͔ͭͬͨͷͰʯࣾ಺Slackʹ௨஌ • etc…
  6. Stripe্ͷग़དྷࣄΛ ಠࣗͷαʔϏεʹ࿈ܞ͢Δ࢓૊Έ Ὃ Stripe Webhook

  7. DigitalcubeͰͷWebhookར༻ྫʢҰ෦ʣ • ఆظ՝ۚͷܾࡁ͕ࣦഊͨ͠৔߹ɺαʔϏεʹ੍ݶΛઃఆ • FailedܥΠϕϯτ͕ൃੜͨ͠৔߹ɺSlackʹ௨஌ • ղ໿(subscriptions.delete)࣌ʹɺؔ࿈σʔλͷ࡟আ • ϓϥϯมߋޙɺαʔϏε্ݶ΁ͷ఍৮͕ͳ͍͔ͷ֬ೝ •

    etc…
  8. Agenda • Stripe WebhookΛ࢖͏ϝϦοτ •Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe CLIͰWebhook

    APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  9. StripeͷαʔϏε͔Β HTTPSϦΫΤετΛ౤͛Δ Ὃ API͸Internetʹ ެ։͞Ε͍ͯΔඞཁ͕͋Δ

  10. Before Stripe CLI • APIαʔόʔΛςετ༻ʹσϓϩΠʢsls deploy -s webhookʣ • Stripe

    Dashboard͔ΒWebhookΠϕϯτΛखಈͰൃՐ • αʔόʔͷϩάΛ؂ࢹͯ͠ɺҙਤͨ͠ಈ࡞ʹͳ͍ͬͯΔ͔֬ೝ • ໰୊͕͋Ε͹ɺϩʔΧϧͰमਖ਼ͯ͠࠶σϓϩΠ • ʮ࣮૷ -> ςετʯʹ͕͔͔࣌ؒΔ
  11. ngrokΛ࢖͏ͱ͍͏ख΋͋Δʹ͸͋Δ • ϩʔΧϧʹཱͯͨAPIαʔόʔʹ֎෦͔Β઀ଓͰ͖ΔΑ͏ʹ͢ΔSaaS • ແྉ࿮͕͋ΔͷͰɺϫʔΫγϣοϓ΍νϡʔτϦΞϧͰΑ͘ొ৔͢Δ • ແྉ൛͸઀ଓ਺্ݶ΍URL͕ຖճมΘΔͳͲͷ੍ݶ͕͋Δ • ʮStripe CLI͕࢖͑ͳ͍ʯͳΒɺngrokΛ࢖͏ͷ΋ͻͱͭ

  12. Agenda • Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe

    CLIͰWebhook APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  13. stripe listenͰ ϩʔΧϧAPIʹ ϦΫΤετதܧ # ϩʔΧϧͷAPIαʔόʔΛىಈ % npm start #

    ϩʔΧϧͷAPIαʔόʔ͕localhost:4242ͷ৔߹ % stripe listen \ -—forward-to localhost:4242/webhook > Ready! Your webhook signing secret is whsec_xxxxxxxxxxxxx (^C to quit) # Webhook༻ͷॺ໊γʔΫϨοτΛ؀ڥม਺ʹઃఆ % export STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxxxx x • DashboardͰͷ Webhookొ࿥ • ઐ༻ॺ໊Secretͷൃߦ • HTTPSϦΫΤετͷதܧ
  14. Stripe CLIͰWebhookΠϕϯτΛखಈൃՐ • ʮstripe trigger [WEBHOOK_EVENT_NAME]ʯͰൃՐՄೳ • test؀ڥଆʹ࣮σʔλ͕࡞ΒΕɺWebhookΠϕϯτ͕ൃՐ͢Δܗ • productͷશ݅औಘͳͲΛ͍ͯ͠Δ৔߹ʹ͸ཁ஫ҙ

    • ࡞੒͞ΕΔσʔλͷத਎͸ઃఆෆՄೳ • stripe products createͳͲɺݸผίϚϯυͰ࡞੒͢΂͠
  15. Before Stripe CLI • APIαʔόʔΛςετ༻ʹσϓϩΠʢsls deploy -s webhookʣ • Stripe

    Dashboard͔ΒWebhookΠϕϯτΛखಈͰൃՐ • αʔόʔͷϩάΛ؂ࢹͯ͠ɺҙਤͨ͠ಈ࡞ʹͳ͍ͬͯΔ͔֬ೝ • ໰୊͕͋Ε͹ɺϩʔΧϧͰमਖ਼ͯ͠࠶σϓϩΠ • ʮ࣮૷ -> ςετʯʹ͕͔͔࣌ؒΔ
  16. After Stripe CLI • APIαʔόʔΛϩʔΧϧͰىಈ • stripe listen —forward-to localhost:XXXXͰStripeͱ઀ଓ

    • whsec_xxxͷ஋Λ؀ڥม਺ʹอଘ(or ߋ৽) • stripe trigger [EVENT_NAME]ͰΠϕϯτΛൃՐͯ͠ಈ࡞֬ೝ • ϩʔΧϧ্ͰWebhook APIͷ࣮૷͕Ͱ͖Δ
  17. Stripe CLIͰͷϩʔΧϧ։ൃͷ஫ҙ఺ • listen͸σϑΥϧτͩͱCatch ALLʢશΠϕϯτड͚෇͚Δʣ • ʮ૝ఆ͍ͯ͠ͳ͍Πϕϯτʯ͕ૹΒΕͯ͘Δέʔε͕͋ΓಘΔ • test؀ڥΛ࢖͍ͬͯΔΞϓϦ͕Stripe APIʹϦΫΤετΛૹͬͨ৔߹

    • DashboardͰ୭͔͕࡞ۀΛͨ͠৔߹ • ผͷ࡞ۀऀ͕stripe triggerίϚϯυΛ࣮ߦͨ͠৔߹ • —events EVENT_NAMEϑϥάͰड͚෇͚ΔΠϕϯτΛߜΖ͏
  18. Agenda • Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe

    CLIͰWebhook APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  19. Stripe CLIͰWebhookΠϕϯτͷϩά؂ࢹ • ʮstripe listenʯͰൃੜͨ͠ΠϕϯτͷϩάΛ؂ࢹͰ͖Δ • σϑΥϧτͰ͸ʮ͍ͭɺͲΜͳΠϕϯτ͕ɺͲͷIDͰʯͷΈग़ྗ • flagΛ࢖ͬͯΠϕϯτͷߜΓࠐΈ΍JSONग़ྗ΋Մೳ •

    Ϣχοτςετ / E2Eςετʹ࢖͍͍ͨͳΒɺ—print-jsonϑϥά • —liveϑϥάͰຊ൪σϓϩΠޙͷΠϕϯτϩά؂ࢹ΋Ͱ͖Δ
  20. ্: σϑΥϧτ Լ: JSONग़ྗ % stripe --project-name test listen 2021-04-21

    15:43:52 --> payment_intent.created [evt_1IiZmtDHnG67uihbHble0dMs] 2021-04-21 15:44:39 --> customer.created [evt_1IiZneDHnG67uihbz8CqDMXa] 2021-04-21 15:44:39 --> customer.deleted [evt_1IiZneDHnG67uihb1fkojgcc] % stripe --project-name test listen \ --print-json { "id": "evt_1IiZoVDHnG67uihb6NbbKShX", "object": "event", "api_version": "2020-03-02", "created": 1618987531, "data": { "object": { "id": "cus_JLGLsScTeXQ2Ve", "object": "customer", "address": null, "balance": 0, …
  21. Appendix 1: ஌ͬಘ Stripe CLI ίϚϯυ • stripe fixture: JSONͰෳ਺ͷAPIίʔϧΛॱংཱ࣮ͯͯߦࢦࣔͰ͖Δ

    • stripe logs: StripeʹૹΒΕͨAPIίʔϧΛ؂ࢹʢonly test modeʣ • stripe open: Dashboard΍DocumentϖʔδΛϒϥ΢βͰ։͚Δ • stripe samples: github.com/stripe-samplesͷΞϓϦΛcloneͰ͖Δ
  22. Appendix 2: Webhook͸ ॺ໊ݕূͰ อޢ͠Α͏ • Stripe͔ΒͷϦΫΤετ ͷΈॲཧ͢΂͖ • webhooks.constructEventΛ࢖ͬ

    ͯϦΫΤετΛݕূ • whsec_xxx͸AWSͷ Secret ManagerͳͲͰอޢ͠Α͏
  23. Need more information?

  24. https://zenn.dev/hideokamoto/books/e961b4bad92429

  25. Thanks! • Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe

    CLIͰWebhook APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI