RecurlySubscription.mjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import mongodb from 'mongodb-legacy'
  2. import Subscription from './Subscription.mjs'
  3. import MockRecurlyApiClass from '../mocks/MockRecurlyApi.mjs'
  4. import RecurlyWrapper from '../../../../app/src/Features/Subscription/RecurlyWrapper.js'
  5. import { promisifyClass } from '@overleaf/promise-utils'
  6. const { ObjectId } = mongodb
  7. let MockRecurlyApi
  8. before(function () {
  9. MockRecurlyApi = MockRecurlyApiClass.instance()
  10. })
  11. class RecurlySubscription {
  12. constructor(options = {}) {
  13. options.recurlySubscription_id = new ObjectId().toString()
  14. this.subscription = new Subscription(options)
  15. this.uuid = options.recurlySubscription_id
  16. this.state = options.state || 'active'
  17. this.tax_in_cents = 100
  18. this.tax_rate = 0.2
  19. this.unit_amount_in_cents = 500
  20. this.currency = 'GBP'
  21. this.current_period_ends_at = new Date(2018, 4, 5)
  22. this.trial_ends_at = new Date(2018, 6, 7)
  23. this.account = {
  24. id: this.subscription.admin_id.toString(),
  25. email: options.account && options.account.email,
  26. hosted_login_token: options.account && options.account.hosted_login_token,
  27. }
  28. this.planCode = options.planCode || 'personal'
  29. }
  30. ensureExists(callback) {
  31. this.subscription.ensureExists(error => {
  32. if (error) {
  33. return callback(error)
  34. }
  35. MockRecurlyApi.addMockSubscription(this)
  36. callback()
  37. })
  38. }
  39. buildCallbackXml(event) {
  40. return RecurlyWrapper._buildXml(event, {
  41. subscription: {
  42. uuid: this.uuid,
  43. state: this.state,
  44. plan: {
  45. plan_code: this.planCode,
  46. },
  47. },
  48. account: {
  49. account_code: this.account.id,
  50. },
  51. })
  52. }
  53. }
  54. export default RecurlySubscription
  55. export const promises = promisifyClass(RecurlySubscription, {
  56. without: ['buildCallbackXml'],
  57. })