diff --git a/app/controllers/admin/plans_controller.rb b/app/controllers/admin/plans_controller.rb index f6c479a..d5974e2 100644 --- a/app/controllers/admin/plans_controller.rb +++ b/app/controllers/admin/plans_controller.rb @@ -2,12 +2,22 @@ module DiscoursePatrons class PlansController < ::Admin::AdminController - def index - head 204 - end + include DiscoursePatrons::Stripe - def show - head 204 + before_action :set_api_key + + def create + plan = ::Stripe::Plan.create( + amount: params[:amount], + interval: params[:interval], + product: { + name: 'Gold special', + }, + currency: 'usd', + id: 'gold-special', + ) + + plan.to_json end end end diff --git a/app/controllers/admin/subscriptions_controller.rb b/app/controllers/admin/subscriptions_controller.rb index 4b19b4b..be781ee 100644 --- a/app/controllers/admin/subscriptions_controller.rb +++ b/app/controllers/admin/subscriptions_controller.rb @@ -2,11 +2,12 @@ module DiscoursePatrons class SubscriptionsController < ::Admin::AdminController + include DiscoursePatrons::Stripe + + before_action :set_api_key + def index - ::Stripe.api_key = SiteSetting.discourse_patrons_secret_key - subscriptions = ::Stripe::Subscription.list - subscriptions.to_json end end diff --git a/app/controllers/concerns/stripe.rb b/app/controllers/concerns/stripe.rb new file mode 100644 index 0000000..a7cefdf --- /dev/null +++ b/app/controllers/concerns/stripe.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module DiscoursePatrons + module Stripe + extend ActiveSupport::Concern + + def set_api_key + ::Stripe.api_key = 'SiteSetting.discourse_patrons_secret_key' + end + end +end diff --git a/app/controllers/patrons_controller.rb b/app/controllers/patrons_controller.rb index 10de186..0f2f42a 100644 --- a/app/controllers/patrons_controller.rb +++ b/app/controllers/patrons_controller.rb @@ -2,6 +2,8 @@ module DiscoursePatrons class PatronsController < ::ApplicationController + include DiscoursePatrons::Stripe + skip_before_action :verify_authenticity_token, only: [:create] before_action :set_api_key @@ -16,7 +18,7 @@ module DiscoursePatrons end def show - payment_intent = Stripe::PaymentIntent.retrieve(params[:pid]) + payment_intent = ::Stripe::PaymentIntent.retrieve(params[:pid]) if current_user && (current_user.admin || payment_intent[:customer] == current_user.id) result = payment_intent @@ -61,10 +63,6 @@ module DiscoursePatrons private - def set_api_key - ::Stripe.api_key = SiteSetting.discourse_patrons_secret_key - end - def param_currency_to_number params[:amount].to_s.sub('.', '').to_i end diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 index c00a762..0503ea9 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 @@ -1,7 +1,11 @@ export default Ember.Controller.extend({ actions: { createPlan() { - this.transitionToRoute("adminPlugins.discourse-patrons.plans"); + this.get("model") + .save() + .then(() => { + this.transitionToRoute("adminPlugins.discourse-patrons.plans"); + }); } } }); diff --git a/assets/javascripts/discourse/discourse-patrons-route-map.js.es6 b/assets/javascripts/discourse/discourse-patrons-route-map.js.es6 index d4adfc1..1b55db0 100644 --- a/assets/javascripts/discourse/discourse-patrons-route-map.js.es6 +++ b/assets/javascripts/discourse/discourse-patrons-route-map.js.es6 @@ -5,7 +5,7 @@ export default { this.route("discourse-patrons", function() { this.route("subscriptions"); this.route("plans", function() { - this.route("show", { path: '/:plan-id' }); + this.route("show", { path: "/:plan-id" }); }); }); } diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 index 55af882..08d7523 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 @@ -1 +1,22 @@ -export default Discourse.Route.extend({}); +import { ajax } from "discourse/lib/ajax"; + +export default Discourse.Route.extend({ + model() { + return Ember.Object.create({ + name: "", + interval: "month", + amount: 0, + intervals: ["day", "week", "month", "year"], + + save() { + const data = { + interval: this.interval, + amount: this.amount, + name: this.name + }; + + return ajax("/patrons/admin/plans", { method: "post", data }); + } + }); + } +}); diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 index e69de29..55af882 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 @@ -0,0 +1 @@ +export default Discourse.Route.extend({}); diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs index ed7674a..a0f55fc 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs @@ -1,7 +1,22 @@ -

[plans show]

+

{{i18n 'discourse_patrons.admin.plans.title'}}

+ +
+
+ + {{input type="text" name="name" value=model.name}} +
+
+ + {{input type="text" name="name" value=model.amount}} +
+
+ + {{combo-box valueAttribute="value" content=model.intervals value=model.interval}} +
+
- {{d-button label="plans.show" action="createPlan" icon="plus"}} + {{d-button label="discourse_patrons.admin.plans.show.create" action="createPlan" icon="plus"}}
diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans.hbs index 6f9fdbb..fc522ff 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans.hbs @@ -1,7 +1,10 @@ +

{{i18n 'discourse_patrons.admin.plans.title'}}

{{#link-to 'adminPlugins.discourse-patrons.plans.show' 'new' class="btn btn-primary"}} {{d-icon "plus"}} {{i18n 'discourse_patrons.admin.plans.new'}} {{/link-to}} + +{{outlet}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 5674dd9..235a7c5 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -49,6 +49,11 @@ en: plans: title: Plans new: New + show: + create: Create + name: Name + amount: Amount + interval: Interval table: head: plan: Plan diff --git a/config/routes.rb b/config/routes.rb index bb414b4..525ee82 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ DiscoursePatrons::Engine.routes.draw do get '/admin' => 'admin#index' get '/admin/subscriptions' => 'subscriptions#index' get '/admin/plans/:plan_id' => 'plans#show' + post '/admin/plans' => 'plans#create' get '/' => 'patrons#index' get '/:pid' => 'patrons#show' resources :patrons, only: [:index, :create] diff --git a/plugin.rb b/plugin.rb index 54fa5df..8d0e9dd 100644 --- a/plugin.rb +++ b/plugin.rb @@ -37,6 +37,7 @@ after_initialize do [ "../lib/discourse_patrons/engine", "../config/routes", + "../app/controllers/concerns/stripe", "../app/controllers/admin_controller", "../app/controllers/admin/plans_controller", "../app/controllers/admin/subscriptions_controller", diff --git a/spec/requests/plans_controller_spec.rb b/spec/requests/plans_controller_spec.rb index e69de29..5425f83 100644 --- a/spec/requests/plans_controller_spec.rb +++ b/spec/requests/plans_controller_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +module DiscoursePatrons + RSpec.describe PlansController do + let(:admin) { Fabricate(:admin) } + + before { sign_in(admin) } + + it 'is a subclass of AdminController' do + expect(DiscoursePatrons::PlansController < Admin::AdminController).to eq(true) + end + + it "creates a plan" do + ::Stripe::Plan.expects(:create) + post "/patrons/admin/plans.json", params: {} + end + end +end