diff --git a/app/models/spree/gateway/stripe_connect.rb b/app/models/spree/gateway/stripe_connect.rb index f4a0435ace..6efd2f79ef 100644 --- a/app/models/spree/gateway/stripe_connect.rb +++ b/app/models/spree/gateway/stripe_connect.rb @@ -33,6 +33,9 @@ module Spree def purchase(money, creditcard, gateway_options) provider.purchase(*options_for_purchase_or_auth(money, creditcard, gateway_options)) + rescue Stripe::StripeError => e + # This will be an error caused by generating a stripe token + failed_activemerchant_billing_response(e.message) end def void(response_code, _creditcard, gateway_options) @@ -88,9 +91,10 @@ module Spree def tokenize_instance_customer_card(customer, card) token = Stripe::Token.create({card: card, customer: customer}, stripe_account: stripe_account_id) token.id - rescue Stripe::StripeError => e - Rails.logger.error("Stripe Error: #{e}") - nil + end + + def failed_activemerchant_billing_response(error_message) + ActiveMerchant::Billing::Response.new(false, error_message) end def ensure_enterprise_selected diff --git a/spec/requests/stripe_connect_checkout_spec.rb b/spec/requests/stripe_connect_checkout_spec.rb index 2bd2909cf0..b3d21afdd5 100644 --- a/spec/requests/stripe_connect_checkout_spec.rb +++ b/spec/requests/stripe_connect_checkout_spec.rb @@ -62,13 +62,13 @@ describe "Submitting Stripe Connect charge requests", type: :request do end context "when the charge request returns an error message" do - let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } + let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "charge-failure"}) } } it "should not process the payment" do put update_checkout_path, params expect(response.status).to be 400 json_response = JSON.parse(response.body) - expect(json_response["flash"]["error"]).to eq "Bup-bow..." + expect(json_response["flash"]["error"]).to eq "charge-failure" expect(order.payments.completed.count).to be 0 end end @@ -110,44 +110,38 @@ describe "Submitting Stripe Connect charge requests", type: :request do end context "when the store request returns an error message" do - let(:store_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } + let(:store_response_mock) { { status: 402, body: JSON.generate(error: { message: "store-failure"}) } } it "should not process the payment" do put update_checkout_path, params expect(response.status).to be 400 json_response = JSON.parse(response.body) - expect(json_response["flash"]["error"]).to eq I18n.t(:spree_gateway_error_flash_for_checkout, error: 'Bup-bow...') + expect(json_response["flash"]["error"]).to eq I18n.t(:spree_gateway_error_flash_for_checkout, error: 'store-failure') expect(order.payments.completed.count).to be 0 end end context "when the charge request returns an error message" do - let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } + let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "charge-failure"}) } } it "should not process the payment" do put update_checkout_path, params expect(response.status).to be 400 json_response = JSON.parse(response.body) - expect(json_response["flash"]["error"]).to eq "Bup-bow..." + expect(json_response["flash"]["error"]).to eq "charge-failure" expect(order.payments.completed.count).to be 0 end end context "when the token request returns an error message" do - let(:token_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } - let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } - - before do - # Attempts to charge the card without a token, which will return an error - stub_request(:post, "https://sk_test_12345:@api.stripe.com/v1/charges") - .with(body: /#{order.number}/).to_return(charge_response_mock) - end + let(:token_response_mock) { { status: 402, body: JSON.generate(error: { message: "token-failure"}) } } + # Note, no requests have been stubbed it "should not process the payment" do put update_checkout_path, params expect(response.status).to be 400 json_response = JSON.parse(response.body) - expect(json_response["flash"]["error"]).to eq "Bup-bow..." + expect(json_response["flash"]["error"]).to eq "token-failure" expect(order.payments.completed.count).to be 0 end end @@ -203,32 +197,25 @@ describe "Submitting Stripe Connect charge requests", type: :request do end context "when the charge request returns an error message" do - let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } + let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "charge-failure"}) } } it "should not process the payment" do put update_checkout_path, params expect(response.status).to be 400 json_response = JSON.parse(response.body) - expect(json_response["flash"]["error"]).to eq "Bup-bow..." + expect(json_response["flash"]["error"]).to eq "charge-failure" expect(order.payments.completed.count).to be 0 end end context "when the token request returns an error message" do - let(:token_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } - let(:charge_response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..."}) } } - - before do - # Attempts to charge the card without a token, which will return an error - stub_request(:post, "https://sk_test_12345:@api.stripe.com/v1/charges") - .with(body: /#{order.number}/).to_return(charge_response_mock) - end + let(:token_response_mock) { { status: 402, body: JSON.generate(error: { message: "token-error"}) } } it "should not process the payment" do put update_checkout_path, params expect(response.status).to be 400 json_response = JSON.parse(response.body) - expect(json_response["flash"]["error"]).to eq "Bup-bow..." + expect(json_response["flash"]["error"]).to eq "token-error" expect(order.payments.completed.count).to be 0 end end