Validate presence and auth of default card for customer

This commit is contained in:
Rob Harrington
2018-05-09 11:35:13 +10:00
committed by Maikel Linke
parent cf8ca1f8c1
commit 21c24eb69b
2 changed files with 16 additions and 16 deletions

View File

@@ -76,10 +76,11 @@ class SubscriptionValidator
end
def credit_card_ok?
return unless payment_method.andand.type == "Spree::Gateway::StripeConnect"
return errors.add(:credit_card, :blank) unless credit_card_id
return if customer.andand.user.andand.credit_card_ids.andand.include? credit_card_id
errors.add(:credit_card, :not_available)
return unless customer && payment_method
return unless payment_method.type == "Spree::Gateway::StripeConnect"
return errors.add(:credit_card, :charges_not_allowed) unless customer.allow_charges
return if customer.user.andand.default_card.present?
errors.add(:credit_card, :no_default_card)
end
def subscription_line_items_present?

View File

@@ -332,10 +332,10 @@ describe SubscriptionValidator do
context "when using the StripeConnect payment gateway" do
let(:payment_method) { instance_double(Spree::PaymentMethod, type: "Spree::Gateway::StripeConnect") }
before { expect(subscription).to receive(:credit_card_id).at_least(:once) { credit_card_id } }
before { expect(subscription).to receive(:customer).at_least(:once) { customer } }
context "when a credit card is not present" do
let(:credit_card_id) { nil }
context "when the customer does not allow charges" do
let(:customer) { instance_double(Customer, allow_charges: false) }
it "adds an error and returns false" do
expect(validator.valid?).to be false
@@ -343,12 +343,11 @@ describe SubscriptionValidator do
end
end
context "when a credit card is present" do
let(:credit_card_id) { 12 }
before { expect(subscription).to receive(:customer).at_least(:once) { customer } }
context "when the customer allows charges" do
let(:customer) { instance_double(Customer, allow_charges: true) }
context "and the customer is not associated with a user" do
let(:customer) { instance_double(Customer, user: nil) }
before { allow(customer).to receive(:user) { nil } }
it "adds an error and returns false" do
expect(validator.valid?).to be false
@@ -357,10 +356,10 @@ describe SubscriptionValidator do
end
context "and the customer is associated with a user" do
let(:customer) { instance_double(Customer, user: user) }
before { expect(customer).to receive(:user).once { user } }
context "and the user has no credit cards which match that specified" do
let(:user) { instance_double(Spree::User, credit_card_ids: [1, 2, 3, 4]) }
context "and the user has no default card set" do
let(:user) { instance_double(Spree::User, default_card: nil) }
it "adds an error and returns false" do
expect(validator.valid?).to be false
@@ -368,8 +367,8 @@ describe SubscriptionValidator do
end
end
context "and the user has a credit card which matches that specified" do
let(:user) { instance_double(Spree::User, credit_card_ids: [1, 2, 3, 12]) }
context "and the user has a default card set" do
let(:user) { instance_double(Spree::User, default_card: 'some card') }
it "returns true" do
expect(validator.valid?).to be true