MockRecurlyApi.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const AbstractMockApi = require('./AbstractMockApi')
  2. const SubscriptionController = require('../../../../app/src/Features/Subscription/SubscriptionController')
  3. const { xmlResponse } = require('../../../../app/src/infrastructure/Response')
  4. class MockRecurlyApi extends AbstractMockApi {
  5. reset() {
  6. this.mockSubscriptions = []
  7. this.redemptions = {}
  8. this.coupons = {}
  9. }
  10. addMockSubscription(recurlySubscription) {
  11. this.mockSubscriptions.push(recurlySubscription)
  12. }
  13. getMockSubscriptionByAccountId(accountId) {
  14. return this.mockSubscriptions.find(
  15. mockSubscription => mockSubscription.account.id === accountId
  16. )
  17. }
  18. getMockSubscriptionById(uuid) {
  19. return this.mockSubscriptions.find(
  20. mockSubscription => mockSubscription.uuid === uuid
  21. )
  22. }
  23. applyRoutes() {
  24. this.app.get('/subscriptions/:id', (req, res) => {
  25. const subscription = this.getMockSubscriptionById(req.params.id)
  26. if (!subscription) {
  27. res.sendStatus(404)
  28. } else {
  29. xmlResponse(
  30. res,
  31. `\
  32. <subscription>
  33. <plan><plan_code>${subscription.planCode}</plan_code></plan>
  34. <currency>${subscription.currency}</currency>
  35. <state>${subscription.state}</state>
  36. <tax_in_cents type="integer">${subscription.tax_in_cents}</tax_in_cents>
  37. <tax_rate type="float">${subscription.tax_rate}</tax_rate>
  38. <current_period_ends_at type="datetime">${subscription.current_period_ends_at}</current_period_ends_at>
  39. <unit_amount_in_cents type="integer">${subscription.unit_amount_in_cents}</unit_amount_in_cents>
  40. <account href="accounts/${subscription.account.id}" />
  41. <trial_ends_at type="datetime">${subscription.trial_ends_at}</trial_ends_at>
  42. </subscription>\
  43. `
  44. )
  45. }
  46. })
  47. this.app.get('/accounts/:id', (req, res) => {
  48. const subscription = this.getMockSubscriptionByAccountId(req.params.id)
  49. if (!subscription) {
  50. res.sendStatus(404)
  51. } else {
  52. xmlResponse(
  53. res,
  54. `\
  55. <account>
  56. <account_code>${req.params.id}</account_code>
  57. <hosted_login_token>${subscription.account.hosted_login_token}</hosted_login_token>
  58. <email>${subscription.account.email}</email>
  59. </account>\
  60. `
  61. )
  62. }
  63. })
  64. this.app.put(
  65. '/accounts/:id',
  66. SubscriptionController.recurlyNotificationParser, // required to parse XML requests
  67. (req, res) => {
  68. const subscription = this.getMockSubscriptionByAccountId(req.params.id)
  69. if (!subscription) {
  70. res.sendStatus(404)
  71. } else {
  72. Object.assign(subscription.account, req.body.account)
  73. xmlResponse(
  74. res,
  75. `\
  76. <account>
  77. <account_code>${req.params.id}</account_code>
  78. <email>${subscription.account.email}</email>
  79. </account>\
  80. `
  81. )
  82. }
  83. }
  84. )
  85. this.app.get('/coupons/:code', (req, res) => {
  86. const coupon = this.coupons[req.params.code]
  87. if (!coupon) {
  88. res.sendStatus(404)
  89. } else {
  90. xmlResponse(
  91. res,
  92. `\
  93. <coupon>
  94. <coupon_code>${req.params.code}</coupon_code>
  95. <name>${coupon.name || ''}</name>
  96. <description>${coupon.description || ''}</description>
  97. </coupon>\
  98. `
  99. )
  100. }
  101. })
  102. this.app.get('/accounts/:id/redemptions', (req, res) => {
  103. const redemptions = this.redemptions[req.params.id] || []
  104. let redemptionsListXml = ''
  105. for (const redemption of Array.from(redemptions)) {
  106. redemptionsListXml += `\
  107. <redemption>
  108. <state>${redemption.state}</state>
  109. <coupon_code>${redemption.coupon_code}</coupon_code>
  110. </redemption>\
  111. `
  112. }
  113. xmlResponse(
  114. res,
  115. `\
  116. <redemptions type="array">
  117. ${redemptionsListXml}
  118. </redemptions>\
  119. `
  120. )
  121. })
  122. }
  123. }
  124. module.exports = MockRecurlyApi
  125. // type hint for the inherited `instance` method
  126. /**
  127. * @function instance
  128. * @memberOf MockRecurlyApi
  129. * @static
  130. * @returns {MockRecurlyApi}
  131. */