- close order
- query order from kbz api
This commit is contained in:
aungthetkhaing
2025-05-23 16:20:30 +06:30
parent 11b484ea5c
commit 2cbd1c7918

View File

@@ -5,23 +5,29 @@ class KbzMerchant
def initialize(payment_method) def initialize(payment_method)
@payment_method = payment_method @payment_method = payment_method
@api_url ='http://api.kbzpay.com/payment/gateway/uat/precreate'
end end
def create_order(amount:, merch_order_id:) def create_order(amount:, merch_order_id:, timeout: '120m')
payload = build_payload(amount, merch_order_id) api_url = 'http://api.kbzpay.com/payment/gateway/uat/precreate'
response = send_request(payload) payload = build_create_payload(amount, merch_order_id, timeout)
response = send_request(payload, api_url)
handle_response(response) handle_response(response)
end end
def close_order(merch_order_id:)
api_url ='https://api.kbzpay.com/payment/gateway/uat/closeorder'
payload = build_close_payload(merch_order_id)
send_request(payload, api_url)
end
private private
def build_payload(amount, merch_order_id, timeout='120m') def build_create_payload(amount, merch_order_id, timeout)
base_params = { base_params = {
method: 'kbz.payment.precreate', method: 'kbz.payment.precreate',
timestamp: Time.now.utc.to_i.to_s, timestamp: Time.now.utc.to_i.to_s,
nonce_str: SecureRandom.hex(16), nonce_str: SecureRandom.hex(16),
notify_url: "http://localhost:3000/notify", notify_url: 'https://example.com/notify',
sign_type: 'SHA256', sign_type: 'SHA256',
version: '1.0', version: '1.0',
biz_content: { biz_content: {
@@ -31,7 +37,25 @@ class KbzMerchant
trade_type: 'PAY_BY_QRCODE', trade_type: 'PAY_BY_QRCODE',
total_amount: amount.to_s, total_amount: amount.to_s,
trans_currency: 'MMK', trans_currency: 'MMK',
timeout_express: timeout || '120m' 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 }.compact
} }
@@ -59,7 +83,7 @@ class KbzMerchant
Digest::SHA256.hexdigest(string_to_sign).upcase Digest::SHA256.hexdigest(string_to_sign).upcase
end end
def send_request(payload) def send_request(payload, url)
headers = { headers = {
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'User-Agent' => 'KBZPay/1.0' 'User-Agent' => 'KBZPay/1.0'
@@ -70,7 +94,7 @@ class KbzMerchant
begin begin
response = HTTParty.post( response = HTTParty.post(
@api_url, url,
headers: headers, headers: headers,
body: { Request: payload }.to_json, body: { Request: payload }.to_json,
timeout: 15 timeout: 15