RecurlyEntities.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // @ts-check
  2. const OError = require('@overleaf/o-error')
  3. const { DuplicateAddOnError, AddOnNotPresentError } = require('./Errors')
  4. const PlansLocator = require('./PlansLocator')
  5. const SubscriptionHelper = require('./SubscriptionHelper')
  6. const AI_ADD_ON_CODE = 'assistant'
  7. const STANDALONE_AI_ADD_ON_CODES = ['assistant', 'assistant-annual']
  8. class RecurlySubscription {
  9. /**
  10. * @param {object} props
  11. * @param {string} props.id
  12. * @param {string} props.userId
  13. * @param {string} props.planCode
  14. * @param {string} props.planName
  15. * @param {number} props.planPrice
  16. * @param {RecurlySubscriptionAddOn[]} [props.addOns]
  17. * @param {number} props.subtotal
  18. * @param {number} [props.taxRate]
  19. * @param {number} [props.taxAmount]
  20. * @param {string} props.currency
  21. * @param {number} props.total
  22. * @param {Date} props.periodStart
  23. * @param {Date} props.periodEnd
  24. * @param {RecurlySubscriptionChange} [props.pendingChange]
  25. */
  26. constructor(props) {
  27. this.id = props.id
  28. this.userId = props.userId
  29. this.planCode = props.planCode
  30. this.planName = props.planName
  31. this.planPrice = props.planPrice
  32. this.addOns = props.addOns ?? []
  33. this.subtotal = props.subtotal
  34. this.taxRate = props.taxRate ?? 0
  35. this.taxAmount = props.taxAmount ?? 0
  36. this.currency = props.currency
  37. this.total = props.total
  38. this.periodStart = props.periodStart
  39. this.periodEnd = props.periodEnd
  40. this.pendingChange = props.pendingChange ?? null
  41. }
  42. /**
  43. * Returns whether this subscription currently has the given add-on
  44. *
  45. * @param {string} code
  46. * @return {boolean}
  47. */
  48. hasAddOn(code) {
  49. return this.addOns.some(addOn => addOn.code === code)
  50. }
  51. /**
  52. * Returns whether this subscription is a standalone AI add-on subscription
  53. *
  54. * @return {boolean}
  55. */
  56. isStandaloneAiAddOn() {
  57. return isStandaloneAiAddOnPlanCode(this.planCode)
  58. }
  59. /**
  60. * Returns whether this subcription will have the given add-on next billing
  61. * period.
  62. *
  63. * There are two cases: either the subscription already has the add-on and
  64. * won't change next period, or the subscription will change next period and
  65. * the change includes the add-on.
  66. *
  67. * @param {string} code
  68. * @return {boolean}
  69. */
  70. hasAddOnNextPeriod(code) {
  71. if (this.pendingChange != null) {
  72. return this.pendingChange.nextAddOns.some(addOn => addOn.code === code)
  73. } else {
  74. return this.hasAddOn(code)
  75. }
  76. }
  77. /**
  78. * Change this subscription's plan
  79. *
  80. * @return {RecurlySubscriptionChangeRequest}
  81. */
  82. getRequestForPlanChange(planCode) {
  83. const currentPlan = PlansLocator.findLocalPlanInSettings(this.planCode)
  84. if (currentPlan == null) {
  85. throw new OError('Unable to find plan in settings', {
  86. planCode: this.planCode,
  87. })
  88. }
  89. const newPlan = PlansLocator.findLocalPlanInSettings(planCode)
  90. if (newPlan == null) {
  91. throw new OError('Unable to find plan in settings', { planCode })
  92. }
  93. const shouldChangeAtTermEnd = SubscriptionHelper.shouldPlanChangeAtTermEnd(
  94. currentPlan,
  95. newPlan
  96. )
  97. const changeRequest = new RecurlySubscriptionChangeRequest({
  98. subscription: this,
  99. timeframe: shouldChangeAtTermEnd ? 'term_end' : 'now',
  100. planCode,
  101. })
  102. // Carry the AI add-on to the new plan if applicable
  103. if (
  104. this.isStandaloneAiAddOn() ||
  105. (!shouldChangeAtTermEnd && this.hasAddOn(AI_ADD_ON_CODE)) ||
  106. (shouldChangeAtTermEnd && this.hasAddOnNextPeriod(AI_ADD_ON_CODE))
  107. ) {
  108. const addOnUpdate = new RecurlySubscriptionAddOnUpdate({
  109. code: AI_ADD_ON_CODE,
  110. quantity: 1,
  111. })
  112. changeRequest.addOnUpdates = [addOnUpdate]
  113. }
  114. return changeRequest
  115. }
  116. /**
  117. * Purchase an add-on on this subscription
  118. *
  119. * @param {string} code
  120. * @param {number} [quantity]
  121. * @return {RecurlySubscriptionChangeRequest} - the change request to send to
  122. * Recurly
  123. *
  124. * @throws {DuplicateAddOnError} if the add-on is already present on the subscription
  125. */
  126. getRequestForAddOnPurchase(code, quantity = 1) {
  127. if (this.hasAddOn(code)) {
  128. throw new DuplicateAddOnError('Subscription already has add-on', {
  129. subscriptionId: this.id,
  130. addOnCode: code,
  131. })
  132. }
  133. const addOnUpdates = this.addOns.map(addOn => addOn.toAddOnUpdate())
  134. addOnUpdates.push(new RecurlySubscriptionAddOnUpdate({ code, quantity }))
  135. return new RecurlySubscriptionChangeRequest({
  136. subscription: this,
  137. timeframe: 'now',
  138. addOnUpdates,
  139. })
  140. }
  141. /**
  142. * Update an add-on on this subscription
  143. *
  144. * @param {string} code
  145. * @param {number} quantity
  146. * @return {RecurlySubscriptionChangeRequest} - the change request to send to
  147. * Recurly
  148. *
  149. * @throws {AddOnNotPresentError} if the subscription doesn't have the add-on
  150. */
  151. getRequestForAddOnUpdate(code, quantity) {
  152. if (!this.hasAddOn(code)) {
  153. throw new AddOnNotPresentError(
  154. 'Subscription does not have add-on to update',
  155. {
  156. subscriptionId: this.id,
  157. addOnCode: code,
  158. }
  159. )
  160. }
  161. const addOnUpdates = this.addOns.map(addOn => {
  162. const update = addOn.toAddOnUpdate()
  163. if (update.code === code) {
  164. update.quantity = quantity
  165. }
  166. return update
  167. })
  168. return new RecurlySubscriptionChangeRequest({
  169. subscription: this,
  170. timeframe: 'now',
  171. addOnUpdates,
  172. })
  173. }
  174. /**
  175. * Remove an add-on from this subscription
  176. *
  177. * @param {string} code
  178. * @return {RecurlySubscriptionChangeRequest}
  179. *
  180. * @throws {AddOnNotPresentError} if the subscription doesn't have the add-on
  181. */
  182. getRequestForAddOnRemoval(code) {
  183. if (!this.hasAddOn(code)) {
  184. throw new AddOnNotPresentError(
  185. 'Subscription does not have add-on to remove',
  186. {
  187. subscriptionId: this.id,
  188. addOnCode: code,
  189. }
  190. )
  191. }
  192. const addOnUpdates = this.addOns
  193. .filter(addOn => addOn.code !== code)
  194. .map(addOn => addOn.toAddOnUpdate())
  195. return new RecurlySubscriptionChangeRequest({
  196. subscription: this,
  197. timeframe: 'term_end',
  198. addOnUpdates,
  199. })
  200. }
  201. /**
  202. * Upgrade group plan with the plan code provided
  203. *
  204. * @param {string} newPlanCode
  205. * @return {RecurlySubscriptionChangeRequest}
  206. */
  207. getRequestForFlexibleLicensingGroupPlanUpgrade(newPlanCode) {
  208. // Ensure all the existing add-ons are added to the new plan
  209. const existingAddOns = this.addOns.map(
  210. addOn =>
  211. new RecurlySubscriptionAddOnUpdate({
  212. code: addOn.code,
  213. quantity: addOn.quantity,
  214. })
  215. )
  216. return new RecurlySubscriptionChangeRequest({
  217. subscription: this,
  218. timeframe: 'now',
  219. addOnUpdates: existingAddOns,
  220. planCode: newPlanCode,
  221. })
  222. }
  223. }
  224. /**
  225. * An add-on attached to a subscription
  226. */
  227. class RecurlySubscriptionAddOn {
  228. /**
  229. * @param {object} props
  230. * @param {string} props.code
  231. * @param {string} props.name
  232. * @param {number} props.quantity
  233. * @param {number} props.unitPrice
  234. */
  235. constructor(props) {
  236. this.code = props.code
  237. this.name = props.name
  238. this.quantity = props.quantity
  239. this.unitPrice = props.unitPrice
  240. this.preTaxTotal = this.quantity * this.unitPrice
  241. }
  242. /**
  243. * Return an add-on update that doesn't modify the add-on
  244. */
  245. toAddOnUpdate() {
  246. return new RecurlySubscriptionAddOnUpdate({
  247. code: this.code,
  248. quantity: this.quantity,
  249. unitPrice: this.unitPrice,
  250. })
  251. }
  252. }
  253. class RecurlySubscriptionChangeRequest {
  254. /**
  255. * @param {object} props
  256. * @param {RecurlySubscription} props.subscription
  257. * @param {"now" | "term_end"} props.timeframe
  258. * @param {string} [props.planCode]
  259. * @param {RecurlySubscriptionAddOnUpdate[]} [props.addOnUpdates]
  260. */
  261. constructor(props) {
  262. if (props.planCode == null && props.addOnUpdates == null) {
  263. throw new OError('Invalid RecurlySubscriptionChangeRequest', { props })
  264. }
  265. this.subscription = props.subscription
  266. this.timeframe = props.timeframe
  267. this.planCode = props.planCode ?? null
  268. this.addOnUpdates = props.addOnUpdates ?? null
  269. }
  270. }
  271. class RecurlySubscriptionAddOnUpdate {
  272. /**
  273. * @param {object} props
  274. * @param {string} props.code
  275. * @param {number} [props.quantity]
  276. * @param {number} [props.unitPrice]
  277. */
  278. constructor(props) {
  279. this.code = props.code
  280. this.quantity = props.quantity ?? null
  281. this.unitPrice = props.unitPrice ?? null
  282. }
  283. }
  284. class RecurlySubscriptionChange {
  285. /**
  286. * @param {object} props
  287. * @param {RecurlySubscription} props.subscription
  288. * @param {string} props.nextPlanCode
  289. * @param {string} props.nextPlanName
  290. * @param {number} props.nextPlanPrice
  291. * @param {RecurlySubscriptionAddOn[]} props.nextAddOns
  292. * @param {RecurlyImmediateCharge} [props.immediateCharge]
  293. */
  294. constructor(props) {
  295. this.subscription = props.subscription
  296. this.nextPlanCode = props.nextPlanCode
  297. this.nextPlanName = props.nextPlanName
  298. this.nextPlanPrice = props.nextPlanPrice
  299. this.nextAddOns = props.nextAddOns
  300. this.immediateCharge =
  301. props.immediateCharge ??
  302. new RecurlyImmediateCharge({ subtotal: 0, tax: 0, total: 0 })
  303. this.subtotal = this.nextPlanPrice
  304. for (const addOn of this.nextAddOns) {
  305. this.subtotal += addOn.preTaxTotal
  306. }
  307. this.tax = Math.round(this.subtotal * 100 * this.subscription.taxRate) / 100
  308. this.total = this.subtotal + this.tax
  309. }
  310. getAddOn(addOnCode) {
  311. return this.nextAddOns.find(addOn => addOn.code === addOnCode)
  312. }
  313. }
  314. class PaypalPaymentMethod {
  315. toString() {
  316. return 'Paypal'
  317. }
  318. }
  319. class CreditCardPaymentMethod {
  320. /**
  321. * @param {object} props
  322. * @param {string} props.cardType
  323. * @param {string} props.lastFour
  324. */
  325. constructor(props) {
  326. this.cardType = props.cardType
  327. this.lastFour = props.lastFour
  328. }
  329. toString() {
  330. return `${this.cardType} **** ${this.lastFour}`
  331. }
  332. }
  333. class RecurlyImmediateCharge {
  334. /**
  335. * @param {object} props
  336. * @param {number} props.subtotal
  337. * @param {number} props.tax
  338. * @param {number} props.total
  339. */
  340. constructor(props) {
  341. this.subtotal = props.subtotal
  342. this.tax = props.tax
  343. this.total = props.total
  344. }
  345. }
  346. /**
  347. * An add-on configuration, independent of any subscription
  348. */
  349. class RecurlyAddOn {
  350. /**
  351. * @param {object} props
  352. * @param {string} props.code
  353. * @param {string} props.name
  354. */
  355. constructor(props) {
  356. this.code = props.code
  357. this.name = props.name
  358. }
  359. }
  360. /**
  361. * A plan configuration
  362. */
  363. class RecurlyPlan {
  364. /**
  365. * @param {object} props
  366. * @param {string} props.code
  367. * @param {string} props.name
  368. */
  369. constructor(props) {
  370. this.code = props.code
  371. this.name = props.name
  372. }
  373. }
  374. /**
  375. * Returns whether the given plan code is a standalone AI plan
  376. *
  377. * @param {string} planCode
  378. */
  379. function isStandaloneAiAddOnPlanCode(planCode) {
  380. return STANDALONE_AI_ADD_ON_CODES.includes(planCode)
  381. }
  382. module.exports = {
  383. AI_ADD_ON_CODE,
  384. RecurlySubscription,
  385. RecurlySubscriptionAddOn,
  386. RecurlySubscriptionChange,
  387. RecurlySubscriptionChangeRequest,
  388. RecurlySubscriptionAddOnUpdate,
  389. PaypalPaymentMethod,
  390. CreditCardPaymentMethod,
  391. RecurlyAddOn,
  392. RecurlyPlan,
  393. isStandaloneAiAddOnPlanCode,
  394. RecurlyImmediateCharge,
  395. }