routes.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const config = require('./config');
  2. const stripe = require('stripe')(config.stripeKey);
  3. const db = require('sqlite');
  4. const request = require('request-promise');
  5. const fs = require('fs');
  6. function lastOrder() {
  7. let _ref;
  8. return fs.readFileAsync('orderRef.json')
  9. .then(buf => JSON.parse(buf.toString()))
  10. .then(json => {
  11. json.ref++;
  12. _ref = json.ref;
  13. return fs.writeFileAsync('orderRef.json', JSON.stringify(json))
  14. .then(() => ('order-' + _ref));
  15. })
  16. }
  17. /**
  18. * Send index page
  19. */
  20. function getIndex(req, res) {
  21. // Build Stripe Authorization URL
  22. const authUrl = 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=' +
  23. config.stripeClientId + '&scope=read_write&redirect_uri=http://localhost:' + config.port + '/stripe-connect';
  24. // Render template
  25. res.render('index', {
  26. authUrl
  27. });
  28. }
  29. var mappers = {
  30. string: v => ("'" + v.replace(/'/g, '\'') + "'"),
  31. boolean: v => (v ? 1 : 0)
  32. };
  33. function buildInsertQuery(table, fields) {
  34. const keys = Object.keys(fields);
  35. const values = Object.values(fields);
  36. const valuesString = values.map(v => (
  37. mappers[typeof v] ? mappers[typeof v](v) : v
  38. ));
  39. return 'INSERT INTO ' + table + ' (' +
  40. keys.join(',') + ') VALUES(' +
  41. valuesString + ')';
  42. }
  43. /**
  44. * Handle return from Stripe Authorization page
  45. */
  46. function getStripeCallback(req, res) {
  47. // res.json(req.query);
  48. // Sending to slack channel
  49. request({
  50. uri: 'https://connect.stripe.com/oauth/token',
  51. method: "POST",
  52. json: {
  53. client_secret: config.stripeKey,
  54. grant_type: 'authorization_code',
  55. code: req.query.code
  56. }
  57. })
  58. .then(response => {
  59. const query = buildInsertQuery('access_tokens', response);
  60. console.log('stripe response', response, query);
  61. db.get(query)
  62. .then(result => {
  63. console.log(result);
  64. res.json({ success: true });
  65. })
  66. })
  67. .catch(err => {
  68. console.log(err);
  69. });
  70. }
  71. /**
  72. * Create a Stripe Charge from Stripe Checkout form
  73. */
  74. function postStripeCharge(req, res) {
  75. const { stripeEmail, stripeToken, stripeTokenType } = req.body;
  76. lastOrder()
  77. .then(transfer_group => {
  78. console.log('got last order ref', transfer_group)
  79. return stripe.charges.create({
  80. amount: 999,
  81. currency: "eur",
  82. description: "Example charge",
  83. source: stripeToken,
  84. transfer_group,
  85. destination: {
  86. account: 'acct_1BJQ2TCxNRnLITys'
  87. }
  88. })
  89. })
  90. .then(charge => {
  91. res.json(charge);
  92. })
  93. .catch(err => {
  94. console.log(err)
  95. res.send(err.toString());
  96. });
  97. }
  98. function postStripeImmediateCharge(req, res) {
  99. stripe.charges.create({
  100. amount: 2085,
  101. description: "Immediate charge",
  102. currency: "eur",
  103. source: 'tok_bypassPending'
  104. })
  105. .then(charge => {
  106. console.log('charge created', charge);
  107. res.json(charge);
  108. })
  109. }
  110. function postStripeTransfer(req, res) {
  111. db.get('SELECT * from access_tokens WHERE id=2')
  112. .then(record => {
  113. // res.json(record);
  114. console.log(record, {
  115. amount: 100,
  116. currency: "eur",
  117. destination: 'acct_1BHuOvI6vDethKu9', // record.stripe_user_id,
  118. transfer_group: "order-7",
  119. });
  120. stripe.transfers.create({
  121. amount: 100,
  122. currency: "eur",
  123. destination: record.stripe_user_id,
  124. transfer_group: "order-7",
  125. }).then(function(transfer) {
  126. // asynchronously called
  127. res.json({ transfer });
  128. });
  129. })
  130. }
  131. module.exports = {
  132. getIndex,
  133. getStripeCallback,
  134. postStripeCharge,
  135. postStripeImmediateCharge,
  136. postStripeTransfer
  137. };