Files
linumiq-invoice/app/routes/webhooks.orders.create.tsx
T
Gerhard Scheikl 93aec2f368 refactor(automations): detect manual payment via OrderTransaction.manualPaymentGateway
- Drop wireTransferGatewayNames from ShopSettings (new migration).
- Replace string-matching with a GraphQL query against
  Order.transactions[].manualPaymentGateway, the first-class flag
  Shopify exposes for any merchant-defined manual payment method.
- Both webhook handlers now fetch the order on the fly to classify it,
  removing the configurable gateway-names field from settings.
2026-05-09 20:31:31 +02:00

47 lines
1.7 KiB
TypeScript

import type { ActionFunctionArgs } from "react-router";
import { authenticate } from "../shopify.server";
import db from "../db.server";
import {
generateAndEmailInvoice,
isManualPaymentOrder,
} from "../services/invoice/automations.server";
/**
* orders/create — Automation 1: when a wire-transfer (manual-payment-gateway)
* order is placed, immediately generate and email the invoice (which includes
* the bank details + GiroCode) so the customer can pay. Other orders are
* ignored here; they're handled by orders/fulfilled (Automation 2).
*/
export const action = async ({ request }: ActionFunctionArgs) => {
const { shop, topic, payload, session, admin } = await authenticate.webhook(request);
console.log(`Received ${topic} webhook for ${shop}`);
if (!session || !admin) return new Response();
const settings = await db.shopSettings.findUnique({ where: { shopDomain: shop } });
if (!settings?.autoEmailOnWireTransferPlaced) return new Response();
const orderId = payload?.id;
if (orderId == null) return new Response();
const orderGid = `gid://shopify/Order/${String(orderId).replace(/^.*\//, "")}`;
const isManual = await isManualPaymentOrder(admin, orderGid);
if (!isManual) return new Response();
try {
const result = await generateAndEmailInvoice({
shopDomain: shop,
admin,
orderId,
customerLocale: typeof payload?.customer_locale === "string" ? payload.customer_locale : undefined,
});
if (!result.ok) {
console.warn(`auto-email (wire-transfer placed) failed for order ${orderId} on ${shop}: ${result.reason}`);
}
} catch (err) {
console.error(`auto-email (wire-transfer placed) crashed for order ${orderId} on ${shop}:`, err);
}
return new Response();
};