Merge pull request #1108 from rishabhs95/stripe

Online payment feature - direct stripe integration
This commit is contained in:
Hernán Schmidt 2016-08-18 16:04:11 +02:00 committed by GitHub
commit 32019a11e1
35 changed files with 523 additions and 110 deletions

View file

@ -82,9 +82,8 @@ describe ConferenceRegistrationsController, type: :controller do
get :show, conference_id: conference.short_title
end
it 'assigns price of purchased tickets to total_price and purchased tickets to tickets' do
expect(assigns(:total_price)).to eq Money.new(10000, 'USD')
expect(assigns(:tickets)).to match_array [@purchased_ticket]
it 'does not assign price of purchased tickets to total_price and purchased tickets to tickets without payment' do
expect(assigns(:total_price)).to eq Money.new(0, 'USD')
end
end

View file

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :payment do
user
conference
status 'unpaid'
end
end

View file

@ -16,7 +16,7 @@ feature Registration do
context 'who is not registered' do
scenario 'purchases a ticket', feature: true, js: true do
scenario 'purchases and pays for a ticket succcessfully', feature: true, js: true do
visit root_path
click_link 'Register'
@ -26,26 +26,66 @@ feature Registration do
fill_in "tickets__#{ticket.id}", with: '2'
expect(current_path).to eq(conference_tickets_path(conference.short_title))
click_button 'Support'
click_button 'Continue'
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
expect(flash).to eq('Please pay here to get tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2)
expect(current_path).to eq(conference_conference_registration_path(conference.short_title))
expect(flash).
to eq("Thank you for supporting #{conference.title} by purchasing a ticket.")
expect(page.has_content?("2 #{ticket.title} Tickets for 10")).to be true
if Rails.application.secrets.stripe_publishable_key
find('.stripe-button-el').click
stripe_iframe = all('iframe[name=stripe_checkout_app]').last
sleep(5)
Capybara.within_frame stripe_iframe do
expect(page).to have_content('book your tickets')
page.execute_script(%{ $('input#card_number').val('4242424242424242'); })
page.execute_script(%{ $('input#cc-exp').val('08/22'); })
page.execute_script(%{ $('input#cc-csc').val('123'); })
page.execute_script(%{ $('#submitButton').click(); })
sleep(20)
end
expect(current_path).to eq(conference_conference_registration_path(conference.short_title))
expect(page.has_content?("2 #{ticket.title} Tickets for $ 10")).to be true
end
end
scenario 'deletes a purchased ticket', feature: true, js: true do
create(:registration, conference: conference, user: participant)
create(:ticket_purchase, conference: conference, user: participant, ticket: ticket, quantity: 4)
scenario 'purchases ticket but payment fails', feature: true, js: true do
visit root_path
click_link 'Register'
visit conference_conference_registration_path(conference.short_title)
expect(page.has_content?("4 #{ticket.title} Tickets for 10")).to be true
expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title))
click_button 'Register'
click_link "ticket-#{ticket.id}-delete"
expect(flash).to eq('Ticket successfully deleted.')
expect(TicketPurchase.count).to eq(0)
fill_in "tickets__#{ticket.id}", with: '2'
expect(current_path).to eq(conference_tickets_path(conference.short_title))
click_button 'Continue'
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
expect(flash).to eq('Please pay here to get tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2)
if Rails.application.secrets.stripe_publishable_key
find('.stripe-button-el').click
stripe_iframe = all('iframe[name=stripe_checkout_app]').last
sleep(5)
Capybara.within_frame stripe_iframe do
expect(page).to have_content('book your tickets')
page.execute_script(%{ $('input#card_number').val('4000000000000341'); })
page.execute_script(%{ $('input#cc-exp').val('08/22'); })
page.execute_script(%{ $('input#cc-csc').val('123'); })
page.execute_script(%{ $('#submitButton').click(); })
sleep(20)
end
expect(current_path).to eq(conference_payments_path(conference.short_title))
expect(flash).to eq('Your card was declined. Please try again with correct credentials.')
end
end
end
end

View file

@ -105,6 +105,9 @@ describe 'User' do
it{ should be_able_to(:index, Ticket) }
it{ should be_able_to(:manage, TicketPurchase.new(user_id: user.id)) }
it{ should be_able_to(:new, Payment.new(user_id: user.id)) }
it{ should be_able_to(:create, Payment.new(user_id: user.id)) }
it{ should be_able_to(:create, Subscription.new(user_id: user.id)) }
it{ should be_able_to(:destroy, subscription) }

133
spec/models/payment_spec.rb Normal file
View file

@ -0,0 +1,133 @@
require 'spec_helper'
require 'stripe_mock'
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
it 'has a valid factory' do
expect(build(:payment)).to be_valid
end
it { is_expected.to validate_presence_of(:status) }
it { is_expected.to validate_presence_of(:user_id) }
it { is_expected.to validate_presence_of(:conference_id) }
end
describe '#amount_to_pay' do
let!(:user) { create(:user) }
let!(:conference) { create(:conference) }
let(:ticket_1) { create(:ticket, price: 10, price_currency: 'USD', conference: conference) }
let(:payment) { create(:payment, user: user, conference: conference) }
it ' returns correct unpaid amount' do
create(:ticket_purchase, ticket: ticket_1, user: user, quantity: 8)
expect(payment.amount_to_pay).to eq(8000)
end
end
describe '#purchase' do
let!(:user) { create(:user) }
let!(:conference) { create(:conference) }
let!(:ticket_1) { create(:ticket, price: 10, price_currency: 'USD', conference: conference) }
let!(:tickets) { {ticket_1.id.to_s => '2'} }
let(:stripe_helper) { StripeMock.create_test_helper }
before { StripeMock.start }
after { StripeMock.stop }
before { TicketPurchase.purchase(conference, user, tickets) }
let(:payment) { create(:payment, user: user, conference: conference, stripe_customer_token: stripe_helper.generate_card_token, stripe_customer_email: user.email) }
context 'when the payment is successful' do
before { payment.purchase }
it 'assigns amount' do
expect(payment.amount).to eq(2000)
end
it 'assigns last4' do
expect(payment.last4).to eq('4242')
end
it "assigns 'success' to payment.status" do
expect(payment.status).to eq('success')
end
it 'assigns authorization_code' do
expect(payment.authorization_code).to eq('test_ch_3')
end
end
context 'if the payment is not successful' do
let(:payment) { create(:payment, user: user, conference: conference, stripe_customer_token: 'bogus_card_token', stripe_customer_email: user.email) }
before { payment.purchase }
context 'when the card is invalid' do
it 'returns false' do
payment_result = payment.purchase
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 the connection to Stripe drops' do
it 'raises exception' do
StripeMock.prepare_error(Stripe::APIConnectionError.new)
expect{ payment.purchase }.not_to raise_error
end
end
context 'when there is a Stripe API Error' do
it 'raises exception' do
StripeMock.prepare_error(Stripe::APIError.new)
expect{ payment.purchase }.not_to raise_error
end
end
context 'when there is authentication error' do
it 'raises exception' do
StripeMock.prepare_error(Stripe::AuthenticationError.new)
expect{ payment.purchase }.not_to raise_error
end
end
context 'when there is a card error' do
it 'raises exception' do
StripeMock.prepare_card_error(:card_declined)
expect{ payment.purchase }.not_to raise_error
end
end
context 'when the request to Stripe is invalid' do
it 'raises exception' do
StripeMock.prepare_error(Stripe::InvalidRequestError.new('Your request is invalid.', code: 402))
expect{ payment.purchase }.not_to raise_error
end
end
context 'when Stripe rate limit exceeds' do
it 'raises exception' do
StripeMock.prepare_error(Stripe::RateLimitError.new)
expect{ payment.purchase }.not_to raise_error
end
end
end
end
end

View file

@ -82,31 +82,72 @@ describe Ticket do
end
end
describe '#quantity_bought_by' do
it 'returns the correct value if the user has bought this ticket' do
create(:ticket_purchase,
user: user,
ticket: ticket,
quantity: 20)
expect(ticket.quantity_bought_by(user)).to eq(20)
describe '#unpaid?' do
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) }
context 'user has not paid' do
it 'returns true' do
expect(ticket.unpaid?(user)).to eq(true)
end
end
it 'returns zero if the user has not bought this ticket' do
expect(ticket.quantity_bought_by(user)).to eq(0)
context 'user has paid' do
before { ticket_purchase.update_attributes(paid: true) }
it 'returns false' do
expect(ticket.unpaid?(user)).to eq(false)
end
end
end
describe '#quantity_bought_by' do
context 'user has not paid' do
it 'returns the correct value if the user has bought this ticket' do
create(:ticket_purchase,
user: user,
ticket: ticket,
quantity: 20)
expect(ticket.quantity_bought_by(user, paid: false)).to eq(20)
end
it 'returns zero if the user has not bought this ticket' do
expect(ticket.quantity_bought_by(user, paid: false)).to eq(0)
end
end
context 'user has paid' do
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket, quantity: 20) }
before { ticket_purchase.update_attributes(paid: true) }
it 'returns the correct value if the user has bought and paid for this ticket' do
expect(ticket.quantity_bought_by(user, paid: true)).to eq(20)
end
end
end
describe '#total_price' do
it 'returns the correct value if the user has bought this ticket' do
create(:ticket_purchase,
user: user,
ticket: ticket,
quantity: 20)
expect(ticket.total_price(user)).to eq(Money.new(100000, 'USD'))
context 'user has not paid' do
it 'returns the correct value if the user has bought this ticket' do
create(:ticket_purchase,
user: user,
ticket: ticket,
quantity: 20)
expect(ticket.total_price(user, paid: false)).to eq(Money.new(100000, 'USD'))
end
it 'returns zero if the user has not bought this ticket' do
expect(ticket.total_price(user, paid: false)).to eq(Money.new(0, 'USD'))
end
end
it 'returns zero if the user has not bought this ticket' do
expect(ticket.total_price(user)).to eq(Money.new(0, 'USD'))
context 'user has paid' do
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket, quantity: 20) }
before { ticket_purchase.update_attributes(paid: true) }
it 'returns the correct value if the user has bought this ticket' do
expect(ticket.total_price(user, paid: true)).to eq(Money.new(100000, 'USD'))
end
end
end
@ -116,7 +157,7 @@ describe Ticket do
describe 'user has bought' do
context 'no tickets' do
it 'returns zero' do
expect(Ticket.total_price(conference, user)).to eq(Money.new(0, 'USD'))
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(0, 'USD'))
end
end
@ -126,7 +167,7 @@ describe Ticket do
end
it 'returns the correct total price' do
expect(Ticket.total_price(conference, user)).to eq(Money.new(100000, 'USD'))
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(100000, 'USD'))
end
end
@ -138,7 +179,7 @@ describe Ticket do
it 'returns the correct total price' do
total_price = Money.new(200000, 'USD')
expect(Ticket.total_price(conference, user)).to eq(total_price)
expect(Ticket.total_price(conference, user, paid: false)).to eq(total_price)
end
end
end

View file

@ -1,6 +1,6 @@
# Mock external requests to youtube
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
WebMock.disable_net_connect!(allow_localhost: true, allow: /stripe.com/)
RSpec.configure do |config|
config.before(:each) do