diff --git a/app/controllers/user/subscriptions_controller.rb b/app/controllers/user/subscriptions_controller.rb index 4965374..1afd15d 100644 --- a/app/controllers/user/subscriptions_controller.rb +++ b/app/controllers/user/subscriptions_controller.rb @@ -9,16 +9,24 @@ module DiscoursePatrons def index begin + plans = ::Stripe::Plan.list( + expand: ['data.product'] + ) + customers = ::Stripe::Customer.list( email: current_user.email, expand: ['data.subscriptions'] ) - # TODO: Serialize and remove stuff subscriptions = customers[:data].map do |customer| customer[:subscriptions][:data] end.flatten(1) + 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 + render_json_dump subscriptions rescue ::Stripe::InvalidRequestError => e @@ -39,7 +47,7 @@ module DiscoursePatrons deleted = ::Stripe::Subscription.delete(params[:id]) render_json_dump deleted else - render_json_error "Customer ID not found" + render_json_error I18n.t('discourse_patrons.customer_id_not_found') end rescue ::Stripe::InvalidRequestError => e diff --git a/assets/javascripts/discourse/templates/user/subscriptions.hbs b/assets/javascripts/discourse/templates/user/subscriptions.hbs index 5c29c11..8370eae 100644 --- a/assets/javascripts/discourse/templates/user/subscriptions.hbs +++ b/assets/javascripts/discourse/templates/user/subscriptions.hbs @@ -5,6 +5,7 @@
{{i18n 'discourse_patrons.user.subscriptions.id'}} | +{{i18n 'discourse_patrons.user.plans.product'}} | {{i18n 'discourse_patrons.user.plans.rate'}} | {{i18n 'discourse_patrons.user.subscriptions.status'}} | {{i18n 'discourse_patrons.user.subscriptions.created_at'}} | @@ -13,6 +14,7 @@ {{#each model as |subscription|}}
---|---|---|---|---|
{{subscription.id}} | +{{subscription.product.name}} | {{subscription.plan.subscriptionRate}} | {{subscription.status}} | {{format-unix-date subscription.created}} | diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8f030a3..77f20d5 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -27,6 +27,7 @@ en: user: plans: rate: Rate + product: Name subscriptions_help: You have no subscriptions. subscriptions: title: Subscriptions diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml new file mode 100644 index 0000000..5a99cbc --- /dev/null +++ b/config/locales/server.en.yml @@ -0,0 +1,3 @@ +en: + discourse_patrons: + customer_id_not_found: Customer ID not found diff --git a/spec/requests/user/subscriptions_controller_spec.rb b/spec/requests/user/subscriptions_controller_spec.rb index 0118455..46dc045 100644 --- a/spec/requests/user/subscriptions_controller_spec.rb +++ b/spec/requests/user/subscriptions_controller_spec.rb @@ -28,18 +28,40 @@ module DiscoursePatrons end describe "index" do + let(:plans) do + { + data: [ + { + id: "plan_1", + product: { name: 'ACME Subscriptions' }, + }, + { + id: "plan_2", + product: { name: 'ACME Other Subscriptions' }, + } + ] + } + end + let(:customers) do { data: [{ id: "cus_23456", subscriptions: { - data: [{ id: "sub_1234" }, { id: "sub_4567" }] + data: [ + { id: "sub_1234", plan: { id: "plan_1" } }, + { id: "sub_4567", plan: { id: "plan_2" } } + ] }, }] } end it "gets subscriptions" do + ::Stripe::Plan.expects(:list).with( + expand: ['data.product'] + ).returns(plans) + ::Stripe::Customer.expects(:list).with( email: user.email, expand: ['data.subscriptions'] @@ -47,7 +69,13 @@ module DiscoursePatrons get "/patrons/user/subscriptions.json" - expect(JSON.parse(response.body)).to eq([{ "id" => "sub_1234" }, { "id" => "sub_4567" }]) + subscription = JSON.parse(response.body).first + + expect(subscription).to eq( + "id" => "sub_1234", + "plan" => { "id" => "plan_1" }, + "product" => { "name" => "ACME Subscriptions" } + ) end end