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,18 +28,16 @@ module DiscoursePatrons
def destroy def destroy
begin begin
customer = Customer.find_user(current_user) subscription = ::Stripe::Subscription.retrieve(params[:id])
customer = Customer.find_by(
user_id: current_user.id,
customer_id: subscription[:customer]
)
if customer.present? if customer.present?
subscription = ::Stripe::Subscription.retrieve(params[:id]) deleted = ::Stripe::Subscription.delete(params[:id])
render_json_dump deleted
if subscription[:customer] == customer.customer_id
deleted = ::Stripe::Subscription.delete(params[:id])
render_json_dump deleted
else
render_json_error "Customer ID not found"
end
else else
render_json_error "Customer ID not found" render_json_error "Customer ID not found"
end end

View File

@ -2,8 +2,8 @@ en:
site_settings: site_settings:
discourse_patrons_enabled: Enable the Discourse Patrons plugin. discourse_patrons_enabled: Enable the Discourse Patrons plugin.
discourse_patrons_extra_nav_subscribe: Show the subscribe button in the primary navigation 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_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_currency: Default Currency Code. This can be overridden when creating a subscription plan
discourse_patrons_zip_code: "Show Zip Code" discourse_patrons_zip_code: "Show Zip Code"
discourse_patrons_billing_address: "Collect billing address" discourse_patrons_billing_address: "Collect billing address"

View File

@ -2,7 +2,7 @@
# name: discourse-patrons # name: discourse-patrons
# about: Integrates Stripe into Discourse to allow visitors to make payments and Subscribe # 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 # url: https://github.com/rimian/discourse-patrons
# authors: Rimian Perkins # authors: Rimian Perkins
@ -40,7 +40,7 @@ end
after_initialize do after_initialize do
::Stripe.api_version = "2019-11-05" ::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", "../lib/discourse_patrons/engine",

View File

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