REFACTOR: Use the Prices API in place of Plans (#17)

Stripe has a newer API called Prices where you can create a price for any product and it can either be recurring or one-time. The easy part is existing Plans work with the Prices API by passing a Plan ID, but objects are returned in the slightly-different Prices API object format.

This commit is a refactor to the new API to handle the data in its new form, and lays the foundation for a one time payment plan to be added to any subscriptions product.
This commit is contained in:
Justin DiRose
2020-07-15 08:44:40 -05:00
committed by GitHub
parent 8bcb7aa93c
commit c9ff55b46a
22 changed files with 128 additions and 136 deletions
+12 -8
View File
@@ -6,7 +6,7 @@ module DiscourseSubscriptions
RSpec.describe SubscriptionsController do
context "not authenticated" do
it "does not create a subscription" do
::Stripe::Plan.expects(:retrieve).never
::Stripe::Price.expects(:retrieve).never
::Stripe::Subscription.expects(:create).never
post "/s/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
end
@@ -21,15 +21,19 @@ module DiscourseSubscriptions
describe "create" do
it "creates a subscription" do
::Stripe::Plan.expects(:retrieve).returns(
::Stripe::Price.expects(:retrieve).returns(
product: 'product_12345',
metadata: { group_name: 'awesome' }
metadata: {
group_name: 'awesome',
trial_period_days: 0
}
)
::Stripe::Subscription.expects(:create).with(
customer: 'cus_1234',
items: [ plan: 'plan_1234' ],
items: [ price: 'plan_1234' ],
metadata: { user_id: user.id, username: user.username_lower },
trial_period_days: 0
).returns(status: 'active')
expect {
@@ -38,7 +42,7 @@ module DiscourseSubscriptions
end
it "creates a customer model" do
::Stripe::Plan.expects(:retrieve).returns(metadata: {})
::Stripe::Price.expects(:retrieve).returns(metadata: {})
::Stripe::Subscription.expects(:create).returns(status: 'active')
expect {
@@ -57,13 +61,13 @@ module DiscourseSubscriptions
end
it "does not add the user to the admins group" do
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'admins' })
::Stripe::Price.expects(:retrieve).returns(metadata: { group_name: 'admins' })
post "/s/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
expect(user.admin).to eq false
end
it "does not add the user to other group" do
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'other' })
::Stripe::Price.expects(:retrieve).returns(metadata: { group_name: 'other' })
post "/s/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
expect(user.groups).to be_empty
end
@@ -71,7 +75,7 @@ module DiscourseSubscriptions
context "plan has group in metadata" do
before do
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: group_name })
::Stripe::Price.expects(:retrieve).returns(metadata: { group_name: group_name })
end
it "does not add the user to the group when subscription fails" do