This commit is contained in:
Myat Zin Wai Maw
2019-06-21 14:45:35 +06:30
4 changed files with 127 additions and 47 deletions

View File

@@ -1,14 +1,11 @@
class Api::Payment::MobilepaymentController < Api::ApiController class Api::Payment::MobilepaymentController < Api::ApiController
# skip_before_action :authenticate # skip_before_action :authenticate
def cash def cash
cash = params[:cash] cash = params[:cash]
sale_id = params[:sale_id] sale_id = params[:sale_id]
member_info = nil
type = params[:type]
tax_type = params[:tax_type]
path = request.fullpath
latest_order_no = nil
cashier_id = params[:cashier_id] cashier_id = params[:cashier_id]
current_user = Employee.find(cashier_id) current_user = Employee.find(cashier_id)
@@ -47,7 +44,17 @@ class Api::Payment::MobilepaymentController < Api::ApiController
else else
@out =false,"Something wrong!" @out =false,"Something wrong!"
sale_payment = SalePayment.new
sale_payment = sale_payment.process_payment(saleObj, current_user, cash, "cash")
if sale_payment[0]
@cash = {"change_amount"=>sale_payment[3],"balance_amount"=>sale_payment[4] ,"receipt_url"=>''}
end
end end
end end
def kbz_pay
end
end end

54
app/models/kbz_pay.rb Normal file
View File

@@ -0,0 +1,54 @@
class KbzPay
def self.pay(amount, receipt_no)
datetime = DateTime.now.strftime("%d%m%Y%H%M")
kbz_app_id = "kp1e78f7efddca190042638341afb88d"
kbz_merch_code = "200004"
kbz_method = 'kbz.payment.precreate'
kbz_trade_type = "PAY_BY_QRCODE"
kbz_api_key = "code2lab123456"
kbz_version = "1.0"
kbz_provider_url = "http://api.kbzpay.com/payment/gateway/uat/precreate"
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+"&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
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
}
}
result = HTTParty.post(kbz_provider_url,
:body => { :Request => str}.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
)
Rails.logger.info result
if result['Response']['result'] == "SUCCESS"
#TODO QR return
qr = result['Response']['qrCode']
return true, qr
else
return false, result['Response']
puts result['Response']
end
end
end

View File

@@ -44,7 +44,7 @@ class SalePayment < ApplicationRecord
invoice_sale_payments = invoice.sale_payments invoice_sale_payments = invoice.sale_payments
amount_due = invoice.grand_total amount_due = invoice.grand_total
end end
invoice_sale_payments.each do |payment| invoice_sale_payments.each do |payment|
if (payment.payment_status == "paid" ) if (payment.payment_status == "paid" )
amount_due = amount_due - payment.payment_amount amount_due = amount_due - payment.payment_amount
@@ -90,6 +90,8 @@ class SalePayment < ApplicationRecord
payment_status,membership_data = dinga_payment payment_status,membership_data = dinga_payment
when "GiftVoucher" when "GiftVoucher"
payment_status = giftvoucher_payment payment_status = giftvoucher_payment
when "KbzPay"
payment_status = giftvoucher_payment
else else
puts "it was something else" puts "it was something else"
end end
@@ -105,7 +107,7 @@ class SalePayment < ApplicationRecord
shift = ShiftSale.current_shift shift = ShiftSale.current_shift
shift_sale_id = shift.id shift_sale_id = shift.id
end end
remark = "#{self.sale_payment_id}||#{shift_sale_id} -> #{remark}" remark = "#{self.sale_payment_id}||#{shift_sale_id} -> #{remark}"
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by.name) sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by.name)
@@ -332,7 +334,7 @@ class SalePayment < ApplicationRecord
if sp.payment_method == "jcb" || sp.payment_method == "mpu" || sp.payment_method == "visa" || sp.payment_method == "master" || sp.payment_method == "unionpay" || sp.payment_method == "alipay" if sp.payment_method == "jcb" || sp.payment_method == "mpu" || sp.payment_method == "visa" || sp.payment_method == "master" || sp.payment_method == "unionpay" || sp.payment_method == "alipay"
return true; return true;
end end
end end
end end
private private
@@ -342,7 +344,7 @@ class SalePayment < ApplicationRecord
if sale_payments_data.nil? if sale_payments_data.nil?
status = true status = true
end end
payment_status = false payment_status = false
self.payment_method = "cash" self.payment_method = "cash"
self.payment_amount = self.received_amount self.payment_amount = self.received_amount
@@ -374,7 +376,7 @@ class SalePayment < ApplicationRecord
# add to sale item with foc # add to sale item with foc
sale_items = SaleItem.where("sale_id='#{ self.sale.sale_id }'") sale_items = SaleItem.where("sale_id='#{ self.sale.sale_id }'")
sale_items.each do|item| sale_items.each do|item|
SaleItem.update_existing_item(item.qty, item, self.sale.sale_id, "foc", item.unit_price, item.price) SaleItem.update_existing_item(item.qty, item, self.sale.sale_id, "foc", item.unit_price, item.price)
end end
@@ -385,11 +387,11 @@ class SalePayment < ApplicationRecord
self.outstanding_amount = 0.00 self.outstanding_amount = 0.00
self.payment_status = "paid" self.payment_status = "paid"
payment_status = self.save! payment_status = self.save!
# sale_update_payment_status(self.received_amount) # sale_update_payment_status(self.received_amount)
sale_update_payment_status(0) sale_update_payment_status(0)
return payment_status return payment_status
end end
def creditnote_payment(customer_id) def creditnote_payment(customer_id)
payment_status = false payment_status = false
@@ -404,7 +406,7 @@ class SalePayment < ApplicationRecord
return payment_status return payment_status
end end
def external_terminal_card_payment(method, payment_for=false) def external_terminal_card_payment(method, payment_for=false)
payment_status = false payment_status = false
self.payment_method = method self.payment_method = method
self.payment_amount = self.received_amount self.payment_amount = self.received_amount
@@ -582,7 +584,16 @@ class SalePayment < ApplicationRecord
return payment_status return payment_status
end end
def sale_update_payment_status(paid_amount,check_foc = false) def kbz_payment
payment_status = false
status, response = KbzPay.pay(amount, receipt_no)
if status
payment_status = true
return
end
end
def sale_update_payment_status(paid_amount,check_foc = false)
#update amount_outstanding #update amount_outstanding
self.sale.amount_received = self.sale.amount_received.to_f + paid_amount.to_f self.sale.amount_received = self.sale.amount_received.to_f + paid_amount.to_f
self.sale.save! self.sale.save!
@@ -617,12 +628,12 @@ class SalePayment < ApplicationRecord
else else
self.sale.payment_status = "foc" self.sale.payment_status = "foc"
end end
self.sale.sale_status = "completed" self.sale.sale_status = "completed"
if MembershipSetting.find_by_rebate(1) && is_foc == 0 && is_credit == 0 if MembershipSetting.find_by_rebate(1) && is_foc == 0 && is_credit == 0
response = rebat(sObj) response = rebat(sObj)
#record an payment in sale-audit #record an payment in sale-audit
remark = "#{response} Rebate- for Customer #{self.sale.customer_id} | Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} " remark = "#{response} Rebate- for Customer #{self.sale.customer_id} | Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
sale_audit = SaleAudit.record_paymal(sObj.sale_id, remark, 1) sale_audit = SaleAudit.record_paymal(sObj.sale_id, remark, 1)
@@ -645,7 +656,7 @@ class SalePayment < ApplicationRecord
end end
self.sale.save! self.sale.save!
if check_foc if check_foc
table_update_status(sObj) table_update_status(sObj)
update_shift update_shift
@@ -661,13 +672,13 @@ class SalePayment < ApplicationRecord
# update for cashier shift # update for cashier shift
def update_shift def update_shift
shift = ShiftSale.current_open_shift(self.action_by.id) shift = ShiftSale.current_open_shift(self.action_by.id)
if shift.nil? if shift.nil?
shift = ShiftSale.current_open_shift(self.sale.cashier_id) shift = ShiftSale.current_open_shift(self.sale.cashier_id)
end end
if !shift.nil? if !shift.nil?
shift.update(self.sale) shift.update(self.sale)
self.sale.shift_sale_id = shift.id self.sale.shift_sale_id = shift.id
@@ -695,7 +706,7 @@ class SalePayment < ApplicationRecord
else else
# extra_changed_amount = self.received_amount.to_f + credit_payment_left # extra_changed_amount = self.received_amount.to_f + credit_payment_left
shift.cash_sales = shift.cash_sales.to_f + (self.received_amount.to_f + credit_payment_left) shift.cash_sales = shift.cash_sales.to_f + (self.received_amount.to_f + credit_payment_left)
self.sale.amount_received = self.sale.amount_received.to_f - credit_payment_left self.sale.amount_received = self.sale.amount_received.to_f - credit_payment_left
self.sale.amount_changed = self.sale.amount_changed.to_f - credit_payment_left self.sale.amount_changed = self.sale.amount_changed.to_f - credit_payment_left
self.sale.save! self.sale.save!
@@ -707,7 +718,7 @@ class SalePayment < ApplicationRecord
shift.credit_sales = shift.credit_sales.to_f - self.received_amount.to_f shift.credit_sales = shift.credit_sales.to_f - self.received_amount.to_f
else else
shift.credit_sales = shift.credit_sales.to_f - (self.received_amount.to_f + credit_payment_left) shift.credit_sales = shift.credit_sales.to_f - (self.received_amount.to_f + credit_payment_left)
end end
shift.save shift.save
end end
end end
@@ -723,7 +734,7 @@ class SalePayment < ApplicationRecord
bookings.each do |tablebooking| bookings.each do |tablebooking|
if tablebooking.booking_status != 'moved' if tablebooking.booking_status != 'moved'
if tablebooking.sale_id if tablebooking.sale_id
if tablebooking.sale.sale_status != 'completed' && tablebooking.sale.sale_status != 'void' && tablebooking.sale.sale_status != 'spoile' && tablebooking.sale.sale_status != 'waste' if tablebooking.sale.sale_status != 'completed' && tablebooking.sale.sale_status != 'void' && tablebooking.sale.sale_status != 'spoile' && tablebooking.sale.sale_status != 'waste'
status = false status = false
sale_count += 1 sale_count += 1
else else
@@ -747,7 +758,7 @@ class SalePayment < ApplicationRecord
#if ENV["SERVER_MODE"] != 'cloud' #if ENV["SERVER_MODE"] != 'cloud'
# if ENV["SERVER_MODE"] == 'cloud' # if ENV["SERVER_MODE"] == 'cloud'
# from = request.subdomain + "." + request.domain # from = request.subdomain + "." + request.domain
# else # else
# from = "" # from = ""
# end # end
# ActionCable.server.broadcast "order_channel",table: table,type:type,from:from # ActionCable.server.broadcast "order_channel",table: table,type:type,from:from
@@ -776,7 +787,7 @@ class SalePayment < ApplicationRecord
end end
# overall_dis = SaleItem.get_overall_discount(sObj.id) # overall_dis = SaleItem.get_overall_discount(sObj.id)
overall_dis = sObj.total_discount overall_dis = sObj.total_discount
if credit != 1 if credit != 1
membership = MembershipSetting.find_by_membership_type("paypar_url") membership = MembershipSetting.find_by_membership_type("paypar_url")
memberaction = MembershipAction.find_by_membership_type("get_member_campaign") memberaction = MembershipAction.find_by_membership_type("get_member_campaign")
@@ -787,8 +798,8 @@ class SalePayment < ApplicationRecord
# Control for Paypar Cloud # Control for Paypar Cloud
begin begin
response = HTTParty.get(url, response = HTTParty.get(url,
:body => { :body => {
member_group_id:sObj.customer.membership_type, member_group_id:sObj.customer.membership_type,
merchant_uid:merchant_uid, merchant_uid:merchant_uid,
campaign_type_id: campaign_type_id, campaign_type_id: campaign_type_id,
@@ -802,7 +813,7 @@ class SalePayment < ApplicationRecord
response = { "status": false , "message": " Connection timeout" } response = { "status": false , "message": " Connection timeout" }
rescue OpenURI::HTTPError rescue OpenURI::HTTPError
response = { "status": false, "message": "Can't connect server"} response = { "status": false, "message": "Can't connect server"}
rescue SocketError rescue SocketError
response = { "status": false, "message": "Can't connect server"} response = { "status": false, "message": "Can't connect server"}
end end
@@ -822,7 +833,7 @@ class SalePayment < ApplicationRecord
if cm[:amount] > 0 if cm[:amount] > 0
data = {:type => a["rules_type"], :percentage => a["change_unit"].to_i * a["base_unit"].to_i} data = {:type => a["rules_type"], :percentage => a["change_unit"].to_i * a["base_unit"].to_i}
total_percentage = total_percentage + a["change_unit"].to_i * a["base_unit"].to_i total_percentage = total_percentage + a["change_unit"].to_i * a["base_unit"].to_i
type_arr.push(data) type_arr.push(data)
end end
end end
@@ -834,7 +845,7 @@ class SalePayment < ApplicationRecord
Rails.logger.debug type_arr.to_json Rails.logger.debug type_arr.to_json
rebate_arr =[] rebate_arr =[]
campaign_method.each do |a| campaign_method.each do |a|
data = {:type => a[:type], :amount => a[:amount]} data = {:type => a[:type], :amount => a[:amount]}
type_arr.each do |si| type_arr.each do |si|
@@ -846,14 +857,14 @@ class SalePayment < ApplicationRecord
actual = a[:amount].to_f - amount.to_f actual = a[:amount].to_f - amount.to_f
data[:amount] = actual data[:amount] = actual
end end
end end
end end
rebate_arr.push(data) rebate_arr.push(data)
end end
Rails.logger.debug "---------------Rebate Response----------------" Rails.logger.debug "---------------Rebate Response----------------"
Rails.logger.debug rebate_arr.to_json Rails.logger.debug rebate_arr.to_json
total_amount = rebate_prices - payparcost - overall_dis total_amount = rebate_prices - payparcost - overall_dis
Rails.logger.debug total_amount Rails.logger.debug total_amount
@@ -872,8 +883,8 @@ class SalePayment < ApplicationRecord
# Control for Paypar Cloud # Control for Paypar Cloud
begin begin
response = HTTParty.post(url, response = HTTParty.post(url,
:body => { :body => {
generic_customer_id:generic_customer_id , generic_customer_id:generic_customer_id ,
total_sale_transaction_amount: sObj.grand_total, total_sale_transaction_amount: sObj.grand_total,
merchant_uid:merchant_uid, merchant_uid:merchant_uid,
@@ -891,7 +902,7 @@ class SalePayment < ApplicationRecord
response = { "status": false , "message": "Connect To" } response = { "status": false , "message": "Connect To" }
rescue OpenURI::HTTPError rescue OpenURI::HTTPError
response = { "status": false, "message": "Can't connect server"} response = { "status": false, "message": "Can't connect server"}
rescue SocketError rescue SocketError
response = { "status": false, "message": "Can't connect server"} response = { "status": false, "message": "Can't connect server"}
end end
@@ -901,7 +912,7 @@ class SalePayment < ApplicationRecord
end end
end end
else else
response = { "status": "no_member", "message": "Not membership"} response = { "status": "no_member", "message": "Not membership"}
end end
end end
@@ -920,18 +931,18 @@ class SalePayment < ApplicationRecord
order_source_query = "(select orders.source FROM orders JOIN sale_orders so ON so.order_id=orders.order_id WHERE so.sale_id=s.sale_id GROUP BY so.sale_id)" order_source_query = "(select orders.source FROM orders JOIN sale_orders so ON so.order_id=orders.order_id WHERE so.sale_id=s.sale_id GROUP BY so.sale_id)"
query = SalePayment.select("s.receipt_no, sale_payments.sale_payment_id, query = SalePayment.select("s.receipt_no, sale_payments.sale_payment_id,
sale_payments.payment_method, sale_payments.payment_method,
SUM(sale_payments.payment_amount) as payment_amount, SUM(sale_payments.payment_amount) as payment_amount,
s.receipt_date as sale_date, s.receipt_date as sale_date,
s.sale_id, s.sale_id,
s.cashier_name as cashier_name, c.name as customer_name") s.cashier_name as cashier_name, c.name as customer_name")
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.joins("INNER JOIN customers c ON c.customer_id = s.customer_id") .joins("INNER JOIN customers c ON c.customer_id = s.customer_id")
if params[:type].nil? if params[:type].nil?
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}") query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
elsif params[:type] == "cashier" elsif params[:type] == "cashier"
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' OR #{order_source_query}='emenu' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}") query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' OR #{order_source_query}='emenu' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
else else
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}") query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
@@ -942,8 +953,8 @@ class SalePayment < ApplicationRecord
end end
def self.get_credit_amount_due_left(sale_id) def self.get_credit_amount_due_left(sale_id)
query = SalePayment.select("(SUM(sale_payments.payment_amount) - query = SalePayment.select("(SUM(sale_payments.payment_amount) -
(CASE WHEN SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id (CASE WHEN SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id
THEN SUM(sale_payments.payment_amount) ELSE 0 END)) as payment_amount") THEN SUM(sale_payments.payment_amount) ELSE 0 END)) as payment_amount")
.joins(" LEFT JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id") .joins(" LEFT JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id")
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'") .where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'")
@@ -951,7 +962,7 @@ class SalePayment < ApplicationRecord
end end
def self.get_credit_total_left(sale_id) def self.get_credit_total_left(sale_id)
query = SalePayment.select("(SUM(sale_payments.payment_amount) - query = SalePayment.select("(SUM(sale_payments.payment_amount) -
(SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) AS payment_amount (SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) AS payment_amount
FROM sale_payments FROM sale_payments
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
@@ -963,7 +974,7 @@ class SalePayment < ApplicationRecord
def self.get_sale_payment_for_credit(sale_data) def self.get_sale_payment_for_credit(sale_data)
query = sale_data.sale_payments query = sale_data.sale_payments
.joins(" JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id") .joins(" JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id")
.where("sa.action='SALEPAYMENT' AND sa.remark IS NOT NULL .where("sa.action='SALEPAYMENT' AND sa.remark IS NOT NULL
AND DATE_FORMAT(sale_payments.created_at,'%Y-%m-%d') = '#{DateTime.now.strftime('%Y-%m-%d')}' OR DATE_FORMAT(sale_payments.created_at,'%Y-%m-%d') = '#{Date.today.prev_day}' AND DATE_FORMAT(sale_payments.created_at,'%Y-%m-%d') = '#{DateTime.now.strftime('%Y-%m-%d')}' OR DATE_FORMAT(sale_payments.created_at,'%Y-%m-%d') = '#{Date.today.prev_day}'
") #AND sale_payments.payment_method!='cash' ") #AND sale_payments.payment_method!='cash'
.group("sale_payments.sale_payment_id") .group("sale_payments.sale_payment_id")

View File

@@ -1,2 +1,10 @@
json.set! :status, @out[0] # json.set! :status, @out[0]
json.set! :data, @out[1] # json.set! :data, @out[1]
#
if @cash
json.status true
else
json.status false
json.data @cash
end