mirror of
https://github.com/internetee/registry.git
synced 2025-08-03 16:32:04 +02:00
Merge pull request #1502 from internetee/1422-record-payment-method-and-failed-payments
Record every payment attempt
This commit is contained in:
commit
444a07c62a
35 changed files with 798 additions and 342 deletions
|
@ -60,7 +60,7 @@ module Admin
|
|||
end
|
||||
|
||||
def bind_invoices
|
||||
@bank_statement.bind_invoices
|
||||
@bank_statement.bind_invoices(manual: true)
|
||||
|
||||
flash[:notice] = t('invoices_were_fully_binded') if @bank_statement.fully_binded?
|
||||
flash[:warning] = t('invoices_were_partially_binded') if @bank_statement.partially_binded?
|
||||
|
|
|
@ -34,7 +34,7 @@ module Admin
|
|||
end
|
||||
|
||||
def bind
|
||||
if @bank_transaction.bind_invoice(params[:invoice_no])
|
||||
if @bank_transaction.bind_invoice(params[:invoice_no], manual: true)
|
||||
flash[:notice] = I18n.t('record_created')
|
||||
redirect_to [:admin, @bank_transaction]
|
||||
else
|
||||
|
|
|
@ -5,50 +5,51 @@ class Registrar
|
|||
skip_authorization_check # actually anyone can pay, no problems at all
|
||||
skip_before_action :authenticate_registrar_user!, :check_ip_restriction,
|
||||
only: [:back, :callback]
|
||||
before_action :check_supported_payment_method
|
||||
|
||||
before_action :check_supported_payment_method, only: [:pay]
|
||||
|
||||
def pay
|
||||
invoice = Invoice.find(params[:invoice_id])
|
||||
bank = params[:bank]
|
||||
opts = {
|
||||
return_url: registrar_return_payment_with_url(
|
||||
bank, invoice_id: invoice
|
||||
),
|
||||
response_url: registrar_response_payment_with_url(
|
||||
bank, invoice_id: invoice
|
||||
)
|
||||
}
|
||||
@payment = ::PaymentOrders.create_with_type(bank, invoice, opts)
|
||||
@payment.create_transaction
|
||||
channel = params[:bank]
|
||||
|
||||
@payment_order = PaymentOrder.new_with_type(type: channel, invoice: invoice)
|
||||
@payment_order.save
|
||||
@payment_order.reload
|
||||
|
||||
@payment_order.return_url = registrar_return_payment_with_url(@payment_order)
|
||||
@payment_order.response_url = registrar_response_payment_with_url(@payment_order)
|
||||
|
||||
@payment_order.save
|
||||
@payment_order.reload
|
||||
end
|
||||
|
||||
def back
|
||||
invoice = Invoice.find(params[:invoice_id])
|
||||
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_order = PaymentOrder.find_by!(id: params[:payment_order])
|
||||
@payment_order.update!(response: params.to_unsafe_h)
|
||||
|
||||
@payment.complete_transaction
|
||||
if @payment_order.payment_received?
|
||||
@payment_order.complete_transaction
|
||||
|
||||
if invoice.paid?
|
||||
flash[:notice] = t(:pending_applied)
|
||||
if @payment_order.invoice.paid?
|
||||
flash[:notice] = t('.payment_successful')
|
||||
else
|
||||
flash[:alert] = t(:something_wrong)
|
||||
flash[:alert] = t('.successful_payment_backend_error')
|
||||
end
|
||||
else
|
||||
flash[:alert] = t(:something_wrong)
|
||||
@payment_order.create_failure_report
|
||||
flash[:alert] = t('.payment_not_received')
|
||||
end
|
||||
redirect_to registrar_invoice_path(invoice)
|
||||
redirect_to registrar_invoice_path(@payment_order.invoice)
|
||||
end
|
||||
|
||||
def callback
|
||||
invoice = Invoice.find(params[:invoice_id])
|
||||
opts = { response: params }
|
||||
@payment = ::PaymentOrders.create_with_type(params[:bank], invoice, opts)
|
||||
@payment_order = PaymentOrder.find_by!(id: params[:payment_order])
|
||||
@payment_order.update!(response: params.to_unsafe_h)
|
||||
|
||||
if @payment.valid_response_from_intermediary? && @payment.settled_payment?
|
||||
@payment.complete_transaction
|
||||
if @payment_order.payment_received?
|
||||
@payment_order.complete_transaction
|
||||
else
|
||||
@payment_order.create_failure_report
|
||||
end
|
||||
|
||||
render status: 200, json: { status: 'ok' }
|
||||
|
@ -57,13 +58,9 @@ class Registrar
|
|||
private
|
||||
|
||||
def check_supported_payment_method
|
||||
return if supported_payment_method?
|
||||
raise StandardError.new("Not supported payment method")
|
||||
end
|
||||
return if PaymentOrder.supported_method?(params[:bank], shortname: true)
|
||||
|
||||
|
||||
def supported_payment_method?
|
||||
PaymentOrders::PAYMENT_METHODS.include?(params[:bank])
|
||||
raise(StandardError, 'Not supported payment method')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -86,7 +86,9 @@ class BankStatement < ApplicationRecord
|
|||
status == FULLY_BINDED
|
||||
end
|
||||
|
||||
def bind_invoices
|
||||
bank_transactions.unbinded.each(&:autobind_invoice)
|
||||
def bind_invoices(manual: false)
|
||||
bank_transactions.unbinded.each do |transaction|
|
||||
transaction.autobind_invoice(manual: manual)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,6 +13,7 @@ class BankTransaction < ApplicationRecord
|
|||
|
||||
def binded_invoice
|
||||
return unless binded?
|
||||
|
||||
account_activity.invoice
|
||||
end
|
||||
|
||||
|
@ -30,31 +31,54 @@ class BankTransaction < ApplicationRecord
|
|||
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
|
||||
end
|
||||
|
||||
|
||||
# For successful binding, reference number, invoice id and sum must match with the invoice
|
||||
def autobind_invoice
|
||||
def autobind_invoice(manual: false)
|
||||
return if binded?
|
||||
return unless registrar
|
||||
return unless invoice
|
||||
return unless invoice.payable?
|
||||
|
||||
create_activity(registrar, invoice)
|
||||
channel = if manual
|
||||
'admin_payment'
|
||||
else
|
||||
'system_payment'
|
||||
end
|
||||
create_internal_payment_record(channel: channel, invoice: invoice,
|
||||
registrar: registrar)
|
||||
end
|
||||
|
||||
def bind_invoice(invoice_no)
|
||||
def create_internal_payment_record(channel: nil, invoice:, registrar:)
|
||||
if channel.nil?
|
||||
create_activity(invoice.buyer, invoice)
|
||||
return
|
||||
end
|
||||
|
||||
payment_order = PaymentOrder.new_with_type(type: channel, invoice: invoice)
|
||||
payment_order.save!
|
||||
|
||||
if create_activity(registrar, invoice)
|
||||
payment_order.paid!
|
||||
else
|
||||
payment_order.update(notes: 'Failed to create activity', status: 'failed')
|
||||
end
|
||||
end
|
||||
|
||||
def bind_invoice(invoice_no, manual: false)
|
||||
if binded?
|
||||
errors.add(:base, I18n.t('transaction_is_already_binded'))
|
||||
return
|
||||
end
|
||||
|
||||
invoice = Invoice.find_by(number: invoice_no)
|
||||
@registrar = invoice.buyer
|
||||
errors.add(:base, I18n.t('invoice_was_not_found')) unless invoice
|
||||
validate_invoice_data(invoice)
|
||||
return if errors.any?
|
||||
|
||||
unless invoice
|
||||
errors.add(:base, I18n.t('invoice_was_not_found'))
|
||||
return
|
||||
end
|
||||
create_internal_payment_record(channel: (manual ? 'admin_payment' : nil), invoice: invoice,
|
||||
registrar: invoice.buyer)
|
||||
end
|
||||
|
||||
def validate_invoice_data(invoice)
|
||||
if invoice.paid?
|
||||
errors.add(:base, I18n.t('invoice_is_already_binded'))
|
||||
return
|
||||
|
@ -65,23 +89,21 @@ class BankTransaction < ApplicationRecord
|
|||
return
|
||||
end
|
||||
|
||||
if invoice.total != sum
|
||||
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match'))
|
||||
return
|
||||
end
|
||||
|
||||
create_activity(invoice.buyer, invoice)
|
||||
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) if invoice.total != sum
|
||||
end
|
||||
|
||||
def create_activity(registrar, invoice)
|
||||
ActiveRecord::Base.transaction do
|
||||
create_account_activity!(account: registrar.cash_account,
|
||||
invoice: invoice,
|
||||
sum: invoice.subtotal,
|
||||
currency: currency,
|
||||
description: description,
|
||||
activity_type: AccountActivity::ADD_CREDIT)
|
||||
activity = AccountActivity.new(
|
||||
account: registrar.cash_account, bank_transaction: self,
|
||||
invoice: invoice, sum: invoice.subtotal,
|
||||
currency: currency, description: description,
|
||||
activity_type: AccountActivity::ADD_CREDIT
|
||||
)
|
||||
if activity.save
|
||||
reset_pending_registrar_balance_reload
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ class Invoice < ApplicationRecord
|
|||
has_one :account_activity
|
||||
has_many :items, class_name: 'InvoiceItem', dependent: :destroy
|
||||
has_many :directo_records, as: :item, class_name: 'Directo'
|
||||
has_many :payment_orders
|
||||
|
||||
accepts_nested_attributes_for :items
|
||||
|
||||
|
|
102
app/models/payment_order.rb
Normal file
102
app/models/payment_order.rb
Normal file
|
@ -0,0 +1,102 @@
|
|||
class PaymentOrder < ApplicationRecord
|
||||
include Versions
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].to_s.strip.split(', ').freeze
|
||||
PAYMENT_BANKLINK_BANKS = ENV['payments_banks'].to_s.strip.split(', ').freeze
|
||||
INTERNAL_PAYMENT_METHODS = %w[admin_payment system_payment].freeze
|
||||
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS,
|
||||
INTERNAL_PAYMENT_METHODS].flatten.freeze
|
||||
CUSTOMER_PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS].flatten.freeze
|
||||
|
||||
belongs_to :invoice, optional: false
|
||||
|
||||
validate :invoice_cannot_be_already_paid, on: :create
|
||||
validate :supported_payment_method
|
||||
|
||||
enum status: { issued: 'issued', paid: 'paid', cancelled: 'cancelled',
|
||||
failed: 'failed' }
|
||||
|
||||
attr_accessor :return_url, :response_url
|
||||
|
||||
def self.supported_methods
|
||||
supported = []
|
||||
|
||||
PAYMENT_METHODS.each do |method|
|
||||
class_name = ('PaymentOrders::' + method.camelize).constantize
|
||||
raise(NoMethodError, class_name) unless class_name < PaymentOrder
|
||||
|
||||
supported << class_name
|
||||
end
|
||||
|
||||
supported
|
||||
end
|
||||
|
||||
def self.new_with_type(type:, invoice:)
|
||||
channel = ('PaymentOrders::' + type.camelize).constantize
|
||||
|
||||
PaymentOrder.new(type: channel, invoice: invoice)
|
||||
end
|
||||
|
||||
# Name of configuration namespace
|
||||
def self.config_namespace_name; end
|
||||
|
||||
def supported_payment_method
|
||||
return if PaymentOrder.supported_method?(type)
|
||||
|
||||
errors.add(:type, 'is not supported')
|
||||
end
|
||||
|
||||
def invoice_cannot_be_already_paid
|
||||
return unless invoice&.paid?
|
||||
|
||||
errors.add(:invoice, 'is already paid')
|
||||
end
|
||||
|
||||
def self.supported_method?(name, shortname: false)
|
||||
some_class = if shortname
|
||||
('PaymentOrders::' + name.camelize).constantize
|
||||
else
|
||||
name.constantize
|
||||
end
|
||||
supported_methods.include? some_class
|
||||
rescue NameError
|
||||
false
|
||||
end
|
||||
|
||||
def base_transaction(sum:, paid_at:, buyer_name:)
|
||||
BankTransaction.new(
|
||||
description: invoice.order,
|
||||
reference_no: invoice.reference_no,
|
||||
currency: invoice.currency,
|
||||
iban: invoice.seller_iban,
|
||||
sum: sum,
|
||||
paid_at: paid_at,
|
||||
buyer_name: buyer_name
|
||||
)
|
||||
end
|
||||
|
||||
def complete_transaction
|
||||
return NoMethodError unless payment_received?
|
||||
|
||||
paid!
|
||||
transaction = composed_transaction
|
||||
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 channel
|
||||
type.gsub('PaymentOrders::', '')
|
||||
end
|
||||
|
||||
def form_url
|
||||
ENV["payments_#{self.class.config_namespace_name}_url"]
|
||||
end
|
||||
end
|
|
@ -1,15 +0,0 @@
|
|||
module PaymentOrders
|
||||
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].to_s.strip.split(', ').freeze
|
||||
PAYMENT_BANKLINK_BANKS = ENV['payments_banks'].to_s.strip.split(', ').freeze
|
||||
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS].flatten.freeze
|
||||
|
||||
def self.create_with_type(type, invoice, opts = {})
|
||||
raise ArgumentError unless PAYMENT_METHODS.include?(type)
|
||||
|
||||
if PAYMENT_BANKLINK_BANKS.include?(type)
|
||||
BankLink.new(type, invoice, opts)
|
||||
elsif type == 'every_pay'
|
||||
EveryPay.new(type, invoice, opts)
|
||||
end
|
||||
end
|
||||
end
|
9
app/models/payment_orders/admin_payment.rb
Normal file
9
app/models/payment_orders/admin_payment.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module PaymentOrders
|
||||
class AdminPayment < PaymentOrder
|
||||
CONFIG_NAMESPACE = 'admin_payment'.freeze
|
||||
|
||||
def self.config_namespace_name
|
||||
CONFIG_NAMESPACE
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,44 +1,44 @@
|
|||
module PaymentOrders
|
||||
class BankLink < Base
|
||||
BANK_LINK_VERSION = '008'
|
||||
class BankLink < PaymentOrder
|
||||
BANK_LINK_VERSION = '008'.freeze
|
||||
|
||||
NEW_TRANSACTION_SERVICE_NUMBER = '1012'
|
||||
SUCCESSFUL_PAYMENT_SERVICE_NUMBER = '1111'
|
||||
CANCELLED_PAYMENT_SERVICE_NUMBER = '1911'
|
||||
NEW_TRANSACTION_SERVICE_NUMBER = '1012'.freeze
|
||||
SUCCESSFUL_PAYMENT_SERVICE_NUMBER = '1111'.freeze
|
||||
CANCELLED_PAYMENT_SERVICE_NUMBER = '1911'.freeze
|
||||
|
||||
NEW_MESSAGE_KEYS = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_STAMP VK_AMOUNT
|
||||
NEW_MESSAGE_KEYS = %w[VK_SERVICE VK_VERSION VK_SND_ID VK_STAMP VK_AMOUNT
|
||||
VK_CURR VK_REF VK_MSG VK_RETURN VK_CANCEL
|
||||
VK_DATETIME).freeze
|
||||
SUCCESS_MESSAGE_KEYS = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
|
||||
VK_DATETIME].freeze
|
||||
SUCCESS_MESSAGE_KEYS = %w[VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
|
||||
VK_T_NO VK_AMOUNT VK_CURR VK_REC_ACC VK_REC_NAME
|
||||
VK_SND_ACC VK_SND_NAME VK_REF VK_MSG
|
||||
VK_T_DATETIME).freeze
|
||||
CANCEL_MESSAGE_KEYS = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
|
||||
VK_REF VK_MSG).freeze
|
||||
VK_T_DATETIME].freeze
|
||||
CANCEL_MESSAGE_KEYS = %w[VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
|
||||
VK_REF VK_MSG].freeze
|
||||
|
||||
def form_fields
|
||||
hash = {}
|
||||
hash["VK_SERVICE"] = NEW_TRANSACTION_SERVICE_NUMBER
|
||||
hash["VK_VERSION"] = BANK_LINK_VERSION
|
||||
hash["VK_SND_ID"] = seller_account
|
||||
hash["VK_STAMP"] = invoice.number
|
||||
hash["VK_AMOUNT"] = number_with_precision(invoice.total, precision: 2, separator: ".")
|
||||
hash["VK_CURR"] = invoice.currency
|
||||
hash["VK_REF"] = ""
|
||||
hash["VK_MSG"] = invoice.order
|
||||
hash["VK_RETURN"] = return_url
|
||||
hash["VK_CANCEL"] = return_url
|
||||
hash["VK_DATETIME"] = Time.zone.now.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
hash["VK_MAC"] = calc_mac(hash)
|
||||
hash["VK_ENCODING"] = "UTF-8"
|
||||
hash["VK_LANG"] = "ENG"
|
||||
hash['VK_SERVICE'] = NEW_TRANSACTION_SERVICE_NUMBER
|
||||
hash['VK_VERSION'] = BANK_LINK_VERSION
|
||||
hash['VK_SND_ID'] = seller_account
|
||||
hash['VK_STAMP'] = invoice.number
|
||||
hash['VK_AMOUNT'] = number_with_precision(invoice.total, precision: 2, separator: ".")
|
||||
hash['VK_CURR'] = invoice.currency
|
||||
hash['VK_REF'] = ''
|
||||
hash['VK_MSG'] = invoice.order
|
||||
hash['VK_RETURN'] = return_url
|
||||
hash['VK_CANCEL'] = return_url
|
||||
hash['VK_DATETIME'] = Time.zone.now.strftime('%Y-%m-%dT%H:%M:%S%z')
|
||||
hash['VK_MAC'] = calc_mac(hash)
|
||||
hash['VK_ENCODING'] = 'UTF-8'
|
||||
hash['VK_LANG'] = 'ENG'
|
||||
hash
|
||||
end
|
||||
|
||||
def valid_response_from_intermediary?
|
||||
return false unless response
|
||||
|
||||
case response["VK_SERVICE"]
|
||||
case response['VK_SERVICE']
|
||||
when SUCCESSFUL_PAYMENT_SERVICE_NUMBER
|
||||
valid_successful_transaction?
|
||||
when CANCELLED_PAYMENT_SERVICE_NUMBER
|
||||
|
@ -48,29 +48,31 @@ module PaymentOrders
|
|||
end
|
||||
end
|
||||
|
||||
def complete_transaction
|
||||
return unless valid_successful_transaction?
|
||||
def payment_received?
|
||||
valid_response_from_intermediary? && settled_payment?
|
||||
end
|
||||
|
||||
transaction = compose_or_find_transaction
|
||||
def create_failure_report
|
||||
notes = "User failed to make payment. Bank responded with code #{response['VK_SERVICE']}"
|
||||
status = 'cancelled'
|
||||
update!(notes: notes, status: status)
|
||||
end
|
||||
|
||||
transaction.sum = response['VK_AMOUNT']
|
||||
transaction.bank_reference = response['VK_T_NO']
|
||||
transaction.buyer_bank_code = response["VK_SND_ID"]
|
||||
transaction.buyer_iban = response["VK_SND_ACC"]
|
||||
transaction.buyer_name = response["VK_SND_NAME"]
|
||||
transaction.paid_at = Time.parse(response["VK_T_DATETIME"])
|
||||
def composed_transaction
|
||||
paid_at = Time.parse(response['VK_T_DATETIME'])
|
||||
transaction = base_transaction(sum: response['VK_AMOUNT'],
|
||||
paid_at: paid_at,
|
||||
buyer_name: response['VK_SND_NAME'])
|
||||
|
||||
transaction.save!
|
||||
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
|
||||
transaction.bank_reference = response['VK_T_NO']
|
||||
transaction.buyer_bank_code = response['VK_SND_ID']
|
||||
transaction.buyer_iban = response['VK_SND_ACC']
|
||||
|
||||
transaction
|
||||
end
|
||||
|
||||
def settled_payment?
|
||||
response["VK_SERVICE"] == SUCCESSFUL_PAYMENT_SERVICE_NUMBER
|
||||
response['VK_SERVICE'] == SUCCESSFUL_PAYMENT_SERVICE_NUMBER
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -89,17 +91,15 @@ module PaymentOrders
|
|||
|
||||
def valid_amount?
|
||||
source = number_with_precision(
|
||||
BigDecimal(response["VK_AMOUNT"]), precision: 2, separator: "."
|
||||
)
|
||||
target = number_with_precision(
|
||||
invoice.total, precision: 2, separator: "."
|
||||
BigDecimal(response['VK_AMOUNT']), precision: 2, separator: '.'
|
||||
)
|
||||
target = number_with_precision(invoice.total, precision: 2, separator: '.')
|
||||
|
||||
source == target
|
||||
end
|
||||
|
||||
def valid_currency?
|
||||
invoice.currency == response["VK_CURR"]
|
||||
invoice.currency == response['VK_CURR']
|
||||
end
|
||||
|
||||
def sign(data)
|
||||
|
@ -117,7 +117,7 @@ module PaymentOrders
|
|||
|
||||
def valid_mac?(hash, keys)
|
||||
data = keys.map { |element| prepend_size(hash[element]) }.join
|
||||
verify_mac(data, hash["VK_MAC"])
|
||||
verify_mac(data, hash['VK_MAC'])
|
||||
end
|
||||
|
||||
def verify_mac(data, mac)
|
||||
|
@ -126,22 +126,22 @@ module PaymentOrders
|
|||
end
|
||||
|
||||
def prepend_size(value)
|
||||
value = (value || "").to_s.strip
|
||||
string = ""
|
||||
value = (value || '').to_s.strip
|
||||
string = ''
|
||||
string << format("%03i", value.size)
|
||||
string << value
|
||||
end
|
||||
|
||||
def seller_account
|
||||
ENV["payments_#{type}_seller_account"]
|
||||
ENV["payments_#{self.class.config_namespace_name}_seller_account"]
|
||||
end
|
||||
|
||||
def seller_certificate
|
||||
ENV["payments_#{type}_seller_private"]
|
||||
ENV["payments_#{self.class.config_namespace_name}_seller_private"]
|
||||
end
|
||||
|
||||
def bank_certificate
|
||||
ENV["payments_#{type}_bank_certificate"]
|
||||
ENV["payments_#{self.class.config_namespace_name}_bank_certificate"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
module PaymentOrders
|
||||
class Base
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
attr_reader :type,
|
||||
:invoice,
|
||||
:return_url,
|
||||
:response_url,
|
||||
:response
|
||||
|
||||
def initialize(type, invoice, opts = {})
|
||||
@type = type
|
||||
@invoice = invoice
|
||||
@return_url = opts[:return_url]
|
||||
@response_url = opts[:response_url]
|
||||
@response = opts[:response]
|
||||
end
|
||||
|
||||
def create_transaction
|
||||
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
|
||||
reference_no: invoice.reference_no,
|
||||
currency: invoice.currency,
|
||||
iban: invoice.seller_iban
|
||||
)
|
||||
|
||||
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
|
||||
end
|
||||
end
|
|
@ -1,9 +1,15 @@
|
|||
module PaymentOrders
|
||||
class EveryPay < Base
|
||||
USER = ENV['payments_every_pay_api_user'].freeze
|
||||
KEY = ENV['payments_every_pay_api_key'].freeze
|
||||
ACCOUNT_ID = ENV['payments_every_pay_seller_account'].freeze
|
||||
SUCCESSFUL_PAYMENT = %w(settled authorized).freeze
|
||||
class EveryPay < PaymentOrder
|
||||
USER = ENV['payments_every_pay_api_user']
|
||||
KEY = ENV['payments_every_pay_api_key']
|
||||
ACCOUNT_ID = ENV['payments_every_pay_seller_account']
|
||||
SUCCESSFUL_PAYMENT = %w[settled authorized].freeze
|
||||
|
||||
CONFIG_NAMESPACE = 'every_pay'.freeze
|
||||
|
||||
def self.config_namespace_name
|
||||
CONFIG_NAMESPACE
|
||||
end
|
||||
|
||||
def form_fields
|
||||
base_json = base_params
|
||||
|
@ -25,25 +31,23 @@ module PaymentOrders
|
|||
end
|
||||
|
||||
def settled_payment?
|
||||
SUCCESSFUL_PAYMENT.include?(response[:payment_state])
|
||||
SUCCESSFUL_PAYMENT.include?(response['payment_state'])
|
||||
end
|
||||
|
||||
def complete_transaction
|
||||
return unless valid_response_from_intermediary? && settled_payment?
|
||||
def payment_received?
|
||||
valid_response_from_intermediary? && settled_payment?
|
||||
end
|
||||
|
||||
transaction = compose_or_find_transaction
|
||||
def composed_transaction
|
||||
base_transaction(sum: response['amount'],
|
||||
paid_at: Date.strptime(response['timestamp'], '%s'),
|
||||
buyer_name: response['cc_holder_name'])
|
||||
end
|
||||
|
||||
transaction.sum = response[:amount]
|
||||
transaction.paid_at = Date.strptime(response[:timestamp], '%s')
|
||||
transaction.buyer_name = response[:cc_holder_name]
|
||||
|
||||
transaction.save!
|
||||
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
|
||||
def create_failure_report
|
||||
notes = "User failed to make valid payment. Payment state: #{response['payment_state']}"
|
||||
status = 'cancelled'
|
||||
update!(notes: notes, status: status)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -63,24 +67,27 @@ module PaymentOrders
|
|||
end
|
||||
|
||||
def valid_hmac?
|
||||
hmac_fields = response[:hmac_fields].split(',')
|
||||
hmac_fields = response['hmac_fields'].split(',')
|
||||
hmac_hash = {}
|
||||
hmac_fields.map do |field|
|
||||
symbol = field.to_sym
|
||||
hmac_hash[symbol] = response[symbol]
|
||||
hmac_hash[field] = response[field]
|
||||
end
|
||||
|
||||
hmac_string = hmac_hash.map { |key, _v| "#{key}=#{hmac_hash[key]}" }.join('&')
|
||||
expected_hmac = OpenSSL::HMAC.hexdigest('sha1', KEY, hmac_string)
|
||||
expected_hmac == response[:hmac]
|
||||
expected_hmac == response['hmac']
|
||||
rescue NoMethodError
|
||||
false
|
||||
end
|
||||
|
||||
def valid_amount?
|
||||
invoice.total == BigDecimal(response[:amount])
|
||||
return false unless response.key? 'amount'
|
||||
|
||||
invoice.total == BigDecimal(response['amount'])
|
||||
end
|
||||
|
||||
def valid_account?
|
||||
response[:account_id] == ACCOUNT_ID
|
||||
response['account_id'] == ACCOUNT_ID
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
7
app/models/payment_orders/lhv.rb
Normal file
7
app/models/payment_orders/lhv.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module PaymentOrders
|
||||
class Lhv < BankLink
|
||||
def self.config_namespace_name
|
||||
'lhv'
|
||||
end
|
||||
end
|
||||
end
|
7
app/models/payment_orders/seb.rb
Normal file
7
app/models/payment_orders/seb.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module PaymentOrders
|
||||
class Seb < BankLink
|
||||
def self.config_namespace_name
|
||||
'seb'
|
||||
end
|
||||
end
|
||||
end
|
7
app/models/payment_orders/swed.rb
Normal file
7
app/models/payment_orders/swed.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module PaymentOrders
|
||||
class Swed < BankLink
|
||||
def self.config_namespace_name
|
||||
'swed'
|
||||
end
|
||||
end
|
||||
end
|
9
app/models/payment_orders/system_payment.rb
Normal file
9
app/models/payment_orders/system_payment.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module PaymentOrders
|
||||
class SystemPayment < PaymentOrder
|
||||
CONFIG_NAMESPACE = 'system_payment'.freeze
|
||||
|
||||
def self.config_namespace_name
|
||||
CONFIG_NAMESPACE
|
||||
end
|
||||
end
|
||||
end
|
4
app/models/version/payment_order_version.rb
Normal file
4
app/models/version/payment_order_version.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class PaymentOrderVersion < PaperTrail::Version
|
||||
self.table_name = :log_payment_orders
|
||||
self.sequence_name = :log_payment_orders_id_seq
|
||||
end
|
|
@ -21,3 +21,5 @@
|
|||
.col-md-6= render 'registrar/invoices/partials/buyer'
|
||||
.row
|
||||
.col-md-12= render 'registrar/invoices/partials/items'
|
||||
.row
|
||||
.col-md-12= render 'registrar/invoices/partials/payment_orders'
|
||||
|
|
19
app/views/registrar/invoices/partials/_payment_orders.haml
Normal file
19
app/views/registrar/invoices/partials/_payment_orders.haml
Normal file
|
@ -0,0 +1,19 @@
|
|||
%h4= "Payment Orders"
|
||||
%hr
|
||||
.table-responsive
|
||||
%table.table.table-hover.table-condensed
|
||||
%thead
|
||||
%tr
|
||||
%th{class: 'col-xs-1'}= "#"
|
||||
%th{class: 'col-xs-1'}= "Channel"
|
||||
%th{class: 'col-xs-2'}= "Status"
|
||||
%th{class: 'col-xs-3'}= "Initiated"
|
||||
%th{class: 'col-xs-4'}= "Notes"
|
||||
%tbody
|
||||
- @invoice.payment_orders.each do |payment_order|
|
||||
%tr
|
||||
%td= payment_order.id
|
||||
%td= payment_order.channel
|
||||
%td= payment_order.status
|
||||
%td= payment_order.created_at
|
||||
%td= payment_order.notes
|
|
@ -17,4 +17,4 @@
|
|||
|
||||
- if @invoice.payable?
|
||||
.row.semifooter
|
||||
.col-md-6-offset-6.text-right= render 'registrar/invoices/partials/banklinks', locals: { payment_channels: PaymentOrders::PAYMENT_METHODS }
|
||||
.col-md-6-offset-6.text-right= render 'registrar/invoices/partials/banklinks', locals: { payment_channels: PaymentOrder::CUSTOMER_PAYMENT_METHODS }
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
= t('registrar.invoices.redirected_to_intermediary')
|
||||
|
||||
.payment-form
|
||||
= form_tag @payment.form_url, method: :post do
|
||||
- @payment.form_fields.each do |k, v|
|
||||
= form_tag @payment_order.form_url, method: :post do
|
||||
- @payment_order.form_fields.each do |k, v|
|
||||
= hidden_field_tag k, v
|
||||
= submit_tag t('registrar.invoices.go_to_intermediary')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue