paymentMethod.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * How a payment's method reads to a person.
  3. *
  4. * A payment stores its method as a code: one of the four the payments form
  5. * offers, or the vendor that took it online. The code is not a word anybody
  6. * should read; a Norwegian invoice saying "(card)" is a bug. The four are
  7. * translated by whoever draws them (the app in the viewer's language, a
  8. * printed or shared document in the document's), and a vendor is its own
  9. * name in every language. Anything else, from an old backup or an import,
  10. * prints as it was stored rather than not at all.
  11. */
  12. /** The methods the payments form offers, in the order it offers them. */
  13. export const PAYMENT_METHODS = ['cash', 'card', 'transfer', 'other'] as const
  14. export type PaymentMethod = (typeof PAYMENT_METHODS)[number]
  15. /** The vendors a payment can come in through online, as they write their own names. */
  16. const VENDOR_NAMES: Record<string, string> = {
  17. stripe: 'Stripe',
  18. paypal: 'PayPal',
  19. vipps: 'Vipps',
  20. paystack: 'Paystack',
  21. }
  22. export function isPaymentMethod(method: string): method is PaymentMethod {
  23. return (PAYMENT_METHODS as readonly string[]).includes(method)
  24. }
  25. export function paymentMethodLabel(
  26. method: string,
  27. translate: (method: PaymentMethod) => string
  28. ): string {
  29. if (isPaymentMethod(method)) return translate(method)
  30. return VENDOR_NAMES[method] ?? method
  31. }
  32. /**
  33. * A payment of less than nothing is money handed back: a refund booked in the
  34. * ledger and synced in, or a correction. It is listed as a refund, never as a
  35. * payment with a minus in front.
  36. */
  37. export function isRefund(amount: number): boolean {
  38. return amount < 0
  39. }