Repository navigation
Expand file tree
/
Copy pathpaymill.js
More file actions
190 lines (172 loc) · 5.53 KB
/
Copy pathpaymill.js
File metadata and controls
190 lines (172 loc) · 5.53 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/** @module paymill */
var __ = require("underscore");
var apiHost = "api.paymill.com";
var apiBaseUrl = "/v2.1";
var apiEncoding = "utf8";
/* note, we have to edit this manually, as the package.json is available only in node*/
var version = "2.0.3";
var sourcePrefix = "paymill-js";
function ExternalHandler() {
}
ExternalHandler.prototype.apiKey = null;
ExternalHandler.prototype.setApiKey = function(apiKey) {
this.apiKey = apiKey;
};
ExternalHandler.prototype.getDeferedObject = function() {
throw new PMError(PMError.Type.INTERNAL, "ExternalHandler.getDeferedObject() is abstract! Did you initialze?");
};
ExternalHandler.prototype.getPromiseObject = function(defer) {
throw new PMError(PMError.Type.INTERNAL, "ExternalHandler.getPromiseObject() is abstract! Did you initialze?");
};
/**
* Make a single http request.
* @param {HttpRequest} httpRequest a http request object, describing all properties of the request
* @abstract
* @return {object} a promise
*/
ExternalHandler.prototype.httpRequest = function(httpRequest) {
throw new PMError(PMError.Type.INTERNAL, "ExternalHandler.httpRequest() is abstract! Did you initialze?");
};
ExternalHandler.prototype.includeCallbackInPromise = function(httpRequest) {
throw new PMError(PMError.Type.INTERNAL, "ExternalHandler.includeCallbackInPromise() is abstract! Did you initialze?");
};
/*
* Identifis the handler type.
* @abstract
* @return {string} the identifier of this handler, e.g. "node","parse"
*/
ExternalHandler.prototype.getHandlerIdentifier = function() {
throw new PMError(PMError.Type.INTERNAL, "ExternalHandler.getSourceIdentifier() is abstract! Did you initialze?");
};
/*
* Identify the wrapper version against the REST API.
* @abstract
* @return {string} the source parameter for transactions and preauthorizations. handler, e.g. "paymill-js-node-1.0.1"
*/
function getSourceIdentifier() {
return sourcePrefix + "-" + platformIdentifier + "-" + version;
}
/**
* Callback for HttpClient requests.
* @callback HttpClient~requestCallback
* @param {PMError} error a PMError for failure
* @param {string} responseMessage
*/
function HttpRequest(path, method, params) {
this.path = path;
this.method = method;
this.params = params;
this.requestBody = null;
this.headers = {};
if (method === "GET" || method === "DELETE") {
this.path = this.path + urlEncode(params, true);
this.headers = {
"Content-Length" : 0
};
} else {
if (params !== null) {
this.requestBody = urlEncode(params, false);
this.headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"Content-Length" : this.requestBody.length
};
}
}
}
/**
* Checks if an http text response contains json and a data field.
* @param data the http response as string
* @returns {boolean} true if json and data is present, else otherwise (e.g. error)
*/
function isDataPresent(data) {
try {
var parsedData = JSON.parse(data);
if (parsedData.data !== undefined && parsedData.data !== null) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
/**
* PaymillContecxt loads the context of PAYMILL for a single account, by providing a merchants private key<br />
* It creates 8 services, which represents the PAYMILL API:
* @param apiKey
* @constructor
*/
/**
*
* A PaymillContext represents one client of the PAYMILL API. It holds the API key and exposes the 8 services,
* which correspond to the API endpoints.
* @class PaymillContext
*/
function PaymillContext(apiKey) {
// initalize services
this.handler = handlerConstructor(apiKey);
this.clients = new ClientService();
this.clients.setHandler(this.handler);
this.offers = new OfferService();
this.offers.setHandler(this.handler);
this.payments = new PaymentService();
this.payments.setHandler(this.handler);
this.preauthorizations = new PreauthorizationService();
this.preauthorizations.setHandler(this.handler);
this.refunds = new RefundService();
this.refunds.setHandler(this.handler);
this.subscriptions = new SubscriptionService();
this.subscriptions.setHandler(this.handler);
this.transactions = new TransactionService();
this.transactions.setHandler(this.handler);
this.webhooks = new WebhookService();
this.webhooks.setHandler(this.handler);
this.checksums = new ChecksumService();
this.checksums.setHandler(this.handler);
}
PaymillContext.prototype.constructor = PaymillContext;
PaymillContext.prototype.handler = null;
PaymillContext.prototype.apiKey = null;
PaymillContext.prototype.setApiKey = function(apiKey) {
this.handler.setApiKey(apiKey);
};
/**
* The {@link ClientService} service.
*/
PaymillContext.prototype.clients = null;
/**
* The {@link OfferService} service.
*/
PaymillContext.prototype.offers = null;
/**
* The {@link PaymentService} service.
*/
PaymillContext.prototype.payments = null;
/**
* The {@link PreauthorizationService} service.
*/
PaymillContext.prototype.preauthorizations = null;
/**
* The {@link RefundService} service.
*/
PaymillContext.prototype.refunds = null;
/**
* The {@link TransactionService} service.
*/
PaymillContext.prototype.transactions = null;
/**
* The {@link SubscriptionService} service.
*/
PaymillContext.prototype.subscriptions = null;
/**
* The {@link WebhookService} service.
*/
PaymillContext.prototype.webhooks = null;
/**
* The {@link ChecksumService} service.
*/
PaymillContext.prototype.checksums = null;
exports.PaymillContext = PaymillContext;
exports.getContext = function(apiKey) {
return new PaymillContext(apiKey);
};