fix customer id not found

This commit is contained in:
Rimian Perkins 2019-11-26 11:09:54 +11:00
parent 22d5e6f7a0
commit 7340bf5bc5
4 changed files with 32 additions and 33 deletions

View File

@ -28,22 +28,20 @@ module DiscoursePatrons
def destroy
begin
customer = Customer.find_user(current_user)
if customer.present?
subscription = ::Stripe::Subscription.retrieve(params[:id])
if subscription[:customer] == customer.customer_id
customer = Customer.find_by(
user_id: current_user.id,
customer_id: subscription[:customer]
)
if customer.present?
deleted = ::Stripe::Subscription.delete(params[:id])
render_json_dump deleted
else
render_json_error "Customer ID not found"
end
else
render_json_error "Customer ID not found"
end
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end

View File

@ -2,8 +2,8 @@ en:
site_settings:
discourse_patrons_enabled: Enable the Discourse Patrons plugin.
discourse_patrons_extra_nav_subscribe: Show the subscribe button in the primary navigation
discourse_patrons_public_key: Stripe Publishable Key
discourse_patrons_secret_key: Stripe Secret Key
discourse_patrons_public_key: Stripe Public Key
discourse_patrons_currency: Default Currency Code. This can be overridden when creating a subscription plan
discourse_patrons_zip_code: "Show Zip Code"
discourse_patrons_billing_address: "Collect billing address"

View File

@ -2,7 +2,7 @@
# name: discourse-patrons
# about: Integrates Stripe into Discourse to allow visitors to make payments and Subscribe
# version: 2.2.3
# version: 2.2.4
# url: https://github.com/rimian/discourse-patrons
# authors: Rimian Perkins
@ -40,7 +40,7 @@ end
after_initialize do
::Stripe.api_version = "2019-11-05"
::Stripe.set_app_info('Discourse Patrons', version: '2.2.3', url: 'https://github.com/rimian/discourse-patrons')
::Stripe.set_app_info('Discourse Patrons', version: '2.2.4', url: 'https://github.com/rimian/discourse-patrons')
[
"../lib/discourse_patrons/engine",

View File

@ -52,26 +52,28 @@ module DiscoursePatrons
end
describe "delete" do
context "no customer record" do
it "deletes a subscription" do
::Stripe::Subscription.expects(:delete).never
delete "/patrons/user/subscriptions/sub_12345.json"
expect(response.status).to eq 422
before do
# Users can have more than one customer id
Customer.create(user_id: user.id, customer_id: 'customer_id_1')
Customer.create(user_id: user.id, customer_id: 'customer_id_2')
end
end
context "customer exists" do
let!(:customer) { Fabricate(:customer, customer_id: 'cus_tmp76543g', user_id: user.id) }
it "does not delete a subscription" do
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'other')
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'wrong_id')
::Stripe::Subscription.expects(:delete).never
delete "/patrons/user/subscriptions/sub_12345.json"
expect(response.status).to eq 422
end
it "deletes a subscription" do
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'cus_tmp76543g')
it "deletes the first subscription" do
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_1')
::Stripe::Subscription.expects(:delete).with('sub_12345')
delete "/patrons/user/subscriptions/sub_12345.json"
expect(response.status).to eq 200
end
it "deletes the second subscription" do
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_2')
::Stripe::Subscription.expects(:delete).with('sub_12345')
delete "/patrons/user/subscriptions/sub_12345.json"
expect(response.status).to eq 200
@ -80,4 +82,3 @@ module DiscoursePatrons
end
end
end
end