chore(payment): Payment module DML (#10553)
* chore(payment): Payment module DML * rm log * migration
This commit is contained in:
committed by
GitHub
parent
91cd9aad47
commit
0264294ab5
@@ -1,7 +1,7 @@
|
||||
import { IPaymentModuleService } from "@medusajs/framework/types"
|
||||
import { Module, Modules, promiseAll } from "@medusajs/framework/utils"
|
||||
import { PaymentModuleService } from "@services"
|
||||
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import { PaymentModuleService } from "@services"
|
||||
import {
|
||||
createPaymentCollections,
|
||||
createPayments,
|
||||
@@ -24,10 +24,9 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual([
|
||||
"payment",
|
||||
"paymentCollection",
|
||||
"paymentProvider",
|
||||
"paymentSession",
|
||||
"payment",
|
||||
"refundReason",
|
||||
])
|
||||
|
||||
@@ -54,15 +53,6 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
|
||||
field: "paymentCollection",
|
||||
},
|
||||
},
|
||||
paymentProvider: {
|
||||
id: {
|
||||
linkable: "payment_provider_id",
|
||||
entity: "PaymentProvider",
|
||||
primaryKey: "id",
|
||||
serviceName: "payment",
|
||||
field: "paymentProvider",
|
||||
},
|
||||
},
|
||||
paymentSession: {
|
||||
id: {
|
||||
field: "paymentSession",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,45 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
export class Migration20241211151053 extends Migration {
|
||||
|
||||
async up(): Promise<void> {
|
||||
this.addSql('alter table if exists "payment_session" drop constraint if exists "payment_session_payment_collection_id_foreign";');
|
||||
|
||||
this.addSql('alter table if exists "payment" drop constraint if exists "payment_payment_collection_id_foreign";');
|
||||
|
||||
this.addSql('alter table if exists "payment_provider" add column if not exists "created_at" timestamptz not null default now(), add column if not exists "updated_at" timestamptz not null default now(), add column if not exists "deleted_at" timestamptz null;');
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_payment_provider_deleted_at" ON "payment_provider" (deleted_at) WHERE deleted_at IS NULL;');
|
||||
|
||||
this.addSql('alter table if exists "payment_session" alter column "data" type jsonb using ("data"::jsonb);');
|
||||
this.addSql('alter table if exists "payment_session" alter column "data" set default \'{}\';');
|
||||
this.addSql('alter table if exists "payment_session" add constraint "payment_session_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment_collection" ("id") on update cascade on delete cascade;');
|
||||
|
||||
this.addSql('alter table if exists "payment" add constraint "payment_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment_collection" ("id") on update cascade on delete cascade;');
|
||||
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_refund_reason_deleted_at" ON "refund_reason" (deleted_at) WHERE deleted_at IS NULL;');
|
||||
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_refund_refund_reason_id" ON "refund" (refund_reason_id) WHERE deleted_at IS NULL;');
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql('alter table if exists "payment_session" drop constraint if exists "payment_session_payment_collection_id_foreign";');
|
||||
|
||||
this.addSql('alter table if exists "payment" drop constraint if exists "payment_payment_collection_id_foreign";');
|
||||
|
||||
this.addSql('drop index if exists "IDX_payment_provider_deleted_at";');
|
||||
this.addSql('alter table if exists "payment_provider" drop column if exists "created_at";');
|
||||
this.addSql('alter table if exists "payment_provider" drop column if exists "updated_at";');
|
||||
this.addSql('alter table if exists "payment_provider" drop column if exists "deleted_at";');
|
||||
|
||||
this.addSql('alter table if exists "payment_session" alter column "data" drop default;');
|
||||
this.addSql('alter table if exists "payment_session" alter column "data" type jsonb using ("data"::jsonb);');
|
||||
this.addSql('alter table if exists "payment_session" add constraint "payment_session_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment_collection" ("id") on update cascade;');
|
||||
|
||||
this.addSql('alter table if exists "payment" add constraint "payment_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment_collection" ("id") on update cascade;');
|
||||
|
||||
this.addSql('drop index if exists "IDX_refund_reason_deleted_at";');
|
||||
|
||||
this.addSql('drop index if exists "IDX_refund_refund_reason_id";');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +1,21 @@
|
||||
import { BigNumberRawValue } from "@medusajs/framework/types"
|
||||
import {
|
||||
BigNumber,
|
||||
MikroOrmBigNumberProperty,
|
||||
generateEntityId,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Payment from "./payment"
|
||||
|
||||
type OptionalCaptureProps = "created_at"
|
||||
|
||||
@Entity({ tableName: "capture" })
|
||||
export default class Capture {
|
||||
[OptionalProps]?: OptionalCaptureProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
amount: BigNumber | number
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_amount: BigNumberRawValue
|
||||
|
||||
@ManyToOne(() => Payment, {
|
||||
onDelete: "cascade",
|
||||
index: "IDX_capture_payment_id",
|
||||
fieldName: "payment_id",
|
||||
const Capture = model
|
||||
.define("Capture", {
|
||||
id: model.id({ prefix: "capt" }).primaryKey(),
|
||||
amount: model.bigNumber(),
|
||||
payment: model.belongsTo(() => Payment, {
|
||||
mappedBy: "captures",
|
||||
}),
|
||||
metadata: model.json().nullable(),
|
||||
created_by: model.text().nullable(),
|
||||
})
|
||||
payment!: Payment
|
||||
.indexes([
|
||||
{
|
||||
name: "IDX_capture_payment_id",
|
||||
on: ["payment_id"],
|
||||
},
|
||||
])
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@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_capture_deleted_at",
|
||||
})
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
created_by: string | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "capt")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "capt")
|
||||
}
|
||||
}
|
||||
export default Capture
|
||||
|
||||
@@ -1,127 +1,40 @@
|
||||
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
|
||||
import {
|
||||
BigNumber,
|
||||
DALUtils,
|
||||
MikroOrmBigNumberProperty,
|
||||
PaymentCollectionStatus,
|
||||
generateEntityId,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Cascade,
|
||||
Collection,
|
||||
Entity,
|
||||
Enum,
|
||||
Filter,
|
||||
ManyToMany,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model, PaymentCollectionStatus } from "@medusajs/framework/utils"
|
||||
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()",
|
||||
const PaymentCollection = model
|
||||
.define("PaymentCollection", {
|
||||
id: model.id({ prefix: "pay_col" }).primaryKey(),
|
||||
currency_code: model.text(),
|
||||
amount: model.bigNumber(),
|
||||
authorized_amount: model.bigNumber().nullable(),
|
||||
captured_amount: model.bigNumber().nullable(),
|
||||
refunded_amount: model.bigNumber().nullable(),
|
||||
region_id: model.text(),
|
||||
completed_at: model.dateTime().nullable(),
|
||||
status: model
|
||||
.enum(PaymentCollectionStatus)
|
||||
.default(PaymentCollectionStatus.NOT_PAID),
|
||||
metadata: model.json().nullable(),
|
||||
payment_providers: model.manyToMany(() => PaymentProvider, {
|
||||
mappedBy: "payment_collections",
|
||||
}),
|
||||
payment_sessions: model.hasMany(() => PaymentSession, {
|
||||
mappedBy: "payment_collection",
|
||||
}),
|
||||
payments: model.hasMany(() => Payment, {
|
||||
mappedBy: "payment_collection",
|
||||
}),
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
.cascades({
|
||||
delete: ["payment_sessions", "payments"],
|
||||
})
|
||||
updated_at: Date
|
||||
.indexes([
|
||||
{
|
||||
name: "IDX_payment_collection_region_id",
|
||||
on: ["region_id"],
|
||||
},
|
||||
])
|
||||
|
||||
@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")
|
||||
}
|
||||
}
|
||||
export default PaymentCollection
|
||||
|
||||
@@ -1,64 +1,13 @@
|
||||
import { generateEntityId } from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
|
||||
@Entity({ tableName: "payment_method_token" })
|
||||
export default class PaymentMethodToken {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
const PaymentMethodToken = model.define("PaymentMethodToken", {
|
||||
id: model.id({ prefix: "paymttok" }).primaryKey(),
|
||||
provider_id: model.text(),
|
||||
data: model.json().nullable(),
|
||||
name: model.text(),
|
||||
type_detail: model.text().nullable(),
|
||||
description_detail: model.text().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
})
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
provider_id: string
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
data: Record<string, unknown> | null = null
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
name: string
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
type_detail: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
description_detail: string | null = null
|
||||
|
||||
@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_method_token_deleted_at",
|
||||
})
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "paymttok")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "paymttok")
|
||||
}
|
||||
}
|
||||
export default PaymentMethodToken
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import { Entity, OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import PaymentCollection from "./payment-collection"
|
||||
|
||||
@Entity({ tableName: "payment_provider" })
|
||||
export default class PaymentProvider {
|
||||
[OptionalProps]?: "is_enabled"
|
||||
const PaymentProvider = model.define("PaymentProvider", {
|
||||
id: model.id().primaryKey(),
|
||||
is_enabled: model.boolean().default(true),
|
||||
payment_collections: model.manyToMany(() => PaymentCollection, {
|
||||
mappedBy: "payment_providers",
|
||||
}),
|
||||
})
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({
|
||||
default: true,
|
||||
columnType: "boolean",
|
||||
})
|
||||
is_enabled: boolean = true
|
||||
}
|
||||
export default PaymentProvider
|
||||
|
||||
@@ -1,120 +1,37 @@
|
||||
import { BigNumberRawValue } from "@medusajs/framework/types"
|
||||
import {
|
||||
BigNumber,
|
||||
generateEntityId,
|
||||
MikroOrmBigNumberProperty,
|
||||
PaymentSessionStatus,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
Enum,
|
||||
ManyToOne,
|
||||
OneToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model, PaymentSessionStatus } from "@medusajs/framework/utils"
|
||||
import Payment from "./payment"
|
||||
import PaymentCollection from "./payment-collection"
|
||||
|
||||
@Entity({ tableName: "payment_session" })
|
||||
export default class PaymentSession {
|
||||
[OptionalProps]?: "status" | "data"
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
currency_code: string
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
amount: BigNumber | number
|
||||
|
||||
@Property({
|
||||
columnType: "jsonb",
|
||||
const PaymentSession = model
|
||||
.define("PaymentSession", {
|
||||
id: model.id({ prefix: "payses" }).primaryKey(),
|
||||
currency_code: model.text(),
|
||||
amount: model.bigNumber(),
|
||||
provider_id: model.text(),
|
||||
data: model.json().default({}),
|
||||
context: model.json().nullable(),
|
||||
status: model
|
||||
.enum(PaymentSessionStatus)
|
||||
.default(PaymentSessionStatus.PENDING),
|
||||
authorized_at: model.dateTime().nullable(),
|
||||
payment_collection: model.belongsTo<() => typeof PaymentCollection>(
|
||||
() => PaymentCollection,
|
||||
{
|
||||
mappedBy: "payment_sessions",
|
||||
}
|
||||
),
|
||||
payment: model
|
||||
.hasOne(() => Payment, {
|
||||
mappedBy: "payment_session",
|
||||
})
|
||||
.nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
})
|
||||
raw_amount: BigNumberRawValue
|
||||
.indexes([
|
||||
{
|
||||
name: "IDX_payment_session_payment_collection_id",
|
||||
on: ["payment_collection_id"],
|
||||
},
|
||||
])
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
provider_id: string
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
data: Record<string, unknown> = {}
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
context: Record<string, unknown> | null
|
||||
|
||||
@Enum({
|
||||
items: () => PaymentSessionStatus,
|
||||
})
|
||||
status: PaymentSessionStatus = PaymentSessionStatus.PENDING
|
||||
|
||||
@Property({
|
||||
columnType: "timestamptz",
|
||||
nullable: true,
|
||||
})
|
||||
authorized_at: Date | null = null
|
||||
|
||||
@ManyToOne(() => PaymentCollection, {
|
||||
persist: false,
|
||||
})
|
||||
payment_collection: Rel<PaymentCollection>
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => PaymentCollection,
|
||||
columnType: "text",
|
||||
index: "IDX_payment_session_payment_collection_id",
|
||||
fieldName: "payment_collection_id",
|
||||
mapToPk: true,
|
||||
})
|
||||
payment_collection_id: string
|
||||
|
||||
@OneToOne({
|
||||
entity: () => Payment,
|
||||
nullable: true,
|
||||
mappedBy: "payment_session",
|
||||
})
|
||||
payment?: Rel<Payment> | null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@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_session_deleted_at",
|
||||
})
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "payses")
|
||||
this.payment_collection_id ??=
|
||||
this.payment_collection_id ?? this.payment_collection?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "payses")
|
||||
this.payment_collection_id ??=
|
||||
this.payment_collection_id ?? this.payment_collection?.id
|
||||
}
|
||||
}
|
||||
export default PaymentSession
|
||||
|
||||
@@ -1,157 +1,51 @@
|
||||
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
|
||||
import {
|
||||
BigNumber,
|
||||
DALUtils,
|
||||
MikroOrmBigNumberProperty,
|
||||
Searchable,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Cascade,
|
||||
Collection,
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Capture from "./capture"
|
||||
import PaymentCollection from "./payment-collection"
|
||||
import PaymentSession from "./payment-session"
|
||||
import Refund from "./refund"
|
||||
|
||||
type OptionalPaymentProps = DAL.ModelDateColumns
|
||||
|
||||
const tableName = "payment"
|
||||
const ProviderIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName,
|
||||
columns: "provider_id",
|
||||
})
|
||||
|
||||
@Entity({ tableName })
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class Payment {
|
||||
[OptionalProps]?: OptionalPaymentProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
amount: BigNumber | number
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_amount: BigNumberRawValue
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
currency_code: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
@ProviderIdIndex.MikroORMIndex()
|
||||
provider_id: string
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
cart_id: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
order_id: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
customer_id: string | null = null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
data: Record<string, unknown> | null = null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
const Payment = model
|
||||
.define("Payment", {
|
||||
id: model.id({ prefix: "pay" }).primaryKey(),
|
||||
amount: model.bigNumber(),
|
||||
currency_code: model.text(),
|
||||
provider_id: model.text(),
|
||||
cart_id: model.text().searchable().nullable(),
|
||||
order_id: model.text().searchable().nullable(),
|
||||
customer_id: model.text().searchable().nullable(),
|
||||
data: model.json().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
captured_at: model.dateTime().nullable(),
|
||||
canceled_at: model.dateTime().nullable(),
|
||||
payment_collection: model.belongsTo(() => PaymentCollection, {
|
||||
mappedBy: "payments",
|
||||
}),
|
||||
payment_session: model.belongsTo(() => PaymentSession, {
|
||||
mappedBy: "payment",
|
||||
}),
|
||||
refunds: model.hasMany(() => Refund, {
|
||||
mappedBy: "payment",
|
||||
}),
|
||||
captures: model.hasMany(() => Capture, {
|
||||
mappedBy: "payment",
|
||||
}),
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
.cascades({
|
||||
delete: ["refunds", "captures"],
|
||||
})
|
||||
updated_at: Date
|
||||
.indexes([
|
||||
{
|
||||
name: "IDX_payment_provider_id",
|
||||
on: ["provider_id"],
|
||||
},
|
||||
{
|
||||
name: "IDX_payment_payment_collection_id",
|
||||
on: ["payment_collection_id"],
|
||||
},
|
||||
{
|
||||
name: "IDX_payment_payment_session_id",
|
||||
on: ["payment_session_id"],
|
||||
},
|
||||
])
|
||||
|
||||
@Property({
|
||||
columnType: "timestamptz",
|
||||
nullable: true,
|
||||
index: "IDX_payment_deleted_at",
|
||||
})
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@Property({
|
||||
columnType: "timestamptz",
|
||||
nullable: true,
|
||||
})
|
||||
captured_at: Date | null = null
|
||||
|
||||
@Property({
|
||||
columnType: "timestamptz",
|
||||
nullable: true,
|
||||
})
|
||||
canceled_at: Date | null = null
|
||||
|
||||
@OneToMany(() => Refund, (refund) => refund.payment, {
|
||||
cascade: [Cascade.REMOVE],
|
||||
})
|
||||
refunds = new Collection<Rel<Refund>>(this)
|
||||
|
||||
@OneToMany(() => Capture, (capture) => capture.payment, {
|
||||
cascade: [Cascade.REMOVE],
|
||||
})
|
||||
captures = new Collection<Rel<Capture>>(this)
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => PaymentCollection,
|
||||
persist: false,
|
||||
})
|
||||
payment_collection: Rel<PaymentCollection>
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => PaymentCollection,
|
||||
columnType: "text",
|
||||
index: "IDX_payment_payment_collection_id",
|
||||
fieldName: "payment_collection_id",
|
||||
mapToPk: true,
|
||||
})
|
||||
payment_collection_id: string
|
||||
|
||||
@OneToOne({
|
||||
entity: () => PaymentSession,
|
||||
owner: true,
|
||||
fieldName: "payment_session_id",
|
||||
index: "IDX_payment_payment_session_id",
|
||||
})
|
||||
payment_session: Rel<PaymentSession>
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "pay")
|
||||
this.payment_collection_id ??=
|
||||
this.payment_collection_id ?? this.payment_collection?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "pay")
|
||||
this.payment_collection_id ??=
|
||||
this.payment_collection_id ?? this.payment_collection?.id
|
||||
}
|
||||
}
|
||||
export default Payment
|
||||
|
||||
@@ -1,58 +1,14 @@
|
||||
import {
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
Searchable,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
Filter,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Refund from "./refund"
|
||||
|
||||
@Entity({ tableName: "refund_reason" })
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class RefundReason {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
const RefundReason = model.define("RefundReason", {
|
||||
id: model.id({ prefix: "refr" }).primaryKey(),
|
||||
label: model.text().searchable(),
|
||||
description: model.text().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
refunds: model.hasMany(() => Refund, {
|
||||
mappedBy: "refund_reason",
|
||||
}),
|
||||
})
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text" })
|
||||
label: string
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
description: string | null = null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@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 })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "refr")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "refr")
|
||||
}
|
||||
}
|
||||
export default RefundReason
|
||||
|
||||
@@ -1,101 +1,28 @@
|
||||
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
|
||||
import {
|
||||
BigNumber,
|
||||
MikroOrmBigNumberProperty,
|
||||
generateEntityId,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Payment from "./payment"
|
||||
import RefundReason from "./refund-reason"
|
||||
|
||||
type OptionalProps =
|
||||
| "note"
|
||||
| "refund_reason_id"
|
||||
| "refund_reason"
|
||||
| DAL.ModelDateColumns
|
||||
|
||||
@Entity({ tableName: "refund" })
|
||||
export default class Refund {
|
||||
[OptionalProps]?: OptionalProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
amount: BigNumber | number
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_amount: BigNumberRawValue
|
||||
|
||||
@ManyToOne(() => Payment, {
|
||||
onDelete: "cascade",
|
||||
index: "IDX_refund_payment_id",
|
||||
fieldName: "payment_id",
|
||||
const Refund = model
|
||||
.define("Refund", {
|
||||
id: model.id({ prefix: "ref" }).primaryKey(),
|
||||
amount: model.bigNumber(),
|
||||
payment: model.belongsTo(() => Payment, {
|
||||
mappedBy: "refunds",
|
||||
}),
|
||||
refund_reason: model
|
||||
.belongsTo(() => RefundReason, {
|
||||
mappedBy: "refunds",
|
||||
})
|
||||
.nullable(),
|
||||
note: model.text().nullable(),
|
||||
created_by: model.text().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
})
|
||||
payment!: Rel<Payment>
|
||||
.indexes([
|
||||
{
|
||||
name: "IDX_refund_payment_id",
|
||||
on: ["payment_id"],
|
||||
},
|
||||
])
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
payment_id: string
|
||||
|
||||
@ManyToOne(() => RefundReason, {
|
||||
columnType: "text",
|
||||
mapToPk: true,
|
||||
fieldName: "refund_reason_id",
|
||||
nullable: true,
|
||||
})
|
||||
refund_reason_id: string | null = null
|
||||
|
||||
@ManyToOne(() => RefundReason, { persist: false, nullable: true })
|
||||
refund_reason: Rel<RefundReason> | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
note: string | null = null
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@Property({
|
||||
columnType: "timestamptz",
|
||||
nullable: true,
|
||||
index: "IDX_refund_deleted_at",
|
||||
})
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
created_by: string | null = null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ref")
|
||||
this.refund_reason_id ??= this.refund_reason?.id || null
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ref")
|
||||
this.refund_reason_id ??= this.refund_reason?.id || null
|
||||
}
|
||||
}
|
||||
export default Refund
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
FilterablePaymentProviderProps,
|
||||
FilterablePaymentSessionProps,
|
||||
FindConfig,
|
||||
InferEntityType,
|
||||
InternalModuleDeclaration,
|
||||
IPaymentModuleService,
|
||||
Logger,
|
||||
@@ -87,11 +88,21 @@ export default class PaymentModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
|
||||
protected paymentService_: ModulesSdkTypes.IMedusaInternalService<Payment>
|
||||
protected captureService_: ModulesSdkTypes.IMedusaInternalService<Capture>
|
||||
protected refundService_: ModulesSdkTypes.IMedusaInternalService<Refund>
|
||||
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<PaymentSession>
|
||||
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<PaymentCollection>
|
||||
protected paymentService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof Payment
|
||||
>
|
||||
protected captureService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof Capture
|
||||
>
|
||||
protected refundService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof Refund
|
||||
>
|
||||
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof PaymentSession
|
||||
>
|
||||
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof PaymentCollection
|
||||
>
|
||||
protected paymentProviderService_: PaymentProviderService
|
||||
|
||||
constructor(
|
||||
@@ -157,7 +168,7 @@ export default class PaymentModuleService
|
||||
async createPaymentCollections_(
|
||||
data: CreatePaymentCollectionDTO[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentCollection[]> {
|
||||
): Promise<InferEntityType<typeof PaymentCollection>[]> {
|
||||
return await this.paymentCollectionService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
@@ -218,7 +229,7 @@ export default class PaymentModuleService
|
||||
async updatePaymentCollections_(
|
||||
data: UpdatePaymentCollectionDTO[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentCollection[]> {
|
||||
): Promise<InferEntityType<typeof PaymentCollection>[]> {
|
||||
return await this.paymentCollectionService_.update(data, sharedContext)
|
||||
}
|
||||
|
||||
@@ -244,7 +255,8 @@ export default class PaymentModuleService
|
||||
(collection): collection is CreatePaymentCollectionDTO => !collection.id
|
||||
)
|
||||
|
||||
const operations: Promise<PaymentCollection[]>[] = []
|
||||
const operations: Promise<InferEntityType<typeof PaymentCollection>[]>[] =
|
||||
[]
|
||||
|
||||
if (forCreate.length) {
|
||||
operations.push(this.createPaymentCollections_(forCreate, sharedContext))
|
||||
@@ -300,7 +312,7 @@ export default class PaymentModuleService
|
||||
input: CreatePaymentSessionDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentSessionDTO> {
|
||||
let paymentSession: PaymentSession | undefined
|
||||
let paymentSession: InferEntityType<typeof PaymentSession> | undefined
|
||||
let providerPaymentSession: Record<string, unknown> | undefined
|
||||
|
||||
try {
|
||||
@@ -313,7 +325,7 @@ export default class PaymentModuleService
|
||||
providerPaymentSession = await this.paymentProviderService_.createSession(
|
||||
input.provider_id,
|
||||
{
|
||||
context: { ...input.context, session_id: paymentSession.id },
|
||||
context: { ...input.context, session_id: paymentSession!.id },
|
||||
amount: input.amount,
|
||||
currency_code: input.currency_code,
|
||||
}
|
||||
@@ -322,7 +334,7 @@ export default class PaymentModuleService
|
||||
paymentSession = (
|
||||
await this.paymentSessionService_.update(
|
||||
{
|
||||
id: paymentSession.id,
|
||||
id: paymentSession!.id,
|
||||
data: { ...input.data, ...providerPaymentSession },
|
||||
},
|
||||
sharedContext
|
||||
@@ -354,7 +366,7 @@ export default class PaymentModuleService
|
||||
paymentCollectionId: string,
|
||||
data: CreatePaymentSessionDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentSession> {
|
||||
): Promise<InferEntityType<typeof PaymentSession>> {
|
||||
const paymentSession = await this.paymentSessionService_.create(
|
||||
{
|
||||
payment_collection_id: paymentCollectionId,
|
||||
@@ -493,11 +505,11 @@ export default class PaymentModuleService
|
||||
|
||||
@InjectTransactionManager()
|
||||
async authorizePaymentSession_(
|
||||
session: PaymentSession,
|
||||
session: InferEntityType<typeof PaymentSession>,
|
||||
data: Record<string, unknown>,
|
||||
status: PaymentSessionStatus,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<Payment> {
|
||||
): Promise<InferEntityType<typeof Payment>> {
|
||||
let autoCapture = false
|
||||
if (status === PaymentSessionStatus.CAPTURED) {
|
||||
status = PaymentSessionStatus.AUTHORIZED
|
||||
@@ -620,9 +632,9 @@ export default class PaymentModuleService
|
||||
data: CreateCaptureDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<{
|
||||
payment: Payment
|
||||
payment: InferEntityType<typeof Payment>
|
||||
isFullyCaptured: boolean
|
||||
capture?: Capture
|
||||
capture?: InferEntityType<typeof Capture>
|
||||
}> {
|
||||
const payment = await this.paymentService_.retrieve(
|
||||
data.payment_id,
|
||||
@@ -659,10 +671,10 @@ export default class PaymentModuleService
|
||||
}
|
||||
|
||||
const capturedAmount = payment.captures.reduce((captureAmount, next) => {
|
||||
return MathBN.add(captureAmount, next.raw_amount)
|
||||
return MathBN.add(captureAmount, next.raw_amount as BigNumberInput)
|
||||
}, MathBN.convert(0))
|
||||
|
||||
const authorizedAmount = new BigNumber(payment.raw_amount)
|
||||
const authorizedAmount = new BigNumber(payment.raw_amount as BigNumberInput)
|
||||
const newCaptureAmount = new BigNumber(data.amount)
|
||||
const remainingToCapture = MathBN.sub(authorizedAmount, capturedAmount)
|
||||
|
||||
@@ -692,7 +704,7 @@ export default class PaymentModuleService
|
||||
}
|
||||
@InjectManager()
|
||||
private async capturePaymentFromProvider_(
|
||||
payment: Payment,
|
||||
payment: InferEntityType<typeof Payment>,
|
||||
isFullyCaptured: boolean,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
@@ -756,20 +768,20 @@ export default class PaymentModuleService
|
||||
|
||||
@InjectTransactionManager()
|
||||
private async refundPayment_(
|
||||
payment: Payment,
|
||||
payment: InferEntityType<typeof Payment>,
|
||||
data: CreateRefundDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<Refund> {
|
||||
): Promise<InferEntityType<typeof Refund>> {
|
||||
if (!data.amount) {
|
||||
data.amount = payment.amount as BigNumberInput
|
||||
}
|
||||
|
||||
const capturedAmount = payment.captures.reduce((captureAmount, next) => {
|
||||
const amountAsBigNumber = new BigNumber(next.raw_amount)
|
||||
const amountAsBigNumber = new BigNumber(next.raw_amount as BigNumberInput)
|
||||
return MathBN.add(captureAmount, amountAsBigNumber)
|
||||
}, MathBN.convert(0))
|
||||
const refundedAmount = payment.refunds.reduce((refundedAmount, next) => {
|
||||
return MathBN.add(refundedAmount, next.raw_amount)
|
||||
return MathBN.add(refundedAmount, next.raw_amount as BigNumberInput)
|
||||
}, MathBN.convert(0))
|
||||
|
||||
const totalRefundedAmount = MathBN.add(refundedAmount, data.amount)
|
||||
@@ -797,8 +809,8 @@ export default class PaymentModuleService
|
||||
|
||||
@InjectManager()
|
||||
private async refundPaymentFromProvider_(
|
||||
payment: Payment,
|
||||
refund: Refund,
|
||||
payment: InferEntityType<typeof Payment>,
|
||||
refund: InferEntityType<typeof Refund>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const paymentData = await this.paymentProviderService_.refundPayment(
|
||||
@@ -806,7 +818,7 @@ export default class PaymentModuleService
|
||||
data: payment.data!,
|
||||
provider_id: payment.provider_id,
|
||||
},
|
||||
refund.raw_amount
|
||||
refund.raw_amount as BigNumberInput
|
||||
)
|
||||
|
||||
await this.paymentService_.update(
|
||||
@@ -828,14 +840,6 @@ export default class PaymentModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
// TODO: revisit when totals are implemented
|
||||
// if (payment.captured_amount !== 0) {
|
||||
// throw new MedusaError(
|
||||
// MedusaError.Types.INVALID_DATA,
|
||||
// `Cannot cancel a payment: ${payment.id} that has been captured.`
|
||||
// )
|
||||
// }
|
||||
|
||||
await this.paymentProviderService_.cancelPayment({
|
||||
data: payment.data!,
|
||||
provider_id: payment.provider_id,
|
||||
|
||||
Reference in New Issue
Block a user