{"id":26188105,"url":"https://github.com/enviodev/rescript-stripe","last_synced_at":"2025-06-20T14:35:46.058Z","repository":{"id":275974396,"uuid":"926427077","full_name":"enviodev/rescript-stripe","owner":"enviodev","description":"💸 Stripe Billing as a Config","archived":false,"fork":false,"pushed_at":"2025-06-03T09:09:01.000Z","size":252,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-06-03T19:08:02.515Z","etag":null,"topics":["billing","rescript","rescript-schema","stripe"],"latest_commit_sha":null,"homepage":"","language":"ReScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enviodev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-03T08:31:30.000Z","updated_at":"2025-06-03T09:08:12.000Z","dependencies_parsed_at":"2025-02-05T16:20:34.745Z","dependency_job_id":"e72ccc18-c787-4bce-9ead-4b1bafbffbb6","html_url":"https://github.com/enviodev/rescript-stripe","commit_stats":null,"previous_names":["enviodev/rescript-stripe"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/enviodev/rescript-stripe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enviodev%2Frescript-stripe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enviodev%2Frescript-stripe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enviodev%2Frescript-stripe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enviodev%2Frescript-stripe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enviodev","download_url":"https://codeload.github.com/enviodev/rescript-stripe/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enviodev%2Frescript-stripe/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260962426,"owners_count":23089414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["billing","rescript","rescript-schema","stripe"],"created_at":"2025-03-11T23:56:38.505Z","updated_at":"2025-06-20T14:35:41.045Z","avatar_url":"https://github.com/enviodev.png","language":"ReScript","readme":"# ReScript Stripe 💸\n\nReScript client for the Stripe API.\n\n```\nnpm install rescript-stripe\n```\n\n## Bindings\n\nThe package contains partial bindings for the Stripe NodeJs client.\n\nPlease, create a PR if you need something missing.\n\n## Billing as a Config\n\nDescribe your Billing as a config and interact with Stripe API with the best DX possible and Git history.\n\n```rescript\nmodule CourseSubscription = {\n  type data = {\n    userId: string,\n    courseName: string,\n    courseId: string,\n  }\n  type plan =\n    | Starter\n    | Pro({withExtraSeats: bool})\n\n  let userId = Stripe.Metadata.ref(\"user_id\", S.string)\n  let courseId = Stripe.Metadata.ref(\"course_id\", S.string)\n  let withExtraSeats = Stripe.Metadata.ref(\"with_extra_seats\", S.bool)\n\n  let config = {\n    Stripe.Billing.ref: \"course\",\n    data: s =\u003e {\n      userId: s.primary(userId, ~customerLookup=true),\n      courseId: s.primary(courseId),\n      courseName: s.matches(S.string),\n    },\n    termsOfServiceConsent: true,\n    plans: [\n      (\n        \"starter\",\n        s =\u003e {\n          s.tag(withExtraSeats, false)\n          Starter\n        },\n      ),\n      (\n        \"pro\",\n        s =\u003e {\n          Pro({\n            withExtraSeats: s.field(withExtraSeats),\n          })\n        },\n      ),\n    ],\n    products: (~plan, ~data) =\u003e {\n      switch plan {\n      | Starter =\u003e [\n          {\n            name: data.courseName,\n            ref: `starter_course_${data.courseId}`,\n            prices: [\n              {\n                ref: `starter_course_${data.courseId}`,\n                lookupKey: true,\n                currency: USD,\n                unitAmountInCents: 10_00,\n                recurring: Licensed({\n                  interval: Month,\n                }),\n              },\n              {\n                ref: `starter_course_${data.courseId}_yearly`,\n                lookupKey: true,\n                currency: USD,\n                unitAmountInCents: 100_00,\n                recurring: Licensed({\n                  interval: Year,\n                }),\n              },\n            ],\n          },\n        ]\n      | Pro({withExtraSeats}) =\u003e [\n          {\n            Stripe.ProductCatalog.name: data.courseName,\n            ref: `pro_course_${data.courseId}`,\n            prices: [\n              {\n                ref: `pro_course_${data.courseId}`,\n                lookupKey: true,\n                currency: USD,\n                unitAmountInCents: 50_00,\n                recurring: Licensed({\n                  interval: Month,\n                }),\n              },\n              {\n                ref: `pro_course_${data.courseId}_yearly`,\n                lookupKey: true,\n                currency: USD,\n                unitAmountInCents: 500_00,\n                recurring: Licensed({\n                  interval: Year,\n                }),\n              },\n            ],\n          },\n        ]-\u003eArray.concat(\n          withExtraSeats ? [\n            {\n              Stripe.ProductCatalog.name: data.courseName ++ \" Additional Seats\",\n              ref: `pro_course_${data.courseId}_extra_seat`,\n              unitLabel: \"user\",\n              prices: [\n                {\n                  ref: `pro_course_${data.courseId}_extra_seat`,\n                  lookupKey: true,\n                  currency: USD,\n                  unitAmountInCents: 10_00,\n                  recurring: Metered({\n                    interval: Month,\n                    ref: `extra_seat`,\n                  }),\n                },\n                {\n                  ref: `pro_course_${data.courseId}_extra_seat_yearly`,\n                  currency: USD,\n                  unitAmountInCents: 10_00,\n                  recurring: Metered({\n                    interval: Year,\n                    ref: `extra_seat`,\n                  }),\n                }\n              ]\n            }]\n          : []\n        )\n      }\n    },\n  }\n}\n```\n\nAfter you described the config, you can use it to interact with Stripe API.\n\n### Create subscription\n\n```rescript\nawait stripe-\u003eStripe.Billing.createHostedCheckoutSession({\n  config: CourseSubscription.config,\n  data: {\n    userId: \"dzakh\",\n    courseId: \"rescript-schema-to-the-moon\",\n    courseName: \"ReScript Schema to the Moon\",\n  },\n  plan: Starter,\n  interval: Month,\n  allowPromotionCodes: true,\n  successUrl: `https://x.com/dzakh_dev`,\n})\n```\n\n### Retrieve customer\n\n```rescript\nlet customer = await stripe-\u003eStripe.Billing.retrieveCustomer({\n  userId: \"dzakh\",\n  courseId: \"rescript-schema-to-the-moon\",\n  courseName: \"ReScript Schema to the Moon\",\n}, ~config=CourseSubscription.config)\n```\n\n### Retrieve subscription\n\n```rescript\nlet subscription = await stripe-\u003eStripe.Billing.retrieveSubscription({\n  userId: \"dzakh\",\n  courseId: \"rescript-schema-to-the-moon\",\n  courseName: \"ReScript Schema to the Moon\",\n}, ~config=CourseSubscription.config)\n```\n\n#### Retrieve subscription with customer\n\n```rescript\nlet {subscription, customer} = await stripe-\u003eStripe.Billing.retrieveSubscriptionWithCustomer({\n  userId: \"dzakh\",\n  courseId: \"rescript-schema-to-the-moon\",\n  courseName: \"ReScript Schema to the Moon\",\n}, ~config=CourseSubscription.config)\n```\n\n#### Get subscription metadata\n\n```rescript\nlet userId = subscription-\u003eStripe.Metadata.get(CourseSubscription.userId)\n//? string\n```\n\n\u003e 🧠 This is 100% type safe and works only on subscriptions belonging to the \"Course Subscription\" config.\n\n\u003e ⚠️ This requires that all plans have the same set of metadata fields. There's no explicit validation for this yet.\n\n#### Verify that subscription belongs to the config\n\n```rescript\nlet genericSubscription = await stripe-\u003eStripe.Subscription.retrieve(\"sub_123\")\n\nlet userId = subscription-\u003eStripe.Metadata.get(CourseSubscription.userId)\n//? Compilation error\n\nsubscription-\u003eStripe.Billing.verify(CourseSubscription.config)-\u003eOption.map(subscription =\u003e {\n  let userId = subscription-\u003eStripe.Metadata.get(CourseSubscription.userId)\n  //? string\n  userId\n})\n```\n\n### Get meter event name by reference\n\n```rescript\nlet eventName = subscription-\u003eStripe.Subscription.getMeterEventName(~meterRef=\"extra_seat\")\n```\n\nReScript Stripe might create multiple meters under the hood, so you need to call the function to get the right meter event name to report usage.\n\nThis is done because you can report meter usage per customer, so if a customer has multiple subscriptions, you need to have different meters for each one. ReScript Stripe manages this for you.\n\n### Report usage for a subscription\n\n```rescript\nlet _ =\n  await stripe-\u003eStripe.Subscription.reportMeterUsage(\n    subscription,\n    ~meterRef=\"extra_seat\",\n    ~value=1,\n  )\n```\n\n### Customer portal helpers\n\n```rescript\nlet link = stripe-\u003eStripe.CustomerPortal.prefillEmail(~link=\"https://customer.portal.com\", ~email=\"stripe@customer.com\")\n```\n\n### Handling a WebHook with [rescript-rest](https://github.com/DZakh/rescript-rest) and [Next.js](https://nextjs.org/)\n\n```rescript\nlet stripe = Stripe.make(\"sk_test_...\")\n\nlet route = Rest.route(() =\u003e {\n  path: \"/api/stripe/webhook\",\n  method: Post,\n  input: s =\u003e {\n    \"body\": s.rawBody(S.string),\n    \"sig\": s.header(\"stripe-signature\", S.string),\n  },\n  responses: [\n    s =\u003e {\n      s.status(200)\n      let _ = s.data(S.literal({\"received\": true}))\n      Ok()\n    },\n    s =\u003e {\n      s.status(400)\n      Error(s.data(S.string))\n    },\n  ],\n})\n\n// Disable bodyParsing to make Raw Body work\nlet config: RestNextJs.config = {api: {bodyParser: false}}\n\nlet default = RestNextJs.handler(route, async ({input}) =\u003e {\n  stripe\n  -\u003eStripe.Webhook.constructEvent(\n    ~body=input[\"body\"],\n    ~sig=input[\"sig\"],\n    // You can find your endpoint's secret in your webhook settings\n    ~secret=\"whsec_...\",\n  )\n  -\u003eResult.map(event =\u003e {\n    switch event {\n    | CustomerSubscriptionCreated({data: {object: subscription}}) =\u003e\n      await processSubscription(subscription)\n    | _ =\u003e ()\n    }\n  })\n})\n```\n\n### Create/Find Customer and Checkout Session for selected plan\n\n```rescript\nlet session = await stripe-\u003eStripe.Billing.createHostedCheckoutSession({\n  config: CourseSubscription.config,\n  data: {\n    userId: \"dzakh\",\n    courseId: \"rescript-schema-to-the-moon\",\n    courseName: \"ReScript Schema to the Moon\",\n  },\n  plan: Starter,\n  interval: Year,\n  allowPromotionCodes: true,\n  successUrl: `https://myapp.com/success`,\n})\nConsole.log(session.url)\n```\n\n\u003e 🧠 It'll throw if the subscription already exist\n\u003e 🧠 Customer, products, prices, meters are automatically created when they are not found\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenviodev%2Frescript-stripe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenviodev%2Frescript-stripe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenviodev%2Frescript-stripe/lists"}