Skip to content

Commit

Permalink
Implementing the Stripe Connect feature toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
oeoeaio committed Jun 22, 2017
1 parent e125ec8 commit 2c244e5
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 38 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/stripe_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/helpers/enterprises_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/enterprises/form/_payment_methods.html.haml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions app/views/spree/users/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 42 additions & 29 deletions spec/controllers/admin/stripe_account_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/features/admin/payment_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions spec/features/consumer/account/cards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))

Expand Down
11 changes: 5 additions & 6 deletions spec/features/consumer/shopping/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/helpers/enterprises_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2c244e5

Please sign in to comment.