53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import { env } from "../../config/env";
|
|
const forcedFailAll = String(env.MQTT_PUBLISH_FORCE_FAIL_ALL).toLowerCase() === "true";
|
|
const forcedFailDevices = new Set(String(env.MQTT_PUBLISH_FORCE_FAIL_DEVICE_IDS)
|
|
.split(",")
|
|
.map((item) => item.trim())
|
|
.filter(Boolean));
|
|
function shouldForceFail(deviceId) {
|
|
return forcedFailAll || forcedFailDevices.has(deviceId);
|
|
}
|
|
export function buildPaymentSuccessPayload(input) {
|
|
const displayAmount = `${input.amount.toLocaleString("id-ID")}`;
|
|
return {
|
|
message_type: "payment_success",
|
|
device_id: input.device_id,
|
|
event_id: input.event_id,
|
|
transaction_id: input.transaction_id,
|
|
merchant_id: input.merchant_id,
|
|
merchant_name: input.merchant_name,
|
|
amount: input.amount,
|
|
currency: input.currency,
|
|
paid_at: input.paid_at,
|
|
partner_reference: input.partner_reference,
|
|
audio_text: `Pembayaran diterima ${input.currency} ${displayAmount}`,
|
|
display_text: `Pembayaran diterima Rp${displayAmount}`
|
|
};
|
|
}
|
|
export function makePaymentSuccessTopic(deviceId) {
|
|
return `devices/${deviceId}/downlink/payment/success`;
|
|
}
|
|
export async function publishPaymentSuccess(payload) {
|
|
const publishedAt = new Date().toISOString();
|
|
const topic = makePaymentSuccessTopic(payload.device_id);
|
|
if (shouldForceFail(payload.device_id)) {
|
|
return {
|
|
ok: false,
|
|
topic,
|
|
qos: 1,
|
|
retained: false,
|
|
publishedAt,
|
|
reason: "MQTT_PUBLISH_SIMULATED_FAILURE",
|
|
payload
|
|
};
|
|
}
|
|
return {
|
|
ok: true,
|
|
topic,
|
|
qos: 1,
|
|
retained: false,
|
|
publishedAt,
|
|
payload
|
|
};
|
|
}
|