list customer subscriptions

This commit is contained in:
Rimian Perkins 2019-10-30 14:19:43 +11:00
parent 7963d6dea2
commit 03cbc235b1
6 changed files with 36 additions and 17 deletions

View File

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

View File

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

View File

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

View File

@ -5,7 +5,7 @@
{{#if model}}
<table class="topic-list">
<thead>
<th>{{i18n 'discourse_patrons.user.billing.subscriptions.created_at'}}</th>
<th>{{i18n 'discourse_patrons.user.subscriptions.created_at'}}</th>
<th></th>
</thead>
{{#each model as |subscription|}}

View File

@ -17,6 +17,9 @@ en:
discourse_patrons:
title: Discourse Patrons
optional: Optional
transactions:
payment:
success: Your payment was successful
navigation:
subscriptions: Subscriptions
subscribe: Subscribe

View File

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