Skip to content

Commit

Permalink
Figure out whether it's a debit or credit card.
Browse files Browse the repository at this point in the history
Couple of methods to determine whether it's a debit or credit card
that's just been used. Essentially, this is really to figure out whether
SagePay's merchant account are going to charge us a fixed fee or a
percentage, so that we can pass any percentage on.
  • Loading branch information
mathie committed Nov 22, 2012
1 parent c96940a commit 4f09f34
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/sage_pay/server/notification.rb
@@ -1,6 +1,15 @@
module SagePay
module Server
class Notification
class_attribute :personal_credit_card_types, :instance_writer => false
self.personal_credit_card_types = [ :visa, :mastercard ].freeze

class_attribute :commercial_card_types, :instance_writer => false
self.commercial_card_types = [ :american_express, :diners, :jcb ].freeze

class_attribute :debit_card_types, :instance_writer => false
self.debit_card_types = [ :visa_delta, :maestro, :visa_electron, :solo, :laser ].freeze

attr_reader :vps_protocol, :tx_type, :vendor_tx_code, :vps_tx_id,
:status, :status_detail, :tx_auth_no, :avs_cv2, :address_result,
:post_code_result, :cv2_result, :gift_aid, :threed_secure_status,
Expand Down Expand Up @@ -178,6 +187,22 @@ def valid_signature?
@calculated_hash == vps_signature
end

def credit_card?
personal_credit_card? || commercial_card?
end

def personal_credit_card?
personal_credit_card_types.include?(card_type)
end

def commercial_card?
commercial_card_types.include?(card_type)
end

def debit_card?
debit_card_types.include?(card_type)
end

def response(redirect_url)
if valid_signature?
SagePay::Server::NotificationResponse.new(:status => :ok, :redirect_url => redirect_url)
Expand Down
252 changes: 252 additions & 0 deletions spec/sage_pay/server/notification_spec.rb
Expand Up @@ -189,4 +189,256 @@
end
end
end

describe "divining different card types" do
describe 'VISA card type' do
before(:each) do
params = {
'CardType' => 'VISA'
}
@notification = Notification.from_params(params)
end

it 'is a credit card' do
@notification.should be_a_credit_card
end

it 'is a personal credit card' do
@notification.should be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is not a debit card' do
@notification.should_not be_a_debit_card
end
end

describe 'MC (MasterCard) card type' do
before(:each) do
params = {
'CardType' => 'MC'
}
@notification = Notification.from_params(params)
end

it 'is a credit card' do
@notification.should be_a_credit_card
end

it 'is a personal credit card' do
@notification.should be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is not a debit card' do
@notification.should_not be_a_debit_card
end
end

describe 'VISA Delta card type' do
before(:each) do
params = {
'CardType' => 'DELTA'
}
@notification = Notification.from_params(params)
end

it 'is not a credit card' do
@notification.should_not be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is a debit card' do
@notification.should be_a_debit_card
end
end

describe 'Solo card type' do
before(:each) do
params = {
'CardType' => 'SOLO'
}
@notification = Notification.from_params(params)
end

it 'is not a credit card' do
@notification.should_not be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is a debit card' do
@notification.should be_a_debit_card
end
end

describe 'Maestro card type' do
before(:each) do
params = {
'CardType' => 'MAESTRO'
}
@notification = Notification.from_params(params)
end

it 'is not a credit card' do
@notification.should_not be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is a debit card' do
@notification.should be_a_debit_card
end
end

describe 'VISA Electron card type' do
before(:each) do
params = {
'CardType' => 'UKE'
}
@notification = Notification.from_params(params)
end

it 'is not a credit card' do
@notification.should_not be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is a debit card' do
@notification.should be_a_debit_card
end
end

describe 'Laser card type' do
before(:each) do
params = {
'CardType' => 'LASER'
}
@notification = Notification.from_params(params)
end

it 'is not a credit card' do
@notification.should_not be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is not a commercial card' do
@notification.should_not be_a_commercial_card
end

it 'is a debit card' do
@notification.should be_a_debit_card
end
end

describe 'American Express card type' do
before(:each) do
params = {
'CardType' => 'AMEX'
}
@notification = Notification.from_params(params)
end

it 'is a credit card' do
@notification.should be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is a commercial card' do
@notification.should be_a_commercial_card
end

it 'is not a debit card' do
@notification.should_not be_a_debit_card
end
end

describe 'Diners card type' do
before(:each) do
params = {
'CardType' => 'DC'
}
@notification = Notification.from_params(params)
end

it 'is a credit card' do
@notification.should be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is a commercial card' do
@notification.should be_a_commercial_card
end

it 'is not a debit card' do
@notification.should_not be_a_debit_card
end
end

describe 'JCB card type' do
before(:each) do
params = {
'CardType' => 'JCB'
}
@notification = Notification.from_params(params)
end

it 'is a credit card' do
@notification.should be_a_credit_card
end

it 'is not a personal credit card' do
@notification.should_not be_a_personal_credit_card
end

it 'is a commercial card' do
@notification.should be_a_commercial_card
end

it 'is not a debit card' do
@notification.should_not be_a_debit_card
end
end
end
end

0 comments on commit 4f09f34

Please sign in to comment.