Last active
May 12, 2020 02:56
-
-
Save ryanbelke/16a0d97891f0cb8bb1e1bba5d2f89a4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/** | |
* Order.js controller | |
* | |
* @description: A set of functions called "actions" for managing `Order`. | |
*/ | |
const stripe = require("stripe")("YOUR STRIPE SECRET KEY"); | |
module.exports = { | |
/** | |
* Create a/an order record. | |
* | |
* @return {Object} | |
*/ | |
create: async (ctx) => { | |
const { address, amount, dishes, token, city, state } = JSON.parse( | |
ctx.request.body | |
); | |
const stripeAmount = Math.floor(amount * 100); | |
// charge on stripe | |
const charge = await stripe.charges.create({ | |
// Transform cents to dollars. | |
amount: stripeAmount, | |
currency: "usd", | |
description: `Order ${new Date()} by ${ctx.state.user._id}`, | |
source: token, | |
}); | |
// Register the order in the database | |
const order = await strapi.services.order.create({ | |
user: ctx.state.user.id, | |
charge_id: charge.id, | |
amount: stripeAmount, | |
address, | |
dishes, | |
city, | |
state, | |
}); | |
return order; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment