-m fallback query order when occuers wss error
This commit is contained in:
aungthetkhaing
2025-05-30 14:03:33 +06:30
parent 26ff1da67e
commit 68dadf9959
4 changed files with 236 additions and 156 deletions

View File

@@ -250,6 +250,48 @@ class Foodcourt::QrpayController < BaseFoodcourtController
def test_pay
end
def check_payment_status
receipt_no = params[:receipt_no]
if receipt_no.blank?
render json: { status: 'ERROR', message: "Receipt number is missing" }, status: :unprocessable_entity
return
end
@paymethod = PaymentMethodSetting.find_by(payment_method: "MMQR")
if @paymethod.nil?
render json: { status: 'CONFIG_ERROR', message: "MMQR payment method not configured" }, status: :ok
return
end
begin
@merchant = KbzMerchant.new(@paymethod)
response = @merchant.query_order(merch_order_id: receipt_no)
if response[:data]['code'] == '0'
case response[:data]['trade_status']
when 'PAY_SUCCESS'
render json: { status: 'PAY_SUCCESS' }
else
render json: { status: response[:data]['trade_status'] || 'PENDING' }
end
else
render json: {
status: 'KBZ_ERROR',
message: "KBZ Error #{response[:data]['code']}: #{response['msg']}"
}, status: :ok
end
rescue StandardError => e
render json: {
status: 'SERVER_ERROR',
message: "Payment check failed: #{e.message}"
}, status: :internal_server_error
end
end
def cancel
# cancel orders and related
sale_id = params[:sale_id]

View File

@@ -84,7 +84,7 @@ class KbzMerchant
sign_type: 'SHA256',
version: '3.0',
biz_content: {
appid: @payment_method.gateway_url,
appid: @app_id,
merch_code: @payment_method.merchant_account_id,
merch_order_id: merch_order_id
}.compact

View File

@@ -256,28 +256,14 @@ $(document).ready(function() {
const $paymentWaiting = $('.payment-waiting');
const amountToReceive = <%= number_with_precision(@sale_data.grand_total, precision: precision.to_i) %>;
const receiptNo = $('#receipt_no').text();
const cable = ActionCable.createConsumer("wss://juicecorner-0mo.sx-fc.app/cable");
let paymentProcessed = false;
let fallbackTimeout;
const subscription = cable.subscriptions.create(
{
channel: "TestChannel",
receipt_no: receiptNo
},
{
connected() {
console.log("Nagato channel connected");
},
received(data) {
console.log("Received:", data);
if (data.status === "PAY_SUCCESS") {
function handlePaymentSuccess(){
if(paymentProcessed) return;
paymentProcessed = true;
clearTimeout(fallbackTimeout);
$('#cancel-btn').hide();
this.handlePaymentSuccess();
}
},
handlePaymentSuccess() {
$paymentWaiting.html(`
<img src="/image/mmqr.webp" alt="MMQR Payment" style="max-width: 120px; margin-bottom: 10px;">
<div class="payment-success text-center">
@@ -313,20 +299,71 @@ $(document).ready(function() {
}
});
}
function checkPaymentStatus() {
$.ajax({
url: '/foodcourt/qrpay/check_payment_status',
method: 'POST',
contentType: 'application/json',
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
data: JSON.stringify({ receipt_no: receiptNo }),
success: function(response) {
if (response.status === "PAY_SUCCESS") {
handlePaymentSuccess();
} else if (response.status === "CONFIG_ERROR") {
clearTimeout(fallbackTimeout);
} else {
if (!paymentProcessed) {
fallbackTimeout = setTimeout(checkPaymentStatus, 5000);
}
}
},
error: function() {
if (!paymentProcessed) {
fallbackTimeout = setTimeout(checkPaymentStatus, 5000);
}
}
});
}
const cable = ActionCable.createConsumer("wss://juicecorner-0mo.sx-fc.app/cable");
const subscription = cable.subscriptions.create(
{
channel: "TestChannel",
receipt_no: receiptNo
},
{
connected() {
console.log("Nagato channel connected");
fallbackTimeout = setTimeout(checkPaymentStatus, 60000);
},
received() {
console.log("Received:", data);
if (data.status === "PAY_SUCCESS" && !paymentProcessed) {
clearTimeout(fallbackTimeout);
handlePaymentSuccess();
}
},
disconnected() {
console.log("Nagato channel disconnected");
fallbackTimeout = setTimeout(checkPaymentStatus, 60000);
}
}
);
function customer_display_view(data, status) {
$.post('/foodcourt/customer_view', {
data: data,
status: status
}, function(result) {
// Optional success handler
}, 'json');
}
// Cancel order
$('#cancel-btn').on('click', function(e) {
const postData = {
sale_id: "<%= @sale_data.sale_id %>"

View File

@@ -750,6 +750,7 @@ scope "(:locale)", locale: /en|mm/ do
post 'qrpay/cancel' => 'qrpay#cancel'
get 'qrpay/test-payment' => 'qrpay#test_payment'
post 'qrpay/process_payment' => 'qrpay#create'
post 'qrpay/check_payment_status' => 'qrpay#check_payment_status'
end
end
end