Refactor EveryPay / Banklink processing

This commit is contained in:
Karl Erik Õunapuu 2020-01-30 21:01:15 +02:00
parent cb76a9f962
commit 32d4fc3c83
5 changed files with 55 additions and 28 deletions

View file

@ -31,20 +31,20 @@ class Registrar
end
def back
puts params
@payment_order = PaymentOrder.find_by!(id: params[:bank])
@payment_order.update!(response: params.to_unsafe_h)
if @payment_order.valid_response_from_intermediary? && @payment_order.settled_payment?
@payment_order.complete_transaction
if @payment_order.payment_received?
@payment_order.complete_transaction(@payment_order.composed_transaction)
if @payment_order.invoice.paid?
flash[:notice] = t(:pending_applied)
else
flash[:alert] = t(:something_wrong)
# flash[:alert] = t(:something_wrong)
flash[:alert] = 'We fucked up'
end
else
@payment_order.create_failure_report
flash[:alert] = t(:something_wrong)
end
redirect_to registrar_invoice_path(@payment_order.invoice)
@ -54,8 +54,10 @@ class Registrar
@payment_order = PaymentOrder.find_by!(id: params[:bank])
@payment_order.update!(response: params.to_unsafe_h)
if @payment_order.valid_response_from_intermediary? && @payment_order.settled_payment?
if @payment_order.payment_received?
@payment_order.complete_transaction
else
@payment_order.create_failure_report
end
render status: 200, json: { status: 'ok' }

View file

@ -31,19 +31,13 @@ class BankTransaction < ApplicationRecord
end
# For successful binding, reference number, invoice id and sum must match with the invoice
def autobind_invoice(invoice_no: nil)
def autobind_invoice
return if binded?
return unless registrar
return unless invoice
return unless invoice.payable?
inv = if invoice_no.nil?
invoice
else
Invoice.find_by(number: invoice_no)
end
return unless inv
return unless inv.payable?
create_activity(registrar, inv)
create_activity(registrar, invoice)
end
def bind_invoice(invoice_no)

View file

@ -11,7 +11,8 @@ class PaymentOrder < ApplicationRecord
validate :invoice_cannot_be_already_paid, on: :create
# validates :type, inclusion: { in: PAYMENT_METHODS }
enum status: { issued: 'issued', paid: 'paid', cancelled: 'cancelled' }
enum status: { issued: 'issued', paid: 'paid', cancelled: 'cancelled',
failed: 'failed' }
attr_accessor :return_url, :response_url
@ -34,6 +35,22 @@ class PaymentOrder < ApplicationRecord
end
end
def complete_transaction(transaction)
paid!
transaction.save!
transaction.bind_invoice(invoice.number)
return unless transaction.errors.any?
worded_errors = 'Failed to bind. '
transaction.errors.full_messages.each do |err|
worded_errors << "#{err}, "
end
update!(notes: worded_errors)
end
def self.supported_methods
enabled = []

View file

@ -48,10 +48,17 @@ module PaymentOrders
end
end
def complete_transaction
return unless valid_response_from_intermediary? && settled_payment?
def payment_received?
valid_response_from_intermediary? && settled_payment?
end
self.status = 'paid'
def create_failure_report
notes = "User failed to make valid payment. Bank responded with code #{response['VK_SERVICE']}"
status = 'cancelled'
update!(notes: notes, status: status)
end
def composed_transaction
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
description: invoice.order,
reference_no: invoice.reference_no,
@ -66,8 +73,7 @@ module PaymentOrders
transaction.buyer_name = response['VK_SND_NAME']
transaction.paid_at = Time.parse(response['VK_T_DATETIME'])
transaction.save!
transaction.autobind_invoice(invoice_no: invoice.number)
transaction
end
def settled_payment?

View file

@ -34,11 +34,12 @@ module PaymentOrders
SUCCESSFUL_PAYMENT.include?(response['payment_state'])
end
def complete_transaction
return unless valid_response_from_intermediary? && settled_payment?
self.status = 'paid'
def payment_received?
valid_response_from_intermediary? && settled_payment?
end
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
def composed_transaction
transaction = BankTransaction.new(
description: invoice.order,
reference_no: invoice.reference_no,
currency: invoice.currency,
@ -49,8 +50,13 @@ module PaymentOrders
transaction.paid_at = Date.strptime(response['timestamp'], '%s')
transaction.buyer_name = response['cc_holder_name']
transaction.save!
transaction.autobind_invoice(invoice_no: invoice.number)
transaction
end
def create_failure_report
notes = "User failed to make valid payment. Payment state: #{response['payment_state']}"
status = 'cancelled'
update!(notes: notes, status: status)
end
def base_params
@ -80,6 +86,8 @@ module PaymentOrders
end
def valid_amount?
return false unless response.key? 'amount'
invoice.total == BigDecimal(response['amount'])
end