entities/mandate.js

  1. import axios from 'axios';
  2. import debug from 'debug';
  3. import {defaultHeaders} from '../utils/http';
  4. import {typeValidation} from '../utils/validator';
  5. const log = debug('starling:mandate-service');
  6. /**
  7. * Service to interact with a customer's transactions
  8. */
  9. class Mandate {
  10. /**
  11. * Create a new transaction service
  12. * @param {Object} options - configuration parameters
  13. */
  14. constructor (options) {
  15. this.options = options;
  16. }
  17. /**
  18. * Gets a list of the customer's current direct debit mandates
  19. * @param {string} accessToken - the oauth bearer token.
  20. * @return {Promise} - the http request promise
  21. */
  22. listMandates (accessToken) {
  23. typeValidation(arguments, listMandatesParameterDefinition);
  24. const url = `${this.options.apiUrl}/api/v1/direct-debit/mandates`;
  25. log(`GET ${url}`);
  26. return axios({
  27. method: 'GET',
  28. url,
  29. headers: defaultHeaders(accessToken)
  30. });
  31. }
  32. /**
  33. * Deletes specific direct debit mandate
  34. * @param {string} accessToken - the oauth bearer token.
  35. * @param {string} mandateId - the unique mandate ID
  36. * @return {Promise} - the http request promise
  37. */
  38. deleteMandate (accessToken, mandateId) {
  39. typeValidation(arguments, deleteMandateParameterDefinition);
  40. const url = `${this.options.apiUrl}/api/v1/direct-debit/mandates/${mandateId}`;
  41. log(`DELETE ${url}`);
  42. return axios({
  43. method: 'DELETE',
  44. url,
  45. headers: defaultHeaders(accessToken)
  46. });
  47. }
  48. }
  49. const listMandatesParameterDefinition = [
  50. {name: 'accessToken', validations: ['required', 'string']}
  51. ];
  52. const deleteMandateParameterDefinition = [
  53. {name: 'accessToken', validations: ['required', 'string']},
  54. {name: 'mandateId', validations: ['required', 'string']}
  55. ];
  56. module.exports = Mandate;