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

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

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

Hidetaka Okamoto

April 22, 2021
Tweet

More Decks by Hidetaka Okamoto

Other Decks in Programming

Transcript

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

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

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

    APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  4. Before Stripe CLI • APIαʔόʔΛςετ༻ʹσϓϩΠʢsls deploy -s webhookʣ • Stripe

    Dashboard͔ΒWebhookΠϕϯτΛखಈͰൃՐ • αʔόʔͷϩάΛ؂ࢹͯ͠ɺҙਤͨ͠ಈ࡞ʹͳ͍ͬͯΔ͔֬ೝ • ໰୊͕͋Ε͹ɺϩʔΧϧͰमਖ਼ͯ͠࠶σϓϩΠ • ʮ࣮૷ -> ςετʯʹ͕͔͔࣌ؒΔ
  5. Agenda • Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe

    CLIͰWebhook APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  6. 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ϦΫΤετͷதܧ
  7. Before Stripe CLI • APIαʔόʔΛςετ༻ʹσϓϩΠʢsls deploy -s webhookʣ • Stripe

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

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

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

    CLIͰWebhook APIͷϩʔΧϧ։ൃΛޮ཰Խ • σϓϩΠޙͷσόοάɾϞχλϦϯάʹ΋Stripe CLI
  11. ্: σϑΥϧτ Լ: 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, …
  12. 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Ͱ͖Δ
  13. Thanks! • Stripe WebhookΛ࢖͏ϝϦοτ • Stripe Webhook APIͷ։ൃ͸େมʁ • Stripe

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