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 && ( + { + if (window.confirm(t.shop.confirmCollectPayment)) void api.recordPayment(o.id).then(replace); + }} + > + {t.shop.collectPayment} + + )} {isAdmin && (o.status === "PAID" || o.status === "FULFILLED") && ( {order.status === "PENDING" && ( - Assim que o pagamento for confirmado (por Multibanco pode demorar algumas horas) as fotos aparecem aqui e enviamos-te um email. + {order.checkoutUrl + ? "Assim que o pagamento for confirmado (por Multibanco pode demorar algumas horas) as fotos aparecem aqui e enviamos-te um email." + : "A encomenda ficou registada. O pagamento combina-se à parte; quando estiver confirmado, as fotos aparecem aqui e enviamos-te um email."} {order.checkoutUrl && ( <> {" "} diff --git a/packages/sdk/src/client.ts b/packages/sdk/src/client.ts index 5af4dcf..994f165 100644 --- a/packages/sdk/src/client.ts +++ b/packages/sdk/src/client.ts @@ -591,6 +591,11 @@ export class ApiClient { return this.request("POST", `/v1/orders/${orderId}/items/${itemId}/shipped`); } + /** Payment taken outside the shop. The caller is responsible for delivering the work. */ + recordPayment(orderId: string) { + return this.request("POST", `/v1/orders/${orderId}/payment-received`); + } + retryOrder(orderId: string) { return this.request("POST", `/v1/orders/${orderId}/retry`); }
- Assim que o pagamento for confirmado (por Multibanco pode demorar algumas horas) as fotos aparecem aqui e enviamos-te um email. + {order.checkoutUrl + ? "Assim que o pagamento for confirmado (por Multibanco pode demorar algumas horas) as fotos aparecem aqui e enviamos-te um email." + : "A encomenda ficou registada. O pagamento combina-se à parte; quando estiver confirmado, as fotos aparecem aqui e enviamos-te um email."} {order.checkoutUrl && ( <> {" "} diff --git a/packages/sdk/src/client.ts b/packages/sdk/src/client.ts index 5af4dcf..994f165 100644 --- a/packages/sdk/src/client.ts +++ b/packages/sdk/src/client.ts @@ -591,6 +591,11 @@ export class ApiClient { return this.request("POST", `/v1/orders/${orderId}/items/${itemId}/shipped`); } + /** Payment taken outside the shop. The caller is responsible for delivering the work. */ + recordPayment(orderId: string) { + return this.request("POST", `/v1/orders/${orderId}/payment-received`); + } + retryOrder(orderId: string) { return this.request("POST", `/v1/orders/${orderId}/retry`); }