BillingAgreement.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright 2015-2016 PayPal, Inc. */
  2. "use strict";
  3. var generate = require('../generate');
  4. var api = require('../api');
  5. /**
  6. * The billing agreements allows merchants to have users agree to be billed
  7. * for billing plans
  8. * @return {Object} billing agreement functions
  9. */
  10. function billingAgreement() {
  11. var baseURL = '/v1/payments/billing-agreements/';
  12. var operations = ['create', 'get', 'update', 'cancel'];
  13. /**
  14. * Search for transactions within a billing agreement
  15. * @param {String} id Identifier of the agreement resource for which to list transactions.
  16. * @param {String} start_date YYYY-MM-DD start date of range of transactions to list
  17. * @param {String} end_date YYYY-MM-DD end date of range of transactions to list
  18. * @param {Object|Function} config Configuration parameters e.g. client_id, client_secret override or callback
  19. * @param {Function} cb
  20. * @return {Object} agreement transaction list, array of agreement transaction objects
  21. */
  22. function searchTransactions(id, start_date, end_date, config, cb) {
  23. var date_range = {
  24. "start_date": start_date,
  25. "end_date": end_date
  26. };
  27. api.executeHttp('GET', baseURL + id + '/transactions', date_range, config, cb);
  28. }
  29. /**
  30. * Bill outstanding balance of an agreement
  31. * @param {String} id Identifier of the agreement resource for which to bill balance
  32. * @param {Object} data Agreement state descriptor, fields include note and amount which has two attributes, value and currency
  33. * @param {Object|Function} config Configuration parameters e.g. client_id, client_secret override or callback
  34. * @param {Function} cb
  35. * @return {} Returns the HTTP status of 204 if the call is successful
  36. */
  37. function billBalance(id, data, config, cb) {
  38. api.executeHttp('POST', baseURL + id + '/bill-balance', data, config, cb);
  39. }
  40. /**
  41. * Set the outstanding amount of an agreement
  42. * @param {String} id Identifier of the agreement resource for which to set balance
  43. * @param {Object} data Two attributes currency e.g. "USD" and value e.g. "100"
  44. * @param {Object|Function} config Configuration parameters e.g. client_id, client_secret override or callback
  45. * @param {Function} cb
  46. * @return {} Returns the HTTP status of 204 if the call is successful
  47. */
  48. function setBalance(id, data, config, cb) {
  49. api.executeHttp('POST', baseURL + id + '/set-balance', data, config, cb);
  50. }
  51. var ret = {
  52. baseURL: baseURL,
  53. /**
  54. * Execute an agreement after the buyer approves it
  55. * @param {String} token Payment Token of format EC-XXXXXX, appended to return url as a parameter after buyer approves agreement
  56. * @param {Object|Function} data Empty object or callback. Optional, will be removed in next major release.
  57. * @param {Object|Function} config Configuration parameters e.g. client_id, client_secret override or callback
  58. * @param {Function} cb
  59. * @return {Object} agreement object
  60. */
  61. execute: function execute(token, data, config, cb) {
  62. //support case where neither data nor config is provided
  63. if (typeof data === "function" && arguments.length === 2) {
  64. cb = data;
  65. data = {};
  66. }
  67. api.executeHttp('POST', this.baseURL + token + '/agreement-execute', data, config, cb);
  68. },
  69. /**
  70. * Changes agreement state to suspended, can be reactivated unlike cancelling agreement
  71. * @param {String} id Identifier of the agreement resource for which to suspend
  72. * @param {Object} data Add note attribute, reason for changing state of agreement
  73. * @param {Object|Function} config Configuration parameters e.g. client_id, client_secret override or callback
  74. * @param {Function} cb
  75. * @return {} Returns the HTTP status of 204 if the call is successful
  76. */
  77. suspend: function suspend(id, data, config, cb) {
  78. api.executeHttp('POST', this.baseURL + id + '/suspend', data, config, cb);
  79. },
  80. /**
  81. * Reactivate a suspended agreement
  82. * @param {String} id Identifier of the agreement resource for which to reactivate
  83. * @param {Object} data Add note attribute, reason for changing state of agreement
  84. * @param {Object|Function} config Configuration parameters e.g. client_id, client_secret override or callback
  85. * @param {Function} cb
  86. * @return {} Returns the HTTP status of 204 if the call is successful
  87. */
  88. reactivate: function reactivate(id, data, config, cb) {
  89. api.executeHttp('POST', this.baseURL + id + '/re-activate', data, config, cb);
  90. },
  91. billBalance: billBalance,
  92. setBalance: setBalance,
  93. searchTransactions: searchTransactions,
  94. //entries below are deprecated but provided for compatibility with 0.* versions
  95. bill_balance: billBalance,
  96. set_balance: setBalance,
  97. search_transactions: searchTransactions
  98. };
  99. ret = generate.mixin(ret, operations);
  100. return ret;
  101. }
  102. module.exports = billingAgreement;