123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- const express = require('express');
- const path = require('path');
- const CrudModule = require('../../base/CrudModule');
- const RepositorySystem = require('../../systems/RepositorySystem');
- const RoutingSystem = require('../../systems/RoutingSystem');
- const UserSystem = require('../../systems/UserSystem');
- const Actions = require('../../systems/ActionSystem');
- const braintree = require("braintree");
- var gateway = braintree.connect({
- environment: braintree.Environment.Sandbox,
- merchantId: 'h7rc64shcsrdpcf9',
- publicKey: 'dzgzpjgpcjvngnsp',
- privateKey: 'edb45fef09a688079051cc15de8e481d'
- });
- let PaymentScheme = {
- fields: [
- {
- key: "name",
- type: "text"
- }
- ]
- };
- let OrderPaymentScheme = {
- fields: [
- {
- key: "payment_method",
- type: "integer",
- fk: {
- table: 'Payment',
- key: 'id'
- }
- },
- {
- key: "status",
- type: "text"
- },
- {
- key: "transaction_id",
- type: "text"
- }
- ]
- };
- class PaymentModule extends CrudModule {
- constructor(){
- super('/payment', [3,0,3,4,0], 'Payment');
- this.repository = RepositorySystem.create('Payment', PaymentScheme);
- this.ordersRepository = RepositorySystem.create('Orders', OrderPaymentScheme);
- this.setRepo(this.repository);
- // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
- this.router = express.Router();
- this.init();
- }
- init() {
- let routes = [
- {
- type: 'custom',
- method: 'get',
- endpoint: '/client_token',
- customFn: (req,res) => this.generateToken(req,res)
- },
- {
- type: 'custom',
- method: 'post',
- endpoint: '/checkout',
- customFn: (req,res) => this.checkout(req,res)
- }
- ];
- RoutingSystem.loadRoutes(routes, this.router);
- Actions.on('verifyOrder', (order) => this.verifyOrder(order), true);
- Actions.on('getOrders', (order) => this.getBraintreeData(order) );
- }
- getBraintreeData(order) {
- return new Promise((resolve, reject) => {
- gateway.transaction.find(order.transaction_id, function (err, transaction) {
- if(err) {
- console.log("Braintree error : ")
- console.log(err)
- return resolve(order);
- }
- order.transaction = transaction;
- order.status = transaction.status;
- delete order.transaction_id;
- return resolve(order);
- });
- });
- }
- generateToken(req, res) {
- UserSystem.role(req,res, 1);
- let customerId = req.user.id;
- gateway.clientToken.generate({
- // customerId
- }).then((response) => {
- res.send(response);
- });
- }
- checkout(req, res) {
- let nonceFromTheClient = req.body.nonce;
- let amount = 10;
- gateway.transaction.sale({
- amount,
- paymentMethodNonce: nonceFromTheClient,
- options: {
- submitForSettlement: true
- }
- }, function (err, result) {
- res.send(result);
- });
- }
- verifyOrder(order) {
- let nonceFromTheClient = order.nonce;
- let amount = parseFloat(order.amount).toFixed(2);
- return new Promise((resolve,reject) => {
- gateway.transaction.sale({
- amount,
- paymentMethodNonce: nonceFromTheClient,
- options: {
- submitForSettlement: true
- }
- }, function (err, result) {
- if(err || !result.success) {
- // console.log("Error from Transaction ~~~~~~~~~~~~")
- // console.log(err)
- return reject(err || result);
- }
- // console.log("Transaction submitted", " Amount: ", amount, " RESULT : ")
- // console.log(result);
- order.transaction_id = result.transaction.id;
- order.status = result.transaction.status;
- resolve(order);
- });
- });
- }
- }
- module.exports = PaymentModule;
|