PlansLocator.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // TODO: This file may be deleted when Stripe is fully implemented to all users, so, consider deleting it
  2. const Settings = require('@overleaf/settings')
  3. const logger = require('@overleaf/logger')
  4. /**
  5. * @typedef {import('../../../../types/subscription/plan').RecurlyPlanCode} RecurlyPlanCode
  6. * @typedef {import('../../../../types/subscription/plan').StripeLookupKey} StripeLookupKey
  7. */
  8. function ensurePlansAreSetupCorrectly() {
  9. Settings.plans.forEach(plan => {
  10. if (typeof plan.price_in_cents !== 'number') {
  11. logger.fatal({ plan }, 'missing price on plan')
  12. process.exit(1)
  13. }
  14. if (plan.price) {
  15. logger.fatal({ plan }, 'unclear price attribute on plan')
  16. process.exit(1)
  17. }
  18. if (plan.price_in_unit) {
  19. logger.fatal({ plan }, 'deprecated price_in_unit attribute on plan')
  20. process.exit(1)
  21. }
  22. })
  23. }
  24. const recurlyPlanCodeToStripeLookupKey = {
  25. 'professional-annual': 'professional_annual',
  26. professional: 'professional_monthly',
  27. professional_free_trial_7_days: 'professional_monthly',
  28. 'collaborator-annual': 'standard_annual',
  29. collaborator: 'standard_monthly',
  30. collaborator_free_trial_7_days: 'standard_monthly',
  31. 'student-annual': 'student_annual',
  32. student: 'student_monthly',
  33. student_free_trial_7_days: 'student_monthly',
  34. }
  35. /**
  36. *
  37. * @param {RecurlyPlanCode} recurlyPlanCode
  38. * @returns {StripeLookupKey}
  39. */
  40. function mapRecurlyPlanCodeToStripeLookupKey(recurlyPlanCode) {
  41. return recurlyPlanCodeToStripeLookupKey[recurlyPlanCode]
  42. }
  43. const recurlyPlanCodeToPlanTypeAndPeriod = {
  44. collaborator: { planType: 'standard', period: 'monthly' },
  45. collaborator_free_trial_7_days: { planType: 'standard', period: 'monthly' },
  46. 'collaborator-annual': { planType: 'standard', period: 'annual' },
  47. professional: { planType: 'professional', period: 'monthly' },
  48. professional_free_trial_7_days: {
  49. planType: 'professional',
  50. period: 'monthly',
  51. },
  52. 'professional-annual': { planType: 'professional', period: 'annual' },
  53. student: { planType: 'student', period: 'monthly' },
  54. student_free_trial_7_days: { planType: 'student', period: 'monthly' },
  55. 'student-annual': { planType: 'student', period: 'annual' },
  56. }
  57. /**
  58. *
  59. * @param {RecurlyPlanCode} recurlyPlanCode
  60. * @returns {{ planType: 'standard' | 'professional' | 'student', period: 'annual' | 'monthly'}}
  61. */
  62. function getPlanTypeAndPeriodFromRecurlyPlanCode(recurlyPlanCode) {
  63. return recurlyPlanCodeToPlanTypeAndPeriod[recurlyPlanCode]
  64. }
  65. function findLocalPlanInSettings(planCode) {
  66. for (const plan of Settings.plans) {
  67. if (plan.planCode === planCode) {
  68. return plan
  69. }
  70. }
  71. return null
  72. }
  73. module.exports = {
  74. ensurePlansAreSetupCorrectly,
  75. findLocalPlanInSettings,
  76. mapRecurlyPlanCodeToStripeLookupKey,
  77. getPlanTypeAndPeriodFromRecurlyPlanCode,
  78. }