-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
85 lines (70 loc) · 2.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var BaseGateway = require('42-cent-base').BaseGateway;
var Omise = require('42-cent-omise').factory;
var Stripe = require('42-cent-stripe').factory;
var Braintree = require('42-cent-braintree').factory;
var PayPal = require('42-cent-paypal').factory;
var WorldPay = require('42-cent-worldpay').factory;
var Beanstream = require('42-cent-beanstream').factory;
var Moneris = require('42-cent-moneris');
// Deprecated:
// var AuthorizeNet = require('authorize-net');
// var RocketGate = require('rocketgate').gateway;
// var NMI = require('nmi');
// var PayFlow = require('payflow').gateway;
// var VirtualMerchant = require('virtualmerchant').gateway;
var supportedGateway = {
"Omise": Omise,
"Stripe": Stripe,
"Braintree": Braintree,
"PayPal": PayPal,
"WorldPay": WorldPay,
"Beanstream": Beanstream,
// "Authorize.Net": AuthorizeNet,
// "PayFlow": PayFlow,
// "RocketGate": RocketGate,
// "VirtualMerchant": VirtualMerchant,
// "NMI": NMI,
"Moneris": Moneris
};
var CreditCard = require('42-cent-model').CreditCard;
var Prospect = require('42-cent-model').Prospect;
var SubscriptionPlan = require('42-cent-model').SubscriptionPlan;
var Order = require('42-cent-model').Order;
/**
* @param {String} gateway - the name of a registered gateway
* @param {Object} constructorOption - an object with the properties required by a particular Gateway factory,
* see the relevant gateway factory for more details
* @returns {BaseGateway} an object which inherits (prototype) from BaseGateway
*/
exports.use = function use (gateway, constructorOption) {
var gatewayFactory = supportedGateway[gateway];
var gw;
if (!gatewayFactory) {
throw new Error('the gateway provided does not match any item of the list...todo');
}
gw = gatewayFactory(constructorOption);
if (!gw instanceof BaseGateway) {
throw new Error('the gateway must be an instance of the BaseGateway');
}
return gw;
};
/**
* register a new gateway factory (note it will overwrite an existing one
* @param {String} name - the gateway name
* @param {Function} factory - a factory function which must return an instance of BaseGateway
*/
exports.registerGateway = function registerGateway (name, factory) {
supportedGateway[name] = factory;
};
exports.createCreditCard = function (creditCard) {
return new CreditCard(creditCard);
};
exports.createProspect = function (prospect) {
return new Prospect(prospect);
};
exports.createSubscriptionPlan = function creatSubscriptionPlan (options) {
return new SubscriptionPlan(options);
};
exports.createOrder = function createOrder (order) {
return new Order(order)
};