From 4690cb102bf782a24caf43af84c19385f5f3ba04 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 21 Sep 2022 17:31:10 +0100 Subject: [PATCH] Adds sets-up the cassettes correctly and other configs deletes unecessary files Reverts helper option to turn VCR off --- .env.test | 1 + .../spree/credit_cards_controller_spec.rb | 382 ++++++++++-------- .../saves_the_card_locally.yml | 340 ++++++++++++++++ .../creates_a_payment_intent.yml | 130 ------ .../makes_a_payment.yml | 189 --------- spec/requests/checkout/stripe_sca_vcr_spec.rb | 42 -- spec/support/vcr_setup.rb | 4 +- 7 files changed, 549 insertions(+), 539 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/Spree_CreditCardsController/using_VCR/_new_from_token/when_the_request_to_store_the_customer/card_with_Stripe_is_successful/saves_the_card_locally.yml delete mode 100644 spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/creates_a_payment_intent.yml delete mode 100644 spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/makes_a_payment.yml delete mode 100644 spec/requests/checkout/stripe_sca_vcr_spec.rb diff --git a/.env.test b/.env.test index 75cb9063eb..2729e942fd 100644 --- a/.env.test +++ b/.env.test @@ -3,5 +3,6 @@ SECRET_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" STRIPE_SECRET_TEST_API_KEY="bogus_key" +STRIPE_CUSTOMER="bogus_customer" SITE_URL="test.host" diff --git a/spec/controllers/spree/credit_cards_controller_spec.rb b/spec/controllers/spree/credit_cards_controller_spec.rb index 602d384038..451a75f4e0 100644 --- a/spec/controllers/spree/credit_cards_controller_spec.rb +++ b/spec/controllers/spree/credit_cards_controller_spec.rb @@ -3,202 +3,151 @@ require 'spec_helper' describe Spree::CreditCardsController, type: :controller do - let(:user) { create(:user) } - let(:token) { "tok_234bd2c22" } - - before do - Stripe.api_key = "sk_test_12345" - allow(controller).to receive(:spree_current_user) { user } - end - - describe "#new_from_token" do - let(:params) do - { - format: :json, - exp_month: 12, - exp_year: Time.now.year.next, - last4: 4242, - token: token, - cc_type: "visa" - } - end + describe "using VCR", :vcr do + let(:user) { create(:user) } + let(:secret) { ENV['STRIPE_SECRET_TEST_API_KEY'] } before do - stub_request(:post, "https://api.stripe.com/v1/customers") - .with(body: { email: user.email, source: token }) - .to_return(response_mock) + Stripe.api_key = secret + allow(controller).to receive(:spree_current_user) { user } end - context "when the request to store the customer/card with Stripe is successful" do - let(:response_mock) { - { status: 200, body: JSON.generate(id: "cus_AZNMJ", default_source: "card_1AEEb") } - } - - it "saves the card locally" do - spree_post :new_from_token, params - - expect{ spree_post :new_from_token, params }.to change(Spree::CreditCard, :count).by(1) - - card = Spree::CreditCard.last - expect(card.gateway_payment_profile_id).to eq "card_1AEEb" - expect(card.gateway_customer_profile_id).to eq "cus_AZNMJ" - expect(card.user_id).to eq user.id - expect(card.last_digits).to eq "4242" + describe "#new_from_token" do + let!(:token) do + Stripe::Token.create({ + card: { + number: '4242424242424242', + exp_month: 9, + exp_year: 2024, + cvc: '314', + }, + }) end - - context "when saving the card locally fails" do - before do - allow(controller).to receive(:stored_card_attributes) { {} } + context "when the request to store the customer/card with Stripe is successful" do + let(:params) do + { + format: :json, + exp_month: 9, + exp_year: 2024, + last4: 4242, + token: token['id'], + cc_type: "visa" + } end - it "renders a flash error" do + before do + # there should be no cards stored locally + expect(Spree::CreditCard.count).to eq(0) + end + + it "saves the card locally" do + spree_post :new_from_token, params + + # checks whether a card was created + expect(Spree::CreditCard.count).to eq(1) + card = Spree::CreditCard.last + + # retrieves the created card from Stripe + stripe_card = Stripe::Customer.list_sources( + card.gateway_customer_profile_id, + { object: 'card', limit: 1 }, + ) + + payment_profile = stripe_card['data'][0]['id'] + customer_profile = stripe_card['data'][0]['customer'] + + expect(card.gateway_payment_profile_id).to eq payment_profile + expect(card.gateway_customer_profile_id).to eq customer_profile + expect(card.user_id).to eq user.id + expect(card.last_digits).to eq "4242" + end + end + end + end + + describe "not using VCR" do + let(:user) { create(:user) } + let(:token) { "tok_234bd2c22" } + + before do + Stripe.api_key = "sk_test_12345" + allow(controller).to receive(:spree_current_user) { user } + end + describe "#new_from_token" do + let(:params) do + { + format: :json, + exp_month: 12, + exp_year: Time.now.year.next, + last4: 4242, + token: token, + cc_type: "visa" + } + end + + before do + stub_request(:post, "https://api.stripe.com/v1/customers") + .with(body: { email: user.email, source: token }) + .to_return(response_mock) + end + + context "when the request to store the customer/card with Stripe fails" do + let(:response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..." }) } } + it "doesn't save the card locally, and renders a flash error" do expect{ spree_post :new_from_token, params }.to_not change(Spree::CreditCard, :count) json_response = JSON.parse(response.body) - flash_message = I18n.t(:spree_gateway_error_flash_for_checkout, - error: I18n.t(:card_could_not_be_saved)) + flash_message = I18n.t(:spree_gateway_error_flash_for_checkout, error: "Bup-bow...") expect(json_response["flash"]["error"]).to eq flash_message end end end - context "when the request to store the customer/card with Stripe fails" do - let(:response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..." }) } } - it "doesn't save the card locally, and renders a flash error" do - expect{ spree_post :new_from_token, params }.to_not change(Spree::CreditCard, :count) + describe "#update card to be the default card" do + let(:params) { { format: :json, credit_card: { is_default: true } } } + context "when the specified credit card is not found" do + before { params[:id] = 123 } - json_response = JSON.parse(response.body) - flash_message = I18n.t(:spree_gateway_error_flash_for_checkout, error: "Bup-bow...") - expect(json_response["flash"]["error"]).to eq flash_message - end - end - end - - describe "#update card to be the default card" do - let(:params) { { format: :json, credit_card: { is_default: true } } } - context "when the specified credit card is not found" do - before { params[:id] = 123 } - - it "renders a flash error" do - spree_put :update, params - json_response = JSON.parse(response.body) - expect(json_response['flash']['error']).to eq I18n.t(:card_could_not_be_updated) - end - end - - context "when the specified credit card is found" do - let!(:card) { create(:credit_card, gateway_customer_profile_id: 'cus_AZNMJ') } - before { params[:id] = card.id } - - context "but the card is not owned by the user" do - it "redirects to unauthorized" do + it "renders a flash error" do spree_put :update, params - expect(response).to redirect_to unauthorized_path + json_response = JSON.parse(response.body) + expect(json_response['flash']['error']).to eq I18n.t(:card_could_not_be_updated) end end - context "and the card is owned by the user" do - before { card.update_attribute(:user_id, user.id) } + context "when the specified credit card is found" do + let!(:card) { create(:credit_card, gateway_customer_profile_id: 'cus_AZNMJ') } + before { params[:id] = card.id } - context "when the update completes successfully" do - it "renders a serialized copy of the updated card" do - expect{ spree_put :update, params }.to change { card.reload.is_default }.to(true) - json_response = JSON.parse(response.body) - expect(json_response['id']).to eq card.id - expect(json_response['is_default']).to eq true - end - end - - context "when the update fails" do - before { params[:credit_card][:month] = 'some illegal month' } - it "renders an error" do + context "but the card is not owned by the user" do + it "redirects to unauthorized" do spree_put :update, params - json_response = JSON.parse(response.body) - expect(json_response['flash']['error']).to eq I18n.t(:card_could_not_be_updated) + expect(response).to redirect_to unauthorized_path end end - context "and there are existing authorizations for the user" do - let!(:customer1) { create(:customer, allow_charges: true) } - let!(:customer2) { create(:customer, allow_charges: true) } + context "and the card is owned by the user" do + before { card.update_attribute(:user_id, user.id) } - it "removes the authorizations" do - customer1.user = card.user - customer2.user = card.user - customer1.save - customer2.save - expect(customer1.reload.allow_charges).to be true - expect(customer2.reload.allow_charges).to be true - spree_put :update, params - expect(customer1.reload.allow_charges).to be false - expect(customer2.reload.allow_charges).to be false - end - end - end - end - end - - describe "#destroy" do - context "when the specified credit card is not found" do - let(:params) { { id: 123 } } - - it "redirects to /account with a flash error, does not request deletion with Stripe" do - expect(controller).to_not receive(:destroy_at_stripe) - spree_delete :destroy, params - expect(flash[:error]).to eq I18n.t(:card_could_not_be_removed) - expect(response.status).to eq 200 - end - end - - context "when the specified credit card is found" do - let!(:card) { create(:credit_card, gateway_customer_profile_id: 'cus_AZNMJ') } - let(:params) { { id: card.id } } - - context "but the card is not owned by the user" do - it "redirects to unauthorized" do - spree_delete :destroy, params - expect(response).to redirect_to unauthorized_path - end - end - - context "and the card is owned by the user" do - before do - card.update_attribute(:user_id, user.id) - - stub_request(:get, "https://api.stripe.com/v1/customers/cus_AZNMJ"). - to_return(status: 200, body: JSON.generate(id: "cus_AZNMJ")) - end - - context "where the request to destroy the Stripe customer fails" do - before do - stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ"). - to_return(status: 402, body: JSON.generate(error: { message: 'Bup-bow!' })) - end - - it "doesn't delete the card" do - expect{ spree_delete :destroy, params }.to_not change(Spree::CreditCard, :count) - expect(flash[:error]).to eq I18n.t(:card_could_not_be_removed) - expect(response.status).to eq 422 - end - end - - context "where the request to destroy the Stripe customer succeeds" do - before do - stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ"). - to_return(status: 200, body: JSON.generate(deleted: true, id: "cus_AZNMJ")) - end - - it "deletes the card and redirects to account_path" do - expect{ spree_delete :destroy, params }.to change(Spree::CreditCard, :count).by(-1) - expect(flash[:success]).to eq I18n.t(:card_has_been_removed, - number: "x-#{card.last_digits}") - expect(response.status).to eq 200 - end - - context "the card is the default card and there are existing authorizations for the user" do - before do - card.update_attribute(:is_default, true) + context "when the update completes successfully" do + it "renders a serialized copy of the updated card" do + expect{ spree_put :update, params }.to change { card.reload.is_default }.to(true) + json_response = JSON.parse(response.body) + expect(json_response['id']).to eq card.id + expect(json_response['is_default']).to eq true end + end + + context "when the update fails" do + before { params[:credit_card][:month] = 'some illegal month' } + it "renders an error" do + spree_put :update, params + json_response = JSON.parse(response.body) + expect(json_response['flash']['error']).to eq I18n.t(:card_could_not_be_updated) + end + end + + context "and there are existing authorizations for the user" do let!(:customer1) { create(:customer, allow_charges: true) } let!(:customer2) { create(:customer, allow_charges: true) } @@ -209,19 +158,100 @@ describe Spree::CreditCardsController, type: :controller do customer2.save expect(customer1.reload.allow_charges).to be true expect(customer2.reload.allow_charges).to be true - spree_delete :destroy, params + spree_put :update, params expect(customer1.reload.allow_charges).to be false expect(customer2.reload.allow_charges).to be false end + end + end + end + end - context "when has any other saved cards" do - let!(:second_card) { - create(:stored_credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_AZNMJ') - } + describe "#destroy" do + context "when the specified credit card is not found" do + let(:params) { { id: 123 } } - it "should assign the second one as the default one" do + it "redirects to /account with a flash error, does not request deletion with Stripe" do + expect(controller).to_not receive(:destroy_at_stripe) + spree_delete :destroy, params + expect(flash[:error]).to eq I18n.t(:card_could_not_be_removed) + expect(response.status).to eq 200 + end + end + + context "when the specified credit card is found" do + let!(:card) { create(:credit_card, gateway_customer_profile_id: 'cus_AZNMJ') } + let(:params) { { id: card.id } } + + context "but the card is not owned by the user" do + it "redirects to unauthorized" do + spree_delete :destroy, params + expect(response).to redirect_to unauthorized_path + end + end + + context "and the card is owned by the user" do + before do + card.update_attribute(:user_id, user.id) + + stub_request(:get, "https://api.stripe.com/v1/customers/cus_AZNMJ"). + to_return(status: 200, body: JSON.generate(id: "cus_AZNMJ")) + end + + context "where the request to destroy the Stripe customer fails" do + before do + stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ"). + to_return(status: 402, body: JSON.generate(error: { message: 'Bup-bow!' })) + end + + it "doesn't delete the card" do + expect{ spree_delete :destroy, params }.to_not change(Spree::CreditCard, :count) + expect(flash[:error]).to eq I18n.t(:card_could_not_be_removed) + expect(response.status).to eq 422 + end + end + + context "where the request to destroy the Stripe customer succeeds" do + before do + stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ"). + to_return(status: 200, body: JSON.generate(deleted: true, id: "cus_AZNMJ")) + end + + it "deletes the card and redirects to account_path" do + expect{ spree_delete :destroy, params }.to change(Spree::CreditCard, :count).by(-1) + expect(flash[:success]).to eq I18n.t(:card_has_been_removed, + number: "x-#{card.last_digits}") + expect(response.status).to eq 200 + end + + context "the card is the default card and there are existing authorizations for the user" do + before do + card.update_attribute(:is_default, true) + end + let!(:customer1) { create(:customer, allow_charges: true) } + let!(:customer2) { create(:customer, allow_charges: true) } + + it "removes the authorizations" do + customer1.user = card.user + customer2.user = card.user + customer1.save + customer2.save + expect(customer1.reload.allow_charges).to be true + expect(customer2.reload.allow_charges).to be true spree_delete :destroy, params - expect(Spree::CreditCard.find_by(id: second_card.id).is_default).to eq true + expect(customer1.reload.allow_charges).to be false + expect(customer2.reload.allow_charges).to be false + end + + context "when has any other saved cards" do + let!(:second_card) { + create(:stored_credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_AZNMJ') + } + + it "should assign the second one as the default one" do + spree_delete :destroy, params + expect(Spree::CreditCard.find_by(id: second_card.id).is_default).to eq true + end end end end diff --git a/spec/fixtures/vcr_cassettes/Spree_CreditCardsController/using_VCR/_new_from_token/when_the_request_to_store_the_customer/card_with_Stripe_is_successful/saves_the_card_locally.yml b/spec/fixtures/vcr_cassettes/Spree_CreditCardsController/using_VCR/_new_from_token/when_the_request_to_store_the_customer/card_with_Stripe_is_successful/saves_the_card_locally.yml new file mode 100644 index 0000000000..7e2bba6ffd --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Spree_CreditCardsController/using_VCR/_new_from_token/when_the_request_to_store_the_customer/card_with_Stripe_is_successful/saves_the_card_locally.yml @@ -0,0 +1,340 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.stripe.com/v1/tokens + body: + encoding: UTF-8 + string: card[number]=4242424242424242&card[exp_month]=9&card[exp_year]=2024&card[cvc]=314 + headers: + User-Agent: + - Stripe/v1 RubyBindings/7.1.0 + Authorization: + - Bearer + Content-Type: + - application/x-www-form-urlencoded + X-Stripe-Client-User-Agent: + - '{"bindings_version":"7.1.0","lang":"ruby","lang_version":"3.0.3 p157 (2021-11-24)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux + version 5.15.0-48-generic (buildd@lcy02-amd64-080) (gcc (Ubuntu 11.2.0-19ubuntu1) + 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #54-Ubuntu SMP Fri Aug 26 13:26:29 + UTC 2022","hostname":"ff-LAT"}' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Tue, 27 Sep 2022 06:15:03 GMT + Content-Type: + - application/json + Content-Length: + - '781' + Connection: + - keep-alive + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST, HEAD, OPTIONS, DELETE + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required + Access-Control-Max-Age: + - '300' + Cache-Control: + - no-cache, no-store + Idempotency-Key: + - 8a2c6373-a053-4674-8f35-5e5b8d9cf8fb + Original-Request: + - req_4XXW0nwQlmbWiH + Request-Id: + - req_4XXW0nwQlmbWiH + Stripe-Should-Retry: + - 'false' + Stripe-Version: + - '2019-11-05' + Strict-Transport-Security: + - max-age=63072000; includeSubDomains; preload + body: + encoding: UTF-8 + string: |- + { + "id": "tok_1LmX4NKuuB1fWySnNTd7lIuK", + "object": "token", + "card": { + "id": "card_1LmX4MKuuB1fWySn6nKumo1M", + "object": "card", + "address_city": null, + "address_country": null, + "address_line1": null, + "address_line1_check": null, + "address_line2": null, + "address_state": null, + "address_zip": null, + "address_zip_check": null, + "brand": "Visa", + "country": "US", + "cvc_check": "unchecked", + "dynamic_last4": null, + "exp_month": 9, + "exp_year": 2024, + "fingerprint": "6E6tgVjx6U65iHFV", + "funding": "credit", + "last4": "4242", + "metadata": {}, + "name": null, + "tokenization_method": null + }, + "client_ip": "188.251.215.147", + "created": 1664259303, + "livemode": false, + "type": "card", + "used": false + } + recorded_at: Tue, 27 Sep 2022 06:15:03 GMT +- request: + method: post + uri: https://api.stripe.com/v1/customers + body: + encoding: UTF-8 + string: email=emerita_stroman%40gusikowski.ca&source=tok_1LmX4NKuuB1fWySnNTd7lIuK + headers: + User-Agent: + - Stripe/v1 RubyBindings/7.1.0 + Authorization: + - Bearer + Content-Type: + - application/x-www-form-urlencoded + X-Stripe-Client-Telemetry: + - '{"last_request_metrics":{"request_id":"req_4XXW0nwQlmbWiH","request_duration_ms":725}}' + X-Stripe-Client-User-Agent: + - '{"bindings_version":"7.1.0","lang":"ruby","lang_version":"3.0.3 p157 (2021-11-24)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux + version 5.15.0-48-generic (buildd@lcy02-amd64-080) (gcc (Ubuntu 11.2.0-19ubuntu1) + 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #54-Ubuntu SMP Fri Aug 26 13:26:29 + UTC 2022","hostname":"ff-LAT"}' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Tue, 27 Sep 2022 06:15:04 GMT + Content-Type: + - application/json + Content-Length: + - '1964' + Connection: + - keep-alive + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST, HEAD, OPTIONS, DELETE + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required + Access-Control-Max-Age: + - '300' + Cache-Control: + - no-cache, no-store + Idempotency-Key: + - a9c15df5-51a9-4aa3-9a98-89346ac4220f + Original-Request: + - req_Bv2J8ejL6FSWwT + Request-Id: + - req_Bv2J8ejL6FSWwT + Stripe-Should-Retry: + - 'false' + Stripe-Version: + - '2019-11-05' + Strict-Transport-Security: + - max-age=63072000; includeSubDomains; preload + body: + encoding: UTF-8 + string: |- + { + "id": "cus_MVYAeGp44F6wS3", + "object": "customer", + "address": null, + "balance": 0, + "created": 1664259304, + "currency": null, + "default_currency": null, + "default_source": "card_1LmX4MKuuB1fWySn6nKumo1M", + "delinquent": false, + "description": null, + "discount": null, + "email": "emerita_stroman@gusikowski.ca", + "invoice_prefix": "D81408D1", + "invoice_settings": { + "custom_fields": null, + "default_payment_method": null, + "footer": null, + "rendering_options": null + }, + "livemode": false, + "metadata": {}, + "name": null, + "next_invoice_sequence": 1, + "phone": null, + "preferred_locales": [], + "shipping": null, + "sources": { + "object": "list", + "data": [ + { + "id": "card_1LmX4MKuuB1fWySn6nKumo1M", + "object": "card", + "address_city": null, + "address_country": null, + "address_line1": null, + "address_line1_check": null, + "address_line2": null, + "address_state": null, + "address_zip": null, + "address_zip_check": null, + "brand": "Visa", + "country": "US", + "customer": "cus_MVYAeGp44F6wS3", + "cvc_check": "pass", + "dynamic_last4": null, + "exp_month": 9, + "exp_year": 2024, + "fingerprint": "6E6tgVjx6U65iHFV", + "funding": "credit", + "last4": "4242", + "metadata": {}, + "name": null, + "tokenization_method": null + } + ], + "has_more": false, + "total_count": 1, + "url": "/v1/customers/cus_MVYAeGp44F6wS3/sources" + }, + "subscriptions": { + "object": "list", + "data": [], + "has_more": false, + "total_count": 0, + "url": "/v1/customers/cus_MVYAeGp44F6wS3/subscriptions" + }, + "tax_exempt": "none", + "tax_ids": { + "object": "list", + "data": [], + "has_more": false, + "total_count": 0, + "url": "/v1/customers/cus_MVYAeGp44F6wS3/tax_ids" + }, + "tax_info": null, + "tax_info_verification": null, + "test_clock": null + } + recorded_at: Tue, 27 Sep 2022 06:15:04 GMT +- request: + method: get + uri: https://api.stripe.com/v1/customers/cus_MVYAeGp44F6wS3/sources?limit=1&object=card + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Stripe/v1 RubyBindings/7.1.0 + Authorization: + - Bearer + Content-Type: + - application/x-www-form-urlencoded + X-Stripe-Client-Telemetry: + - '{"last_request_metrics":{"request_id":"req_Bv2J8ejL6FSWwT","request_duration_ms":1037}}' + X-Stripe-Client-User-Agent: + - '{"bindings_version":"7.1.0","lang":"ruby","lang_version":"3.0.3 p157 (2021-11-24)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux + version 5.15.0-48-generic (buildd@lcy02-amd64-080) (gcc (Ubuntu 11.2.0-19ubuntu1) + 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #54-Ubuntu SMP Fri Aug 26 13:26:29 + UTC 2022","hostname":"ff-LAT"}' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Tue, 27 Sep 2022 06:15:05 GMT + Content-Type: + - application/json + Content-Length: + - '790' + Connection: + - keep-alive + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST, HEAD, OPTIONS, DELETE + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required + Access-Control-Max-Age: + - '300' + Cache-Control: + - no-cache, no-store + Request-Id: + - req_4NyryTpY3ov7vf + Stripe-Version: + - '2019-11-05' + Strict-Transport-Security: + - max-age=63072000; includeSubDomains; preload + body: + encoding: UTF-8 + string: |- + { + "object": "list", + "data": [ + { + "id": "card_1LmX4MKuuB1fWySn6nKumo1M", + "object": "card", + "address_city": null, + "address_country": null, + "address_line1": null, + "address_line1_check": null, + "address_line2": null, + "address_state": null, + "address_zip": null, + "address_zip_check": null, + "brand": "Visa", + "country": "US", + "customer": "cus_MVYAeGp44F6wS3", + "cvc_check": "pass", + "dynamic_last4": null, + "exp_month": 9, + "exp_year": 2024, + "fingerprint": "6E6tgVjx6U65iHFV", + "funding": "credit", + "last4": "4242", + "metadata": {}, + "name": null, + "tokenization_method": null + } + ], + "has_more": false, + "url": "/v1/customers/cus_MVYAeGp44F6wS3/sources" + } + recorded_at: Tue, 27 Sep 2022 06:15:05 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/creates_a_payment_intent.yml b/spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/creates_a_payment_intent.yml deleted file mode 100644 index f6217e943f..0000000000 --- a/spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/creates_a_payment_intent.yml +++ /dev/null @@ -1,130 +0,0 @@ ---- -http_interactions: -- request: - method: post - uri: https://api.stripe.com/v1/payment_intents - body: - encoding: UTF-8 - string: amount=1099¤cy=usd&payment_method_types[0]=card&metadata[order_id]=6735 - headers: - User-Agent: - - Stripe/v1 RubyBindings/7.1.0 - Authorization: - - Bearer - Content-Type: - - application/x-www-form-urlencoded - X-Stripe-Client-Telemetry: - - '{"last_request_metrics":{"request_id":"req_topki4SXaxGGwg","request_duration_ms":1301}}' - X-Stripe-Client-User-Agent: - - '{"bindings_version":"7.1.0","lang":"ruby","lang_version":"3.0.3 p157 (2021-11-24)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux - version 5.15.0-47-generic (buildd@lcy02-amd64-060) (gcc (Ubuntu 11.2.0-19ubuntu1) - 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #51-Ubuntu SMP Thu Aug 11 07:51:15 - UTC 2022","hostname":"ff-LAT"}' - Accept-Encoding: - - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 - Accept: - - "*/*" - response: - status: - code: 200 - message: OK - headers: - Server: - - nginx - Date: - - Tue, 20 Sep 2022 16:48:31 GMT - Content-Type: - - application/json - Content-Length: - - '1448' - Connection: - - keep-alive - Access-Control-Allow-Credentials: - - 'true' - Access-Control-Allow-Methods: - - GET, POST, HEAD, OPTIONS, DELETE - Access-Control-Allow-Origin: - - "*" - Access-Control-Expose-Headers: - - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required - Access-Control-Max-Age: - - '300' - Cache-Control: - - no-cache, no-store - Idempotency-Key: - - 707e990e-8c39-4c2c-a041-d5837f66028a - Original-Request: - - req_BEYmwDryWum2vQ - Request-Id: - - req_BEYmwDryWum2vQ - Stripe-Should-Retry: - - 'false' - Stripe-Version: - - '2019-11-05' - Strict-Transport-Security: - - max-age=63072000; includeSubDomains; preload - body: - encoding: UTF-8 - string: |- - { - "id": "pi_3Lk9cZKuuB1fWySn0ESKNLJ8", - "object": "payment_intent", - "amount": 1099, - "amount_capturable": 0, - "amount_details": { - "tip": {} - }, - "amount_received": 0, - "application": null, - "application_fee_amount": null, - "automatic_payment_methods": null, - "canceled_at": null, - "cancellation_reason": null, - "capture_method": "automatic", - "charges": { - "object": "list", - "data": [], - "has_more": false, - "total_count": 0, - "url": "/v1/charges?payment_intent=pi_3Lk9cZKuuB1fWySn0ESKNLJ8" - }, - "client_secret": "pi_3Lk9cZKuuB1fWySn0ESKNLJ8_secret_5kWuLHVLm38pRmGgFEqlNkgB2", - "confirmation_method": "automatic", - "created": 1663692511, - "currency": "usd", - "customer": null, - "description": null, - "invoice": null, - "last_payment_error": null, - "livemode": false, - "metadata": { - "order_id": "6735" - }, - "next_action": null, - "on_behalf_of": null, - "payment_method": null, - "payment_method_options": { - "card": { - "installments": null, - "mandate_options": null, - "network": null, - "request_three_d_secure": "automatic" - } - }, - "payment_method_types": [ - "card" - ], - "processing": null, - "receipt_email": null, - "review": null, - "setup_future_usage": null, - "shipping": null, - "source": null, - "statement_descriptor": null, - "statement_descriptor_suffix": null, - "status": "requires_payment_method", - "transfer_data": null, - "transfer_group": null - } - recorded_at: Tue, 20 Sep 2022 16:48:31 GMT -recorded_with: VCR 6.1.0 diff --git a/spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/makes_a_payment.yml b/spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/makes_a_payment.yml deleted file mode 100644 index 3a6ba555ad..0000000000 --- a/spec/fixtures/vcr_cassettes/checking_out_an_order_with_a_Stripe_SCA_payment_method/when_the_user_submits_a_new_card_and_requests_that_the_card_is_saved_for_later/sends_a_request_to_stripe_API/makes_a_payment.yml +++ /dev/null @@ -1,189 +0,0 @@ ---- -http_interactions: -- request: - method: post - uri: https://api.stripe.com/v1/charges - body: - encoding: UTF-8 - string: amount=2000¤cy=usd&source=tok_visa&metadata[order_id]=6735 - headers: - User-Agent: - - Stripe/v1 RubyBindings/7.1.0 - Authorization: - - Bearer - Content-Type: - - application/x-www-form-urlencoded - X-Stripe-Client-User-Agent: - - '{"bindings_version":"7.1.0","lang":"ruby","lang_version":"3.0.3 p157 (2021-11-24)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux - version 5.15.0-47-generic (buildd@lcy02-amd64-060) (gcc (Ubuntu 11.2.0-19ubuntu1) - 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #51-Ubuntu SMP Thu Aug 11 07:51:15 - UTC 2022","hostname":"ff-LAT"}' - Accept-Encoding: - - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 - Accept: - - "*/*" - response: - status: - code: 200 - message: OK - headers: - Server: - - nginx - Date: - - Tue, 20 Sep 2022 16:48:30 GMT - Content-Type: - - application/json - Content-Length: - - '3033' - Connection: - - keep-alive - Access-Control-Allow-Credentials: - - 'true' - Access-Control-Allow-Methods: - - GET, POST, HEAD, OPTIONS, DELETE - Access-Control-Allow-Origin: - - "*" - Access-Control-Expose-Headers: - - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required - Access-Control-Max-Age: - - '300' - Cache-Control: - - no-cache, no-store - Idempotency-Key: - - 761bfeb4-7ee1-4c81-bf0d-1f85cc0949c0 - Original-Request: - - req_topki4SXaxGGwg - Request-Id: - - req_topki4SXaxGGwg - Stripe-Should-Retry: - - 'false' - Stripe-Version: - - '2019-11-05' - Strict-Transport-Security: - - max-age=63072000; includeSubDomains; preload - body: - encoding: UTF-8 - string: |- - { - "id": "ch_3Lk9cXKuuB1fWySn0TI8AMrw", - "object": "charge", - "amount": 2000, - "amount_captured": 2000, - "amount_refunded": 0, - "application": null, - "application_fee": null, - "application_fee_amount": null, - "balance_transaction": "txn_3Lk9cXKuuB1fWySn0Ko2bgNN", - "billing_details": { - "address": { - "city": null, - "country": null, - "line1": null, - "line2": null, - "postal_code": null, - "state": null - }, - "email": null, - "name": null, - "phone": null - }, - "calculated_statement_descriptor": "OFNOFNOFN", - "captured": true, - "created": 1663692509, - "currency": "usd", - "customer": null, - "description": null, - "destination": null, - "dispute": null, - "disputed": false, - "failure_balance_transaction": null, - "failure_code": null, - "failure_message": null, - "fraud_details": {}, - "invoice": null, - "livemode": false, - "metadata": { - "order_id": "6735" - }, - "on_behalf_of": null, - "order": null, - "outcome": { - "network_status": "approved_by_network", - "reason": null, - "risk_level": "normal", - "risk_score": 42, - "seller_message": "Payment complete.", - "type": "authorized" - }, - "paid": true, - "payment_intent": null, - "payment_method": "card_1Lk9cXKuuB1fWySnABaPXBws", - "payment_method_details": { - "card": { - "brand": "visa", - "checks": { - "address_line1_check": null, - "address_postal_code_check": null, - "cvc_check": null - }, - "country": "US", - "exp_month": 9, - "exp_year": 2023, - "fingerprint": "6E6tgVjx6U65iHFV", - "funding": "credit", - "installments": null, - "last4": "4242", - "mandate": null, - "network": "visa", - "three_d_secure": null, - "wallet": null - }, - "type": "card" - }, - "receipt_email": null, - "receipt_number": null, - "receipt_url": "https://pay.stripe.com/receipts/payment/CAcaFwoVYWNjdF8xRmlxRXNLdXVCMWZXeVNuKN7dp5kGMgbN8weGDbs6LBZM2-baa3d6OcpNkyFBlD9ntXsZkBiC7cOLrRBSiMjTMmSYDpspP88klsk0", - "refunded": false, - "refunds": { - "object": "list", - "data": [], - "has_more": false, - "total_count": 0, - "url": "/v1/charges/ch_3Lk9cXKuuB1fWySn0TI8AMrw/refunds" - }, - "review": null, - "shipping": null, - "source": { - "id": "card_1Lk9cXKuuB1fWySnABaPXBws", - "object": "card", - "address_city": null, - "address_country": null, - "address_line1": null, - "address_line1_check": null, - "address_line2": null, - "address_state": null, - "address_zip": null, - "address_zip_check": null, - "brand": "Visa", - "country": "US", - "customer": null, - "cvc_check": null, - "dynamic_last4": null, - "exp_month": 9, - "exp_year": 2023, - "fingerprint": "6E6tgVjx6U65iHFV", - "funding": "credit", - "last4": "4242", - "metadata": {}, - "name": null, - "tokenization_method": null - }, - "source_transfer": null, - "statement_descriptor": null, - "statement_descriptor_suffix": null, - "status": "succeeded", - "transfer_data": null, - "transfer_group": null - } - recorded_at: Tue, 20 Sep 2022 16:48:30 GMT -recorded_with: VCR 6.1.0 diff --git a/spec/requests/checkout/stripe_sca_vcr_spec.rb b/spec/requests/checkout/stripe_sca_vcr_spec.rb deleted file mode 100644 index 64e7106eeb..0000000000 --- a/spec/requests/checkout/stripe_sca_vcr_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'stripe' - -describe "checking out an order with a Stripe SCA payment method", type: :request do - include ShopWorkflow - include AuthenticationHelper - include OpenFoodNetwork::ApiHelper - include StripeHelper - include StripeStubs - - context "when the user submits a new card and requests that the card is saved for later" do - context "sends a request to stripe API", :vcr do - let(:secret) { ENV['STRIPE_SECRET_TEST_API_KEY'] } - - before do - Stripe.api_key = secret - end - - it "makes a payment" do - response = Stripe::Charge.create({ - amount: 2000, - currency: 'usd', - source: 'tok_visa', # obtained with Stripe.js - metadata: { order_id: '6735' }, - }) - end - - it "creates a payment intent" do - intent = Stripe::PaymentIntent.create({ - amount: 1099, - currency: 'usd', - payment_method_types: ['card'], - metadata: { - order_id: '6735', - }, - }) - end - end - end -end diff --git a/spec/support/vcr_setup.rb b/spec/support/vcr_setup.rb index 550e179f57..c4af4c19b5 100644 --- a/spec/support/vcr_setup.rb +++ b/spec/support/vcr_setup.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -ENV["RAILS_ENV"] ||= 'test' - require 'vcr' VCR.configure do |config| @@ -9,4 +7,6 @@ VCR.configure do |config| config.hook_into :webmock config.ignore_localhost = true config.configure_rspec_metadata! + config.filter_sensitive_data('') { ENV['STRIPE_SECRET_TEST_API_KEY'] } + config.filter_sensitive_data('') { ENV['STRIPE_CUSTOMER'] } end