- 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)
@payment_method = payment_method
@api_url ='http://api.kbzpay.com/payment/gateway/uat/precreate'
end
def create_order(amount:, merch_order_id:)
payload = build_payload(amount, merch_order_id)
response = send_request(payload)
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 ='https://api.kbzpay.com/payment/gateway/uat/closeorder'
payload = build_close_payload(merch_order_id)
send_request(payload, api_url)
end
private
def build_payload(amount, merch_order_id, timeout='120m')
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: "http://localhost:3000/notify",
notify_url: 'https://example.com/notify',
sign_type: 'SHA256',
version: '1.0',
biz_content: {
@@ -31,7 +37,25 @@ class KbzMerchant
trade_type: 'PAY_BY_QRCODE',
total_amount: amount.to_s,
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
}
@@ -59,7 +83,7 @@ class KbzMerchant
Digest::SHA256.hexdigest(string_to_sign).upcase
end
def send_request(payload)
def send_request(payload, url)
headers = {
'Content-Type' => 'application/json',
'User-Agent' => 'KBZPay/1.0'
@@ -70,7 +94,7 @@ class KbzMerchant
begin
response = HTTParty.post(
@api_url,
url,
headers: headers,
body: { Request: payload }.to_json,
timeout: 15