From 069a8851753ab1add95eb081442cf4a455632325 Mon Sep 17 00:00:00 2001 From: Dev Team Date: Wed, 28 May 2025 14:40:45 +0630 Subject: [PATCH] listen websocket with action cable consumer --- app/views/foodcourt/qrpay/init.html.erb | 114 ++++++++++++------------ 1 file changed, 55 insertions(+), 59 deletions(-) diff --git a/app/views/foodcourt/qrpay/init.html.erb b/app/views/foodcourt/qrpay/init.html.erb index dc060573..d1965337 100644 --- a/app/views/foodcourt/qrpay/init.html.erb +++ b/app/views/foodcourt/qrpay/init.html.erb @@ -254,70 +254,66 @@ $(document).ready(function() { const $paymentWaiting = $('.payment-waiting'); const amountToReceive = <%= number_with_precision(@sale_data.grand_total, precision: precision.to_i) %>; - const $receiptNo = $('#receipt_no'); - const ws = new WebSocket("wss://juicecorner-0mo.sx-fc.app/cable"); + const receiptNo = $('#receipt_no').text(); + const cable = ActionCable.createConsumer("wss://juicecorner-0mo.sx-fc.app/cable"); - ws.onopen = function() { - console.log("Nagato channel connected"); - ws.send(JSON.stringify({ - command: "subscribe", - identifier: JSON.stringify({ - channel: "TestChannel", - receipt_no: $receiptNo.text() - }) - })); - }; + const subscription = cable.subscriptions.create( + { + channel: "TestChannel", + receipt_no: receiptNo + }, + { + connected() { + console.log("Nagato channel connected"); + }, - ws.onmessage = function(e) { - const msg = JSON.parse(e.data); - console.log("Received:", msg); + received(data) { + console.log("Received:", data); - if (msg.type === 'confirm_subscription') { - console.log("This world shall know pain"); + if (data.status === "PAY_SUCCESS") { + this.handlePaymentSuccess(); + } + }, + + handlePaymentSuccess() { + $paymentWaiting.html(` + MMQR Payment +
+ check_circle +

Payment Successful!

+

Amount Received: ${amountToReceive}

+
+ `); + + $.ajax({ + url: '/foodcourt/qrpay/process_payment', + method: 'POST', + contentType: 'application/json', + headers: { + 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') + }, + data: JSON.stringify({ sale_id: "<%= @sale_data.sale_id %>" }), + success: (data) => { + if (data.status) { + customer_display_view({ + msg: "Payment Successful", + paid_amount: amountToReceive, + receipt_no: receiptNo + }, "pay_success"); + + setTimeout(() => { + window.location.href = "/"; + }, 3000); + } + }, + error: (xhr, status, error) => { + console.log("Error:", error); + } + }); } + } + ); - if (msg?.message?.status === "PAY_SUCCESS") { - $paymentWaiting.html(` - MMQR Payment -
- check_circle -

Payment Successful!

-

Amount Received: ${amountToReceive}

-
- `); - - $.ajax({ - url: '/foodcourt/qrpay/process_payment', - method: 'POST', - contentType: 'application/json', - headers: { - 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') - }, - data: JSON.stringify({ sale_id: "<%= @sale_data.sale_id %>" }), - success: function(data) { - if (data.status) { - customer_display_view({ - msg: "Payment Successful", - paid_amount: amountToReceive, - receipt_no: $receiptNo.text() - }, "pay_success"); - - setTimeout(function() { - window.location.href = "/"; - }, 3000); - ws.close(); - } - }, - error: function(xhr, status, error) { - console.log("Error:", error); - } - }); - } - }; - - ws.onerror = function(e) { - console.error("WebSocket error:", e); - }; function customer_display_view(data, status) { $.post('/foodcourt/customer_view', {