import fs from 'fs/promises';
import path from 'path';
import { select } from '@evershop/postgres-query-builder';
import { CONSTANTS } from '../../../../lib/helpers.js';
import { translate } from '../../../../lib/locale/translate/translate.js';
import { debug, error } from '../../../../lib/log/logger.js';
import { sendEmail } from '../../../../lib/mail/emailHelper.js';
import { pool } from '../../../../lib/postgres/connection.js';
import { getConfig } from '../../../../lib/util/getConfig.js';
import { getValue } from '../../../../lib/util/registry.js';
import { EventData } from '../../../../types/event.js';
import { getStoreLanguage } from '../../../setting/services/setting.js';
// Renders into the shared email layout (header/logo + footer) like the other
// transactional emails, so the redesign is consistent across all of them.
// Overridable per store via `system.notification_emails.order_refunded.templatePath`.
const TEMPLATE = `{{#> emailLayout preheader=(t "A refund of \${amount} was issued for order #\${number}." amount=(currency refund.amount) number=order.order_number)}}
{{#if refund.isFullRefund}}{{t "Your order has been refunded"}}{{else}}{{t "A refund has been issued"}}{{/if}}
{{t "Order #\${number}" number=order.order_number}}
{{t "We've refunded \${amount} to your original payment method. Depending on your bank, it may take a few business days to appear on your statement." amount=(currency refund.amount)}}
| {{t "Order number"}} | #{{order.order_number}} |
| {{t "Refund amount"}} | {{currency refund.amount}} |
{{/emailLayout}}`;
/**
* Emails the customer when their payment is refunded (full or partial). Fires on
* every refund, whatever the payment method or trigger — admin action or gateway
* webhook — because they all converge on `recordRefund`, which emits
* `order_refunded`.
*/
export default async function sendRefundEmail(
data: EventData<'order_refunded'>
) {
try {
const config = getConfig('system.notification_emails.order_refunded', {
enabled: true
});
if (config?.enabled === false) {
return;
}
const order = await select()
.from('order')
.where('order_id', '=', data.orderId)
.load(pool);
if (!order || !order.customer_email) {
return;
}
let template = TEMPLATE;
if (config?.templatePath) {
const filePath = path.join(CONSTANTS.ROOTPATH, config.templatePath);
try {
await fs.access(filePath);
template = await fs.readFile(filePath, 'utf8');
} catch {
debug(
`Refund email template not found at ${filePath}. Using default template.`
);
}
}
const dynamicData = await getValue('orderRefundedEmailData', {
order,
refund: {
amount: data.amount,
currency: data.currency,
isFullRefund: data.isFullRefund,
transactionId: data.transactionId
}
});
// Off-request subscriber (D7): resolve the store locale explicitly and pass
// it so the subject and the body's currency helper agree.
const locale = await getStoreLanguage();
const subject = translate(
data.isFullRefund
? 'Your order has been refunded'
: 'A refund has been issued for your order',
{},
locale
);
const args = await getValue(
'orderRefundedEmailArguments',
{
to: order.customer_email,
subject,
template,
data: dynamicData,
locale
},
{ order }
);
await sendEmail('order_refunded', args);
} catch (e) {
error(e);
}
}