Implementing the Stripe Connect feature toggle

This commit is contained in:
Rob Harrington
2017-06-22 09:38:48 +10:00
parent 6499d17cb1
commit f890927423
12 changed files with 89 additions and 38 deletions

View File

@@ -30,6 +30,7 @@ module Admin
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

View File

@@ -57,12 +57,19 @@ module Spree
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

View File

@@ -21,6 +21,7 @@ module EnterprisesHelper
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)

View File

@@ -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

View File

@@ -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")

View File

@@ -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" }

View File

@@ -2109,6 +2109,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

View File

@@ -46,6 +46,7 @@ describe Admin::StripeAccountsController, type: :controller do
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 @@ describe Admin::StripeAccountsController, type: :controller do
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

View File

@@ -40,6 +40,7 @@ feature %q{
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)

View File

@@ -9,6 +9,8 @@ feature "Credit Cards", js: true do
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"}))

View File

@@ -157,6 +157,7 @@ feature "As a consumer I want to check out my cart", js: true, retry: 3 do
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 @@ feature "As a consumer I want to check out my cart", js: true, retry: 3 do
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

View File

@@ -219,5 +219,26 @@ describe EnterprisesHelper do
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