Files
medusa-store/packages/modules/payment/src/models/payment-collection.ts
Riqwan Thamir fa44e3f5a8 feat(dashboard,core-flows,js-sdk,types,link-modules,payment): ability to copy payment link (#8630)
what: 

- enables a button to create a payment link when a payment delta is present
- api to delete order payment collection
- adds a pending amount to payment collections

Note: Not the happiest with the decision on when to create a payment collection and when not to. The code should programatically create or delete payment collections currently to generate the right collection for the payment delta. Adding a more specific flow to create and manage a payment collection will help reduce this burden from the code path and onto CX/merchant.

Another issue I found is that the payment collection status doesn't get updated when payment is complete as it still gets stuck to "authorized" state

https://github.com/user-attachments/assets/037a10f9-3621-43c2-94ba-1ada4b0a041b
2024-08-20 10:30:17 +00:00

128 lines
3.1 KiB
TypeScript

import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
PaymentCollectionStatus,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Filter,
ManyToMany,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import Payment from "./payment"
import PaymentProvider from "./payment-provider"
import PaymentSession from "./payment-session"
type OptionalPaymentCollectionProps = "status" | DAL.ModelDateColumns
@Entity({ tableName: "payment_collection" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PaymentCollection {
[OptionalProps]?: OptionalPaymentCollectionProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
currency_code: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@MikroOrmBigNumberProperty({ nullable: true })
authorized_amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_authorized_amount: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({ nullable: true })
captured_amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_captured_amount: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({ nullable: true })
refunded_amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_refunded_amount: BigNumberRawValue | null = null
@Property({ columnType: "text", index: "IDX_payment_collection_region_id" })
region_id: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({
columnType: "timestamptz",
nullable: true,
index: "IDX_payment_collection_deleted_at",
})
deleted_at: Date | null = null
@Property({
columnType: "timestamptz",
nullable: true,
})
completed_at: Date | null = null
@Enum({
items: () => PaymentCollectionStatus,
default: PaymentCollectionStatus.NOT_PAID,
})
status: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID
@ManyToMany(() => PaymentProvider)
payment_providers = new Collection<Rel<PaymentProvider>>(this)
@OneToMany(() => PaymentSession, (ps) => ps.payment_collection, {
cascade: [Cascade.PERSIST] as any,
})
payment_sessions = new Collection<Rel<PaymentSession>>(this)
@OneToMany(() => Payment, (payment) => payment.payment_collection, {
cascade: [Cascade.PERSIST] as any,
})
payments = new Collection<Rel<Payment>>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "pay_col")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "pay_col")
}
}