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:
const socket = new WebSocket("ws://localhost:5000"); // Use "localhost" for same device or replace with "IP address" for external connections.
// Response format
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.method === "WebSocketOpen" && data.DeviceName !== 'null') {
// Initialization successful
const deviceName = data.DeviceName; // Device name if supported by Easypay
}
};
Secure Connection Example:
const socket = new WebSocket("wss://localhost:9000"); // Use "localhost" for same device or replace with "IP address" for external connections.
// Response format
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.method === "WebSocketOpen" && data.DeviceName !== 'null') {
// Initialization successful
const deviceName = 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 Integration
function makePaymentWebSocket(socket, amount, customerReferenceNumber) {
// Prepare the payment data
const paymentData = [
{
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 WebSocket
try {
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 user
return;
}
// Event listener for handling responses
socket.onmessage = (event) => {
try {
const responseData = JSON.parse(event.data);
// Simplified Error Handling
if (responseData.error || responseData === false) {
console.error("Payment Error:", responseData.error || "Invalid response.");
// Optionally, notify the user about the error
return;
} else {
const paymentResult = 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:
const socket = new WebSocket("wss://your-ip-address:9000");
socket.onopen = () => {
console.log("WebSocket connection established.");
makePaymentWebSocket(socket, 100, "1234");
};
socket.onclose = () => {
console.log("WebSocket connection closed.");
};