From cbd3f318d57d27ab7427edb40d1bb77f1c66f380 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Wed, 25 Nov 2026 00:11:38 +0000 Subject: [PATCH] fix(shop): accept an order when no payment method is connected A buyer can still place the order. It stays unpaid until an admin records money collected out of band, and that admin is then responsible for delivering the work. No invoice goes out while invoicing is off. --- apps/api/src/shop/admin-routes.ts | 11 ++++++++++ apps/api/src/shop/service.ts | 7 ++++++- apps/api/test/shop.test.ts | 25 +++++++++++++++++++++++ apps/app/src/i18n.ts | 2 ++ apps/app/src/pages/ShopPage.tsx | 10 +++++++++ apps/site/src/app/encomenda/[id]/page.tsx | 4 +++- packages/sdk/src/client.ts | 5 +++++ 7 files changed, 62 insertions(+), 2 deletions(-) diff --git a/apps/api/src/shop/admin-routes.ts b/apps/api/src/shop/admin-routes.ts index 92566a4..c7feaa4 100644 --- a/apps/api/src/shop/admin-routes.ts +++ b/apps/api/src/shop/admin-routes.ts @@ -388,6 +388,17 @@ export async function registerShopAdminRoutes(app: FastifyInstance, deps: { db: return { orders: views }; }); + /** Payment collected outside the shop. Delivery starts; the admin owns handing the work over. */ + app.post("/v1/orders/:id/payment-received", { preHandler: authenticate }, async (req) => { + const { id } = IdParams.parse(req.params); + const order = await db.order.findUnique({ where: { id } }); + if (!order) throw notFound("order_not_found"); + await requireMembership(db, req, order.organizationId, "ADMIN"); + if (order.status !== "PENDING") throw badRequest("order_not_pending"); + await shop.markPaid(id, "manual"); + return shop.orderView(id, null); + }); + app.post("/v1/orders/:id/items/:itemId/shipped", { preHandler: authenticate }, async (req) => { const { id, itemId } = z.object({ id: z.uuid(), itemId: z.uuid() }).parse(req.params); const order = await db.order.findUnique({ where: { id }, include: { items: { where: { id: itemId } } } }); diff --git a/apps/api/src/shop/service.ts b/apps/api/src/shop/service.ts index c5d36ad..758f797 100644 --- a/apps/api/src/shop/service.ts +++ b/apps/api/src/shop/service.ts @@ -497,7 +497,12 @@ export class ShopService { await this.markPaid(order.id, press ? "press" : "free"); return { order: await this.orderView(order.id, order.accessToken), checkoutUrl: null, token: order.accessToken }; } - if (!org.stripeSecretSealed || !secrets) throw new ShopError("payments_not_configured", 503); + // No payment method: the order is still accepted. An admin collects the money out of band + // and only then does delivery start. Invoicing stays off until a provider is connected. + if (!org.stripeSecretSealed || !secrets) { + this.deps.log.info({ orderId: order.id }, "shop: order accepted without a payment method"); + return { order: await this.orderView(order.id, order.accessToken), checkoutUrl: null, token: order.accessToken }; + } const checkout = await createCheckout( secrets.open(org.stripeSecretSealed), { diff --git a/apps/api/test/shop.test.ts b/apps/api/test/shop.test.ts index c4dbee6..aa83b93 100644 --- a/apps/api/test/shop.test.ts +++ b/apps/api/test/shop.test.ts @@ -212,6 +212,31 @@ describe("shop", () => { expect((await ctx.db.order.findUniqueOrThrow({ where: { id: order.id } })).status).toBe("FULFILLED"); }); + it("accepts an order when Stripe is off, and delivery waits until an admin records the payment", async () => { + const s = await setup(); + expect((await s.api("PUT", `/v1/orgs/${s.orgId}/shop-settings`, { stripeSecretKey: "", stripeWebhookSecret: "" })).statusCode).toBe(200); + const g = await gallery(s, { title: "Sem Stripe" }); + const created = await pub("POST", "/v1/public/orders", { + gallery: g.slug, + email: "mae@example.pt", + name: "Ana Costa", + country: "PT", + items: [{ photoId: s.photos[0], productId: s.digital.id, quantity: 1 }], + }); + expect(created.statusCode).toBe(201); + const body = created.json() as { order: { id: string; status: string; totalCents: number }; checkoutUrl: string | null }; + expect(body.checkoutUrl).toBeNull(); + expect(body.order).toMatchObject({ status: "PENDING" }); + expect(body.order.totalCents).toBeGreaterThan(0); + expect(await shop.claimDownloads(5)).toEqual([]); + + const paid = await s.api("POST", `/v1/orders/${body.order.id}/payment-received`); + expect(paid.statusCode).toBe(200); + expect(paid.json()).toMatchObject({ status: "PAID" }); + const [itemId] = await shop.claimDownloads(5); + expect(itemId).toEqual(expect.any(String)); + }); + it("reverse-charges VAT for EU businesses with a valid number, and gives free galleries away", async () => { const s = await setup(); const g = await gallery(s, { title: "Clube" }); diff --git a/apps/app/src/i18n.ts b/apps/app/src/i18n.ts index 050f6f7..a139308 100644 --- a/apps/app/src/i18n.ts +++ b/apps/app/src/i18n.ts @@ -1113,6 +1113,8 @@ export const t = { orderStatus: { PENDING: "por pagar", PAID: "paga", FULFILLED: "concluída", CANCELLED: "cancelada", REFUNDED: "reembolsada" }, itemStatus: { PENDING: "a preparar", RUNNING: "a preparar", READY: "pronto", SHIPPED: "enviado", FAILED: "falhou" }, markShipped: "Marcar enviada", + collectPayment: "Pagamento recebido", + confirmCollectPayment: "Recebeste o pagamento fora da loja? A encomenda passa a paga e ficas responsável por entregar o trabalho.", reverseCharge: "Autoliquidação", noOrders: "Sem encomendas.", productsHelp: "Preços com IVA incluído. JPEG: tamanho do lado maior (0 = original). Impressões: tamanho e prazo de envio.", diff --git a/apps/app/src/pages/ShopPage.tsx b/apps/app/src/pages/ShopPage.tsx index 0cf8c18..db07c47 100644 --- a/apps/app/src/pages/ShopPage.tsx +++ b/apps/app/src/pages/ShopPage.tsx @@ -387,6 +387,16 @@ function Orders({ orgId, isAdmin }: { orgId: string; isAdmin: boolean }) { {o.invoice.number} )} + {isAdmin && o.status === "PENDING" && !o.checkoutUrl && o.totalCents > 0 && ( + + )} {isAdmin && (o.status === "PAID" || o.status === "FULFILLED") && (