REFACTOR: Use models to store data (#11)

* REFACTOR: Use api to add subscribe link

* FIX: I18n subscribe link

* REFACTOR: Use models to store some data

This enables the plugin to show only subscription information which was
generated on Discourse. Subscription data storage is limited to the
external identifiers Stripe generates so we can interact with the API.

* DEV: Test/linting fixes/rake task
This commit is contained in:
Justin DiRose 2020-05-22 11:20:05 -05:00 committed by GitHub
parent 7ba08ab7da
commit fb4fac197b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 308 additions and 225 deletions

138
README.md
View File

@ -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

View File

@ -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.

View File

@ -1 +0,0 @@
theme: jekyll-theme-cayman

View File

@ -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

View File

@ -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

View File

@ -29,7 +29,7 @@ module DiscourseSubscriptions
end
def find_customer
DiscourseSubscriptions::Customer.find_user(current_user)
Customer.find_user(current_user)
end
end
end

View File

@ -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
)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

6
app/models/product.rb Normal file
View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
module DiscourseSubscriptions
class Product < ActiveRecord::Base
end
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
module DiscourseSubscriptions
class Subscription < ActiveRecord::Base
belongs_to :customer
end
end

View File

@ -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,

View File

@ -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;
});

View File

@ -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"
});
}
});
}
};

View File

@ -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))
);
}
});

View File

@ -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() {

View File

@ -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) {

View File

@ -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() {

View File

@ -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",

View File

@ -1,5 +0,0 @@
{{#if (show-extra-nav)}}
{{#link-to 's' class='discourse-patrons-subscribe'}}
{{i18n 'discourse_subscriptions.navigation.subscribe'}}
{{/link-to}}
{{/if}}

View File

@ -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

View File

@ -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

View File

@ -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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

View File

@ -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

View File

@ -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__) }

View File

@ -1,3 +1,3 @@
# frozen_string_literal: true
Fabricator(:customer, from: "DiscourseSubscriptions::Customer")
Fabricator(:product, from: "DiscourseSubscriptions::Product")

View File

@ -0,0 +1,3 @@
# frozen_string_literal: true
Fabricator(:customer, from: "DiscourseSubscriptions::Customer")

View File

@ -0,0 +1,3 @@
# frozen_string_literal: true
Fabricator(:subscription, from: "DiscourseSubscriptions::Subscription")

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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" }

View File

@ -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",

View File

@ -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

View File

@ -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",

View File

@ -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"
);
});