listen websocket with action cable consumer

This commit is contained in:
Dev Team
2025-05-28 14:40:45 +06:30
parent f724f7a91a
commit 069a885175

View File

@@ -254,29 +254,28 @@
$(document).ready(function() { $(document).ready(function() {
const $paymentWaiting = $('.payment-waiting'); const $paymentWaiting = $('.payment-waiting');
const amountToReceive = <%= number_with_precision(@sale_data.grand_total, precision: precision.to_i) %>; const amountToReceive = <%= number_with_precision(@sale_data.grand_total, precision: precision.to_i) %>;
const $receiptNo = $('#receipt_no'); const receiptNo = $('#receipt_no').text();
const ws = new WebSocket("wss://juicecorner-0mo.sx-fc.app/cable"); const cable = ActionCable.createConsumer("wss://juicecorner-0mo.sx-fc.app/cable");
ws.onopen = function() { const subscription = cable.subscriptions.create(
console.log("Nagato channel connected"); {
ws.send(JSON.stringify({
command: "subscribe",
identifier: JSON.stringify({
channel: "TestChannel", channel: "TestChannel",
receipt_no: $receiptNo.text() receipt_no: receiptNo
}) },
})); {
}; connected() {
console.log("Nagato channel connected");
},
ws.onmessage = function(e) { received(data) {
const msg = JSON.parse(e.data); console.log("Received:", data);
console.log("Received:", msg);
if (msg.type === 'confirm_subscription') { if (data.status === "PAY_SUCCESS") {
console.log("This world shall know pain"); this.handlePaymentSuccess();
} }
},
if (msg?.message?.status === "PAY_SUCCESS") { handlePaymentSuccess() {
$paymentWaiting.html(` $paymentWaiting.html(`
<img src="/image/mmqr.webp" alt="MMQR Payment" style="max-width: 120px; margin-bottom: 10px;"> <img src="/image/mmqr.webp" alt="MMQR Payment" style="max-width: 120px; margin-bottom: 10px;">
<div class="payment-success text-center"> <div class="payment-success text-center">
@@ -294,30 +293,27 @@ $(document).ready(function() {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}, },
data: JSON.stringify({ sale_id: "<%= @sale_data.sale_id %>" }), data: JSON.stringify({ sale_id: "<%= @sale_data.sale_id %>" }),
success: function(data) { success: (data) => {
if (data.status) { if (data.status) {
customer_display_view({ customer_display_view({
msg: "Payment Successful", msg: "Payment Successful",
paid_amount: amountToReceive, paid_amount: amountToReceive,
receipt_no: $receiptNo.text() receipt_no: receiptNo
}, "pay_success"); }, "pay_success");
setTimeout(function() { setTimeout(() => {
window.location.href = "/"; window.location.href = "/";
}, 3000); }, 3000);
ws.close();
} }
}, },
error: function(xhr, status, error) { error: (xhr, status, error) => {
console.log("Error:", error); console.log("Error:", error);
} }
}); });
} }
}; }
);
ws.onerror = function(e) {
console.error("WebSocket error:", e);
};
function customer_display_view(data, status) { function customer_display_view(data, status) {
$.post('/foodcourt/customer_view', { $.post('/foodcourt/customer_view', {