2021-04-07 17:17:17 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-09-28 12:54:29 +02:00
|
|
|
require 'payment/helper'
|
|
|
|
|
2021-04-07 17:17:17 +02:00
|
|
|
# PayZen payement gateway
|
|
|
|
module PayZen; end
|
|
|
|
|
|
|
|
## Provides various methods around the PayZen payment gateway
|
2022-09-28 12:54:29 +02:00
|
|
|
class PayZen::Helper < Payment::Helper
|
2021-04-07 17:17:17 +02:00
|
|
|
class << self
|
2021-04-13 17:16:05 +02:00
|
|
|
## Is the PayZen gateway enabled?
|
2021-04-07 17:17:17 +02:00
|
|
|
def enabled?
|
2021-04-08 17:11:48 +02:00
|
|
|
return false unless Setting.get('online_payment_module')
|
2021-04-07 17:17:17 +02:00
|
|
|
return false unless Setting.get('payment_gateway') == 'payzen'
|
|
|
|
|
|
|
|
res = true
|
|
|
|
%w[payzen_username payzen_password payzen_endpoint payzen_public_key payzen_hmac payzen_currency].each do |pz_setting|
|
2022-09-28 12:54:29 +02:00
|
|
|
res = false if Setting.get(pz_setting).blank?
|
2021-04-07 17:17:17 +02:00
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
2021-04-12 12:16:12 +02:00
|
|
|
|
2022-09-28 12:54:29 +02:00
|
|
|
def human_error(error)
|
|
|
|
I18n.t('errors.messages.gateway_error', { MESSAGE: error.message })
|
|
|
|
end
|
|
|
|
|
2021-04-13 17:16:05 +02:00
|
|
|
## generate an unique string reference for the content of a cart
|
2021-06-14 14:53:59 +02:00
|
|
|
def generate_ref(cart_items, customer)
|
2021-04-12 12:16:12 +02:00
|
|
|
require 'sha3'
|
|
|
|
|
2021-06-14 14:53:59 +02:00
|
|
|
content = { cart_items: cart_items, customer: customer }.to_json + DateTime.current.to_s
|
2021-04-14 17:56:04 +02:00
|
|
|
# It's safe to truncate a hash. See https://crypto.stackexchange.com/questions/74646/sha3-255-one-bit-less
|
|
|
|
SHA3::Digest.hexdigest(:sha224, content)[0...24]
|
2021-04-13 17:16:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
## Generate a hash map compatible with PayZen 'V4/Customer/Customer'
|
2021-04-27 17:18:20 +02:00
|
|
|
def generate_customer(customer_id, operator_id, cart_items)
|
2021-04-13 17:16:05 +02:00
|
|
|
customer = User.find(customer_id)
|
2021-04-27 17:18:20 +02:00
|
|
|
operator = User.find(operator_id)
|
|
|
|
|
2021-04-13 17:16:05 +02:00
|
|
|
address = if customer.organization?
|
|
|
|
customer.invoicing_profile.organization.address&.address
|
|
|
|
else
|
|
|
|
customer.invoicing_profile.address&.address
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
reference: customer.id,
|
|
|
|
email: customer.invoicing_profile.email,
|
|
|
|
billingDetails: {
|
|
|
|
firstName: customer.invoicing_profile.first_name,
|
|
|
|
lastName: customer.invoicing_profile.last_name,
|
|
|
|
legalName: customer.organization? ? customer.invoicing_profile.organization.name : nil,
|
|
|
|
address: address
|
|
|
|
},
|
|
|
|
shippingDetails: {
|
|
|
|
category: customer.organization? ? 'COMPANY' : 'PRIVATE',
|
|
|
|
shippingMethod: 'ETICKET'
|
2021-04-23 12:52:06 +02:00
|
|
|
},
|
2021-04-27 17:18:20 +02:00
|
|
|
shoppingCart: generate_shopping_cart(cart_items, customer, operator)
|
2021-04-13 17:16:05 +02:00
|
|
|
}
|
2021-04-12 12:16:12 +02:00
|
|
|
end
|
2021-04-14 17:56:04 +02:00
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
## Generate a hash map compatible with PayZen 'V4/Customer/ShoppingCart'
|
2021-04-27 17:18:20 +02:00
|
|
|
def generate_shopping_cart(cart_items, customer, operator)
|
2022-09-28 12:54:29 +02:00
|
|
|
cart = case cart_items
|
|
|
|
when ShoppingCart, Order
|
2022-08-25 08:52:17 +02:00
|
|
|
cart_items
|
2021-06-04 18:26:20 +02:00
|
|
|
else
|
|
|
|
cs = CartService.new(operator)
|
|
|
|
cs.from_hash(cart_items)
|
|
|
|
end
|
2022-08-25 08:52:17 +02:00
|
|
|
if cart.is_a? Order
|
|
|
|
return {
|
|
|
|
cartItemInfo: cart.order_items.map do |item|
|
|
|
|
{
|
|
|
|
productAmount: item.amount.to_i.to_s,
|
|
|
|
productLabel: item.orderable_id,
|
|
|
|
productQty: item.quantity.to_s,
|
|
|
|
productType: customer.organization? ? 'SERVICE_FOR_BUSINESS' : 'SERVICE_FOR_INDIVIDUAL'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
{
|
2021-04-23 12:52:06 +02:00
|
|
|
cartItemInfo: cart.items.map do |item|
|
2021-04-22 19:24:08 +02:00
|
|
|
{
|
2021-04-27 17:18:20 +02:00
|
|
|
productAmount: item.price[:amount].to_i.to_s,
|
2021-04-23 12:52:06 +02:00
|
|
|
productLabel: item.name,
|
2021-04-27 17:18:20 +02:00
|
|
|
productQty: 1.to_s,
|
2021-04-23 12:52:06 +02:00
|
|
|
productType: customer.organization? ? 'SERVICE_FOR_BUSINESS' : 'SERVICE_FOR_INDIVIDUAL'
|
2021-04-22 19:24:08 +02:00
|
|
|
}
|
2021-04-23 12:52:06 +02:00
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-04-14 17:56:04 +02:00
|
|
|
## Check the PayZen signature for integrity
|
|
|
|
def check_hash(algorithm, hash_key, hash_proof, data, key = nil)
|
|
|
|
supported_hash_algorithm = ['sha256_hmac']
|
|
|
|
|
|
|
|
# check if the hash algorithm is supported
|
2021-05-28 09:10:34 +02:00
|
|
|
raise ::PayzenError, "hash algorithm not supported: #{algorithm}. Update your SDK" unless supported_hash_algorithm.include? algorithm
|
2021-04-14 17:56:04 +02:00
|
|
|
|
|
|
|
# if key is not defined, we use kr-hash-key parameter to choose it
|
|
|
|
if key.nil?
|
2022-09-28 12:54:29 +02:00
|
|
|
case hash_key
|
|
|
|
when 'sha256_hmac'
|
2021-04-14 17:56:04 +02:00
|
|
|
key = Setting.get('payzen_hmac')
|
2022-09-28 12:54:29 +02:00
|
|
|
when 'password'
|
2021-04-14 17:56:04 +02:00
|
|
|
key = Setting.get('payzen_password')
|
|
|
|
else
|
2021-05-28 09:10:34 +02:00
|
|
|
raise ::PayzenError, 'invalid hash-key parameter'
|
2021-04-14 17:56:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
hash = OpenSSL::HMAC.hexdigest('SHA256', key, data)
|
|
|
|
|
|
|
|
# return true if calculated hash and sent hash are the same
|
|
|
|
hash == hash_proof
|
|
|
|
end
|
2021-04-07 17:17:17 +02:00
|
|
|
end
|
|
|
|
end
|