Click Running in Background to keep the service active.
2- Create WebSocket Connection:
Easypay service operates on two ports:
5000: Insecure connection (HTTP)
9000: Secure connection (HTTPS)
Insecure Connection Example:
constsocket=newWebSocket("ws://localhost:5000"); // Use "localhost" for same device or replace with "IP address" for external connections.// Response formatsocket.onmessage= (event) => {constdata=JSON.parse(event.data);if (data.method ==="WebSocketOpen"&&data.DeviceName !=='null') {// Initialization successfulconstdeviceName=data.DeviceName; // Device name if supported by Easypay }};
Secure Connection Example:
constsocket=newWebSocket("wss://localhost:9000"); // Use "localhost" for same device or replace with "IP address" for external connections.// Response formatsocket.onmessage= (event) => {constdata=JSON.parse(event.data);if (data.method ==="WebSocketOpen"&&data.DeviceName !=='null') {// Initialization successfulconstdeviceName=data.DeviceName; // Device name if supported by Easypay }};
Purchase
Important: The amount should be entered without decimals. For example, 100 represents 1.00 SAR, and 1455 represents 14.55 SAR. The maximum length is 12 digits, including the exponent (e.g., 123456789012 equals 1,234,567,890.12 SAR).
Reference ID: You may pass a Reference ID in the customerReferenceNumber field to attach it to the transaction. Otherwise, set it to null.
Processing a Purchase Request Using WiFi Integration
To initiate a payment using the WiFi Integration method, establish a WebSocket connection and communicate with the Easypay service in real-time.
Example Code:
// Function to make a payment using WebSocket IntegrationfunctionmakePaymentWebSocket(socket, amount, customerReferenceNumber) {// Prepare the payment dataconstpaymentData= [ { method:"PURCHASE", amount: amount,// [Required] Amount to set (e.g., 100 represents 1.00 SAR) customerReferenceNumber: customerReferenceNumber ||null// [Optional] Any string as a reference number } ];// Send the payment request through the WebSockettry {socket.send(JSON.stringify(paymentData));console.log("Purchase request sent:", paymentData); } catch (sendError) {console.error("Failed to send purchase request:", sendError);// Optionally, implement retry logic or notify the userreturn; }// Event listener for handling responsessocket.onmessage= (event) => {try {constresponseData=JSON.parse(event.data);// Simplified Error Handlingif (responseData.error || responseData ===false) {console.error("Payment Error:",responseData.error ||"Invalid response.");// Optionally, notify the user about the errorreturn; } else {constpaymentResult=JSON.parse(responseData.message);if (paymentResult.is_approved) {console.log("Payment Successful:", paymentResult);// Optionally, proceed with post-payment actions } else {console.warn("Payment Rejected:", paymentResult);// Optionally, notify the user about the rejection } } } catch (parseError) {console.error("Failed to parse WebSocket message:", parseError);// Optionally, handle parsing errors or notify the user } };}// Example usage:constsocket=newWebSocket("wss://your-ip-address:9000");socket.onopen= () => {console.log("WebSocket connection established.");makePaymentWebSocket(socket,100,"1234");};socket.onclose= () => {console.log("WebSocket connection closed.");};
To process a refund, provide the transactionUuid of the original transaction you wish to refund.
Processing a Refund Using WiFi Integration
To initiate a refund using the WiFi Integration method, establish a WebSocket connection and communicate with the Easypay service in real-time.
Example Code:
// Function to process a refund using WebSocket IntegrationfunctionprocessRefundWebSocket(socket, transactionUuid, amount, customerReferenceNumber) {// Prepare the refund dataconstrefundData= [ { method:"REFUND", transactionUuid: transactionUuid,// [Required] Original transaction UUID. amount: amount,// [Required] Amount to refund. customerReferenceNumber: customerReferenceNumber ||null// [Optional] Any string as a reference number. } ];// Send the refund request through the WebSockettry {socket.send(JSON.stringify(refundData));console.log("Refund request sent:", refundData); } catch (sendError) {console.error("Failed to send refund request:", sendError);// Optionally, implement retry logic or notify the userreturn; }// Event listener for handling responsessocket.onmessage= (event) => {try {constresponseData=JSON.parse(event.data);// Simplified Error Handlingif (responseData.error || responseData ===false) {console.error("Refund Error:",responseData.error ||"Invalid response.");// Optionally, notify the user about the errorreturn; } else {constrefundResult=JSON.parse(responseData.message);if (refundResult.is_approved) {console.log("Refund Successful:", refundResult);// Optionally, proceed with post-refund actions } else {console.warn("Refund Rejected:", refundResult);// Optionally, notify the user about the rejection } } } catch (parseError) {console.error("Failed to parse WebSocket message:", parseError);// Optionally, handle parsing errors or notify the user } };}// Example usage:constsocket=newWebSocket("wss://your-ip-address:9000");socket.onopen= () => {console.log("WebSocket connection established.");processRefundWebSocket(socket,"f5079b9d-b61c-4180-8a4d-9780f7a9cd8f",100,"ORDER1234");};socket.onclose= () => {console.log("WebSocket connection closed.");};
To initiate a Reconcile using the WiFi Integration method, establish a WebSocket connection and communicate with the Easypay service in real-time.
Example Code:
// Function to reconcile transactions using WebSocket IntegrationfunctionreconcileWebSocket(socket) {// Prepare the reconcile datavar data = {"method":"reconcile" };// Send the reconcile request through the WebSockettry {socket.send(JSON.stringify(data));console.log("Reconciliation request sent:", data); } catch (sendError) {console.error("Failed to send reconciliation request:", sendError);// Optionally, implement retry logic or notify the userreturn; }// Event listener for handling responsessocket.onmessage= (event) => {try {constresponseData=JSON.parse(event.data);// Simplified Error Handlingif (responseData.error || responseData ===false) {console.error("Reconciliation Error:",responseData.error ||"Invalid response.");// Optionally, notify the user about the errorreturn; } else {constreconcileResult=JSON.parse(responseData.message);if (reconcileResult .is_balanced !==undefined&&reconcileResult .is_balanced.value) {console.log("Reconciliation Successful:", reconcileResult );// Optionally, proceed with post-reconciliation actions } else {console.warn("Reconciliation Rejected:", reconcileResult );// Optionally, notify the user about the rejection } } } catch (parseError) {console.error("Failed to parse WebSocket message:", parseError);// Optionally, handle parsing errors or notify the user } };}// Example usage:constsocket=newWebSocket("wss://your-ip-address:9000");socket.onopen= () => {console.log("WebSocket connection established.");reconcileWebSocket(socket);};socket.onclose= () => {console.log("WebSocket connection closed.");};
Receipt Management
Printing the Last Transaction Receipt
// Function to print the last transaction receipt using WebSocket IntegrationfunctionprintLastReceiptWebSocket(socket) {// Prepare the print request dataconstprintRequest= { method:"PrintLastResult" };// Send the print request through the WebSockettry {socket.send(JSON.stringify(printRequest));console.log("Print request sent:", printRequest); } catch (sendError) {console.error("Failed to send print request:", sendError);// Optionally, implement retry logic or notify the userreturn; }}// Example usage:constsocket=newWebSocket("wss://your-ip-address:9000");socket.onopen= () => {console.log("WebSocket connection established.");printLastReceiptWebSocket(socket);};socket.onclose= () => {console.log("WebSocket connection closed.");};