From 2c244e50a467c60d1ef8d60049f7bfb5ce25d3fa Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 22 Jun 2017 09:38:48 +1000 Subject: [PATCH] Implementing the Stripe Connect feature toggle --- .../admin/stripe_accounts_controller.rb | 1 + .../payment_methods_controller_decorator.rb | 7 ++ app/helpers/enterprises_helper.rb | 1 + .../form/_payment_methods.html.haml | 3 +- .../payment_methods/_stripe_connect.html.haml | 3 + app/views/spree/users/show.html.haml | 5 +- config/locales/en.yml | 1 + .../admin/stripe_account_controller_spec.rb | 71 +++++++++++-------- spec/features/admin/payment_method_spec.rb | 1 + spec/features/consumer/account/cards_spec.rb | 2 + .../consumer/shopping/checkout_spec.rb | 11 ++- spec/helpers/enterprises_helper_spec.rb | 21 ++++++ 12 files changed, 89 insertions(+), 38 deletions(-) diff --git a/app/controllers/admin/stripe_accounts_controller.rb b/app/controllers/admin/stripe_accounts_controller.rb index 93afa8a92d32..c63ba934c887 100644 --- a/app/controllers/admin/stripe_accounts_controller.rb +++ b/app/controllers/admin/stripe_accounts_controller.rb @@ -30,6 +30,7 @@ def destroy_from_webhook def status authorize! :stripe_account, Enterprise.find_by_id(params[:enterprise_id]) + return render json: { status: :stripe_disabled } unless Spree::Config.stripe_connect_enabled stripe_account = StripeAccount.find_by_enterprise_id(params[:enterprise_id]) return render json: { status: :account_missing } unless stripe_account diff --git a/app/controllers/spree/admin/payment_methods_controller_decorator.rb b/app/controllers/spree/admin/payment_methods_controller_decorator.rb index fc15324d0b84..2456f7c5aa3e 100644 --- a/app/controllers/spree/admin/payment_methods_controller_decorator.rb +++ b/app/controllers/spree/admin/payment_methods_controller_decorator.rb @@ -57,12 +57,19 @@ def load_data else @providers = Gateway.providers.reject{ |p| p.name.include? "Bogus" }.sort{|p1, p2| p1.name <=> p2.name } end + @providers.reject!{ |p| p.name.ends_with? "StripeConnect" } unless show_stripe? @calculators = PaymentMethod.calculators.sort_by(&:name) end def load_hubs @hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by!{ |d| [(@payment_method.has_distributor? d) ? 0 : 1, d.name] } end + + # Show Stripe as an option if enabled, or if the + # current payment_method is already a Stripe method + def show_stripe? + Spree::Config.stripe_connect_enabled || @payment_method.try(:type) == "Spree::Gateway::StripeConnect" + end end end end diff --git a/app/helpers/enterprises_helper.rb b/app/helpers/enterprises_helper.rb index 5987b20475e6..0268b85515d3 100644 --- a/app/helpers/enterprises_helper.rb +++ b/app/helpers/enterprises_helper.rb @@ -21,6 +21,7 @@ def available_shipping_methods def available_payment_methods return [] unless current_distributor.present? payment_methods = current_distributor.payment_methods.available(:front_end).all + payment_methods.reject!{ |p| p.type.ends_with? "StripeConnect" } unless Spree::Config.stripe_connect_enabled applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterPaymentMethods", current_customer.andand.tag_list) applicator.filter!(payment_methods) diff --git a/app/views/admin/enterprises/form/_payment_methods.html.haml b/app/views/admin/enterprises/form/_payment_methods.html.haml index 261fa33bcc57..30a5852816b4 100644 --- a/app/views/admin/enterprises/form/_payment_methods.html.haml +++ b/app/views/admin/enterprises/form/_payment_methods.html.haml @@ -1,4 +1,5 @@ -= render 'admin/enterprises/form/stripe_connect' +- if Spree::Config.stripe_connect_enabled || @enterprise.stripe_account + = render 'admin/enterprises/form/stripe_connect' - if @payment_methods.count > 0 %table diff --git a/app/views/spree/admin/payment_methods/_stripe_connect.html.haml b/app/views/spree/admin/payment_methods/_stripe_connect.html.haml index 6eb55b50d802..9ccc73372049 100644 --- a/app/views/spree/admin/payment_methods/_stripe_connect.html.haml +++ b/app/views/spree/admin/payment_methods/_stripe_connect.html.haml @@ -14,6 +14,9 @@ .alert-box.warning{ ng: { hide: "stripe_account.status" } } = t(".loading_account_information_msg") + .alert-box.error{ ng: { show: "stripe_account.status == 'stripe_disabled'" } } + = t(".stripe_disabled_msg") + .alert-box.error{ ng: { show: "stripe_account.status == 'request_failed'" } } = t(".request_failed_msg") diff --git a/app/views/spree/users/show.html.haml b/app/views/spree/users/show.html.haml index 43a7e04f6464..23c9e3b52df2 100644 --- a/app/views/spree/users/show.html.haml +++ b/app/views/spree/users/show.html.haml @@ -21,8 +21,9 @@ .row.tabset-ctrl#account-tabs{ style: 'margin-bottom: 100px', navigate: 'true', selected: 'orders', prefix: 'account' } .small.12.medium-3.columns.tab{ name: "orders" } %a{ href: 'javascript:void(0)' }=t('.tabs.orders') - .small.12.medium-3.columns.tab{ name: "cards" } - %a{ href: 'javascript:void(0)' }=t('.tabs.cards') + - if Spree::Config.stripe_connect_enabled + .small.12.medium-3.columns.tab{ name: "cards" } + %a{ href: 'javascript:void(0)' }=t('.tabs.cards') .small.12.medium-3.columns.tab{ name: "transactions" } %a{ href: 'javascript:void(0)' }=t('.tabs.transactions') .small.12.medium-3.columns.tab{ name: "settings" } diff --git a/config/locales/en.yml b/config/locales/en.yml index 643bba34d835..71cc4e9484df 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1698,6 +1698,7 @@ Please follow the instructions there to make your enterprise visible on the Open stripe_connect: enterprise_select_placeholder: Choose... loading_account_information_msg: Loading account information from stripe, please wait... + stripe_disabled_msg: Stripe payments have been disabled by the system administrator. request_failed_msg: Sorry. Something went wrong when trying to verify account details with Stripe... account_missing_msg: No Stripe account exists for this enterprise. connect_one: Connect One diff --git a/spec/controllers/admin/stripe_account_controller_spec.rb b/spec/controllers/admin/stripe_account_controller_spec.rb index 44e41eb6e660..415baac67364 100644 --- a/spec/controllers/admin/stripe_account_controller_spec.rb +++ b/spec/controllers/admin/stripe_account_controller_spec.rb @@ -46,6 +46,7 @@ before do Stripe.api_key = "sk_test_12345" + Spree::Config.set({stripe_connect_enabled: false}) end context "where I don't manage the specified enterprise" do @@ -69,49 +70,61 @@ allow(controller).to receive(:spree_current_user) { enterprise.owner } end - context "but it has no associated stripe account" do - it "returns with a status of 'account_missing'" do + context "but Stripe is not enabled" do + it "returns with a status of 'stripe_disabled'" do spree_get :status, params json_response = JSON.parse(response.body) - expect(json_response["status"]).to eq "account_missing" + expect(json_response["status"]).to eq "stripe_disabled" end end - context "and it has an associated stripe account" do - let!(:account) { create(:stripe_account, stripe_user_id: "acc_123", enterprise: enterprise) } + context "and Stripe is enabled" do + before { Spree::Config.set({stripe_connect_enabled: true}) } - context "which has been revoked or does not exist" do - before do - stub_request(:get, "https://api.stripe.com/v1/accounts/acc_123").to_return(status: 404) - end - - it "returns with a status of 'access_revoked'" do + context "but it has no associated stripe account" do + it "returns with a status of 'account_missing'" do spree_get :status, params json_response = JSON.parse(response.body) - expect(json_response["status"]).to eq "access_revoked" + expect(json_response["status"]).to eq "account_missing" end end - context "which is connected" do - let(:stripe_account_mock) { { - id: "acc_123", - business_name: "My Org", - charges_enabled: true, - some_other_attr: "something" - } } + context "and it has an associated stripe account" do + let!(:account) { create(:stripe_account, stripe_user_id: "acc_123", enterprise: enterprise) } - before do - stub_request(:get, "https://api.stripe.com/v1/accounts/acc_123").to_return(body: JSON.generate(stripe_account_mock)) + context "which has been revoked or does not exist" do + before do + stub_request(:get, "https://api.stripe.com/v1/accounts/acc_123").to_return(status: 404) + end + + it "returns with a status of 'access_revoked'" do + spree_get :status, params + json_response = JSON.parse(response.body) + expect(json_response["status"]).to eq "access_revoked" + end end - it "returns with a status of 'connected'" do - spree_get :status, params - json_response = JSON.parse(response.body) - expect(json_response["status"]).to eq "connected" - # serializes required attrs - expect(json_response["business_name"]).to eq "My Org" - # ignores other attrs - expect(json_response["some_other_attr"]).to be nil + context "which is connected" do + let(:stripe_account_mock) { { + id: "acc_123", + business_name: "My Org", + charges_enabled: true, + some_other_attr: "something" + } } + + before do + stub_request(:get, "https://api.stripe.com/v1/accounts/acc_123").to_return(body: JSON.generate(stripe_account_mock)) + end + + it "returns with a status of 'connected'" do + spree_get :status, params + json_response = JSON.parse(response.body) + expect(json_response["status"]).to eq "connected" + # serializes required attrs + expect(json_response["business_name"]).to eq "My Org" + # ignores other attrs + expect(json_response["some_other_attr"]).to be nil + end end end end diff --git a/spec/features/admin/payment_method_spec.rb b/spec/features/admin/payment_method_spec.rb index 68f1d0035c45..6a82e7951b0a 100644 --- a/spec/features/admin/payment_method_spec.rb +++ b/spec/features/admin/payment_method_spec.rb @@ -40,6 +40,7 @@ let!(:stripe_account_mock) { { id: "acc_connected123", business_name: "My Org", charges_enabled: true } } before do + Spree::Config.set({stripe_connect_enabled: true}) Stripe.api_key = "sk_test_12345" stub_request(:get, "https://api.stripe.com/v1/accounts/acc_connected123").to_return(body: JSON.generate(stripe_account_mock)) stub_request(:get, "https://api.stripe.com/v1/accounts/acc_revoked123").to_return(status: 404) diff --git a/spec/features/consumer/account/cards_spec.rb b/spec/features/consumer/account/cards_spec.rb index 56d760c1f3b3..ce0b846d00f2 100644 --- a/spec/features/consumer/account/cards_spec.rb +++ b/spec/features/consumer/account/cards_spec.rb @@ -9,6 +9,8 @@ before do quick_login_as user + Spree::Config.set({stripe_connect_enabled: true}) + stub_request(:get, "https://api.stripe.com/v1/customers/cus_AZNMJ"). to_return(:status => 200, :body => JSON.generate({id: "cus_AZNMJ"})) diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 75867ab86f5c..1201e5b0e96f 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -157,6 +157,7 @@ def fill_out_form before do Stripe.api_key = "sk_test_123456" + Spree::Config.set({stripe_connect_enabled: true}) stub_request(:post, "https://sk_test_123456:@api.stripe.com/v1/charges") .to_return(body: JSON.generate(response_mock)) @@ -166,17 +167,15 @@ def fill_out_form choose stripe_pm.name end - it "shows the saved credit card dropdown" do + it "allows use of a saved card" do + # shows the saved credit card dropdown page.should have_content "Previously Used Credit Cards" - end - it "disables the input fields when a saved card is selected" do + # disables the input fields when a saved card is selected" do select "Visa x-1111 Exp:01/2025", from: "selected_card" page.should have_css "#secrets\\.card_number[disabled]" - end - it "allows use of a saved card" do - select "Visa x-1111 Exp:01/2025", from: "selected_card" + # allows checkout place_order page.should have_content "Your order has been processed successfully" end diff --git a/spec/helpers/enterprises_helper_spec.rb b/spec/helpers/enterprises_helper_spec.rb index 8e8fa3ed5e8a..ebfaa8d890f6 100644 --- a/spec/helpers/enterprises_helper_spec.rb +++ b/spec/helpers/enterprises_helper_spec.rb @@ -219,5 +219,26 @@ end end end + + context "when StripeConnect payment methods are present" do + let!(:pm3) { create(:payment_method, type: "Spree::Gateway::StripeConnect", distributors: [distributor])} + before { allow(helper).to receive(:current_distributor) { distributor } } + + context "and Stripe Connect is disabled" do + before { Spree::Config.set({stripe_connect_enabled: false}) } + + it "ignores the Stripe payment method" do + expect(helper.available_payment_methods.map(&:id)).to_not include pm3.id + end + end + + context "and Stripe Connect is enabled" do + before { Spree::Config.set({stripe_connect_enabled: true}) } + + it "includes the Stripe payment method" do + expect(helper.available_payment_methods.map(&:id)).to include pm3.id + end + end + end end end