summaryrefslogtreecommitdiffstats
path: root/functions/createCharge.js
blob: ecd22a1f68596a9a46338521254954aa4436322c (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
44
45
46
47
48
49
50
51
52
53
const stripe = require('stripe')('sk_ycLkGc2cAaBDSCKkHiKVBeT71CMxd')
const graphCoolEndpoint = 'https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj'

require('isomorphic-fetch')

const query = `
query SingleItem {
  Item(id: "cj64bs7vuuqgu0116ul5cm223") {
    price
  }
}
`;

const getPrice = (id) =>  {
  return fetch('https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj', {
    method: 'post',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ query }),
  })
}

const createStripeCharge = (token, amount) => {
  return stripe.charges.create({
    amount,
    currency: 'usd',
    description: `a test charge`,
    source: token
  })
}

module.exports = function(event) {
  return new Promise((resolve, reject) => {
    // first find out the price of the item
    getPrice()
      .then(res => res.json())
      .then(res => {
        console.log(`Back with the price ${res.data.Item.price}!`);
        return createStripeCharge(event.data.token, res.data.Item.price);
      })
      .then(res => {
        console.log(`Back! charge ${res.id} for the amount ${res.amount}`);
        event.data.charge = res.id;
        event.data.amount = res.amount;
        console.log(event.data);
        resolve(event);
      })
      .catch(err => {
        reject(err);
      })
  });
}