Skip to content

Instantly share code, notes, and snippets.

@heymartinadams
Last active March 8, 2017 20:15
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/3417584662cfcfd8b26cf0a5963b632d to your computer and use it in GitHub Desktop.
Save heymartinadams/3417584662cfcfd8b26cf0a5963b632d to your computer and use it in GitHub Desktop.
componentDidUpdate(newProps) {
if (this.props.data.user) {
const userId = this.props.data.user.id
if (!this.stripeTokenSubscription && !this.purchaseSubscription) {
// Logic #5: Create subscription to trigger response once user in User model has been successfully assigned a Stripe customer ID
this.stripeTokenSubscription = newProps.data.subscribeToMore({
document: receiveStripeId,
variables: { userId },
updateQuery: (previousState, { subscriptionData }) => {
console.log('Purchasing...')
this.setState({ status: 'Purchasing...' })
// Logic #6: Now that user has a Stripe customer Id (ie. subscription has been triggered), make a purchase by creating a node in Purchase model.
this.props.createPurchase({ variables: { userId } })
.then(() => {
console.log('Purchase complete')
this.setState({ status: 'Purchase complete...' })
// Cancel current subscription
this.stripeTokenSubscription()
})
.catch((e) => { console.error(e) })
},
onError: (err) => console.error(err)
})
// Logic #9: Create subscription to trigger response once purchase in Purchase model has been successfully updated with isPaid as true.
this.purchaseSubscription = newProps.data.subscribeToMore({
document: checkIfPaid,
variables: { userId },
updateQuery: (previousState, { subscriptionData }) => {
console.log('Upgrading...')
this.setState({ status: 'Upgrading...' })
// Logic #10: Now that Stripe charge was successful and that Purchase model has been updated with isPaid as true upgrade the app
this.props.upgradeApp({ variables: { userId } })
.then(() => {
console.log('Upgrade complete!')
this.setState({ status: 'Upgrade complete!' })
// Cancel current subscription
this.purchaseSubscription()
// Implement any other logic to indicate that the user has successfully upgraded here.
})
.catch(err => console.error(err))
return null
},
onError: (err) => console.error(err)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment