save plan model

This commit is contained in:
Rimian Perkins 2019-09-24 20:44:51 +10:00
parent 0d82bcf37e
commit a2d120c8b5
14 changed files with 109 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1 @@
export default Discourse.Route.extend({});

View File

@ -1,7 +1,22 @@
<h4>[plans show]</h4>
<h4>{{i18n 'discourse_patrons.admin.plans.title'}}</h4>
<form class="form-horizontal">
<div>
<label for="name">{{i18n 'discourse_patrons.admin.plans.show.name'}}</label>
{{input type="text" name="name" value=model.name}}
</div>
<div>
<label for="amount">{{i18n 'discourse_patrons.admin.plans.show.amount'}}</label>
{{input type="text" name="name" value=model.amount}}
</div>
<div>
<label for="interval">{{i18n 'discourse_patrons.admin.plans.show.interval'}}</label>
{{combo-box valueAttribute="value" content=model.intervals value=model.interval}}
</div>
</form>
<div class="buttons">
{{d-button label="plans.show" action="createPlan" icon="plus"}}
{{d-button label="discourse_patrons.admin.plans.show.create" action="createPlan" icon="plus"}}
</div>

View File

@ -1,7 +1,10 @@
<h3>{{i18n 'discourse_patrons.admin.plans.title'}}</h3>
{{#link-to 'adminPlugins.discourse-patrons.plans.show' 'new' class="btn btn-primary"}}
{{d-icon "plus"}}
<span>{{i18n 'discourse_patrons.admin.plans.new'}}</span>
{{/link-to}}
{{outlet}}

View File

@ -49,6 +49,11 @@ en:
plans:
title: Plans
new: New
show:
create: Create
name: Name
amount: Amount
interval: Interval
table:
head:
plan: Plan

View File

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

View File

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

View File

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