Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add payment type and reference #3003

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def status_changed_to_finalized?
# taxes_rate :float default(0.0), not null
# timezone :string default("UTC"), not null
# total_amount_cents :bigint default(0), not null
# total_paid_amount_cents :bigint default(0), not null
# version_number :integer default(4), not null
# voided_at :datetime
# created_at :datetime not null
Expand Down
12 changes: 11 additions & 1 deletion app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ class Payment < ApplicationRecord

belongs_to :payable, polymorphic: true
belongs_to :payment_provider, optional: true, class_name: 'PaymentProviders::BaseProvider'
belongs_to :payment_provider_customer, class_name: 'PaymentProviderCustomers::BaseCustomer'
belongs_to :payment_provider_customer, optional: true, class_name: 'PaymentProviderCustomers::BaseCustomer'

has_many :refunds
has_many :integration_resources, as: :syncable

PAYMENT_TYPES = {provider: "provider", manual: "manual"}
attribute :payment_type, :string
enum :payment_type, PAYMENT_TYPES, default: :provider, prefix: :payment_type
validates :payment_type, presence: true
validates :reference, presence: true, length: {maximum: 40}, if: -> { payment_type_manual? }
validates :reference, absence: true, if: -> { payment_type_provider? }

delegate :customer, to: :payable

enum payable_payment_status: PAYABLE_PAYMENT_STATUS.map { |s| [s, s] }.to_h
Expand All @@ -32,7 +39,9 @@ def should_sync_payment?
# amount_currency :string not null
# payable_payment_status :enum
# payable_type :string default("Invoice"), not null
# payment_type :enum default("provider"), not null
# provider_payment_data :jsonb
# reference :string
# status :string not null
# created_at :datetime not null
# updated_at :datetime not null
Expand All @@ -49,6 +58,7 @@ def should_sync_payment?
# index_payments_on_payable_type_and_payable_id (payable_type,payable_id)
# index_payments_on_payment_provider_customer_id (payment_provider_customer_id)
# index_payments_on_payment_provider_id (payment_provider_id)
# index_payments_on_payment_type (payment_type)
#
# Foreign Keys
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

class AddPaymentTypeAndReferenceToPayments < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
create_enum :payment_type, %w[provider manual]

safety_assured do
change_table :payments, bulk: true do |t|
t.column :payment_type, :enum, enum_type: 'payment_type', null: true
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
t.column :reference, :string, default: nil
end
end

# Backfill existing records
Payment.in_batches(of: 10_000).update_all(payment_type: 'provider') # rubocop:disable Rails/SkipsModelValidations

safety_assured do
execute <<~SQL
ALTER TABLE payments ALTER COLUMN payment_type SET DEFAULT 'provider';
SQL
execute <<~SQL
ALTER TABLE payments ALTER COLUMN payment_type SET NOT NULL;
SQL
end

add_index :payments, :payment_type, algorithm: :concurrently
end

def down
remove_index :payments, column: :payment_type

change_table :payments, bulk: true do |t|
t.remove :payment_type
t.remove :reference
end

drop_enum :payment_type
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class AddTotalPaidAmountCentsToInvoices < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
add_column :invoices, :total_paid_amount_cents, :bigint, null: true, default: 0
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
# Backfill
Invoice.in_batches(of: 10_000).each do |batch|
batch.update_all(total_paid_amount_cents: 0) # rubocop:disable Rails/SkipsModelValidations
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end

safety_assured do
execute <<~SQL
ALTER TABLE invoices ALTER COLUMN total_paid_amount_cents SET DEFAULT 0;
SQL
execute <<~SQL
ALTER TABLE invoices ALTER COLUMN total_paid_amount_cents SET NOT NULL;
SQL
end
end

def down
# Remove the column
remove_column :invoices, :total_paid_amount_cents
end
end
7 changes: 6 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/factories/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
amount_currency { 'EUR' }
provider_payment_id { SecureRandom.uuid }
status { 'pending' }
payment_type { 'provider' }

trait :adyen_payment do
association :payment_provider, factory: :adyen_provider
Expand Down
61 changes: 60 additions & 1 deletion spec/models/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,72 @@
require 'rails_helper'

RSpec.describe Payment, type: :model do
subject(:payment) { create(:payment) }
subject(:payment) { build(:payment, payment_type:, provider_payment_id:, reference:) }

let(:payment_type) { 'provider' }
let(:provider_payment_id) { SecureRandom.uuid }
let(:reference) { nil }

it_behaves_like 'paper_trail traceable'

it { is_expected.to have_many(:integration_resources) }
it { is_expected.to belong_to(:payable) }
it { is_expected.to delegate_method(:customer).to(:payable) }
it { is_expected.to validate_presence_of(:payment_type) }

it do
expect(subject)
.to define_enum_for(:payment_type)
.with_values(Payment::PAYMENT_TYPES)
.with_prefix(:payment_type)
.backed_by_column_of_type(:enum)
end

describe 'validations' do
let(:errors) { payment.errors }

before { payment.valid? }

describe 'of reference' do
context 'when payment type is provider' do
context 'when reference is present' do
let(:reference) { '123' }

it 'adds an error' do
expect(errors.where(:reference, :present)).to be_present
end
end

context 'when reference is not present' do
it 'does not add an error' do
expect(errors.where(:reference, :present)).not_to be_present
end
end
end

context 'when payment type is manual' do
let(:payment_type) { 'manual' }

context 'when reference is present' do
context 'when reference is less than 40 characters' do
let(:reference) { '123' }

it 'does not add an error' do
expect(errors.where(:reference, :blank)).not_to be_present
end
end

context 'when reference is more than 40 characters' do
let(:reference) { 'a' * 41 }

it 'adds an error' do
expect(errors.where(:reference, :too_long)).to be_present
end
end
end
end
end
end

describe '#should_sync_payment?' do
subject(:method_call) { payment.should_sync_payment? }
Expand Down
Loading