|
| 1 | +var mount = require('koa-mount'); |
| 2 | +var config = require('config'); |
| 3 | + |
| 4 | +// Interaction with payment systems only. |
| 5 | + |
| 6 | +exports.loadOrder = require('./lib/loadOrder'); |
| 7 | +exports.loadTransaction = require('./lib/loadTransaction'); |
| 8 | + |
| 9 | +var Order = exports.Order = require('./models/order'); |
| 10 | +var Transaction = exports.Transaction = require('./models/transaction'); |
| 11 | +var TransactionLog = exports.TransactionLog = require('./models/transactionLog'); |
| 12 | + |
| 13 | +// all submodules |
| 14 | +var paymentModules = {}; |
| 15 | +for(var name in config.payments.modules) { |
| 16 | + paymentModules[name] = require('./' + name); |
| 17 | +} |
| 18 | + |
| 19 | +// mount('/webmoney', webmoney.middleware()) |
| 20 | +var paymentMounts = []; |
| 21 | +for(var name in paymentModules) { |
| 22 | + paymentMounts.push(mount('/' + name, paymentModules[name].middleware)); |
| 23 | +} |
| 24 | + |
| 25 | +// delegate all HTTP calls to payment modules |
| 26 | +exports.middleware = function*(next) { |
| 27 | + |
| 28 | + for (var i = 0; i < paymentMounts.length; i++) { |
| 29 | + yield* paymentMounts[i].call(this, next); |
| 30 | + } |
| 31 | + |
| 32 | +}; |
| 33 | + |
| 34 | +exports.populateContextMiddleware = function*(next) { |
| 35 | + this.redirectToOrder = function(order) { |
| 36 | + order = order || this.order; |
| 37 | + this.redirect('/' + order.module + '/order/' + order.number); |
| 38 | + }; |
| 39 | + this.loadOrder = exports.loadOrder; |
| 40 | + this.loadTransaction = exports.loadTransaction; |
| 41 | + yield* next; |
| 42 | +}; |
| 43 | + |
| 44 | +// creates transaction and returns the form to submit for its payment |
| 45 | +// delegates form to the method |
| 46 | +exports.createTransactionForm = function* (order, method) { |
| 47 | + |
| 48 | + var transaction = new Transaction({ |
| 49 | + order: order._id, |
| 50 | + amount: order.amount, |
| 51 | + module: method |
| 52 | + }); |
| 53 | + |
| 54 | + yield transaction.persist(); |
| 55 | + |
| 56 | + return paymentModules[method].renderForm(transaction); |
| 57 | + |
| 58 | +}; |
| 59 | + |
| 60 | + |
| 61 | + |
0 commit comments