Skip to content

Instantly share code, notes, and snippets.

View heymartinadams's full-sized avatar
🌎
Another beautiful day on our beautiful planet ❤️

Martin Adams heymartinadams

🌎
Another beautiful day on our beautiful planet ❤️
View GitHub Profile
type User {
id: ID!
name: String!
email: String!
password: String!
stripeId: String
paid: Boolean!
userToPurchase: [Purchase]
userToStripeToken: [StripeToken]
}
{
createdNode{
id
amount
description
isPaid
purchaseToUser {
id
stripeId
}
{
createdNode{
id
stripeToken
stripeTokenToUser {
id
name
email
}
}
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
Stripe.propTypes = {
createCard: React.PropTypes.func.isRequired,
createPurchase: React.PropTypes.func.isRequired,
upgradeApp: React.PropTypes.func.isRequired,
data: React.PropTypes.object.isRequired
}
export default
graphql(signinUser, { name: 'signinUser' })(
graphql(createCard, { name: 'createCard' })(
import { graphql } from 'react-apollo'
import { userQuery, signinUser } from './graphql/user'
import { createCard, receiveStripeId, createPurchase, checkIfPaid, upgradeApp } from './graphql/purchase'
import gql from 'graphql-tag'
export const userQuery = gql`
query {
user {
id
name
email
paid
}
onToken(token) {
// This is where token is received, as described in Logic #1
const userId = this.props.data.user.id
const stripeToken = token.id
// Hide or remove button to prevent duplicate purchases
this.setState({ buttonStyle: 'button hidden' })
// Logic #2
this.props.createCard({ variables: { stripeToken, userId } })
.then(() => {
console.log('Customer created...')
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...')
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...')