From 5db41169aafbf958c179a43ebbf58f833b94d547 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 21 Oct 2021 13:27:23 +0200 Subject: [PATCH] Added @tryghost/members-payments module refs https://github.com/TryGhost/Team/issues/1166 This is a new module which will eventually handle all payment related things. This allows the Offers module to focus exclusively on the Ghost concepts, and the Payments module will handle the association between Offer & Stripe Coupon, Tier & Stripe Product, Cadence & Stripe Price. This decoupling allows us to not have to consider the lack of Stripe data for an Offer, which is the case after a Stripe Disconnect. Instead all of the population/repopulation/lazy-creating can be handled here. --- ghost/payments/.eslintrc.js | 6 +++ ghost/payments/LICENSE | 21 ++++++++ ghost/payments/README.md | 34 +++++++++++++ ghost/payments/index.js | 1 + ghost/payments/lib/payments.js | 68 +++++++++++++++++++++++++ ghost/payments/package.json | 28 ++++++++++ ghost/payments/test/.eslintrc.js | 6 +++ ghost/payments/test/hello.test.js | 10 ++++ ghost/payments/test/utils/assertions.js | 11 ++++ ghost/payments/test/utils/index.js | 11 ++++ ghost/payments/test/utils/overrides.js | 10 ++++ 11 files changed, 206 insertions(+) create mode 100644 ghost/payments/.eslintrc.js create mode 100644 ghost/payments/LICENSE create mode 100644 ghost/payments/README.md create mode 100644 ghost/payments/index.js create mode 100644 ghost/payments/lib/payments.js create mode 100644 ghost/payments/package.json create mode 100644 ghost/payments/test/.eslintrc.js create mode 100644 ghost/payments/test/hello.test.js create mode 100644 ghost/payments/test/utils/assertions.js create mode 100644 ghost/payments/test/utils/index.js create mode 100644 ghost/payments/test/utils/overrides.js diff --git a/ghost/payments/.eslintrc.js b/ghost/payments/.eslintrc.js new file mode 100644 index 0000000000..c9c1bcb522 --- /dev/null +++ b/ghost/payments/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/node' + ] +}; diff --git a/ghost/payments/LICENSE b/ghost/payments/LICENSE new file mode 100644 index 0000000000..366ae5f624 --- /dev/null +++ b/ghost/payments/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2013-2021 Ghost Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/ghost/payments/README.md b/ghost/payments/README.md new file mode 100644 index 0000000000..fbb05f713e --- /dev/null +++ b/ghost/payments/README.md @@ -0,0 +1,34 @@ +# Payments + +## Install + +`npm install @tryghost/payments --save` + +or + +`yarn add @tryghost/payments` + + +## Usage + + +## Develop + +This is a mono repository, managed with [lerna](https://lernajs.io/). + +Follow the instructions for the top-level repo. +1. `git clone` this repo & `cd` into it as usual +2. Run `yarn` to install top-level dependencies. + + +## Run + +- `yarn dev` + + +## Test + +- `yarn lint` run just eslint +- `yarn test` run lint and tests + + diff --git a/ghost/payments/index.js b/ghost/payments/index.js new file mode 100644 index 0000000000..f3c7d8bc8c --- /dev/null +++ b/ghost/payments/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/payments'); diff --git a/ghost/payments/lib/payments.js b/ghost/payments/lib/payments.js new file mode 100644 index 0000000000..b87fc719c0 --- /dev/null +++ b/ghost/payments/lib/payments.js @@ -0,0 +1,68 @@ +class PaymentsService { + /** + * @param {object} deps + * @param {any} deps.Offer + * @param {import('@tryghost/members-offers/lib/application/OffersAPI')} deps.offersAPI + * @param {any} deps.stripeAPIService + */ + constructor(deps) { + /** @private */ + this.OfferModel = deps.Offer; + /** @private */ + this.offersAPI = deps.offersAPI; + /** @private */ + this.stripeAPIService = deps.stripeAPIService; + } + + /** + * @param {string} offerId + * + * @returns {Promise<{id: string}>} + */ + async getCouponForOffer(offerId) { + const row = await this.OfferModel.where({id: offerId}).query().select('stripe_coupon_id').first(); + if (!row) { + return null; + } + if (!row.stripe_coupon_id) { + const offer = await this.offersAPI.getOffer({id: offerId}); + await this.createCouponForOffer(offer); + return this.getCouponForOffer(offerId); + } + return { + id: row.stripe_coupon_id + }; + } + + /** + * @param {import('@tryghost/members-offers/lib/application/OfferMapper').OfferDTO} offer + */ + async createCouponForOffer(offer) { + /** @type {import('stripe').Stripe.CouponCreateParams} */ + const couponData = { + name: offer.name, + duration: offer.duration + }; + + if (offer.duration === 'repeating') { + couponData.duration_in_months = offer.duration_in_months; + } + + if (offer.type === 'percent') { + couponData.percent_off = offer.amount; + } else { + couponData.amount_off = offer.amount; + couponData.currency = offer.currency; + } + + const coupon = await this.stripeAPIService.createCoupon(couponData); + + await this.OfferModel.edit({ + stripe_coupon_id: coupon.id + }, { + id: offer.id + }); + } +} + +module.exports = PaymentsService; diff --git a/ghost/payments/package.json b/ghost/payments/package.json new file mode 100644 index 0000000000..e4b51c0648 --- /dev/null +++ b/ghost/payments/package.json @@ -0,0 +1,28 @@ +{ + "name": "@tryghost/members-payments", + "version": "0.0.0", + "repository": "https://github.com/TryGhost/Members/tree/main/packages/payments", + "author": "Ghost Foundation", + "license": "MIT", + "main": "index.js", + "scripts": { + "dev": "echo \"Implement me!\"", + "test": "NODE_ENV=testing c8 --check-coverage mocha './test/**/*.test.js'", + "lint": "eslint . --ext .js --cache", + "posttest": "yarn lint" + }, + "files": [ + "index.js", + "lib" + ], + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "c8": "7.10.0", + "mocha": "9.1.3", + "should": "13.2.3", + "sinon": "11.1.2" + }, + "dependencies": {} +} diff --git a/ghost/payments/test/.eslintrc.js b/ghost/payments/test/.eslintrc.js new file mode 100644 index 0000000000..829b601eb0 --- /dev/null +++ b/ghost/payments/test/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/test' + ] +}; diff --git a/ghost/payments/test/hello.test.js b/ghost/payments/test/hello.test.js new file mode 100644 index 0000000000..85d69d1e08 --- /dev/null +++ b/ghost/payments/test/hello.test.js @@ -0,0 +1,10 @@ +// Switch these lines once there are useful utils +// const testUtils = require('./utils'); +require('./utils'); + +describe('Hello world', function () { + it('Runs a test', function () { + // TODO: Write me! + 'hello'.should.eql('hello'); + }); +}); diff --git a/ghost/payments/test/utils/assertions.js b/ghost/payments/test/utils/assertions.js new file mode 100644 index 0000000000..7364ee8aa1 --- /dev/null +++ b/ghost/payments/test/utils/assertions.js @@ -0,0 +1,11 @@ +/** + * Custom Should Assertions + * + * Add any custom assertions to this file. + */ + +// Example Assertion +// should.Assertion.add('ExampleAssertion', function () { +// this.params = {operator: 'to be a valid Example Assertion'}; +// this.obj.should.be.an.Object; +// }); diff --git a/ghost/payments/test/utils/index.js b/ghost/payments/test/utils/index.js new file mode 100644 index 0000000000..0d67d86ff8 --- /dev/null +++ b/ghost/payments/test/utils/index.js @@ -0,0 +1,11 @@ +/** + * Test Utilities + * + * Shared utils for writing tests + */ + +// Require overrides - these add globals for tests +require('./overrides'); + +// Require assertions - adds custom should assertions +require('./assertions'); diff --git a/ghost/payments/test/utils/overrides.js b/ghost/payments/test/utils/overrides.js new file mode 100644 index 0000000000..90203424ee --- /dev/null +++ b/ghost/payments/test/utils/overrides.js @@ -0,0 +1,10 @@ +// This file is required before any test is run + +// Taken from the should wiki, this is how to make should global +// Should is a global in our eslint test config +global.should = require('should').noConflict(); +should.extend(); + +// Sinon is a simple case +// Sinon is a global in our eslint test config +global.sinon = require('sinon');