• Design
  • Shotty
  • Blog
  • Reading
  • Photos
Menu

Jacob Ruiz

Product Designer
  • Design
  • Shotty
  • Blog
  • Reading
  • Photos
graphql-notes@2x.png

GraphQL: Adding Subscription Pricing With Stripe

June 30, 2019

Problem

We want to allow users to purchase a Premium subscription to our app, to be billed monthly.

Solution

We can use Stripe to:

  • Tokenize the user’s payment method on the client.

  • Accept the token on our backend

  • Create a Customer in Stripe

  • Subscribe that Customer to our Product

Implementation

Stripe Dashboard

  • Create a new Product

  • Create a new Pricing Plan for that Product

    • We’ll need the Plan ID (eg. plan_FLyf9o01u2bJZU)

GraphQL

Schema

Update our updateRole mutation to accept a stripeToken

updateRole(newRole: Role!, stripeToken: String): User!

Resolver

updateRole needs to do a series of things, in order:

1. Create a Customer in Stripe

2. Create a Subscription in Stripe for that Customer

3. Update the user’s role in the database

4. Reset the cookie so the JWT includes the new role

5. Return the updated user from our resolver

return updatedUser;

Putting it all together

Here’s the code for the resolver in full. It’s pretty long-winded in it’s first iteration, but it gets the job done. Later we’ll want to add more code to handle different cases.

← GraphQL: Canceling A Stripe SubscriptionHow to Test Cookie-based Authentication in Mocha with Axios (Node / GraphQL / Apollo) →
shotty-skinny2x.jpg

Shotty - Faster Access To Your Screenshots on Mac

Shotty is an award-winning Mac app I created to give you instant access to all your recent screenshots, right from the menu bar. You can even add annotations on-the-fly. Stop wasting time digging through Finder for your screenshots. I promise it’ll change your workflow forever (just read the App Store reviews!).



Most popular

information-architecture

Information Architecture: The Most Important Part of Design You're Probably Overlooking

Follow @JacobRuizDesign