DEV: use existing customer id instead of creating. (#92)
Previosuly, new customer record is created in Stripe for each subscription in Discourse.
This commit is contained in:
parent
3ce422ffbe
commit
33008b0b47
|
@ -63,7 +63,7 @@ module DiscourseSubscriptions
|
||||||
def create
|
def create
|
||||||
params.require([:source, :plan])
|
params.require([:source, :plan])
|
||||||
begin
|
begin
|
||||||
customer = create_customer(params[:source])
|
customer = find_or_create_customer(params[:source])
|
||||||
plan = ::Stripe::Price.retrieve(params[:plan])
|
plan = ::Stripe::Price.retrieve(params[:plan])
|
||||||
|
|
||||||
if params[:promo].present?
|
if params[:promo].present?
|
||||||
|
@ -173,11 +173,17 @@ module DiscourseSubscriptions
|
||||||
end.sort_by { |plan| plan[:amount] }
|
end.sort_by { |plan| plan[:amount] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_customer(source)
|
def find_or_create_customer(source)
|
||||||
::Stripe::Customer.create(
|
customer = Customer.find_by_user_id(current_user.id)
|
||||||
email: current_user.email,
|
|
||||||
source: source
|
if customer.present?
|
||||||
)
|
::Stripe::Customer.retrieve(customer.customer_id)
|
||||||
|
else
|
||||||
|
::Stripe::Customer.create(
|
||||||
|
email: current_user.email,
|
||||||
|
source: source
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def retrieve_payment_intent(invoice_id)
|
def retrieve_payment_intent(invoice_id)
|
||||||
|
|
|
@ -210,12 +210,18 @@ module DiscourseSubscriptions
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a customer model" do
|
it "creates a customer model" do
|
||||||
::Stripe::Price.expects(:retrieve).returns(type: 'recurring', metadata: {})
|
::Stripe::Price.expects(:retrieve).returns(type: 'recurring', metadata: {}).twice
|
||||||
::Stripe::Subscription.expects(:create).returns(status: 'active', customer: 'cus_1234')
|
::Stripe::Subscription.expects(:create).returns(status: 'active', customer: 'cus_1234')
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
post "/s/create.json", params: { plan: 'plan_1234', source: 'tok_1234' }
|
post "/s/create.json", params: { plan: 'plan_1234', source: 'tok_1234' }
|
||||||
}.to change { DiscourseSubscriptions::Customer.count }
|
}.to change { DiscourseSubscriptions::Customer.count }
|
||||||
|
|
||||||
|
::Stripe::Customer.expects(:retrieve).with('cus_1234')
|
||||||
|
|
||||||
|
expect {
|
||||||
|
post "/s/create.json", params: { plan: 'plan_5678', source: 'tok_5678' }
|
||||||
|
}.not_to change { DiscourseSubscriptions::Customer.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with promo code" do
|
context "with promo code" do
|
||||||
|
|
Loading…
Reference in New Issue