starling.js

  1. import Customer from './entities/customer';
  2. import Account from './entities/account';
  3. import Address from './entities/address';
  4. import Transaction from './entities/transaction';
  5. import Card from './entities/card';
  6. import OAuth from './entities/oauth';
  7. import Contact from './entities/contact';
  8. import Payment from './entities/payment';
  9. import Mandate from './entities/mandate';
  10. /**
  11. * Facade to dispatch operations to services
  12. */
  13. class Starling {
  14. /**
  15. * Create an instance of the starling client
  16. * @param {Object=} options - configuration parameters
  17. */
  18. constructor (options) {
  19. const defaults = {
  20. apiUrl: 'https://api.starlingbank.com',
  21. oauthUrl: 'https://oauth.starlingbank.com',
  22. clientId: '',
  23. clientSecret: ''
  24. };
  25. this.config = Object.assign({}, defaults, options);
  26. this.customer = new Customer(this.config);
  27. this.account = new Account(this.config);
  28. this.address = new Address(this.config);
  29. this.transaction = new Transaction(this.config);
  30. this.payment = new Payment(this.config);
  31. this.mandate = new Mandate(this.config);
  32. this.contact = new Contact(this.config);
  33. this.card = new Card(this.config);
  34. this.oAuth = new OAuth(this.config);
  35. }
  36. /**
  37. * Gets the customer's details
  38. * @param {string=} accessToken - the oauth bearer token. If not
  39. * specified, the accessToken on the options object is used.
  40. * @return {Promise} - the http request promise
  41. */
  42. getCustomer (accessToken = this.config.accessToken) {
  43. return this.customer.getCustomer(accessToken);
  44. }
  45. /**
  46. * Gets the customer's account details
  47. * @param {string=} accessToken - the oauth bearer token. If not
  48. * specified, the accessToken on the options object is used.
  49. * @return {Promise} - the http request promise
  50. */
  51. getAccount (accessToken = this.config.accessToken) {
  52. return this.account.getAccount(accessToken);
  53. }
  54. /**
  55. * Gets the customer's balance
  56. * @param {string=} accessToken - the oauth bearer token. If not
  57. * specified, the accessToken on the options object is used.
  58. * @return {Promise} - the http request promise
  59. */
  60. getBalance (accessToken = this.config.accessToken) {
  61. return this.account.getBalance(accessToken);
  62. }
  63. /**
  64. * Gets the customer's addresses (current and previous)
  65. * @param {string=} accessToken - the oauth bearer token. If not
  66. * specified, the accessToken on the options object is used.
  67. * @return {Promise} - the http request promise
  68. */
  69. getAddresses (accessToken = this.config.accessToken) {
  70. return this.address.getAddresses(accessToken);
  71. }
  72. /**
  73. * Gets the customer's transaction history
  74. * @param {string=} accessToken - the oauth bearer token. If not
  75. * specified, the accessToken on the options object is used.
  76. * @param {string} fromDate - filter transactions after this date. Format: YYYY-MM-DD (optional,
  77. * defaults to most recent 100 transactions)
  78. * @param {string} toDate - filter transactions before this date. Format: YYYY-MM-DD (optional,
  79. * defaults to current date if not provided)
  80. * @param {string=} source - the transaction type (e.g. faster payments, mastercard).
  81. * If not specified, results are not filtered by source.
  82. * @return {Promise} - the http request promise
  83. */
  84. getTransactions (accessToken = this.config.accessToken, fromDate, toDate, source) {
  85. return this.transaction.getTransactions(accessToken, fromDate, toDate, source);
  86. }
  87. /**
  88. * Gets the full details of a single transaction
  89. * @param {string=} accessToken - the oauth bearer token. If not
  90. * specified, the accessToken on the options object is used.
  91. * @param {string} transactionId - the unique transaction ID
  92. * @param {string=} source - the transaction type (e.g. faster payments, mastercard).
  93. * If not specified, only generic transaction information will be returned.
  94. * @return {Promise} - the http request promise
  95. */
  96. getTransaction (accessToken = this.config.accessToken, transactionId, source) {
  97. return this.transaction.getTransaction(accessToken, transactionId, source);
  98. }
  99. /**
  100. * Gets the customer's current direct-debit mandates
  101. * @param {string=} accessToken - the oauth bearer token. If not
  102. * specified, the accessToken on the options object is used.
  103. * @return {Promise} - the http request promise
  104. */
  105. listMandates (accessToken = this.config.accessToken) {
  106. return this.mandate.listMandates(accessToken);
  107. }
  108. /**
  109. * Deletes specific direct debit mandate
  110. * @param {string} accessToken - the oauth bearer token.
  111. * @param {string} mandateId - the unique mandate ID
  112. * @return {Promise} - the http request promise
  113. */
  114. deleteMandate (accessToken = this.config.accessToken, mandateId) {
  115. return this.mandate.deleteMandate(accessToken, mandateId);
  116. }
  117. /**
  118. * Lists the customer's scheduled payments
  119. * @param {string} accessToken - the oauth bearer token.
  120. * @return {Promise} - the http request promise
  121. */
  122. listScheduledPayments (accessToken = this.config.accessToken) {
  123. return this.payment.listScheduledPayments(accessToken);
  124. }
  125. /**
  126. * Makes a payment on behalf of the customer to another UK bank account using the Faster Payments network
  127. * @param {string} accessToken - the oauth bearer token.
  128. * @param {string} destinationAccountId - the account identifier of the recipient
  129. * @param {string} reference - The payment reference, max. 18 characters.
  130. * @param {string} amount - the amount to be send.
  131. * @param {string=} currency - the currency, optional, defaults to "GBP".
  132. * @return {Promise} - the http request promise
  133. */
  134. makeLocalPayment (accessToken = this.config.accessToken, destinationAccountId, reference, amount, currency = 'GBP') {
  135. return this.payment.makeLocalPayment(accessToken, destinationAccountId, reference, amount, currency);
  136. }
  137. /**
  138. * Gets the customer's contacts (payees)
  139. * @param {string} accessToken - the oauth bearer token.
  140. * @return {Promise} - the http request promise
  141. */
  142. getContacts (accessToken = this.config.accessToken) {
  143. return this.contact.getContacts(accessToken);
  144. }
  145. /**
  146. * Gets a specific contact (payee)
  147. * @param {string} accessToken - the oauth bearer token.
  148. * @param {string} contactId - the contact's ID.
  149. * @return {Promise} - the http request promise
  150. */
  151. getContactAccount (accessToken = this.config.accessToken, contactId) {
  152. return this.contact.getContactAccount(accessToken, contactId);
  153. }
  154. /**
  155. * Creates a contact (payee) for the customer
  156. * @param {string} accessToken - the oauth bearer token.
  157. * @param {string} name - the name of the new contact.
  158. * @param {string=} accountType - the account type (domestic or international), optional and defaults to UK_ACCOUNT_AND_SORT_CODE.
  159. * @param {string} accountNumber - the contact's bank account number.
  160. * @param {string} sortCode - the contact's sort code.
  161. * @param {string} customerId - the customer's ID.
  162. * @return {Promise} - the http request promise
  163. */
  164. createContact (accessToken = this.config.accessToken, name, accountType = 'UK_ACCOUNT_AND_SORT_CODE', accountNumber, sortCode, customerId) {
  165. return this.contact.createContact(accessToken, name, accountType, accountNumber, sortCode, customerId);
  166. }
  167. deleteContact (accessToken, contactId) {
  168. return this.contact.deleteContact(accessToken, contactId);
  169. }
  170. /**
  171. * Gets the customer's card
  172. * @param {string=} accessToken - the oauth bearer token. If not
  173. * specified, the accessToken on the options object is used.
  174. * @return {Promise} - the http request promise
  175. */
  176. getCard (accessToken = this.config.accessToken) {
  177. return this.card.getCard(accessToken);
  178. }
  179. /**
  180. * Exchanges the authorization code for an access token
  181. * @param {string} authorizationCode - the authorization code, acquired from the user agent after the
  182. * user authenticates with starling
  183. * @return {Promise} - the http request promise
  184. */
  185. getAccessToken (authorizationCode) {
  186. return this.oAuth.getAccessToken(authorizationCode);
  187. }
  188. /**
  189. * Exchanges the authorization code for an access token
  190. * @param {string} refreshToken - the oauth refresh token, used to claim a new access token when the access token
  191. * expires. A new refresh token is also returned.
  192. * @return {Promise} - the http request promise
  193. */
  194. refreshAccessToken (refreshToken) {
  195. return this.oAuth.refreshAccessToken(refreshToken);
  196. }
  197. }
  198. module.exports = Starling;