Skip to content

Commit

Permalink
Merge pull request #1503 from internetee/1500-bank-link-payments-fail
Browse files Browse the repository at this point in the history
Bind specific invoice when paid via Banklink / EveryPay
  • Loading branch information
vohmar authored Jan 31, 2020
2 parents cbbfbae + 0cf2ff9 commit a5c2fcd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/controllers/registrar/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def back
opts = { response: params }
@payment = ::PaymentOrders.create_with_type(params[:bank], invoice, opts)
if @payment.valid_response_from_intermediary? && @payment.settled_payment?
Rails.logger.info("User paid invoice ##{invoice.number} successfully")

@payment.complete_transaction

if invoice.paid?
Expand Down
1 change: 1 addition & 0 deletions app/models/bank_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def bind_invoice(invoice_no)
end

invoice = Invoice.find_by(number: invoice_no)
@registrar = invoice.buyer

unless invoice
errors.add(:base, I18n.t('invoice_was_not_found'))
Expand Down
13 changes: 7 additions & 6 deletions app/models/payment_orders/bank_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ def valid_response_from_intermediary?
def complete_transaction
return unless valid_successful_transaction?

transaction = BankTransaction.find_by(
description: invoice.order,
currency: invoice.currency,
iban: invoice.seller_iban
)
transaction = compose_or_find_transaction

transaction.sum = response['VK_AMOUNT']
transaction.bank_reference = response['VK_T_NO']
Expand All @@ -65,7 +61,12 @@ def complete_transaction
transaction.paid_at = Time.parse(response["VK_T_DATETIME"])

transaction.save!
transaction.autobind_invoice
transaction.bind_invoice(invoice.number)
if transaction.errors.empty?
Rails.logger.info("Invoice ##{invoice.number} was marked as paid")
else
Rails.logger.error("Failed to bind invoice ##{invoice.number}")
end
end

def settled_payment?
Expand Down
26 changes: 26 additions & 0 deletions app/models/payment_orders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ def create_transaction
transaction.save!
end

def compose_or_find_transaction
transaction = BankTransaction.find_by(base_transaction_params)

# Transaction already autobinded (possibly) invalid invoice
if transaction.binded?
Rails.logger.info("Transaction #{transaction.id} is already binded")
Rails.logger.info('Creating new BankTransaction record.')

transaction = new_base_transaction
end

transaction
end

def new_base_transaction
BankTransaction.new(base_transaction_params)
end

def base_transaction_params
{
description: invoice.order,
currency: invoice.currency,
iban: invoice.seller_iban,
}
end

def form_url
ENV["payments_#{type}_url"]
end
Expand Down
14 changes: 8 additions & 6 deletions app/models/payment_orders/every_pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def form_fields

def valid_response_from_intermediary?
return false unless response

valid_hmac? && valid_amount? && valid_account?
end

Expand All @@ -30,18 +31,19 @@ def settled_payment?
def complete_transaction
return unless valid_response_from_intermediary? && settled_payment?

transaction = BankTransaction.find_by(
description: invoice.order,
currency: invoice.currency,
iban: invoice.seller_iban
)
transaction = compose_or_find_transaction

transaction.sum = response[:amount]
transaction.paid_at = Date.strptime(response[:timestamp], '%s')
transaction.buyer_name = response[:cc_holder_name]

transaction.save!
transaction.autobind_invoice
transaction.bind_invoice(invoice.number)
if transaction.errors.empty?
Rails.logger.info("Invoice ##{invoice.number} marked as paid")
else
Rails.logger.error("Failed to bind invoice ##{invoice.number}")
end
end

private
Expand Down
4 changes: 3 additions & 1 deletion test/models/payment_orders/bank_link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def test_complete_transaction_calls_methods_on_transaction
mock_transaction.expect(:paid_at= , Date.parse('2018-04-01 00:30:00 +0300'), [Time.parse('2018-04-01T00:30:00+0300')])
mock_transaction.expect(:buyer_name=, 'John Doe', ['John Doe'])
mock_transaction.expect(:save!, true)
mock_transaction.expect(:autobind_invoice, AccountActivity.new)
mock_transaction.expect(:binded?, false)
mock_transaction.expect(:bind_invoice, AccountActivity.new, [1])
mock_transaction.expect(:errors, [])

BankTransaction.stub(:find_by, mock_transaction) do
@completed_bank_link.complete_transaction
Expand Down
4 changes: 3 additions & 1 deletion test/models/payment_orders/every_pay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def test_complete_transaction_calls_methods_on_transaction
mock_transaction.expect(:paid_at= , Date.strptime('1524136727', '%s'), [Date.strptime('1524136727', '%s')])
mock_transaction.expect(:buyer_name=, 'John Doe', ['John Doe'])
mock_transaction.expect(:save!, true)
mock_transaction.expect(:autobind_invoice, AccountActivity.new)
mock_transaction.expect(:binded?, false)
mock_transaction.expect(:bind_invoice, AccountActivity.new, [1])
mock_transaction.expect(:errors, [])

BankTransaction.stub(:find_by, mock_transaction) do
@every_pay.complete_transaction
Expand Down

0 comments on commit a5c2fcd

Please sign in to comment.