summaryrefslogtreecommitdiffstats
path: root/functions
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2017-08-11 14:56:13 -0400
committerWes Bos <wesbos@gmail.com>2017-08-11 14:56:13 -0400
commit416c2cbae0e69b724876d232bf813eefd7550174 (patch)
treece7109c0f61dbeee7077164cb48a4681214e0464 /functions
parent222ab374d53a206332b1ab51c7c43f6d5dc7a31e (diff)
getting there
Diffstat (limited to 'functions')
-rw-r--r--functions/createCharge.js71
-rw-r--r--functions/createCharge.test.js26
-rw-r--r--functions/getPrice.js24
3 files changed, 96 insertions, 25 deletions
diff --git a/functions/createCharge.js b/functions/createCharge.js
index 2e9f7d4..ecd22a1 100644
--- a/functions/createCharge.js
+++ b/functions/createCharge.js
@@ -1,32 +1,53 @@
-const fetch = require('isomorphic-fetch')
const stripe = require('stripe')('sk_ycLkGc2cAaBDSCKkHiKVBeT71CMxd')
-
const graphCoolEndpoint = 'https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj'
-const createStripeCharge = (token, email = 'wesbos@gmail.com') => {
- console.log(`Creating stripe charge for ${email}`)
- return new Promise((resolve, reject) => {
- stripeCharge = stripe.charges.create({
- amount: '99999',
- currency: 'usd',
- description: 'You Rock 😘',
- source: 'tok_visa'
- }, (err, charge) => {
- if (err) {
- console.log(`Error creating Stripe charge: ${JSON.stringify(err)}`)
- reject(err)
- } else {
- console.log(`Successfully created Stripe charge: ${charge.id}`);
- console.log(charge);
- }
- })
- })
- }
+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 }),
+ })
+}
-createStripeCharge();
+const createStripeCharge = (token, amount) => {
+ return stripe.charges.create({
+ amount,
+ currency: 'usd',
+ description: `a test charge`,
+ source: token
+ })
+}
module.exports = function(event) {
- event.data.charge = 'DEF456';
- console.log(event.data)
- return { data: event.data }
+ 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);
+ })
+ });
}
diff --git a/functions/createCharge.test.js b/functions/createCharge.test.js
new file mode 100644
index 0000000..18e13ba
--- /dev/null
+++ b/functions/createCharge.test.js
@@ -0,0 +1,26 @@
+const takeMoney = require('./createCharge');
+
+const event = {
+ "data": {
+ "amount": 5,
+ "createdAt": "2017-08-08T19:48:39.436Z",
+ "id": "cj6400x3g00003i5zufoucron",
+ "token": "tok_visa",
+ "updatedAt": "2017-08-08T19:48:39.436Z"
+ },
+ "context": {
+ "headers": {}
+ }
+}
+
+
+describe('Take Money', () => {
+
+ test('A charge has come back', async () => {
+ const res = await takeMoney(event);
+ console.log(res);
+ expect(res.data.amount).toBeGreaterThanOrEqual(0);
+ });
+
+});
+
diff --git a/functions/getPrice.js b/functions/getPrice.js
new file mode 100644
index 0000000..61c29e6
--- /dev/null
+++ b/functions/getPrice.js
@@ -0,0 +1,24 @@
+require('isomorphic-fetch')
+
+const query = `
+query SingleItem {
+ Item(id: "cj64bs7vuuqgu0116ul5cm223") {
+ price
+ }
+}
+`;
+
+function getItem(id) {
+ return fetch('https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj', {
+ method: 'post',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({ query }),
+ })
+}
+
+getItem().then(x => x.json()).then(res => {
+ console.log(res.data.Item.price);
+}).catch(console.log)
+