summaryrefslogtreecommitdiffstats
path: root/frontend/components/TakeMyMoney.js
blob: 042e9c80abb843c0bba7594af2939776ed1d078d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Component } from 'react';
import StripeCheckout from 'react-stripe-checkout';
import { graphql, compose } from 'react-apollo';
import { CREATE_ORDER_MUTATION, CURRENT_USER_QUERY } from '../queries';

class TakeMyMoney extends Component {
  onToken = async res => {
    console.log('We got a toooken!');
    const token = res.id;
    const userId = this.props.currentUserQuery.user.id;
    const itemId = this.props.id;
    console.log(`Going to make a purchase with ${token}`);
    console.log(`THe person that bought this was ${userId}`);
    console.log(`The item id is ${itemId}`);
    const charge = await this.props.createOrder({ variables: { token, userId, itemId } });
    alert(`Back from the charge! ${charge.id}`);
    console.log(charge);
  };
  render() {
    // const user = this.props.currentUserQuery.user || {};
    // const email = user.email || '';
    const email = 'wesbos@gmail.com';
    return (
      <div>
        <StripeCheckout
          amount={this.props.amount}
          name={this.props.name}
          description={this.props.description}
          token={this.onToken}
          stripeKey="pk_lclTtThFp8CnO3QtEZSd8HA9mFUps"
          currency="USD"
          email={email}
        >
          {this.props.children}
        </StripeCheckout>
      </div>
    );
  }
}

const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUserQuery' });
const createOrderEnhancer = graphql(CREATE_ORDER_MUTATION, { name: 'createOrder' });
export default compose(createOrderEnhancer)(TakeMyMoney);