Skip to content

Instantly share code, notes, and snippets.

@heymartinadams
Last active March 4, 2017 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heymartinadams/7ca9c5e242c55b87bc3f1934d59790b0 to your computer and use it in GitHub Desktop.
Save heymartinadams/7ca9c5e242c55b87bc3f1934d59790b0 to your computer and use it in GitHub Desktop.
import gql from 'graphql-tag'
// Logic #1: Stripe returns token to app.
// No front-end graphql code necessary
// Logic #2: Create node in StripeToken model using customer token received from Stripe and assign user to node
export const createCard = gql`
mutation($stripeToken: String!, $userId: ID!) {
createStripeToken(
stripeToken: $stripeToken
stripeTokenToUserId: $userId
) {
stripeTokenToUser {
id
}
}
}
`
// Logic #3: Mutation Callback 'StripeToken is created' is triggered, server webhook is called, and Stripe customer is created on server
// No front-end graphql code necessary
// Logic #4: On successful Stripe customer creation, server calls updateUser mutation and updates user in User model with Stripe customer ID
// No front-end graphql code necessary
// Logic #5: Create subscription to trigger response once user in User model has been successfully assigned a Stripe customer ID
export const receiveStripeId = gql`
subscription($userId: ID!) {
User(filter: {
mutation_in: [UPDATED]
node: {
id: $userId
stripeId_not: "null"
}
updatedFields_contains: "stripeId"
}) {
node {
id
}
}
}
`
// Logic #6: Now that user has a Stripe customer Id, make a purchase by creating a node in Purchase model.
export const createPurchase = gql`
mutation($userId: ID!) {
createPurchase(
description: "Life Purpose App"
amount: 999
isPaid: false
purchaseToUserId: $userId
) {
purchaseToUser {
id
}
}
}
`
// Logic #7: Mutation Callback 'Purchase is created' is triggered, server webhook is called, and a Stripe charge is created on server
// No front-end graphql code necessary
// Logic #8: On successful charge, server calls updatePurchase mutation and updates purchase in Purchase model with isPaid as true
// No front-end graphql code necessary
// Logic #9: Create subscription to trigger response once purchase in Purchase model has been successfully updated with isPaid as true.
export const checkIfPaid = gql`
subscription($userId: ID!) {
Purchase(filter: {
mutation_in: [UPDATED]
node: {
isPaid: true
purchaseToUser: {
id: $userId
}
}
updatedFields_contains: "isPaid"
}) {
node {
purchaseToUser {
id
}
}
}
}
`
// Logic #10: Now that Stripe charge was successful and that Purchase model has been updated with isPaid as true upgrade the app
export const upgradeApp = gql`
mutation ($userId: ID!) {
updateUser(
id: $userId,
paid: true) {
id
}
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment