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
import React from 'react'
import StripeCheckout from 'react-stripe-checkout'
class Stripe extends React.Component {
constructor(props) {
super(props)
this.state = {
buttonStyle: 'button primary',
status: 'Nothing happened yet'
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
import gql from 'graphql-tag'
export const userQuery = gql`
query {
user {
id
name
email
paid
}
import { graphql } from 'react-apollo'
import { userQuery, signinUser } from './graphql/user'
import { createCard, receiveStripeId, createPurchase, checkIfPaid, upgradeApp } from './graphql/purchase'
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' })(
{
createdNode{
id
stripeToken
stripeTokenToUser {
id
name
email
}
}
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
}
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...')