diff --git a/server/lib/stripe_utils.coffee b/server/lib/stripe_utils.coffee index 3f789c778..90fcede1a 100644 --- a/server/lib/stripe_utils.coffee +++ b/server/lib/stripe_utils.coffee @@ -28,6 +28,7 @@ module.exports = service: 'stripe' amount: parseInt(stripeCharge.amount) payment.set 'description', stripeCharge.metadata.description if stripeCharge.metadata.description + payment.set 'gems', parseInt(stripeCharge.metadata.gems) if stripeCharge.metadata.gems payment.set 'stripe', customerID: stripeCharge.customer timestamp: parseInt(stripeCharge.metadata.timestamp) diff --git a/server/payments/subscription_handler.coffee b/server/payments/subscription_handler.coffee index 7f96821bd..11c6bc70f 100644 --- a/server/payments/subscription_handler.coffee +++ b/server/payments/subscription_handler.coffee @@ -127,6 +127,7 @@ class SubscriptionHandler extends Handler metadata = type: req.body.type userID: req.user._id + '' + gems: subscriptions.basic.gems * 12 timestamp: parseInt(req.body.stripe?.timestamp) description: req.body.description @@ -146,6 +147,14 @@ class SubscriptionHandler extends Handler stripeInfo = _.cloneDeep(req.user.get('stripe') ? {}) stripeInfo.free = endDate.toISOString().substring(0, 10) req.user.set('stripe', stripeInfo) + + # Add year's worth of gems to User + purchased = _.clone(req.user.get('purchased')) + purchased ?= {} + purchased.gems ?= 0 + purchased.gems += parseInt(charge.metadata.gems) + req.user.set('purchased', purchased) + req.user.save (err, user) => if err @logSubscriptionError(req.user, "User save error: #{JSON.stringify(err)}") diff --git a/test/server/functional/subscription.spec.coffee b/test/server/functional/subscription.spec.coffee index 037be1da9..a51200e2e 100644 --- a/test/server/functional/subscription.spec.coffee +++ b/test/server/functional/subscription.spec.coffee @@ -1394,7 +1394,9 @@ describe 'Subscriptions', -> endDate.setUTCFullYear(endDate.getUTCFullYear() + 1) expect(stripeInfo.free).toEqual(endDate.toISOString().substring(0, 10)) expect(stripeInfo.customerID).toBeDefined() + expect(user1.get('purchased')?.gems).toEqual(3500*12) Payment.findOne 'stripe.customerID': stripeInfo.customerID, (err, payment) -> expect(err).toBeNull() expect(payment).toBeDefined() + expect(payment.get('gems')).toEqual(3500*12) done()