-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpaymill.node.js
4734 lines (4320 loc) · 142 KB
/
paymill.node.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @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);
};
function NodeHandler() {
this.when = require("when");
this.https = require("https");
}
NodeHandler.prototype = ExternalHandler.prototype;
NodeHandler.prototype.constructor = NodeHandler;
NodeHandler.prototype.getDeferedObject = function() {
return this.when.defer();
};
NodeHandler.prototype.getPromiseObject = function(defer) {
return defer.promise;
};
NodeHandler.prototype.httpRequest = function(httpRequest) {
var defer = this.getDeferedObject();
var promise = this.getPromiseObject(defer);
var options = {
hostname : apiHost,
port : 443,
path : apiBaseUrl + httpRequest.path,
auth : this.apiKey + ":",
method : httpRequest.method,
headers : httpRequest.headers
};
var req = this.https.request(options, function(res) {
res.setEncoding(apiEncoding);
var status = res.statusCode;
var headers = res.headers;
var data = "";
res.on("data", function(d) {
data = data + d;
});
res.on("end", function() {
if (!isDataPresent(data)) {
defer.reject(new PMError(PMError.Type.API, data, "http status code:" + status + "\nheaders:" + headers + "\ndata:" + data, JSON.stringify(data)));
} else {
defer.resolve(data);
}
});
});
req.on("error", function(e) {
defer.reject(new PMError(PMError.Type.IO, null, e.toString()));
});
if ( typeof httpRequest.requestBody === "string" && httpRequest.requestBody.length > 0) {
req.write(httpRequest.requestBody);
}
req.end();
return promise;
};
NodeHandler.prototype.includeCallbackInPromise = function(promise, callback) {
var positive, negative;
if (__.isFunction(callback)) {
var when = this.when;
positive = function(result) {
callback(null, result);
return when.resolve(result);
};
negative = function(error) {
callback(error);
return when.reject(error);
};
promise.then(positive, negative);
}
return promise;
};
NodeHandler.prototype.getHandlerIdentifier = function() {
return "node";
};
var handlerConstructor = function(apiKey) {
var handler=new NodeHandler();
handler.setApiKey(apiKey);
return handler;
};
var platformIdentifier = 'node';
function PMError(type, message, detailMessage, apiMessage) {
if (message && message.length > 0) {
this.message = type + ":" + message;
} else {
this.message = type + ".";
}
this.detailMessage = detailMessage;
this.apiMessage = apiMessage;
this.type = type;
return this;
}
PMError.prototype = new Error();
PMError.prototype.constructor = PMError;
PMError.prototype.name = "PMError";
PMError.Type = {
API : "API Error",
WRONG_PARAMS : "Invalid arguments",
IO : "Network issue",
NOT_INIT : "Not initialized",
INTERNAL : "Internal error"
};
exports.PMError=PMError;
function deserializePaymillObject(parsedJson, objectType) {
if (parsedJson === undefined || parsedJson === null) {
return null;
}
var result = new objectType.prototype.constructor();
result.fromJson(parsedJson);
return result;
}
function deserializePaymillObjectList(parsedJson, objectType) {
if (parsedJson === undefined || parsedJson === null) {
return null;
}
var result = new Array(parsedJson.length);
for (var i = 0; i < parsedJson.length; i++) {
result[i] = deserializePaymillObject(parsedJson[i], objectType);
}
return result;
}
function deserializeDate(unixTime) {
if (unixTime === undefined || unixTime === null) {
return null;
}
if (!__.isNumber(unixTime)) {
return unixTime;
}
return new Date(unixTime*1000);
}
function urlEncode(params, appendQuestion) {
if (!params) {
return "";
}
var ret = [];
for (var key in params) {
if (key === "" || __.isFunction(params[key])) {
continue;
}
ret.push(key + "=" + encodeURIComponent(params[key]));
}
if (ret.length > 0) {
var result = ret.join("&");
if (appendQuestion) {
return "?" + result;
} else {
return result;
}
} else {
return "";
}
}
function getTimeFromObject(obj) {
if ( obj instanceof Date) {
return obj.getTime();
}
if (__.isString(obj) || __.isNumber(obj)) {
return obj;
}
throw new PMError(PMError.Type.WRONG_PARAMS, obj + "must be either a string, a number or a Date");
}
function validateField(validationFunc, field, fieldName, optional) {
if (field !== undefined && field !== null) {
validationFunc(field,fieldName);
} else {
if (!optional) {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldName + " is mandatory");
}
}
}
function validateMandatory(field,fieldName) {
if (__.isEmpty(field)) {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldName + " is mandatory");
}
}
function validateNumber(field, fieldname, optional) {
return validateField(function (number,numberName) {
if (! (__.isNumber(number) || __.isNumber(parseInt(number,10))) ) {
throw new PMError(PMError.Type.WRONG_PARAMS, numberName + " must be a number or a string representing a number");
}
},field,fieldname,optional);
}
function validateBoolean(field, fieldname, optional) {
return validateField(function (boolean, booleanName) {
if (! (__.isBoolean(boolean)) ) {
throw new PMError(PMError.Type.WRONG_PARAMS, booleanName + " must be a boolean");
}
},field,fieldname,optional);
}
function validateString(field, fieldname, optional) {
return validateField(function (string,stringName) {
if (!(__.isString(string) ) ) {
throw new PMError(PMError.Type.WRONG_PARAMS, stringName + " must be a string");
}
},field,fieldname,optional);
}
function validateList(field, fieldname, fieldtype, optional) {
// check empty and mandatory
if (__.isEmpty(field)) {
if (optional) {
return;
} else {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldname + " is mandatory.");
}
}
// check array
if (!__.isArray(field)) {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldname + " must be an array of type " + fieldtype);
}
for (var i=0; i<field.length; i++) {
if (!(field[i] instanceof fieldtype)) {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldname + "[" + i + "] is not " + fieldtype);
}
}
}
function validateObject(field, fieldname, fieldtype, optional) {
// check empty and mandatory
if (__.isEmpty(field)) {
if (optional) {
return;
} else {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldname + " is mandatory.");
}
}
if (!(field instanceof fieldtype)) {
throw new PMError(PMError.Type.WRONG_PARAMS, fieldname + " is not " + fieldtype);
}
}
function getTimeFilter(from, to) {
return getTimeFromObject(from) + "-" + getTimeFromObject(to);
}
function getIdFromObject(obj, objectType) {
if (!obj) {
throw new PMError(PMError.Type.WRONG_PARAMS, "Object must be either a string or object with a string member 'id', but it is not defined");
}
if (__.isString(obj) && !__.isEmpty(obj)) {
return obj;
}
if (objectType) {
//there is an object type
if ( obj instanceof objectType && obj.id && __.isString(obj.id)) {
return obj.id;
}
} else {
// no object type defined
if (obj.id && __.isString(obj.id)) {
return obj.id;
}
}
throw new PMError(PMError.Type.WRONG_PARAMS, obj + "must be either a string or " + objectType + " with a valid id.");
}
function getRefreshObject(obj,type) {
if ( obj instanceof type ) {
if (!__.isEmpty(obj.id)) {
return obj;
} else {
throw new PMError(PMError.Type.WRONG_PARAMS, obj + " is of correct type ( " + type + " ), but has no valid id.");
}
} else {
var id = getIdFromObject(obj, type);
var result = new type.prototype.constructor();
result.id=id;
return result;
}
}
function getRealEquality(equality) {
if (equality === undefined) {
return Filter.EQUALITY.EQUAL;
} else {
return equality;
}
}
function getUnixTimeFromParam(param, paramName) {
if ( param instanceof Date) {
return Math.floor((param.getTime())/1000);
} else if (__.isNumber(param) || __.isString(param)) {
return param;
} else {
throw new PMError(PMError.Type.WRONG_PARAMS, "parameter " + paramName + " must be a Date, number or string");
}
}
/**
*
* Creates a new Address.
* @class Address
* @extends PaymillObject
* @classdesc An address object belongs to exactly one transaction and can represent either its shipping address or billing address. Note, that state and postal_code are mandatory for PayPal transactions in certain countries, please consult PayPal documentation for more details.
*/
function Address() {
}
Address.prototype = new PaymillObject();
Address.prototype.constructor = Address;
/**
* Street address (incl. street number), max. 100 characters.
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.street_address = null;
/**
* Addition to street address (e.g. building, floor, or c/o), max. 100 characters.
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.street_address_addition = null;
/**
* City, max. 40 characters.
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.city = null;
/**
* State or province, max. 40 characters.
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.state = null;
/**
* Country-specific postal code, max. 20 characters.
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.postal_code = null;
/**
* 2-letter country code according to ISO 3166-1 alpha-2
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.country = null;
/**
* Contact phone number, max. 20 characters
* @type {string}
* @memberof Address.prototype
*/
Address.prototype.phone = null;
/**
* The {@link Address} object.
*/
exports.Address = Address;
/**
*
* Creates a new payment. Generally you should never create a PAYMILL object on your own.
* @class Checksum
* @classdesc Checksum validation is a simple method to ensure the integrity of transferred data. Basically, we generate a hash out of the given parameters and your private API key. If you send us a request with transaction data and the generated checksum, we can easily validate the data. To make the checksum computation as easy as possible we provide this endpoint for you. For transactions that are started client-side, e.g. PayPal checkout, it is required to first create a checksum on your server and then provide that checksum when starting the transaction in the browser. The checksum needs to contain all data required to subsequently create the actual transaction.
*/
function Checksum() {
}
Checksum.prototype = new PaymillObject();
Checksum.prototype.constructor = Checksum;
/**
* Unique identifier for this checksum.
* @type {string}
* @memberof Checksum.prototype
*/
Checksum.prototype.id = null;
/**
* The type of the checksum. Currently only 'paypal'
* @type {string}
* @memberof Checksum.prototype
*/
Checksum.prototype.type = null;
/**
* The actual checksum.
* @type {string}
* @memberof Checksum.prototype
*/
Checksum.prototype.checksum = null;
/**
* Transaction data.
* @type {string}
* @memberof Checksum.prototype
*/
Checksum.prototype.data = null;
Checksum.prototype.created_at = null;
/**
* Unix-Timestamp for the last update.
* @type {Date}
* @memberof Checksum.prototype
*/
Checksum.prototype.updated_at = null;
/**
* App (ID) that created this checksum or null if created by yourself.
* @type {string}
* @memberof Checksum.prototype
*/
Checksum.prototype.app_id = null;
/*
* special fields
*/
Checksum.prototype.getFieldDefinitions = function() {
return {
created_at : deserializeDate,
updated_at : deserializeDate
};
};
exports.Checksum = Checksum;
/**
*
* Creates a new Client. Generally you should never create a PAYMILL object on your own.
* @class Client
* @extends PaymillObject
* @classdesc The clients object is used to edit, delete, update clients as well as to permit refunds, subscriptions, insert credit card details for a client, edit client details and of course make transactions. Clients can be created individually by you or they will be automatically generated with the transaction if there is no client ID transmitted.
*/
function Client() {
}
Client.prototype = new PaymillObject();
Client.prototype.constructor = Client;
/**
* Unique identifier of this preauthorization
* @type {string}
* @memberof Client.prototype
*/
Client.prototype.id = null;
/**
* Mail address of this client.
* @type {string}
* @memberof Client.prototype
*/
Client.prototype.email = null;
/**
* Additional description for this client, perhaps the identifier from your CRM system?
* @type {string}
* @memberof Client.prototype
*/
Client.prototype.description = null;
/**
* Unix-Timestamp for the creation date.
* @type {Date}
* @memberof Client.prototype
*/
Client.prototype.created_at = null;
/**
* Unix-Timestamp for the last update.
* @type {Date}
* @memberof Client.prototype
*/
Client.prototype.updated_at = null;
/**
* List of payments.
* @type {Array.<Payment>}
* @memberof Client.prototype
*/
Client.prototype.payment = null;
/**
* List of subscriptions.
* @type {Array.<Subscription>}
* @memberof Client.prototype
*/
Client.prototype.subscription = null;
/**
* App (ID) that created this client or null if created by yourself.
* @type {string}
* @memberof Client.prototype
*/
Client.prototype.app_id = null;
/*
* special fields
*/
Client.prototype.getFieldDefinitions = function() {
return {
created_at : deserializeDate,
updated_at : deserializeDate,
payment : function(json) {
return deserializePaymillObjectList(json, Payment);
},
subscription : function(json) {
return deserializePaymillObjectList(json, Subscription);
}
};
};
Client.prototype.getUpdateableFields = function() {
return ["email", "description"];
};
/**
* Specify an order for clients.
* @class Client.Order
* @memberof Client
* @extends Order
*/
Client.Order = function() {
Order.call(this);
};
Client.Order.prototype = new Order();
Client.Order.constructor = Client.Order;
/**
* @returns {Order} order by creditcard
* @memberof Client.Order
*/
Client.Order.creditcard = function() {
var order = new Client.Order();
order.type = "creditcard";
return order;
};
/**
* @returns {Order} order by email
* @memberof Client.Order
*/
Client.Order.email = function() {
var order = new Client.Order();
order.type = "email";
return order;
};
/**
* @returns {Order} order by created_at
* @memberof Client.Order
*/
Client.Order.created_at = function() {
var order = new Client.Order();
order.type = "created_at";
return order;
};
/**
* Specify a filter for clients.
* @class Client.Filter
* @memberof Client
* @extends Filter
*/
Client.Filter = function() {
Filter.call(this);
};
Client.Filter.prototype = new Filter();
Client.Filter.constructor = Client.Filter;
/**
* Add filtering by payment
* @param {(string|Payment)} payment the payment object or its id.
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.payment = function(payment) {
this.payment = getIdFromObject(payment);
return this;
};
/**
* Add filtering by subscription
* @param {(string|Subscription)} subscription the subscription object or its id.
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.subscription = function(subscription) {
this.subscription = getIdFromObject(subscription);
return this;
};
/**
* Add filtering by offer
* @param {(string|Offer)} offer the offer object or its id.
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.offer = function(offer) {
this.offer = getIdFromObject(offer);
return this;
};
/**
* Add filtering by description
* @param {string} description the description
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.description = function(description) {
this.description = description;
return this;
};
/**
* Add filtering by email
* @param {string} email the email
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.email = function(email) {
this.email = email;
return this;
};
/**
* Add filtering by created_at
* @param {(number|Date)} from the start date of the filter, either as Date or Unix-time number.
* @param {(number|Date)} to the end date of the filter, either as Date or Unix-time number.
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.created_at = function(from, to) {
this.created_at = getTimeFilter(from,to);
return this;
};
/**
* Add filtering by updated_at
* @param {(number|Date)} from the start date of the filter, either as Date or Unix-time number.
* @param {(number|Date)} to the end date of the filter, either as Date or Unix-time number.
* @returns {Filter} the same filter.
* @memberof Client.Filter
*/
Client.Filter.prototype.updated_at = function(from, to) {
this.updated_at = getTimeFilter(from,to);
return this;
};
/**
* The {@link Client} object.
*/
exports.Client = Client;
/**
*
* Creates a new Fee. Generally you should never create a PAYMILL object on your own.
* @class Fee
* @extends PaymillObject
* @classdesc Describes app fees.
*/
function Fee() {
}
Fee.prototype = new PaymillObject();
Fee.prototype.constructor = Fee;
/**
* Fee type
* @type {Fee.Type}
* @memberof Fee.prototype
*/
Fee.prototype.type = null;
/**
* Unique identifier of the app which charges the fee
* @type {string}
* @memberof Fee.prototype
*/
Fee.prototype.application = null;
/**
* Unique identifier of the payment from which the fee will be charged
* @type {string}
* @memberof Fee.prototype
*/
Fee.prototype.payment = null;
/**
* Fee amount in the smallest currency unit e.g. "420" for 4.20 €
* @type {number}
* @memberof Fee.prototype
*/
Fee.prototype.amount = null;
/**
* ISO 4217 formatted currency code.
* @type {string}
* @memberof Fee.prototype
*/
Fee.prototype.currency = null;
/**
* Unix-Timestamp for the billing date.
* @type {Date}
* @memberof Fee.prototype
*/
Fee.prototype.billed_at = null;
/*
* special fields
*/
Fee.prototype.getFieldDefinitions = function() {
return {
billed_at : deserializeDate
};
};
/**
* Type of a Fee.
* @memberof Fee
* @property {string} APPLICATION
*/
Fee.Type = {
"APPLICATION" : "application"
};
/**
*
* Creates a new Filter. Use factories of implementing classes.
* @class Filter
*/
function Filter() {
}
/**
* Descibes equality for filters.
* @memberof Filter
* @property {string} EQUAL
* @property {string} LESS_THAN
* @property {string} GREATER_THAN
*/
Filter.EQUALITY = {
"LESS_THAN" : "<",
"GREATER_THAN" : ">",
"EQUAL" : ""
};
/**
*
* Creates a new Interval.
* @class Interval
* @param {number} length length of the interval (in units)
* @param {string|Interval.Unit} Unit for the interval
* @param {string|Interval.Weekday} weekday on which a charge should occur
* @extends PaymillObject
* @classdesc Defining an interval for an offer.
*/
function Interval(length, unit, weekday) {
this.length = length;
this.unit = unit;
this.chargeday = weekday;
}
Interval.prototype = new PaymillObject();
Interval.prototype.constructor = Interval;
/**
* Length of the interval (in units)
* @type {number}
* @memberof Interval.prototype
*/
Interval.prototype.length = null;
/**
* Unit for the interval
* @type {Interval.Unit}
* @memberof Interval.prototype
*/
Interval.prototype.unit = null;
/**
* Charge day in the week
* @type {Interval.Weekday}
* @memberof Interval.prototype
*/
Interval.prototype.chargeday = null;
Interval.prototype.fromJson = function(jsonObj) {
var weekdayParts = jsonObj.split(',');
if (weekdayParts.length > 1) {
this.chargeday = weekdayParts[1];
}
var split = weekdayParts[0].split(" ");
this.length = parseInt(split[0],10);
this.unit = split[1];
this.originalJson = jsonObj;
};
Interval.prototype.toString = function() {
var chargedayPart = (__.isEmpty(this.chargeday)) ? '': ',' + this.chargeday;
return "" + this.length + " " + this.unit + chargedayPart;
};
Interval.prototype.getUpdateIdentifier = function() {
return this.toString();
};
/**
* Units for an Interval.
* @memberof Interval
* @property {string} DAY
* @property {string} WEEK
* @property {string} MONTH
* @property {string} YEAR
*/
Interval.Unit = {
"DAY" : "DAY",
"WEEK" : "WEEK",
"MONTH" : "MONTH",
"YEAR" : "YEAR"
};