mirror of
https://github.com/internetee/registry.git
synced 2025-06-08 05:34:46 +02:00
Refactor EveryPay / Banklink processing
This commit is contained in:
parent
cb76a9f962
commit
32d4fc3c83
5 changed files with 55 additions and 28 deletions
|
@ -31,20 +31,20 @@ class Registrar
|
||||||
end
|
end
|
||||||
|
|
||||||
def back
|
def back
|
||||||
puts params
|
|
||||||
|
|
||||||
@payment_order = PaymentOrder.find_by!(id: params[:bank])
|
@payment_order = PaymentOrder.find_by!(id: params[:bank])
|
||||||
@payment_order.update!(response: params.to_unsafe_h)
|
@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
|
@payment_order.complete_transaction(@payment_order.composed_transaction)
|
||||||
|
|
||||||
if @payment_order.invoice.paid?
|
if @payment_order.invoice.paid?
|
||||||
flash[:notice] = t(:pending_applied)
|
flash[:notice] = t(:pending_applied)
|
||||||
else
|
else
|
||||||
flash[:alert] = t(:something_wrong)
|
# flash[:alert] = t(:something_wrong)
|
||||||
|
flash[:alert] = 'We fucked up'
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@payment_order.create_failure_report
|
||||||
flash[:alert] = t(:something_wrong)
|
flash[:alert] = t(:something_wrong)
|
||||||
end
|
end
|
||||||
redirect_to registrar_invoice_path(@payment_order.invoice)
|
redirect_to registrar_invoice_path(@payment_order.invoice)
|
||||||
|
@ -54,8 +54,10 @@ class Registrar
|
||||||
@payment_order = PaymentOrder.find_by!(id: params[:bank])
|
@payment_order = PaymentOrder.find_by!(id: params[:bank])
|
||||||
@payment_order.update!(response: params.to_unsafe_h)
|
@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
|
@payment_order.complete_transaction
|
||||||
|
else
|
||||||
|
@payment_order.create_failure_report
|
||||||
end
|
end
|
||||||
|
|
||||||
render status: 200, json: { status: 'ok' }
|
render status: 200, json: { status: 'ok' }
|
||||||
|
|
|
@ -31,19 +31,13 @@ class BankTransaction < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
# For successful binding, reference number, invoice id and sum must match with the invoice
|
# 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 if binded?
|
||||||
return unless registrar
|
return unless registrar
|
||||||
|
return unless invoice
|
||||||
|
return unless invoice.payable?
|
||||||
|
|
||||||
inv = if invoice_no.nil?
|
create_activity(registrar, invoice)
|
||||||
invoice
|
|
||||||
else
|
|
||||||
Invoice.find_by(number: invoice_no)
|
|
||||||
end
|
|
||||||
return unless inv
|
|
||||||
return unless inv.payable?
|
|
||||||
|
|
||||||
create_activity(registrar, inv)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def bind_invoice(invoice_no)
|
def bind_invoice(invoice_no)
|
||||||
|
|
|
@ -11,7 +11,8 @@ class PaymentOrder < ApplicationRecord
|
||||||
validate :invoice_cannot_be_already_paid, on: :create
|
validate :invoice_cannot_be_already_paid, on: :create
|
||||||
# validates :type, inclusion: { in: PAYMENT_METHODS }
|
# 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
|
attr_accessor :return_url, :response_url
|
||||||
|
|
||||||
|
@ -34,6 +35,22 @@ class PaymentOrder < ApplicationRecord
|
||||||
end
|
end
|
||||||
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
|
def self.supported_methods
|
||||||
enabled = []
|
enabled = []
|
||||||
|
|
||||||
|
|
|
@ -48,10 +48,17 @@ module PaymentOrders
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete_transaction
|
def payment_received?
|
||||||
return unless valid_response_from_intermediary? && settled_payment?
|
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(
|
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
|
||||||
description: invoice.order,
|
description: invoice.order,
|
||||||
reference_no: invoice.reference_no,
|
reference_no: invoice.reference_no,
|
||||||
|
@ -66,8 +73,7 @@ module PaymentOrders
|
||||||
transaction.buyer_name = response['VK_SND_NAME']
|
transaction.buyer_name = response['VK_SND_NAME']
|
||||||
transaction.paid_at = Time.parse(response['VK_T_DATETIME'])
|
transaction.paid_at = Time.parse(response['VK_T_DATETIME'])
|
||||||
|
|
||||||
transaction.save!
|
transaction
|
||||||
transaction.autobind_invoice(invoice_no: invoice.number)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def settled_payment?
|
def settled_payment?
|
||||||
|
|
|
@ -34,11 +34,12 @@ module PaymentOrders
|
||||||
SUCCESSFUL_PAYMENT.include?(response['payment_state'])
|
SUCCESSFUL_PAYMENT.include?(response['payment_state'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete_transaction
|
def payment_received?
|
||||||
return unless valid_response_from_intermediary? && settled_payment?
|
valid_response_from_intermediary? && settled_payment?
|
||||||
self.status = 'paid'
|
end
|
||||||
|
|
||||||
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
|
def composed_transaction
|
||||||
|
transaction = BankTransaction.new(
|
||||||
description: invoice.order,
|
description: invoice.order,
|
||||||
reference_no: invoice.reference_no,
|
reference_no: invoice.reference_no,
|
||||||
currency: invoice.currency,
|
currency: invoice.currency,
|
||||||
|
@ -49,8 +50,13 @@ module PaymentOrders
|
||||||
transaction.paid_at = Date.strptime(response['timestamp'], '%s')
|
transaction.paid_at = Date.strptime(response['timestamp'], '%s')
|
||||||
transaction.buyer_name = response['cc_holder_name']
|
transaction.buyer_name = response['cc_holder_name']
|
||||||
|
|
||||||
transaction.save!
|
transaction
|
||||||
transaction.autobind_invoice(invoice_no: invoice.number)
|
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
|
end
|
||||||
|
|
||||||
def base_params
|
def base_params
|
||||||
|
@ -80,6 +86,8 @@ module PaymentOrders
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_amount?
|
def valid_amount?
|
||||||
|
return false unless response.key? 'amount'
|
||||||
|
|
||||||
invoice.total == BigDecimal(response['amount'])
|
invoice.total == BigDecimal(response['amount'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue