From 4c3f87f047e51772978bc2793cb3d86fbf1832a1 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 15 Nov 2023 12:10:35 +0000 Subject: [PATCH] Adds payment intent tests with invalid cards --- .../stripe/payment_intent_validator_spec.rb | 98 ++++++++++--------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/spec/lib/stripe/payment_intent_validator_spec.rb b/spec/lib/stripe/payment_intent_validator_spec.rb index 3f1e246a8f..4f75389b4a 100644 --- a/spec/lib/stripe/payment_intent_validator_spec.rb +++ b/spec/lib/stripe/payment_intent_validator_spec.rb @@ -11,13 +11,12 @@ module Stripe create(:stripe_sca_payment_method, distributor_ids: [create(:distributor_enterprise).id], preferred_enterprise_id: create(:enterprise).id) } - let(:source) { - create(:credit_card) - } + let(:source) { create(:credit_card) } + let(:year_valid) { Time.zone.now.year.next } - before do + before { Stripe.api_key = secret - end + } describe "#call", :vcr, :stripe_version do let(:payment) { @@ -33,12 +32,11 @@ module Stripe card: { number: '4242424242424242', exp_month: 12, - exp_year: 2034, + exp_year: year_valid, cvc: '314', }, }) end - let!(:payment_intent) do Stripe::PaymentIntent.create({ amount: 100, @@ -48,7 +46,6 @@ module Stripe capture_method: 'manual', }) end - let(:payment_intent_response_body) { [id: payment_intent.id, status: payment_intent.status] } @@ -56,7 +53,6 @@ module Stripe before do Stripe::PaymentIntent.confirm(payment_intent.id) end - it "returns payment intent id and does not raise" do expect { result = validator.call @@ -64,43 +60,55 @@ module Stripe }.to_not raise_error Stripe::StripeError end end - - context "when payment intent contains an error" do - let!(:pm_card) do - Stripe::PaymentMethod.create({ - type: 'card', - card: { - # decline code: insufficient_funds - number: '4000000000009995', - exp_month: 12, - exp_year: 2034, - cvc: '314', - }, - }) + context "when payment intent is invalid" do + shared_examples "payments intents" do |card_type, card_number, error_message| + context "from #{card_type}" do + let!(:pm_card) do + Stripe::PaymentMethod.create({ + type: 'card', + card: { + number: card_number, + exp_month: 12, + exp_year: year_valid, + cvc: '314', + }, + }) + end + let(:payment_intent) do + Stripe::PaymentIntent.create({ + amount: 100, + currency: 'eur', + payment_method: pm_card, + payment_method_types: ['card'], + capture_method: 'manual', + }) + end + it "raises Stripe error with payment intent last_payment_error as message" do + expect { + Stripe::PaymentIntent.confirm(payment_intent.id) + }.to raise_error Stripe::StripeError, error_message + end + end end - - let!(:payment_intent) do - Stripe::PaymentIntent.create({ - amount: 100, - currency: 'eur', - payment_method: pm_card, - payment_method_types: ['card'], - capture_method: 'manual', - }) - end - - let(:payment_intent_response_body) { - JSON.generate(id: payment_intent_id, last_payment_error: { message: "No money" }) - } - - it "raises Stripe error with payment intent last_payment_error as message" do - expect { - Stripe::PaymentIntent.confirm(payment_intent.id) - }.to raise_error Stripe::StripeError, "Your card has insufficient funds." - - expect { - validator.call - }.to raise_error Stripe::StripeError, "Your card has insufficient funds." + context "invalid credit cards are correctly handled" do + it_behaves_like "payments intents", "Generic decline", 4_000_000_000_000_002, + "Your card was declined." + it_behaves_like "payments intents", "Insufficient funds decline", 4_000_000_000_009_995, + "Your card has insufficient funds." + it_behaves_like "payments intents", "Lost card decline", 4_000_000_000_009_987, + "Your card was declined." + it_behaves_like "payments intents", "Stolen card decline", 4_000_000_000_009_979, + "Your card was declined." + it_behaves_like "payments intents", "Expired card decline", 4_000_000_000_000_069, + "Your card has expired." + it_behaves_like "payments intents", "Incorrect CVC decline", 4_000_000_000_000_127, + "Your card's security code is incorrect." + it_behaves_like "payments intents", "Processing error decline", 4_000_000_000_000_119, + "An error occurred while processing your card. Try again in a little bit." + it_behaves_like "payments intents", "Exceeding velocity limit decline", + 4_000_000_000_006_975, + %(Your card was declined for making repeated attempts too frequently + or exceeding its amount limit.).squish end end end