This commit is contained in:
Zoey
2019-06-26 11:42:51 +06:30
parent 56b8115ac9
commit 79d8eb94ef
17 changed files with 306 additions and 78 deletions

View File

@@ -1,5 +1,7 @@
class KbzPay
KBZ_PAY = 'KBZPay'
def self.pay(amount, receipt_no)
datetime = DateTime.now.strftime("%d%m%Y%H%M")
@@ -14,26 +16,28 @@ class KbzPay
kbz_callback_url = "https://staging-v2.doemal.com/api/v3/ordering/kbz_callback"
nounce_str = SecureRandom.base64(32).first(32).upcase
params = "appid="+kbz_app_id+"&merch_code="+kbz_merch_code+"&merch_order_id="+receipt_no.to_s+"&method="+kbz_method+"&nonce_str="+nounce_str.to_s+"&notify_url="+ kbz_callback_url + "&timestamp="+datetime+"&total_amount="+amount.to_s+"&trade_type="+kbz_trade_type+"&trans_currency="+ kbz_currency+"&version="+kbz_version+"&key="+kbz_api_key
params = "appid="+kbz_app_id+"&merch_code="+kbz_merch_code+"&merch_order_id="+receipt_no.to_s+"&method="+kbz_method+"&nonce_str="+nounce_str.to_s+"&notify_url="+ kbz_callback_url + "&timeout_express=20m&timestamp="+datetime+"&total_amount="+amount.to_s+"&trade_type="+kbz_trade_type+"&trans_currency="+ kbz_currency+"&version="+kbz_version+"&key="+kbz_api_key
Rails.logger.info params
sign = Digest::SHA256.hexdigest(params)
str = {"timestamp": datetime,
"method": kbz_method,
"notify_url": kbz_callback_url,
"nonce_str": nounce_str.to_s,
"sign_type": "SHA256",
"sign": sign,
"version": kbz_version,
"biz_content": {
"merch_order_id": receipt_no,
"merch_code": kbz_merch_code,
"appid": kbz_app_id,
"trade_type": kbz_trade_type,
"total_amount": amount.to_s,
"trans_currency": kbz_currency
}
}
str = {
"timestamp": datetime,
"method": kbz_method,
"notify_url": kbz_callback_url,
"nonce_str": nounce_str.to_s,
"sign_type": "SHA256",
"sign": sign,
"version": kbz_version,
"biz_content": {
"merch_order_id": receipt_no,
"merch_code": kbz_merch_code,
"appid": kbz_app_id,
"trade_type": kbz_trade_type,
"total_amount": amount.to_s,
"timeout_express": "20m",
"trans_currency": kbz_currency
}
}
result = HTTParty.post(kbz_provider_url,
:body => { :Request => str}.to_json,
@@ -51,4 +55,71 @@ class KbzPay
end
end
def self.query(receipt_no, current_user)
amount = 0
datetime = DateTime.now.strftime("%d%m%Y%H%M")
kbz_app_id = "kp1e78f7efddca190042638341afb88d"
kbz_merch_code = "200004"
kbz_method = 'kbz.payment.queryorder'
kbz_trade_type = "APP"
kbz_api_key = "code2lab123456"
kbz_version = "1.0"
kbz_provider_url = "http://api.kbzpay.com/payment/gateway/uat/queryorder"
kbz_currency = "MMK"
kbz_callback_url = "https://staging-v2.doemal.com/api/v3/ordering/kbz_callback"
nounce_str = SecureRandom.base64(32).first(32).upcase
params = "appid="+kbz_app_id+"&merch_code="+kbz_merch_code+"&merch_order_id="+receipt_no.to_s+"&method="+kbz_method+"&nonce_str="+nounce_str.to_s+"&timestamp="+datetime+"&version="+kbz_version+"&key="+kbz_api_key
Rails.logger.info params
sign = Digest::SHA256.hexdigest(params)
str = {"timestamp": datetime,
"method": kbz_method,
"nonce_str": nounce_str.to_s,
"sign_type": "SHA256",
"sign": sign,
"version": kbz_version,
"biz_content": {
"merch_order_id": receipt_no,
"merch_code": kbz_merch_code,
"appid": kbz_app_id
}
}
result = HTTParty.post(kbz_provider_url,
:body => { :Request => str}.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
)
Rails.logger.info result
Rails.logger.info "........................."
if result['Response']['result'] == "SUCCESS"
if result['Response']['trade_status'] == "PAY_SUCCESS"
merch_order_id = result['Response']['merch_order_id']
cash = result['Response']['total_amount']
sp = SalePayment.find(merch_order_id)
if !sp.nil?
if sp.payment_status == 'pending'
saleObj = Sale.find(sp.sale_id)
if sp.process_payment(saleObj, current_user, cash, KbzPay::KBZ_PAY)
amount = cash
end
end
end
# return true, "successfully paid by KBZ PAY"
elsif result['Response']['trade_status'] == "PAY_FAILED"
# return false, "pay failed by KBZ PAY"
elsif result['Response']['trade_status'] == "WAIT_PAY"
# return false , "Waiting to pay by KBZ PAY"
end
else
#FAIL result
# return false, "pay by KBZ PAY has failed"
end
return amount
end
end

6
app/models/payment.rb Normal file
View File

@@ -0,0 +1,6 @@
class Payment
# def self.pay(getCloudDomain, cash, sale_id, member_info, type, tax_type, path, latest_order_no, shop_detail, current_user, pay_from, sale_payment_id)
# end
end

View File

@@ -921,6 +921,7 @@ def self.daily_sales_list(from,to)
AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') = DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d')"
payments_total = Sale.select("CAST((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')) AS DATE) as sale_date,
SUM(case when (sale_payments.payment_method='KBZPay') then sale_payments.payment_amount else 0 end) as kbzpay_amount,
SUM(case when (sale_payments.payment_method='mpu') then sale_payments.payment_amount else 0 end) as mpu_amount,
SUM(case when (sale_payments.payment_method='master') then sale_payments.payment_amount else 0 end) as master_amount,
SUM(case when (sale_payments.payment_method='visa') then sale_payments.payment_amount else 0 end) as visa_amount,
@@ -970,6 +971,7 @@ def self.daily_sales_list(from,to)
:paypar_amount => pay.paypar_amount,
:unionpay_amount => pay.unionpay_amount,
:alipay_amount => pay.alipay_amount,
:kbzpay_amount => pay.kbzpay_amount,
:paymal_amount => pay.paymal_amount,
:dinga_amount => pay.dinga_amount,
:junctionpay_amount => pay.junctionpay_amount,
@@ -1453,6 +1455,7 @@ def self.get_payment_method_by_shift(shift_sale_range,shift,from,to,payment_type
SUM(case when (sale_payments.payment_method='paypar') then sale_payments.payment_amount else 0 end) as paypar_amount,
SUM(case when (sale_payments.payment_method='unionpay') then sale_payments.payment_amount else 0 end) as unionpay_amount,
SUM(case when (sale_payments.payment_method='alipay') then sale_payments.payment_amount else 0 end) as alipay_amount,
SUM(case when (sale_payments.payment_method='KBZPay') then sale_payments.payment_amount else 0 end) as kbzpay_amount,
SUM(case when (sale_payments.payment_method='paymal') then sale_payments.payment_amount else 0 end) as paymal_amount,
SUM(case when (sale_payments.payment_method='dinga') then sale_payments.payment_amount else 0 end) as dinga_amount,
SUM(case when (sale_payments.payment_method='JunctionPay') then sale_payments.payment_amount else 0 end) as junctionpay_amount,

View File

@@ -29,6 +29,37 @@ class SalePayment < ApplicationRecord
end
end
def self.get_kbz_pay_amount(sale_id, current_user)
amount = 0
sale_payment = SalePayment.where('sale_id=? and payment_status!=?', sale_id, 'dead').last
if !sale_payment.nil?
if sale_payment.payment_status == 'pending'
amount = KbzPay.query(sale_payment.id, current_user)
elsif sale_payment.payment_status == 'paid'
amount = sale_payment.payment_amount
end
end
return amount
end
def process_kbz_payment(sale_id, grand_total, pay_amount, status)
if status == 'pending'
SalePayment.where("sale_id=? and payment_method=? and payment_status=?", sale_id, KbzPay::KBZ_PAY, 'pending').update_all(:payment_status => 'dead')
end
self.sale_id = sale_id
self.payment_method = KbzPay::KBZ_PAY
self.payment_amount = self.payment_amount.to_i + pay_amount.to_i
if grand_total > 0
self.outstanding_amount = grand_total.to_i - pay_amount.to_i
else
self.outstanding_amount = self.outstanding_amount.to_i - pay_amount.to_i
end
self.payment_status = status
return self.save
end
def process_payment(invoice, action_by, cash_amount, payment_method,remark=nil,payment_for=false)
self.sale = invoice
self.received_amount = cash_amount
@@ -90,8 +121,8 @@ class SalePayment < ApplicationRecord
payment_status,membership_data = dinga_payment
when "GiftVoucher"
payment_status = giftvoucher_payment
when "KbzPay"
payment_status = giftvoucher_payment
when KbzPay::KBZ_PAY
payment_status = kbz_payment
else
puts "it was something else"
end
@@ -346,6 +377,7 @@ class SalePayment < ApplicationRecord
end
payment_status = false
self.payment_method = "cash"
self.payment_amount = self.received_amount
if !payment_for
@@ -354,8 +386,10 @@ class SalePayment < ApplicationRecord
credit_sale_payment = SalePayment.get_credit_total_left(self.sale_id)[0] ? SalePayment.get_credit_total_left(self.sale_id)[0].payment_amount.to_f : 0 ###need to calculate creditnote total in here
self.outstanding_amount = credit_sale_payment - self.received_amount.to_f
end
self.payment_status = "paid"
payment_status = self.save!
if !payment_for
sale_update_payment_status(self.received_amount,status)
end
@@ -586,11 +620,13 @@ class SalePayment < ApplicationRecord
def kbz_payment
payment_status = false
status, response = KbzPay.pay(amount, receipt_no)
if status
payment_status = true
return
end
self.payment_amount = self.received_amount
self.payment_reference = self.payment_reference
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
self.payment_status = "paid"
payment_status = self.save!
sale_update_payment_status(self.received_amount)
return payment_status
end
def sale_update_payment_status(paid_amount,check_foc = false)
@@ -602,6 +638,7 @@ class SalePayment < ApplicationRecord
sObj = Sale.find(self.sale_id)
is_credit = 0
is_foc = 0
is_kbz_pay = 0
method_status = false
sObj.sale_payments.each do |spay|
all_received_amount += spay.payment_amount.to_f
@@ -611,7 +648,10 @@ class SalePayment < ApplicationRecord
if spay.payment_method == "foc"
is_foc = 1
end
if spay.payment_method == "cash" || spay.payment_method == "foc" || spay.payment_method == "creditnote"
if spay.payment_method == KbzPay::KBZ_PAY
is_kbz_pay = 1
end
if spay.payment_method == "cash" || spay.payment_method == "foc" || spay.payment_method == "creditnote" || spay.payment_method == 'kbzpay'
method_status = true
end
end
@@ -629,6 +669,10 @@ class SalePayment < ApplicationRecord
self.sale.payment_status = "foc"
end
if is_kbz_pay > 0
self.sale.payment_status = 'paid'
end
self.sale.sale_status = "completed"
if MembershipSetting.find_by_rebate(1) && is_foc == 0 && is_credit == 0