From 1f9338735c513f0bf52bd77c614f021f46af0b12 Mon Sep 17 00:00:00 2001 From: Rishabh Saxena Date: Thu, 28 Jul 2016 20:42:04 +0530 Subject: [PATCH] improve Stripe error rescue. modify tests. --- app/controllers/payments_controller.rb | 17 +++++---- app/models/payment.rb | 36 ++++++++++---------- db/migrate/20160606040848_create_payments.rb | 2 +- db/schema.rb | 2 +- spec/factories/payments.rb | 5 ++- spec/models/payment_spec.rb | 17 +++++---- 6 files changed, 43 insertions(+), 36 deletions(-) diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index aa0c038a..86f646f6 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -17,14 +17,17 @@ class PaymentsController < ApplicationController @unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference) @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) - @payment = Payment.new payment_params.merge(user: current_user, conference: @conference) - @payment.purchase - @payment.save + @payment = Payment.new payment_params.merge(amount: @total_amount_to_pay.cents, + user: current_user, + conference: @conference) - update_purchased_ticket_purchases - - redirect_to conference_conference_registration_path(@conference.short_title), flash: - { success: 'Thanks! You have purchased your tickets successfully.' } + if @payment.purchase && @payment.save + update_purchased_ticket_purchases + redirect_to conference_conference_registration_path(@conference.short_title), flash: + { success: 'Thanks! You have purchased your tickets successfully.' } + else + render :new + end end private diff --git a/app/models/payment.rb b/app/models/payment.rb index 95ed9a70..52df4071 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -6,8 +6,6 @@ class Payment < ActiveRecord::Base attr_accessor :stripeEmail attr_accessor :stripeToken - validates :last4, presence: true - validates :authorization_code, presence: true validates :status, presence: true validates :amount, presence: true, numericality: { greater_than: 0 } validates :user_id, presence: true @@ -24,24 +22,26 @@ class Payment < ActiveRecord::Base end def purchase - customer = Stripe::Customer.create email: stripeEmail, - source: stripeToken, - description: user.name + begin + customer = Stripe::Customer.create email: stripeEmail, + source: stripeToken, + description: user.name - gateway_response = Stripe::Charge.create customer: customer.id, - receipt_email: stripeEmail, - description: 'ticket purchases', - amount: amount_to_pay, - currency: conference.tickets.first.price_currency + gateway_response = Stripe::Charge.create customer: customer.id, + receipt_email: stripeEmail, + description: 'ticket purchases', + amount: amount_to_pay, + currency: conference.tickets.first.price_currency - self.amount = gateway_response[:amount] - self.last4 = gateway_response[:source][:last4] - self.authorization_code = gateway_response[:id] - self.status = 'success' - true + self.last4 = gateway_response[:source][:last4] + self.authorization_code = gateway_response[:id] + self.status = 'success' + true - rescue Stripe::CardError => e - flash[:error] = e.message - false + rescue => error + errors.add(:base, error.message) + self.status = 'failure' + false + end end end diff --git a/db/migrate/20160606040848_create_payments.rb b/db/migrate/20160606040848_create_payments.rb index 6792ad02..6c56bb5d 100644 --- a/db/migrate/20160606040848_create_payments.rb +++ b/db/migrate/20160606040848_create_payments.rb @@ -2,7 +2,7 @@ class CreatePayments < ActiveRecord::Migration def change create_table :payments do |t| t.string :last4 - t.integer :amount + t.integer :amount, null: false t.string :authorization_code t.integer :status, default: 0, null: false t.integer :user_id, null: false diff --git a/db/schema.rb b/db/schema.rb index 6213090c..6c3ceaef 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -265,7 +265,7 @@ ActiveRecord::Schema.define(version: 20160704092023) do create_table "payments", force: :cascade do |t| t.string "last4" - t.integer "amount" + t.integer "amount", null: false t.string "authorization_code" t.integer "status", default: 0, null: false t.integer "user_id", null: false diff --git a/spec/factories/payments.rb b/spec/factories/payments.rb index 3c718436..f0829c5c 100644 --- a/spec/factories/payments.rb +++ b/spec/factories/payments.rb @@ -2,8 +2,7 @@ FactoryGirl.define do factory :payment do user conference - last4 '4242' - authorization_code '1234567890' - amount 10 + status 'unpaid' + amount 1000 end end diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index 3950e269..e16a8f67 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -14,12 +14,8 @@ describe Payment do expect(build(:payment)).to be_valid end - it { is_expected.to validate_presence_of(:last4) } - it { is_expected.to validate_presence_of(:amount) } - it { is_expected.to validate_presence_of(:authorization_code) } - it { is_expected.to validate_presence_of(:status) } it { is_expected.to validate_presence_of(:user_id) } @@ -37,8 +33,17 @@ describe Payment do it 'is valid with a amount greater than zero' do should allow_value(1).for(:amount) end - end - describe 'self#purchase' + 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 end