diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index df96f06..2390b83 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -8,13 +8,15 @@ module DiscoursePatrons def index begin - customer = DiscoursePatrons::Customer.find_user(current_user) + customers = ::Stripe::Customer.list( + email: current_user.email, + expand: ['data.subscriptions'] + ) - if customer.present? - subscriptions = ::Stripe::Subscription.list(customer: customer.customer_id).data - else - subscriptions = [] - end + + subscriptions = customers[:data].map do |customer| + customer[:subscriptions][:data] + end.flatten(1) render_json_dump subscriptions diff --git a/assets/javascripts/discourse/controllers/patrons-index.js.es6 b/assets/javascripts/discourse/controllers/patrons-index.js.es6 index 0439eb7..70bbf49 100644 --- a/assets/javascripts/discourse/controllers/patrons-index.js.es6 +++ b/assets/javascripts/discourse/controllers/patrons-index.js.es6 @@ -11,8 +11,8 @@ export default Ember.Controller.extend({ }); }, - paymentSuccessHandler(paymentIntentId) { - bootbox.alert("ok payment good... some kind of message"); + paymentSuccessHandler(/* paymentIntentId */) { + bootbox.alert(I18n.t("discourse_patrons.transactions.payment.success")); this.transitionToRoute( "user.billing", Discourse.User.current().username.toLowerCase() diff --git a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 index ad50074..ab8e950 100644 --- a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 @@ -1,4 +1,3 @@ -import Group from "discourse/plugins/discourse-patrons/discourse/models/group"; import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan"; import Subscription from "discourse/plugins/discourse-patrons/discourse/models/subscription"; diff --git a/assets/javascripts/discourse/templates/user/subscriptions.hbs b/assets/javascripts/discourse/templates/user/subscriptions.hbs index aeb361b..1e36462 100644 --- a/assets/javascripts/discourse/templates/user/subscriptions.hbs +++ b/assets/javascripts/discourse/templates/user/subscriptions.hbs @@ -5,7 +5,7 @@ {{#if model}}
{{i18n 'discourse_patrons.user.billing.subscriptions.created_at'}} | +{{i18n 'discourse_patrons.user.subscriptions.created_at'}} | {{#each model as |subscription|}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 884cb5d..2250eb9 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -17,6 +17,9 @@ en: discourse_patrons: title: Discourse Patrons optional: Optional + transactions: + payment: + success: Your payment was successful navigation: subscriptions: Subscriptions subscribe: Subscribe diff --git a/spec/requests/subscriptions_controller_spec.rb b/spec/requests/subscriptions_controller_spec.rb index 947028e..6e243ba 100644 --- a/spec/requests/subscriptions_controller_spec.rb +++ b/spec/requests/subscriptions_controller_spec.rb @@ -5,6 +5,11 @@ require 'rails_helper' module DiscoursePatrons RSpec.describe SubscriptionsController do context "not authenticated" do + it "does not get the subscriptions" do + ::Stripe::Customer.expects(:list).never + get "/patrons/subscriptions.json" + end + it "does not create a subscription" do ::Stripe::Plan.expects(:retrieve).never ::Stripe::Subscription.expects(:create).never @@ -25,16 +30,26 @@ module DiscoursePatrons end describe "index" do - it "does not get subscriptions if there is no customer" do - ::Stripe::Subscription.expects(:create).never - get "/patrons/subscriptions.json" - expect(response.body).to eq "[]" + let(:customers) do + { + data: [{ + id: "cus_23456", + subscriptions: { + data: [{ id: "sub_1234" }, { id: "sub_4567" }] + }, + }] + } end it "gets subscriptions" do - DiscoursePatrons::Customer.create(user_id: user.id, customer_id: 'cus_id5678') - ::Stripe::Subscription.expects(:list).with(customer: 'cus_id5678') + ::Stripe::Customer.expects(:list).with( + email: user.email, + expand: ['data.subscriptions'] + ).returns(customers) + get "/patrons/subscriptions.json" + + expect(JSON.parse(response.body)).to eq([{"id"=>"sub_1234"}, {"id"=>"sub_4567"}]) end end @@ -129,7 +144,7 @@ module DiscoursePatrons describe "delete" do it "deletes a subscription" do ::Stripe::Subscription.expects(:delete).with('sub_12345') - delete "/patrons/subscription/sub_12345.json" + delete "/patrons/subscriptions/sub_12345.json" end end end |
---|