149 lines
4.1 KiB
Ruby
149 lines
4.1 KiB
Ruby
require 'httparty'
|
|
|
|
class KbzMerchant
|
|
class PaymentError < StandardError; end
|
|
|
|
def initialize(payment_method)
|
|
@payment_method = payment_method
|
|
end
|
|
|
|
def create_order(amount:, merch_order_id:, timeout: '120m')
|
|
api_url = 'http://api.kbzpay.com/payment/gateway/uat/precreate'
|
|
payload = build_create_payload(amount, merch_order_id, timeout)
|
|
response = send_request(payload, api_url)
|
|
handle_response(response)
|
|
end
|
|
|
|
def close_order(merch_order_id:)
|
|
api_url ='http://api.kbzpay.com/payment/gateway/uat/closeorder'
|
|
payload = build_close_payload(merch_order_id)
|
|
response = send_request(payload, api_url)
|
|
handle_response(response)
|
|
end
|
|
|
|
def query_order(merch_order_id:)
|
|
api_url = 'http://api.kbzpay.com/payment/gateway/uat/queryorder'
|
|
payload = build_query_payload(merch_order_id)
|
|
response = send_request(payload, api_url)
|
|
handle_response(response)
|
|
end
|
|
|
|
private
|
|
|
|
def build_create_payload(amount, merch_order_id, timeout)
|
|
base_params = {
|
|
method: 'kbz.payment.precreate',
|
|
timestamp: Time.now.utc.to_i.to_s,
|
|
nonce_str: SecureRandom.hex(16),
|
|
notify_url: 'https://example.com/notify',
|
|
sign_type: 'SHA256',
|
|
version: '1.0',
|
|
biz_content: {
|
|
appid: @payment_method.gateway_url,
|
|
merch_code: @payment_method.merchant_account_id,
|
|
merch_order_id: merch_order_id,
|
|
trade_type: 'PAY_BY_QRCODE',
|
|
total_amount: amount.to_s,
|
|
trans_currency: 'MMK',
|
|
timeout_express: timeout
|
|
}.compact
|
|
}
|
|
|
|
flattened = flatten_hash(base_params)
|
|
base_params.merge(sign: generate_signature(flattened))
|
|
end
|
|
|
|
def build_close_payload(merch_order_id)
|
|
base_params = {
|
|
method: 'kbz.payment.closeorder',
|
|
timestamp: Time.now.utc.to_i.to_s,
|
|
nonce_str: SecureRandom.hex(16),
|
|
sign_type: 'SHA256',
|
|
version: '3.0',
|
|
biz_content: {
|
|
appid: @payment_method.gateway_url,
|
|
merch_code: @payment_method.merchant_account_id,
|
|
merch_order_id: merch_order_id
|
|
}.compact
|
|
}
|
|
|
|
flattened = flatten_hash(base_params)
|
|
base_params.merge(sign: generate_signature(flattened))
|
|
end
|
|
|
|
def build_query_payload(merch_order_id)
|
|
base_params = {
|
|
method: 'kbz.payment.queryorder',
|
|
timestamp: Time.now.utc.to_i.to_s,
|
|
nonce_str: SecureRandom.hex(16),
|
|
sign_type: 'SHA256',
|
|
version: '3.0',
|
|
biz_content: {
|
|
appid: @payment_method.gateway_url,
|
|
merch_code: @payment_method.merchant_account_id,
|
|
merch_order_id: merch_order_id
|
|
}.compact
|
|
}
|
|
|
|
flattened = flatten_hash(base_params)
|
|
base_params.merge(sign: generate_signature(flattened))
|
|
end
|
|
|
|
def flatten_hash(hash, parent_key = nil)
|
|
hash.each_with_object({}) do |(k, v), res|
|
|
key = parent_key ? "#{k}" : k.to_s
|
|
if v.is_a?(Hash)
|
|
res.merge!(flatten_hash(v, key))
|
|
else
|
|
res[key] = v.to_s
|
|
end
|
|
end
|
|
end
|
|
|
|
def generate_signature(flattened_params)
|
|
sorted_params = flattened_params.except('sign', 'sign_type').sort
|
|
string_a = sorted_params.map { |k, v| "#{k}=#{v}" }.join('&')
|
|
puts "String a: #{string_a}"
|
|
string_to_sign = "#{string_a}&key=#{@payment_method.auth_token}"
|
|
puts "String to sign: #{string_to_sign}"
|
|
Digest::SHA256.hexdigest(string_to_sign).upcase
|
|
end
|
|
|
|
def send_request(payload, url)
|
|
headers = {
|
|
'Content-Type' => 'application/json',
|
|
'User-Agent' => 'KBZPay/1.0'
|
|
}
|
|
|
|
puts "Headers: #{headers}"
|
|
puts "Payload: #{payload.to_json}"
|
|
|
|
begin
|
|
response = HTTParty.post(
|
|
url,
|
|
headers: headers,
|
|
body: { Request: payload }.to_json,
|
|
timeout: 15
|
|
)
|
|
|
|
puts "Response: #{response}"
|
|
response.body
|
|
# puts "Response: #{response}"
|
|
rescue HTTParty::Error => e
|
|
raise PaymentError, "HTTP error: #{e.message}"
|
|
rescue SocketError => e
|
|
raise PaymentError, "Network error: #{e.message}"
|
|
end
|
|
end
|
|
|
|
|
|
def handle_response(response)
|
|
json_response = JSON.parse(response)
|
|
if json_response.dig('Response', 'result') == 'SUCCESS'
|
|
json_response['Response']
|
|
else
|
|
raise PaymentError, "Payment failed: #{json_response['Response']['msg']}"
|
|
end
|
|
end
|
|
end
|