diff --git a/README.md b/README.md index eced623..611efaf 100644 --- a/README.md +++ b/README.md @@ -1,139 +1,3 @@ # Discourse Subscriptions -The Discourse Subscriptions plugin allows you to set up a subscription based Discourse application. By integrating with the [Stripe](https://stripe.com) payment gateway and setting up this plugin to manage Subscriptions, you can start allowing users access to content on your website on a user pays basis. - -You can test this plugin here: https://discourse.rimian.com.au/s. See testing section below for test credit card numbers. - -### Features - -Discourse Subscriptions supports the following features: - -* A credit card payment page. -* A settings page to manage Stripe configuration. -* Administration to create, update and view *products*, *plans* and *subscriptions*. -* Setting the user group associated with the subscription. -* Cancelling a subscription from admin. -* Adds and removes users from user groups when subscriptions are created or deleted. -* Allows the user to cancel their subscription in their user profile. -* Webhooks that update your website when an event occurs on Stripe. - -See screenshots below. - -## Plugin Installation - -Follow the [plugin](https://github.com/discourse/discourse-subscriptions/) install instructions here: https://meta.discourse.org/t/install-a-plugin/19157 - -## Core concepts - -### Managing your Stripe Account - -Ultimately, your Subscriptions are managed by the Stripe subscription portal. Stripe will handle the recurring billing, invoices, etc at the required intervals and notify your [Discourse Subscriptions](https://github.com/discourse/discourse-subscriptions/) plugin when specific transactions happen. - -This plugin does not store Stripe transaction or subscription details in your database other than the customer and product identifiers associated with those transactions. User group management is not stored in the stripe Portal. - -Be very careful to keep your Stripe private keys safe and secure at all times. - -**It is important to note** that if you were to shut down your instance of Discourse, uninstall this plugin or your site were to go offline, Stripe will continue to bill your customers for your service. It is your responsibility to manage your customers and provide the service they are paying for. - -Stripe has a [portal](https://dashboard.stripe.com) where you can manage all your customer's, payments and subscriptions. - -### Subscriptions - -Suscriptions are major feature of Stripe and this plugin's primary function is to leverage this feature by assigning Subscriptions to Discourse *user groups*. Subscriptions allow you to take payments and controll access to content on your website. - -When a subscription is created or deleted, a user is added or removed from the user group you associate with your subscription. Please note: If you manually remove or add users to a user group via Discourse admin, you'll need to manage subscriptions for those users manually. - -### Products - -A Product describes what the user gets when they subscribe. It is basically a user group on your website. A product has a *name* and *description* and most importantly, it is associated with a Discourse User Group. - -A product can have one or more *plans*. - -### Plans - -A Plan determines how and when you charge your users for the Product. Plans have *rates*, *billing intervals* and *trial periods*. A Product may have multiple Plans. For example: a yearly and a monthly Plan with different pricing. You can't change plans much once they are created but you can archive them and create new ones. - -Together, Products and Plans make up Subscriptions. - -## Getting started with Discourse Subscriptions - -To begin, you can install this plugin and try it out in test mode. You can disable the navigation link in settings while you're testing. - -### Set up your Payment Gateway. - -Firstly, you'll need an account with the [Stripe](https://stripe.com) payment gateway. To get started, you can set up an account in test mode and see how it all works without making any real transactions or having to set up a bank account. - -### Set up Webhooks and Events in your Stripe account - -Once you have an account on Stripe, you'll need to [tell Stripe your website's address](https://dashboard.stripe.com/test/webhooks) so it can notify you about certain transactions. You can enter this in your Stripe dashboard under **Endpoints > URL**. - -The address for webhooks is: `[your server address]/s/hooks` where [your server address] is the URL of your discourse install. - -You'll also need to tell Stripe which events it should notify you about via the webhook URL. You can select specific events or all of them. By allowing all events to be sent to your server, you don't have to worry about which events are important to you, but it will significantly load up your server and could cause problems with your site's availability. If you're concerned about this, only add the events below under **Webhook details**. - -Currently, Discourse Subscriptions responds to the following events: - -* `customer.subscription.deleted` -* `customer.subscription.updated` - -**Warning:** Events supported by this plugin may change, in the future as new features are added to this plugin. - -### Add the Stripe API and Webhook keys to your plugin settings - -Stripe needs to be authorised to communicate with your website. To do this, it publishes a pair of private and public *API keys* and a *signing secret* for your web hooks. - -To authorise webhooks, add the API keys and webhook secret from Stripe to your settings page (under Developers). - -In your Stripe account settings, see: -* https://dashboard.stripe.com/test/apikeys -* https://dashboard.stripe.com/test/webhooks - -### Set up your User Groups in Discourse - -When a user successfully subscribes to your Discourse application, after their credit card transaction has been processed, they are added to a User Group. By assigning users to a User Group, you can manage what your users have access to on your website. User groups are a core functionality of Discourse and this plugin does nothing with them except add and remove users from the group you associated with your Plan. - -## Enter your configuration details - -When you create an account with Stripe, you'll get a public and private key. These are entered in the Discourse Subscriptions admin so your subscriptions can integrate with Stripe. There are different keys for testing and production environments. - -You can also toggle the Subscribe button on and off in case you want to hide the link while you're setting up. - -## Create one or more products with plans. - -In the admin, add a new Product. Once you have a product saved, you can add plans to it. Keep in mind that the pricing and billing intervals of plans cannot be changed once you create them. This is to avoid confusion around subscription management. - -If you take a look at your [Stripe Dashboard](https://dashboard.stripe.com), you'll see all those products and plans are listed. Discourse Subscriptions does not create them locally. They are created in Stripe. - -## Testing - -Test with these credit card numbers: - -* 4111 1111 1111 1111 (no authentication required) -* 4000 0027 6000 3184 (authentication required) - -For more test card numbers: https://stripe.com/docs/testing - -Visit `/s` and enter a few test transactions. - -## Credits - -Many thanks to [Rimian Perkins](https://github.com/rimian/) for his work on this plugin! Also thanks to Chris Beach and Angus McLeod who helped with the previous version of this plugin. - -## Screenshots - -### Products Admin -![Admin Products](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/admin-products.png) -### Product Admin -![Admin Product](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/admin-product.png) -### Plan Admin -![Admin Plan](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/admin-plan.png) -### Subscription Admin -![Admin Subscriptions](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/admin-subscriptions.png) -### Subscription User -![Admin Subscriptions](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/user-subscriptions.png) -### Payments User -![Admin Subscriptions](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/user-payments.png) -### Subscribe -![Admin Subscriptions](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/subscribe.png) -### Settings -![Admin Settings](https://raw.githubusercontent.com/discourse/discourse-subscriptions/master/doc/settings.png) +Please see the topic on Discourse Meta for more information about this plugin: https://meta.discourse.org/t/discourse-subscriptions-plugin/140818 diff --git a/WARRANTY.md b/WARRANTY.md deleted file mode 100644 index de1dd24..0000000 --- a/WARRANTY.md +++ /dev/null @@ -1,3 +0,0 @@ -## No warranty of any kind - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/_config.yml b/_config.yml deleted file mode 100644 index c419263..0000000 --- a/_config.yml +++ /dev/null @@ -1 +0,0 @@ -theme: jekyll-theme-cayman \ No newline at end of file diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 93fc9bf..2e5f6aa 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -9,9 +9,15 @@ module DiscourseSubscriptions def index begin - products = ::Stripe::Product.list + product_ids = Product.all.pluck(:external_id) + products = [] - render_json_dump products.data + if product_ids.present? + products = ::Stripe::Product.list({ ids: product_ids }) + products = products[:data] + end + + render_json_dump products rescue ::Stripe::InvalidRequestError => e render_json_error e.message end @@ -27,6 +33,10 @@ module DiscourseSubscriptions product = ::Stripe::Product.create(create_params) + Product.create( + external_id: product[:id] + ) + render_json_dump product rescue ::Stripe::InvalidRequestError => e @@ -63,6 +73,8 @@ module DiscourseSubscriptions begin product = ::Stripe::Product.delete(params[:id]) + Product.delete_by(external_id: params[:id]) + render_json_dump product rescue ::Stripe::InvalidRequestError => e diff --git a/app/controllers/admin/subscriptions_controller.rb b/app/controllers/admin/subscriptions_controller.rb index b9bb483..78b4153 100644 --- a/app/controllers/admin/subscriptions_controller.rb +++ b/app/controllers/admin/subscriptions_controller.rb @@ -9,7 +9,13 @@ module DiscourseSubscriptions def index begin - subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product']) + subscription_ids = Subscription.all.pluck(:external_id) + subscriptions = [] + + if subscription_ids.present? + subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product']) + subscriptions = subscriptions.select { |sub| subscription_ids.include?(sub[:id]) } + end render_json_dump subscriptions rescue ::Stripe::InvalidRequestError => e @@ -21,15 +27,16 @@ module DiscourseSubscriptions begin subscription = ::Stripe::Subscription.delete(params[:id]) - customer = DiscourseSubscriptions::Customer.find_by( + customer = Customer.find_by( product_id: subscription[:plan][:product], customer_id: subscription[:customer] ) - if customer - customer.delete + Subscription.delete_by(external_id: params[:id]) + if customer user = ::User.find(customer.user_id) + customer.delete group = plan_group(subscription[:plan]) group.remove(user) if group end diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 5ebba81..fe35d7d 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -29,7 +29,7 @@ module DiscourseSubscriptions end def find_customer - DiscourseSubscriptions::Customer.find_user(current_user) + Customer.find_user(current_user) end end end diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index e6479f0..72f6bc0 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -11,7 +11,7 @@ module DiscourseSubscriptions def create begin - customer = DiscourseSubscriptions::Customer.where(user_id: current_user.id, product_id: nil).first_or_create do |c| + customer = Customer.where(user_id: current_user.id, product_id: nil).first_or_create do |c| new_customer = ::Stripe::Customer.create( email: current_user.email ) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index f44b7df..2dab40d 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -8,10 +8,18 @@ module DiscourseSubscriptions def index begin - response = ::Stripe::Product.list(active: true) + product_ids = Product.all.pluck(:external_id) + products = [] - products = response[:data].map do |p| - serialize(p) + if product_ids.present? + response = ::Stripe::Product.list({ + ids: product_ids, + active: true + }) + + products = response[:data].map do |p| + serialize(p) + end end render_json_dump products @@ -46,7 +54,7 @@ module DiscourseSubscriptions def current_user_products return [] if current_user.nil? - ::DiscourseSubscriptions::Customer + Customer .select(:product_id) .where(user_id: current_user.id) .map { |c| c.product_id }.compact diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 0e5101a..4bd6727 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -41,12 +41,17 @@ module DiscourseSubscriptions group.add(current_user) end - DiscourseSubscriptions::Customer.create( + customer = Customer.create( user_id: current_user.id, customer_id: params[:customer], product_id: plan[:product] ) + Subscription.create( + customer_id: customer.id, + external_id: @subscription[:id] + ) + render_json_dump @subscription rescue ::Stripe::InvalidRequestError => e diff --git a/app/controllers/user/payments_controller.rb b/app/controllers/user/payments_controller.rb index 650aaa8..8cf8e08 100644 --- a/app/controllers/user/payments_controller.rb +++ b/app/controllers/user/payments_controller.rb @@ -9,13 +9,19 @@ module DiscourseSubscriptions def index begin - customer = DiscourseSubscriptions::Customer.find_by(user_id: current_user.id, product_id: nil) + customer = Customer.find_by(user_id: current_user.id) + product_ids = Product.all.pluck(:external_id) data = [] - if customer.present? + if customer.present? && product_ids.present? + # lots of matching because the Stripe API doesn't make it easy to match products => payments except from invoices + all_invoices = ::Stripe::Invoice.list(customer: customer[:customer_id]) + invoices_with_products = all_invoices[:data].select { |invoice| product_ids.include?(invoice.dig(:lines, :data, 0, :plan, :product)) } + invoice_ids = invoices_with_products.map { |invoice| invoice[:id] } payments = ::Stripe::PaymentIntent.list(customer: customer[:customer_id]) - data = payments[:data] + payments_from_invoices = payments[:data].select { |payment| invoice_ids.include?(payment[:invoice]) } + data = payments_from_invoices end render_json_dump data diff --git a/app/controllers/user/subscriptions_controller.rb b/app/controllers/user/subscriptions_controller.rb index da79393..6fe8fbf 100644 --- a/app/controllers/user/subscriptions_controller.rb +++ b/app/controllers/user/subscriptions_controller.rb @@ -10,22 +10,31 @@ module DiscourseSubscriptions def index begin - plans = ::Stripe::Plan.list( - expand: ['data.product'] - ) + customer = Customer.find_by(user_id: current_user.id) + subscription_ids = Subscription.where(customer_id: customer.id).pluck(:external_id) if customer - customers = ::Stripe::Customer.list( - email: current_user.email, - expand: ['data.subscriptions'] - ) + subscriptions = [] - subscriptions = customers[:data].map do |customer| - customer[:subscriptions][:data] - end.flatten(1) + if subscription_ids + plans = ::Stripe::Plan.list( + expand: ['data.product'] + ) - subscriptions.map! do |subscription| - plan = plans[:data].find { |p| p[:id] == subscription[:plan][:id] } - subscription.to_h.merge(product: plan[:product].to_h.slice(:id, :name)) + customers = ::Stripe::Customer.list( + email: current_user.email, + expand: ['data.subscriptions'] + ) + + subscriptions = customers[:data].map do |sub_customer| + sub_customer[:subscriptions][:data] + end.flatten(1) + + subscriptions = subscriptions.select { |sub| subscription_ids.include?(sub[:id]) } + + subscriptions.map! do |subscription| + plan = plans[:data].find { |p| p[:id] == subscription[:plan][:id] } + subscription.to_h.merge(product: plan[:product].to_h.slice(:id, :name)) + end end render_json_dump subscriptions @@ -46,9 +55,16 @@ module DiscourseSubscriptions ) if customer.present? + sub_model = Subscription.find_by( + customer_id: customer.id, + external_id: params[:id] + ) + deleted = ::Stripe::Subscription.delete(params[:id]) customer.delete + sub_model.delete if sub_model + group = plan_group(subscription[:plan]) group.remove(current_user) if group diff --git a/app/models/customer.rb b/app/models/customer.rb index 2d4b7df..395f2ff 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -6,6 +6,8 @@ module DiscourseSubscriptions scope :find_user, ->(user) { find_by_user_id(user.id) } + has_many :subscriptions + def self.create_customer(user, customer) create(customer_id: customer[:id], user_id: user.id) end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 0000000..288bc35 --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +module DiscourseSubscriptions + class Product < ActiveRecord::Base + end +end diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 0000000..de0e885 --- /dev/null +++ b/app/models/subscription.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module DiscourseSubscriptions + class Subscription < ActiveRecord::Base + belongs_to :customer + end +end diff --git a/assets/javascripts/discourse/controllers/s-show.js.es6 b/assets/javascripts/discourse/controllers/s-show.js.es6 index d481024..6328ef3 100644 --- a/assets/javascripts/discourse/controllers/s-show.js.es6 +++ b/assets/javascripts/discourse/controllers/s-show.js.es6 @@ -2,6 +2,7 @@ import Customer from "discourse/plugins/discourse-subscriptions/discourse/models import Payment from "discourse/plugins/discourse-subscriptions/discourse/models/payment"; import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription"; import computed from "discourse-common/utils/decorators"; +import I18n from "I18n"; export default Ember.Controller.extend({ planTypeIsSelected: true, diff --git a/assets/javascripts/discourse/helpers/show-extra-nav.js.es6 b/assets/javascripts/discourse/helpers/show-extra-nav.js.es6 deleted file mode 100644 index a7a7ef6..0000000 --- a/assets/javascripts/discourse/helpers/show-extra-nav.js.es6 +++ /dev/null @@ -1,5 +0,0 @@ -import { registerUnbound } from "discourse-common/lib/helpers"; - -export default registerUnbound("show-extra-nav", function() { - return Discourse.SiteSettings.discourse_subscriptions_extra_nav_subscribe; -}); diff --git a/assets/javascripts/discourse/initializers/setup-subscriptions.js.es6 b/assets/javascripts/discourse/initializers/setup-subscriptions.js.es6 new file mode 100644 index 0000000..8e35f34 --- /dev/null +++ b/assets/javascripts/discourse/initializers/setup-subscriptions.js.es6 @@ -0,0 +1,20 @@ +import { withPluginApi } from "discourse/lib/plugin-api"; +import I18n from "I18n"; + +export default { + name: "setup-subscriptions", + initialize(container) { + withPluginApi("0.8.11", api => { + const siteSettings = container.lookup("site-settings:main"); + const isNavLinkEnabled = + siteSettings.discourse_subscriptions_extra_nav_subscribe; + if (isNavLinkEnabled) { + api.addNavigationBarItem({ + name: "subscribe", + displayName: I18n.t("discourse_subscriptions.navigation.subscribe"), + href: "/s" + }); + } + }); + } +}; diff --git a/assets/javascripts/discourse/models/admin-subscription.js.es6 b/assets/javascripts/discourse/models/admin-subscription.js.es6 index 58566be..114b884 100644 --- a/assets/javascripts/discourse/models/admin-subscription.js.es6 +++ b/assets/javascripts/discourse/models/admin-subscription.js.es6 @@ -32,7 +32,7 @@ AdminSubscription.reopenClass({ return ajax("/s/admin/subscriptions", { method: "get" }).then(result => - result.data.map(subscription => AdminSubscription.create(subscription)) + result.map(subscription => AdminSubscription.create(subscription)) ); } }); diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-index.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-index.js.es6 index 6a07b1e..3085a37 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-index.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-index.js.es6 @@ -1,5 +1,6 @@ import Route from "@ember/routing/route"; import AdminProduct from "discourse/plugins/discourse-subscriptions/discourse/models/admin-product"; +import I18n from "I18n"; export default Route.extend({ model() { diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show.js.es6 index 79d64d0..8da744b 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show.js.es6 @@ -1,6 +1,7 @@ import Route from "@ember/routing/route"; import AdminProduct from "discourse/plugins/discourse-subscriptions/discourse/models/admin-product"; import AdminPlan from "discourse/plugins/discourse-subscriptions/discourse/models/admin-plan"; +import I18n from "I18n"; export default Route.extend({ model(params) { diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-subscriptions.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-subscriptions.js.es6 index 0110cef..48ff342 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-subscriptions.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-subscriptions.js.es6 @@ -1,5 +1,6 @@ import Route from "@ember/routing/route"; import AdminSubscription from "discourse/plugins/discourse-subscriptions/discourse/models/admin-subscription"; +import I18n from "I18n"; export default Route.extend({ model() { diff --git a/assets/javascripts/discourse/routes/user-billing-subscriptions.js.es6 b/assets/javascripts/discourse/routes/user-billing-subscriptions.js.es6 index 3527115..264c5a1 100644 --- a/assets/javascripts/discourse/routes/user-billing-subscriptions.js.es6 +++ b/assets/javascripts/discourse/routes/user-billing-subscriptions.js.es6 @@ -1,5 +1,6 @@ import Route from "@ember/routing/route"; import UserSubscription from "discourse/plugins/discourse-subscriptions/discourse/models/user-subscription"; +import I18n from "I18n"; export default Route.extend({ templateName: "user/billing/subscriptions", diff --git a/assets/javascripts/discourse/templates/connectors/extra-nav-item/subscribe.hbs b/assets/javascripts/discourse/templates/connectors/extra-nav-item/subscribe.hbs deleted file mode 100644 index 09a5afe..0000000 --- a/assets/javascripts/discourse/templates/connectors/extra-nav-item/subscribe.hbs +++ /dev/null @@ -1,5 +0,0 @@ -{{#if (show-extra-nav)}} - {{#link-to 's' class='discourse-patrons-subscribe'}} - {{i18n 'discourse_subscriptions.navigation.subscribe'}} - {{/link-to}} -{{/if}} diff --git a/db/migrate/20191203014808_create_subscriptions_customers.rb b/db/migrate/20191203014808_create_subscriptions_customers.rb index c015966..c631073 100644 --- a/db/migrate/20191203014808_create_subscriptions_customers.rb +++ b/db/migrate/20191203014808_create_subscriptions_customers.rb @@ -3,9 +3,9 @@ class CreateSubscriptionsCustomers < ActiveRecord::Migration[6.0] def change create_table :discourse_subscriptions_customers do |t| - t.string :customer_id, null: false + t.string :customer_id, null: false, index: true t.string :product_id - t.references :user, foreign_key: true + t.references :user t.timestamps end end diff --git a/db/migrate/20200514175537_create_products.rb b/db/migrate/20200514175537_create_products.rb new file mode 100644 index 0000000..8ed54e7 --- /dev/null +++ b/db/migrate/20200514175537_create_products.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class CreateProducts < ActiveRecord::Migration[6.0] + def change + create_table :discourse_subscriptions_products do |t| + t.string :external_id, null: false, index: true + + t.timestamps + end + end +end diff --git a/db/migrate/20200518145424_create_subscriptions.rb b/db/migrate/20200518145424_create_subscriptions.rb new file mode 100644 index 0000000..9743629 --- /dev/null +++ b/db/migrate/20200518145424_create_subscriptions.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class CreateSubscriptions < ActiveRecord::Migration[6.0] + def change + create_table :discourse_subscriptions_subscriptions do |t| + t.integer :customer_id, null: false, index: true + t.string :external_id, null: false, index: true + + t.timestamps + end + end +end diff --git a/doc/admin-plan.png b/doc/admin-plan.png deleted file mode 100644 index c98e912..0000000 Binary files a/doc/admin-plan.png and /dev/null differ diff --git a/doc/admin-product.png b/doc/admin-product.png deleted file mode 100644 index 3004cbd..0000000 Binary files a/doc/admin-product.png and /dev/null differ diff --git a/doc/admin-products.png b/doc/admin-products.png deleted file mode 100644 index 4782da8..0000000 Binary files a/doc/admin-products.png and /dev/null differ diff --git a/doc/admin-subscriptions.png b/doc/admin-subscriptions.png deleted file mode 100644 index 97a2790..0000000 Binary files a/doc/admin-subscriptions.png and /dev/null differ diff --git a/doc/settings.png b/doc/settings.png deleted file mode 100644 index ceecb45..0000000 Binary files a/doc/settings.png and /dev/null differ diff --git a/doc/subscribe.png b/doc/subscribe.png deleted file mode 100644 index 1628c1b..0000000 Binary files a/doc/subscribe.png and /dev/null differ diff --git a/doc/user-payments.png b/doc/user-payments.png deleted file mode 100644 index 2ea9654..0000000 Binary files a/doc/user-payments.png and /dev/null differ diff --git a/doc/user-subscriptions.png b/doc/user-subscriptions.png deleted file mode 100644 index cd28500..0000000 Binary files a/doc/user-subscriptions.png and /dev/null differ diff --git a/lib/tasks/subscriptions.rake b/lib/tasks/subscriptions.rake new file mode 100644 index 0000000..2988ff0 --- /dev/null +++ b/lib/tasks/subscriptions.rake @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'stripe' +require 'highline/import' + +desc 'Import subscriptions from Stripe' +task 'subscriptions:import' => :environment do + setup_api + products = get_stripe_products + products_to_import = [] + + products.each do |product| + confirm_import = ask("Do you wish to import product #{product[:name]} (id: #{product[:id]}): (y/N)") + next if confirm_import.downcase != 'y' + products_to_import << product + end + + import_products(products_to_import) + import_subscriptions +end + +def get_stripe_products + puts 'Getting products from Stripe API' + Stripe::Product.list +end + +def import_products(products) + puts 'Importing products' + products.each do |product| + if DiscourseSubscriptions::Product.find_by(external_id: product[:id]).blank? + DiscourseSubscriptions::Product.create(external_id: product[:id]) + end + end +end + +def import_subscriptions + puts 'Importing subscriptions' + product_ids = DiscourseSubscriptions::Product.all.pluck(:external_id) + subscriptions = Stripe::Subscription.list + subscriptions_for_products = subscriptions[:data].select { |sub| product_ids.include?(sub[:items][:data][0][:plan][:product]) } + + subscriptions_for_products.each do |subscription| + product_id = subscription[:items][:data][0][:plan][:product] + customer_id = subscription[:customer] + subscription_id = subscription[:id] + user_id = subscription[:metadata][:user_id].to_i + username = subscription[:metadata][:username] + + if product_id && customer_id && subscription_id + customer = DiscourseSubscriptions::Customer.find_by(user_id: user_id, customer_id: customer_id, product_id: product_id) + + # create the customer record if doesn't exist only if the user_id and username match + # this prevents issues if multiple sites use the same Stripe account + if customer.nil? && user_id && user_id > 0 + user = User.find(user_id) + if user && (user.username == username) + customer = DiscourseSubscriptions::Customer.create( + user_id: user_id, + customer_id: customer_id, + product_id: product_id + ) + end + end + + if customer + if DiscourseSubscriptions::Subscription.find_by(customer_id: customer.id, external_id: subscription_id).blank? + DiscourseSubscriptions::Subscription.create( + customer_id: customer.id, + external_id: subscription_id + ) + end + end + end + end +end + +private + +def setup_api + api_key = SiteSetting.discourse_subscriptions_secret_key || ask('Input Stripe secret key') + Stripe.api_key = api_key +end diff --git a/plugin.rb b/plugin.rb index aae3605..9761d42 100644 --- a/plugin.rb +++ b/plugin.rb @@ -4,7 +4,7 @@ # about: Integrates Stripe into Discourse to allow visitors to subscribe # version: 2.8.1 # url: https://github.com/discourse/discourse-subscriptions -# authors: Rimian Perkins +# authors: Rimian Perkins, Justin DiRose enabled_site_setting :discourse_subscriptions_enabled @@ -45,7 +45,7 @@ after_initialize do ::Stripe.set_app_info( 'Discourse Subscriptions', version: '2.8.1', - url: 'https://github.com/rimian/discourse-subscriptions' + url: 'https://github.com/discourse/discourse-subscriptions' ) [ @@ -67,6 +67,8 @@ after_initialize do "../app/controllers/products_controller", "../app/controllers/subscriptions_controller", "../app/models/customer", + "../app/models/product", + "../app/models/subscription", "../app/serializers/payment_serializer", ].each { |path| require File.expand_path(path, __FILE__) } diff --git a/spec/fabricators/customer_fabricator.rb b/spec/fabricators/customer_fabricator.rb index c3f9174..9c99c79 100644 --- a/spec/fabricators/customer_fabricator.rb +++ b/spec/fabricators/customer_fabricator.rb @@ -1,3 +1,3 @@ # frozen_string_literal: true -Fabricator(:customer, from: "DiscourseSubscriptions::Customer") +Fabricator(:product, from: "DiscourseSubscriptions::Product") diff --git a/spec/fabricators/product_fabricator.rb b/spec/fabricators/product_fabricator.rb new file mode 100644 index 0000000..c3f9174 --- /dev/null +++ b/spec/fabricators/product_fabricator.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +Fabricator(:customer, from: "DiscourseSubscriptions::Customer") diff --git a/spec/fabricators/subscription_fabricator.rb b/spec/fabricators/subscription_fabricator.rb new file mode 100644 index 0000000..5ba9e2e --- /dev/null +++ b/spec/fabricators/subscription_fabricator.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +Fabricator(:subscription, from: "DiscourseSubscriptions::Subscription") diff --git a/spec/requests/admin/plans_controller_spec.rb b/spec/requests/admin/plans_controller_spec.rb index 19d6873..1aa41a7 100644 --- a/spec/requests/admin/plans_controller_spec.rb +++ b/spec/requests/admin/plans_controller_spec.rb @@ -98,7 +98,7 @@ module DiscourseSubscriptions it "upcases the currency" do ::Stripe::Plan.expects(:retrieve).with('plan_12345').returns(currency: 'aud') get "/s/admin/plans/plan_12345.json" - expect(response.body).to eq '{"currency":"AUD"}' + expect(response.parsed_body["currency"]).to eq 'AUD' end end diff --git a/spec/requests/admin/products_controller_spec.rb b/spec/requests/admin/products_controller_spec.rb index d59e726..4e08e8f 100644 --- a/spec/requests/admin/products_controller_spec.rb +++ b/spec/requests/admin/products_controller_spec.rb @@ -48,8 +48,8 @@ module DiscourseSubscriptions describe 'index' do it "gets the empty products" do - ::Stripe::Product.expects(:list) get "/s/admin/products.json" + expect(response.parsed_body).to be_empty end end diff --git a/spec/requests/admin/subscriptions_controller_spec.rb b/spec/requests/admin/subscriptions_controller_spec.rb index 65e8af7..50536e8 100644 --- a/spec/requests/admin/subscriptions_controller_spec.rb +++ b/spec/requests/admin/subscriptions_controller_spec.rb @@ -8,6 +8,13 @@ module DiscourseSubscriptions expect(DiscourseSubscriptions::Admin::SubscriptionsController < ::Admin::AdminController).to eq(true) end + let(:user) { Fabricate(:user) } + let(:customer) { Fabricate(:customer, user_id: user.id, customer_id: 'c_123', product_id: 'pr_34578') } + + before do + Fabricate(:subscription, external_id: "sub_12345", customer_id: customer.id) + end + context 'unauthenticated' do it "does nothing" do ::Stripe::Subscription.expects(:list).never @@ -22,16 +29,23 @@ module DiscourseSubscriptions end context 'authenticated' do - let(:user) { Fabricate(:user) } let(:admin) { Fabricate(:admin) } before { sign_in(admin) } describe "index" do it "gets the subscriptions and products" do - ::Stripe::Subscription.expects(:list).with(expand: ['data.plan.product']) + ::Stripe::Subscription.expects(:list).with(expand: ['data.plan.product']).returns( + [ + { id: "sub_12345" }, + { id: "sub_nope" } + ] + ) get "/s/admin/subscriptions.json" + subscriptions = response.parsed_body[0]["id"] + expect(response.status).to eq(200) + expect(subscriptions).to eq("sub_12345") end end @@ -39,12 +53,6 @@ module DiscourseSubscriptions let(:group) { Fabricate(:group, name: 'subscribers') } before do - DiscourseSubscriptions::Customer.create( - user_id: user.id, - customer_id: 'c_123', - product_id: 'pr_34578' - ) - group.add(user) end diff --git a/spec/requests/plans_controller_spec.rb b/spec/requests/plans_controller_spec.rb index 790a2c5..9e69aea 100644 --- a/spec/requests/plans_controller_spec.rb +++ b/spec/requests/plans_controller_spec.rb @@ -26,7 +26,7 @@ module DiscourseSubscriptions get "/s/plans.json" - expect(JSON.parse(response.body)).to eq([ + expect(response.parsed_body).to eq([ { "amount" => 1000, "currency" => "aud", "id" => "plan_id678", "interval" => "week" }, { "amount" => 1220, "currency" => "aud", "id" => "plan_id123", "interval" => "year" }, { "amount" => 1399, "currency" => "usd", "id" => "plan_id234", "interval" => "year" } diff --git a/spec/requests/products_controller_spec.rb b/spec/requests/products_controller_spec.rb index 1a0869a..79a9347 100644 --- a/spec/requests/products_controller_spec.rb +++ b/spec/requests/products_controller_spec.rb @@ -15,14 +15,19 @@ module DiscourseSubscriptions otherstuff: true, } end + let(:product_ids) { ["prodct_23456"] } + + before do + Fabricate(:product, external_id: "prodct_23456") + end context "unauthenticated" do it "gets products" do - ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + ::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product]) get "/s/products.json" - expect(JSON.parse(response.body)).to eq([{ + expect(response.parsed_body).to eq([{ "id" => "prodct_23456", "name" => "Very Special Product", "description" => "Many people listened to my phone call with the Ukrainian President while it was being made", @@ -40,11 +45,11 @@ module DiscourseSubscriptions describe "index" do it "gets products" do - ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + ::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product]) get "/s/products.json" - expect(JSON.parse(response.body)).to eq([{ + expect(response.parsed_body).to eq([{ "id" => "prodct_23456", "name" => "Very Special Product", "description" => "Many people listened to my phone call with the Ukrainian President while it was being made", @@ -53,20 +58,20 @@ module DiscourseSubscriptions end it "is subscribed" do - ::DiscourseSubscriptions::Customer.create(product_id: product[:id], user_id: user.id, customer_id: 'x') - ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + Fabricate(:customer, product_id: product[:id], user_id: user.id, customer_id: 'x') + ::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product]) get "/s/products.json" - data = JSON.parse(response.body) + data = response.parsed_body expect(data.first["subscribed"]).to eq true end it "is not subscribed" do ::DiscourseSubscriptions::Customer.delete_all - ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + ::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product]) get "/s/products.json" - data = JSON.parse(response.body) + data = response.parsed_body expect(data.first["subscribed"]).to eq false end end @@ -76,7 +81,7 @@ module DiscourseSubscriptions ::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product) get "/s/products/prod_walterwhite.json" - expect(JSON.parse(response.body)).to eq( + expect(response.parsed_body).to eq( "id" => "prodct_23456", "name" => "Very Special Product", "description" => "Many people listened to my phone call with the Ukrainian President while it was being made", diff --git a/spec/requests/user/payments_controller_spec.rb b/spec/requests/user/payments_controller_spec.rb index 9804f92..2ea8ea7 100644 --- a/spec/requests/user/payments_controller_spec.rb +++ b/spec/requests/user/payments_controller_spec.rb @@ -12,6 +12,7 @@ module DiscourseSubscriptions it "does not get the payment intents" do ::Stripe::PaymentIntent.expects(:list).never get "/s/user/payments.json" + expect(response.status).to eq(403) end end @@ -21,14 +22,40 @@ module DiscourseSubscriptions before do sign_in(user) Fabricate(:customer, customer_id: 'c_345678', user_id: user.id) + Fabricate(:product, external_id: 'prod_8675309') end it "gets payment intents" do - ::Stripe::PaymentIntent.expects(:list).with( + ::Stripe::Invoice.expects(:list).with( customer: 'c_345678' + ).returns( + data: [ + id: "inv_900007", + lines: { + data: [ + plan: { + product: "prod_8675309" + } + ] + }, + ] + ) + + ::Stripe::PaymentIntent.expects(:list).with( + customer: 'c_345678', + ).returns( + data: [ + { invoice: "inv_900007" }, + { invoice: "inv_007" } + ] ) get "/s/user/payments.json" + + invoice = response.parsed_body[0]["invoice"] + + expect(invoice).to eq("inv_900007") + end end diff --git a/spec/requests/user/subscriptions_controller_spec.rb b/spec/requests/user/subscriptions_controller_spec.rb index fcabc6d..c9fc7a3 100644 --- a/spec/requests/user/subscriptions_controller_spec.rb +++ b/spec/requests/user/subscriptions_controller_spec.rb @@ -22,9 +22,11 @@ module DiscourseSubscriptions context "authenticated" do let(:user) { Fabricate(:user, email: 'beanie@example.com') } + let(:customer) { Fabricate(:customer, user_id: user.id, customer_id: "cus_23456", product_id: "prod_123") } before do sign_in(user) + Fabricate(:subscription, customer_id: customer.id, external_id: "sub_1234") end describe "index" do @@ -69,7 +71,7 @@ module DiscourseSubscriptions get "/s/user/subscriptions.json" - subscription = JSON.parse(response.body).first + subscription = response.parsed_body.first expect(subscription).to eq( "id" => "sub_1234", diff --git a/test/javascripts/acceptance/plugin-outlets-test.js.es6 b/test/javascripts/acceptance/plugin-outlets-test.js.es6 deleted file mode 100644 index f7c769f..0000000 --- a/test/javascripts/acceptance/plugin-outlets-test.js.es6 +++ /dev/null @@ -1,16 +0,0 @@ -import { acceptance } from "helpers/qunit-helpers"; - -acceptance("Discourse Subscriptions", { - settings: { - discourse_subscriptions_extra_nav_subscribe: true - } -}); - -QUnit.test("plugin outlets", async assert => { - await visit("/"); - - assert.ok( - $("#navigation-bar .discourse-patrons-subscribe").length, - "has a subscribe button" - ); -});