Browse Source

Merge pull request #34484 from overleaf/rh-ol-stripe-id

Sync updated customer email to Stripe/Recurly

GitOrigin-RevId: b2becc50cc63739bdf56e45c37939f84c6fcd7a8
roo hutton 1 month ago
parent
commit
efd12c8feb

+ 6 - 1
services/web/app/src/Features/Subscription/RecurlyWrapper.mjs

@@ -31,11 +31,16 @@ async function updateAccountEmailAddress(userId, newAccountEmail) {
     })
   }
 
-  const { body } = await RecurlyWrapper.promises.apiRequest({
+  const { response, body } = await RecurlyWrapper.promises.apiRequest({
     url: `accounts/${userId}`,
     method: 'PUT',
     body: requestBody,
+    expect404: true,
   })
+  if (response.status === 404) {
+    // the user has no Recurly account
+    return null
+  }
   return await RecurlyWrapper.promises._parseAccountXml(body)
 }
 

+ 4 - 0
services/web/app/src/models/User.mjs

@@ -245,6 +245,10 @@ export const UserSchema = new Schema(
     dsMobileApp: {
       subscribed: { type: Boolean },
     },
+    stripeCustomerIds: {
+      us: { type: String },
+      uk: { type: String },
+    },
   },
   { minimize: false }
 )

+ 29 - 0
services/web/test/unit/src/Subscription/RecurlyWrapper.test.mjs

@@ -301,6 +301,35 @@ describe('RecurlyWrapper', function () {
       expect(ctx.recurlyAccount).to.exist
       ctx.recurlyAccount.account_code.should.equal('104')
     })
+
+    it('should tolerate a missing account', function (ctx) {
+      ctx.requestOptions.expect404.should.equal(true)
+    })
+  })
+
+  describe('updateAccountEmailAddress, when the account does not exist', function () {
+    beforeEach(function (ctx) {
+      ctx.apiRequest = sinon
+        .stub(ctx.RecurlyWrapper.promises, 'apiRequest')
+        .resolves({
+          err: null,
+          response: { status: 404 },
+          body: null,
+        })
+    })
+
+    afterEach(function (ctx) {
+      ctx.RecurlyWrapper.promises.apiRequest.restore()
+    })
+
+    it('should return null', async function (ctx) {
+      const recurlyAccount =
+        await ctx.RecurlyWrapper.promises.updateAccountEmailAddress(
+          'account-id-123',
+          'example@overleaf.com'
+        )
+      expect(recurlyAccount).to.be.null
+    })
   })
 
   describe('updateAccountEmailAddress, with invalid XML', function () {