Implement partial payment logging for EveryPay / Bank links

This commit is contained in:
Karl Erik Õunapuu 2020-01-30 17:24:07 +02:00
parent feb3a5d9da
commit 53fbd2f50c
14 changed files with 470 additions and 345 deletions

View file

@ -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
@ -49,30 +49,31 @@ module PaymentOrders
end
def complete_transaction
return unless valid_successful_transaction?
return unless valid_response_from_intermediary? && settled_payment?
transaction = BankTransaction.find_by(
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
description: invoice.order,
reference_no: invoice.reference_no,
currency: invoice.currency,
iban: invoice.seller_iban
)
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"])
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'])
transaction.save!
transaction.autobind_invoice
end
def settled_payment?
response["VK_SERVICE"] == SUCCESSFUL_PAYMENT_SERVICE_NUMBER
response['VK_SERVICE'] == SUCCESSFUL_PAYMENT_SERVICE_NUMBER
end
private
# private
def valid_successful_transaction?
valid_success_notice? && valid_amount? && valid_currency?
@ -88,17 +89,17 @@ module PaymentOrders
def valid_amount?
source = number_with_precision(
BigDecimal.new(response["VK_AMOUNT"]), precision: 2, separator: "."
BigDecimal(response['VK_AMOUNT']), precision: 2, separator: '.'
)
target = number_with_precision(
invoice.total, precision: 2, separator: "."
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)
@ -116,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)
@ -125,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

View file

@ -1,33 +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 form_url
ENV["payments_#{type}_url"]
end
end
end

View file

@ -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'
def self.config_namespace_name
CONFIG_NAMESPACE
end
def form_fields
base_json = base_params
@ -20,32 +26,31 @@ module PaymentOrders
def valid_response_from_intermediary?
return false unless response
valid_hmac? && valid_amount? && valid_account?
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?
transaction = BankTransaction.find_by(
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
description: invoice.order,
currency: invoice.currency,
iban: invoice.seller_iban
reference_no: invoice.reference_no,
currency: invoice.currency,
iban: invoice.seller_iban
)
transaction.sum = response[:amount]
transaction.paid_at = Date.strptime(response[:timestamp], '%s')
transaction.buyer_name = response[:cc_holder_name]
transaction.sum = response['amount']
transaction.paid_at = Date.strptime(response['timestamp'], '%s')
transaction.buyer_name = response['cc_holder_name']
transaction.save!
transaction.autobind_invoice
end
private
def base_params
{
api_username: USER,
@ -61,24 +66,23 @@ 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']
end
def valid_amount?
invoice.total == BigDecimal.new(response[:amount])
invoice.total == BigDecimal(response['amount'])
end
def valid_account?
response[:account_id] == ACCOUNT_ID
response['account_id'] == ACCOUNT_ID
end
end
end

View file

@ -0,0 +1,7 @@
module PaymentOrders
class LHV < BankLink
def self.config_namespace_name
'lhv'
end
end
end

View file

@ -0,0 +1,7 @@
module PaymentOrders
class SEB < BankLink
def self.config_namespace_name
'seb'
end
end
end

View file

@ -0,0 +1,7 @@
module PaymentOrders
class Swed < BankLink
def self.config_namespace_name
'swed'
end
end
end