refactor payment model and its tests

This commit is contained in:
Rishabh Saxena 2016-07-19 12:37:19 +05:30
parent 25c0046290
commit c4d934d711
4 changed files with 111 additions and 39 deletions

View file

@ -24,7 +24,7 @@ class Payment < ActiveRecord::Base
} }
def credit_card def credit_card
@credit_card = ActiveMerchant::Billing::CreditCard.new( @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
first_name: first_name, first_name: first_name,
last_name: last_name, last_name: last_name,
number: credit_card_number, number: credit_card_number,
@ -35,26 +35,25 @@ class Payment < ActiveRecord::Base
end end
def purchase(user, conference, price_in_cents) def purchase(user, conference, price_in_cents)
begin gateway_response = begin
recieve = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency) GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
rescue rescue
false ActiveMerchant::Billing::Response.new(false, 'Unable to receive any response from the payment gateway.')
end end
unless recieve
errors.add(:base, 'Unable to recieve any response') if gateway_response.success?
return false
end
unless recieve.success?
errors.add(:base, recieve.message)
self.status = 'failure'
return false
end
self.user_id = user.id self.user_id = user.id
self.conference_id = conference.id self.conference_id = conference.id
self.last4 = credit_card.display_number self.last4 = credit_card.display_number
self.authorization_code = recieve.authorization self.authorization_code = gateway_response.authorization
self.status = 'success' self.status = 'success'
recieve.success? else
errors.add(:base, gateway_response.message)
self.status = 'failure'
end
success?
end end
end end

View file

@ -2,10 +2,19 @@ FactoryGirl.define do
factory :payment do factory :payment do
first_name { "#{Faker::Hipster.word} abc" } first_name { "#{Faker::Hipster.word} abc" }
last_name { "#{Faker::Hipster.word} xyz" } last_name { "#{Faker::Hipster.word} xyz" }
credit_card_number { '4242424242424242' } credit_card_number '4242424242424111'
card_verification_value { '123' } card_verification_value '123'
expiration_month { '06' } expiration_month 6
expiration_year { Date.current.year + 2 } expiration_year { Date.current.year + 2 }
amount { '10' } amount 10
end
trait :invalid_credit_card do
credit_card_number '4242424242424222'
end
trait :exception_credit_card do
credit_card_number '4242424242424333'
end end
end end

View file

@ -37,7 +37,7 @@ feature Registration do
fill_in 'last_name', with: 'bar' fill_in 'last_name', with: 'bar'
select Date.current.year + 2, from: 'expiration_year' select Date.current.year + 2, from: 'expiration_year'
fill_in 'card_verification_value', with: '123' fill_in 'card_verification_value', with: '123'
fill_in 'credit_card_number', with: '3' fill_in 'credit_card_number', with: '4242424242423333'
click_button 'Charge Card' click_button 'Charge Card'
@ -65,7 +65,7 @@ feature Registration do
fill_in 'last_name', with: 'bar' fill_in 'last_name', with: 'bar'
select Date.current.year + 2, from: 'expiration_year' select Date.current.year + 2, from: 'expiration_year'
fill_in 'card_verification_value', with: '123' fill_in 'card_verification_value', with: '123'
fill_in 'credit_card_number', with: '2' fill_in 'credit_card_number', with: '4242424242422222'
click_button 'Charge Card' click_button 'Charge Card'
@ -93,7 +93,7 @@ feature Registration do
fill_in 'last_name', with: 'bar' fill_in 'last_name', with: 'bar'
select Date.current.year + 2, from: 'expiration_year' select Date.current.year + 2, from: 'expiration_year'
fill_in 'card_verification_value', with: '123' fill_in 'card_verification_value', with: '123'
fill_in 'credit_card_number', with: '4242424242424242' fill_in 'credit_card_number', with: '4242424242421111'
click_button 'Charge Card' click_button 'Charge Card'

View file

@ -2,6 +2,13 @@ require 'spec_helper'
describe Payment do describe Payment do
context 'new payment' do
let(:payment) { create(:payment) }
it 'sets status to "unpaid" by default' do
expect(payment.status).to eq('unpaid')
end
end
describe 'validations' do describe 'validations' do
it 'has a valid factory' do it 'has a valid factory' do
expect(build(:payment)).to be_valid expect(build(:payment)).to be_valid
@ -35,28 +42,85 @@ describe Payment do
end end
describe 'purchase' do describe '#purchase' do
let!(:participant) { create(:user) } let!(:user) { create(:user) }
let!(:ticket_1) { create(:ticket) } let!(:ticket_1) { create(:ticket) }
let!(:conference) { create(:conference, tickets: [ticket_1]) } let!(:conference) { create(:conference, tickets: [ticket_1]) }
let(:payment) { create(:payment) } let(:payment) { create(:payment) }
it 'creates a purchase and payment for one ticket' do it 'calls the payment gateway with the correct parameters' do
tickets = { ticket_1.id.to_s => '1' } expect(GATEWAY).to receive(:purchase).with(1000, payment.credit_card, currency: 'USD')
message = TicketPurchase.purchase(conference, participant, tickets) .and_return(ActiveMerchant::Billing::Response.new(true, 'Success.'))
purchase = TicketPurchase.where(conference_id: conference.id,
user_id: participant.id,
ticket_id: ticket_1.id).first
expect(TicketPurchase.count).to eq(1) payment.purchase(user, conference, 1000)
expect(purchase.quantity).to eq(1) end
expect(message.blank?).to be true
payment.purchase(participant, conference, 1000) context 'when the payment is successful' do
before { payment.purchase(user, conference, 1000) }
purchase = Payment.first it 'returns true' do
expect(Payment.count).to be(1) payment_result = payment.purchase(user, conference, 1000)
expect(purchase.amount).to eq(10) expect(payment_result).to eq true
end
it "assigns 'success' to payment.status" do
expect(payment.status).to eq('success')
end
it 'assigns user_id' do
expect(payment.user_id).to eq(user.id)
end
it 'assigns conference_id' do
expect(payment.conference_id).to eq(conference.id)
end
it 'assigns last4' do
expect(payment.last4).to eq('XXXX-XXXX-XXXX-4111')
end
it 'assigns authorization_code' do
expect(payment.authorization_code).to eq("53433")
end
end
context 'if the payment is not successful' do
before { payment.purchase(user, conference, 1000) }
let(:payment) { create(:payment, :invalid_credit_card) }
context 'when the card is invalid' do
it 'returns false' do
payment_result = payment.purchase(user, conference, 1000)
expect(payment_result).to eq false
end
it 'assigns "failure" to payment.status' do
expect(payment.status).to eq('failure')
end
it 'adds errors' do
expect(payment.errors[:base].count).to eq(1)
end
end
context 'when there is a connection problem with the gateway' do
let(:payment) { create(:payment, :exception_credit_card) }
it 'returns false' do
payment_result = payment.purchase(user, conference, 1000)
expect(payment_result).to eq false
end
it 'assigns "failure" to payment.status' do
expect(payment.status).to eq('failure')
end
it 'adds errors' do
expect(payment.errors[:base].count).to eq(1)
end
end
end end
end end
end end