index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const express = require('express');
  2. const path = require('path');
  3. const CrudModule = require('../../base/CrudModule');
  4. const RepositorySystem = require('../../systems/RepositorySystem');
  5. const RoutingSystem = require('../../systems/RoutingSystem');
  6. const UserSystem = require('../../systems/UserSystem');
  7. const Actions = require('../../systems/ActionSystem');
  8. const braintree = require("braintree");
  9. var gateway = braintree.connect({
  10. environment: braintree.Environment.Sandbox,
  11. merchantId: 'h7rc64shcsrdpcf9',
  12. publicKey: 'dzgzpjgpcjvngnsp',
  13. privateKey: 'edb45fef09a688079051cc15de8e481d'
  14. });
  15. let PaymentScheme = {
  16. fields: [
  17. {
  18. key: "name",
  19. type: "text"
  20. }
  21. ]
  22. };
  23. let OrderPaymentScheme = {
  24. fields: [
  25. {
  26. key: "payment_method",
  27. type: "integer",
  28. fk: {
  29. table: 'Payment',
  30. key: 'id'
  31. }
  32. },
  33. {
  34. key: "status",
  35. type: "text"
  36. },
  37. {
  38. key: "transaction_id",
  39. type: "text"
  40. }
  41. ]
  42. };
  43. class PaymentModule extends CrudModule {
  44. constructor(){
  45. super('/payment', [3,0,3,4,0], 'Payment');
  46. this.repository = RepositorySystem.create('Payment', PaymentScheme);
  47. this.ordersRepository = RepositorySystem.create('Orders', OrderPaymentScheme);
  48. this.setRepo(this.repository);
  49. // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
  50. this.router = express.Router();
  51. this.init();
  52. }
  53. init() {
  54. let routes = [
  55. {
  56. type: 'custom',
  57. method: 'get',
  58. endpoint: '/client_token',
  59. customFn: (req,res) => this.generateToken(req,res)
  60. },
  61. {
  62. type: 'custom',
  63. method: 'post',
  64. endpoint: '/checkout',
  65. customFn: (req,res) => this.checkout(req,res)
  66. }
  67. ];
  68. RoutingSystem.loadRoutes(routes, this.router);
  69. Actions.on('verifyOrder', (order) => this.verifyOrder(order), true);
  70. Actions.on('getOrders', (order) => this.getBraintreeData(order) );
  71. }
  72. getBraintreeData(order) {
  73. return new Promise((resolve, reject) => {
  74. gateway.transaction.find(order.transaction_id, function (err, transaction) {
  75. if(err) {
  76. console.log("Braintree error : ")
  77. console.log(err)
  78. return resolve(order);
  79. }
  80. order.transaction = transaction;
  81. order.status = transaction.status;
  82. delete order.transaction_id;
  83. return resolve(order);
  84. });
  85. });
  86. }
  87. generateToken(req, res) {
  88. UserSystem.role(req,res, 1);
  89. let customerId = req.user.id;
  90. gateway.clientToken.generate({
  91. // customerId
  92. }).then((response) => {
  93. res.send(response);
  94. });
  95. }
  96. checkout(req, res) {
  97. let nonceFromTheClient = req.body.nonce;
  98. let amount = 10;
  99. gateway.transaction.sale({
  100. amount,
  101. paymentMethodNonce: nonceFromTheClient,
  102. options: {
  103. submitForSettlement: true
  104. }
  105. }, function (err, result) {
  106. res.send(result);
  107. });
  108. }
  109. verifyOrder(order) {
  110. let nonceFromTheClient = order.nonce;
  111. let amount = parseFloat(order.amount).toFixed(2);
  112. return new Promise((resolve,reject) => {
  113. gateway.transaction.sale({
  114. amount,
  115. paymentMethodNonce: nonceFromTheClient,
  116. options: {
  117. submitForSettlement: true
  118. }
  119. }, function (err, result) {
  120. if(err || !result.success) {
  121. // console.log("Error from Transaction ~~~~~~~~~~~~")
  122. // console.log(err)
  123. return reject(err || result);
  124. }
  125. // console.log("Transaction submitted", " Amount: ", amount, " RESULT : ")
  126. // console.log(result);
  127. order.transaction_id = result.transaction.id;
  128. order.status = result.transaction.status;
  129. resolve(order);
  130. });
  131. });
  132. }
  133. }
  134. module.exports = PaymentModule;