forked from Mangopay/mangopay2-nodejs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandates.js
161 lines (140 loc) · 5.27 KB
/
Mandates.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @module Mandates
* @desc [MangoPay Mandates API Reference](https://docs.mangopay.com/endpoints/v2.01/mandates)
*/
var _ = require('underscore');
var Service = require('../service');
var Mandate = require('../models/Mandate');
var Transaction = require('../models/Transaction');
var Mandates = Service.extend({
/**
* Create a new Mandate
* @param {Object} mandate Mandate object
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Promise of the request
*/
create: function(mandate, callback, options) {
options = this._api._getOptions(callback, options, {
data: mandate,
dataClass: Mandate
});
var mandateKey = this.getMandateKey(mandate);
var executionKey = this.getExecutionKey(mandate);
return this._api.method('mandates_' + mandateKey + '-' + executionKey + '_create', callback, options);
},
/**
* Get all mandates
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Request promise
*/
getAll: function(callback, options) {
if (options && !options.hasOwnProperty('parameters'))
Object.assign(options, {parameters: {...options}});
options = this._api._getOptions(callback, options, {
dataClass: Mandate
});
return this._api.method('mandates_all', callback, options);
},
/**
* Get mandate by ID
* @param {number} mandateId Mandate identifier
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Request promise
*/
get: function(mandateId, callback, options) {
options = this._api._getOptions(callback, options, {
path: {
id: mandateId
},
dataClass: Mandate
});
return this._api.method('mandates_get', callback, options);
},
/**
* Cancel a mandate
* @param {number} mandateId Mandate identifier
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Request promise
*/
cancel: function(mandateId, callback, options) {
options = this._api._getOptions(callback, options, {
path: {
id: mandateId
}
});
return this._api.method('mandates_cancel', callback, options);
},
/**
* Gets user's mandates
* @param {number} userId User identifier
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Request promise
*/
getMandatesForUser: function(userId, callback, options) {
if (options && !options.hasOwnProperty('parameters'))
Object.assign(options, {parameters: {...options}});
options = this._api._getOptions(callback, options, {
path: {
id: userId
},
dataClass: Mandate
});
return this._api.method('mandates_get_for_user', callback, options);
},
/**
* Gets bank account mandates
* @param {number} userId User identifier
* @param {number} bankAccountId Bank Account identifier
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Request promise
*/
getMandatesForBankAccount: function(userId, bankAccountId, callback, options) {
if (options && !options.hasOwnProperty('parameters'))
Object.assign(options, {parameters: {...options}});
options = this._api._getOptions(callback, options, {
path: {
id: userId,
bankAccountId: bankAccountId
},
dataClass: Mandate
});
return this._api.method('mandates_get_for_bank_account', callback, options);
},
/**
* Gets Transactions for a Mandate
* @param {number} mandateId Mandate identifier
* @param {Function} callback Callback function
* @param {Object} options Request options
* @return {Object} Request promise
*/
getTransactions: function(mandateId, callback, options) {
if (options && !options.hasOwnProperty('parameters'))
Object.assign(options, {parameters: {...options}});
options = this._api._getOptions(callback, options, {
path: {
id: mandateId
},
dataClass: Transaction
});
return this._api.method('transactions_get_for_mandate', callback, options);
},
getMandateKey: function(mandate) {
if (mandate.MandateType) {
return mandate.MandateType.toLowerCase().replace('_', '');
}
return 'directdebit';
},
getExecutionKey: function(mandate) {
if (mandate.ExecutionType) {
return mandate.ExecutionType.toLowerCase();
}
return 'web';
}
});
module.exports = Mandates;