| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- const config = require('./config');
- const stripe = require('stripe')(config.stripeKey);
- const db = require('sqlite');
- const request = require('request-promise');
- const fs = require('fs');
- function lastOrder() {
- let _ref;
- return fs.readFileAsync('orderRef.json')
- .then(buf => JSON.parse(buf.toString()))
- .then(json => {
- json.ref++;
- _ref = json.ref;
- return fs.writeFileAsync('orderRef.json', JSON.stringify(json))
- .then(() => ('order-' + _ref));
- })
- }
- /**
- * Send index page
- */
- function getIndex(req, res) {
- // Build Stripe Authorization URL
- const authUrl = 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=' +
- config.stripeClientId + '&scope=read_write&redirect_uri=http://localhost:' + config.port + '/stripe-connect';
-
- // Render template
- res.render('index', {
- authUrl
- });
- }
- var mappers = {
- string: v => ("'" + v.replace(/'/g, '\'') + "'"),
- boolean: v => (v ? 1 : 0)
- };
- function buildInsertQuery(table, fields) {
- const keys = Object.keys(fields);
- const values = Object.values(fields);
- const valuesString = values.map(v => (
- mappers[typeof v] ? mappers[typeof v](v) : v
- ));
- return 'INSERT INTO ' + table + ' (' +
- keys.join(',') + ') VALUES(' +
- valuesString + ')';
- }
- /**
- * Handle return from Stripe Authorization page
- */
- function getStripeCallback(req, res) {
- // res.json(req.query);
- // Sending to slack channel
- request({
- uri: 'https://connect.stripe.com/oauth/token',
- method: "POST",
- json: {
- client_secret: config.stripeKey,
- grant_type: 'authorization_code',
- code: req.query.code
- }
- })
- .then(response => {
- const query = buildInsertQuery('access_tokens', response);
- console.log('stripe response', response, query);
- db.get(query)
- .then(result => {
- console.log(result);
- res.json({ success: true });
- })
- })
- .catch(err => {
- console.log(err);
- });
- }
- /**
- * Create a Stripe Charge from Stripe Checkout form
- */
- function postStripeCharge(req, res) {
- const { stripeEmail, stripeToken, stripeTokenType } = req.body;
- lastOrder()
- .then(transfer_group => {
- console.log('got last order ref', transfer_group)
- return stripe.charges.create({
- amount: 999,
- currency: "eur",
- description: "Example charge",
- source: stripeToken,
- transfer_group,
- destination: {
- account: 'acct_1BJQ2TCxNRnLITys'
- }
- })
- })
- .then(charge => {
- res.json(charge);
- })
- .catch(err => {
- console.log(err)
- res.send(err.toString());
- });
- }
- function postStripeImmediateCharge(req, res) {
- stripe.charges.create({
- amount: 2085,
- description: "Immediate charge",
- currency: "eur",
- source: 'tok_bypassPending'
- })
- .then(charge => {
- console.log('charge created', charge);
- res.json(charge);
- })
- }
- function postStripeTransfer(req, res) {
- db.get('SELECT * from access_tokens WHERE id=2')
- .then(record => {
- // res.json(record);
- console.log(record, {
- amount: 100,
- currency: "eur",
- destination: 'acct_1BHuOvI6vDethKu9', // record.stripe_user_id,
- transfer_group: "order-7",
- });
- stripe.transfers.create({
- amount: 100,
- currency: "eur",
- destination: record.stripe_user_id,
- transfer_group: "order-7",
- }).then(function(transfer) {
- // asynchronously called
- res.json({ transfer });
- });
- })
- }
- module.exports = {
- getIndex,
- getStripeCallback,
- postStripeCharge,
- postStripeImmediateCharge,
- postStripeTransfer
- };
|