diff --git a/packages/payment/src/migrations/.snapshot-medusa-payment.json b/packages/payment/src/migrations/.snapshot-medusa-payment.json new file mode 100644 index 0000000000..df741b1f06 --- /dev/null +++ b/packages/payment/src/migrations/.snapshot-medusa-payment.json @@ -0,0 +1,828 @@ +{ + "namespaces": [ + "public" + ], + "name": "public", + "tables": [ + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "authorized_amount": { + "name": "authorized_amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "refunded_amount": { + "name": "refunded_amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "region_id": { + "name": "region_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "completed_at": { + "name": "completed_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "status": { + "name": "status", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "'not_paid'", + "enumItems": [ + "not_paid", + "awaiting", + "authorized", + "partially_authorized", + "canceled" + ], + "mappedType": "enum" + } + }, + "name": "payment-collection", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "deleted_at" + ], + "composite": false, + "keyName": "IDX_payment_collection_deleted_at", + "primary": false, + "unique": false + }, + { + "keyName": "payment-collection_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "type_detail": { + "name": "type_detail", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "description_detail": { + "name": "description_detail", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment-method-token", + "schema": "public", + "indexes": [ + { + "keyName": "payment-method-token_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "true", + "mappedType": "boolean" + } + }, + "name": "payment-provider", + "schema": "public", + "indexes": [ + { + "keyName": "payment-provider_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "payment_collection_id": { + "name": "payment_collection_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "payment_provider_id": { + "name": "payment_provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment-collection_payment_providers", + "schema": "public", + "indexes": [ + { + "keyName": "payment-collection_payment_providers_pkey", + "columnNames": [ + "payment_collection_id", + "payment_provider_id" + ], + "composite": true, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "payment-collection_payment_providers_payment_coll_6b015_foreign": { + "constraintName": "payment-collection_payment_providers_payment_coll_6b015_foreign", + "columnNames": [ + "payment_collection_id" + ], + "localTableName": "public.payment-collection_payment_providers", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-collection", + "deleteRule": "cascade", + "updateRule": "cascade" + }, + "payment-collection_payment_providers_payment_provider_id_foreign": { + "constraintName": "payment-collection_payment_providers_payment_provider_id_foreign", + "columnNames": [ + "payment_provider_id" + ], + "localTableName": "public.payment-collection_payment_providers", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-provider", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "status": { + "name": "status", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "enumItems": [ + "authorized", + "pending", + "requires_more", + "error", + "canceled" + ], + "mappedType": "enum" + }, + "is_selected": { + "name": "is_selected", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "authorised_at": { + "name": "authorised_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "payment_collection_id": { + "name": "payment_collection_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment-session", + "schema": "public", + "indexes": [ + { + "keyName": "payment-session_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "payment-session_payment_collection_id_foreign": { + "constraintName": "payment-session_payment_collection_id_foreign", + "columnNames": [ + "payment_collection_id" + ], + "localTableName": "public.payment-session", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-collection", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "authorized_amount": { + "name": "authorized_amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "currency_code": { + "name": "currency_code", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "cart_id": { + "name": "cart_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "order_id": { + "name": "order_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "customer_id": { + "name": "customer_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "captured_at": { + "name": "captured_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "canceled_at": { + "name": "canceled_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "payment_collection_id": { + "name": "payment_collection_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "session_id": { + "name": "session_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "deleted_at" + ], + "composite": false, + "keyName": "IDX_payment_deleted_at", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "session_id" + ], + "composite": false, + "keyName": "payment_session_id_unique", + "primary": false, + "unique": true + }, + { + "keyName": "payment_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "payment_payment_collection_id_foreign": { + "constraintName": "payment_payment_collection_id_foreign", + "columnNames": [ + "payment_collection_id" + ], + "localTableName": "public.payment", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-collection", + "updateRule": "cascade" + }, + "payment_session_id_foreign": { + "constraintName": "payment_session_id_foreign", + "columnNames": [ + "session_id" + ], + "localTableName": "public.payment", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-session", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "payment_id": { + "name": "payment_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "created_by": { + "name": "created_by", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + } + }, + "name": "capture", + "schema": "public", + "indexes": [ + { + "keyName": "capture_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "capture_payment_id_foreign": { + "constraintName": "capture_payment_id_foreign", + "columnNames": [ + "payment_id" + ], + "localTableName": "public.capture", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "payment_id": { + "name": "payment_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "created_by": { + "name": "created_by", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + } + }, + "name": "refund", + "schema": "public", + "indexes": [ + { + "keyName": "refund_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "refund_payment_id_foreign": { + "constraintName": "refund_payment_id_foreign", + "columnNames": [ + "payment_id" + ], + "localTableName": "public.refund", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + } + ] +} diff --git a/packages/payment/src/migrations/Migration20240111173302.ts b/packages/payment/src/migrations/Migration20240111173302.ts new file mode 100644 index 0000000000..14c2f7b6f1 --- /dev/null +++ b/packages/payment/src/migrations/Migration20240111173302.ts @@ -0,0 +1,72 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240111173302 extends Migration { + async up(): Promise { + this.addSql( + 'create table if not exists "payment-collection" ("id" text not null, "currency_code" text null, "amount" numeric not null, "authorized_amount" numeric null, "refunded_amount" numeric null, "region_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "completed_at" timestamptz null, "status" text check ("status" in (\'not_paid\', \'awaiting\', \'authorized\', \'partially_authorized\', \'canceled\')) not null default \'not_paid\', constraint "payment-collection_pkey" primary key ("id"));' + ) + this.addSql( + 'create index "IDX_payment_collection_deleted_at" on "payment-collection" ("deleted_at");' + ) + + this.addSql( + 'create table if not exists "payment-method-token" ("id" text not null, "data" jsonb null, "name" varchar(255) not null, "type_detail" varchar(255) not null, "description_detail" varchar(255) not null, "metadata" jsonb null, "provider_id" text not null, constraint "payment-method-token_pkey" primary key ("id"));' + ) + + this.addSql( + 'create table if not exists "payment-provider" ("id" text not null, "is_enabled" boolean not null default true, constraint "payment-provider_pkey" primary key ("id"));' + ) + + this.addSql( + 'create table if not exists "payment-collection_payment_providers" ("payment_collection_id" text not null, "payment_provider_id" text not null, constraint "payment-collection_payment_providers_pkey" primary key ("payment_collection_id", "payment_provider_id"));' + ) + + this.addSql( + 'create table if not exists "payment-session" ("id" text not null, "currency_code" text null, "amount" numeric not null, "provider_id" text not null, "data" jsonb null, "status" text check ("status" in (\'authorized\', \'pending\', \'requires_more\', \'error\', \'canceled\')) not null, "is_selected" varchar(255) null, "authorised_at" timestamptz null, "payment_collection_id" text not null, constraint "payment-session_pkey" primary key ("id"));' + ) + + this.addSql( + 'create table if not exists "payment" ("id" text not null, "amount" numeric not null, "authorized_amount" numeric null, "provider_id" varchar(255) not null, "currency_code" varchar(255) null, "cart_id" varchar(255) null, "order_id" varchar(255) null, "customer_id" varchar(255) null, "data" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "captured_at" timestamptz null, "canceled_at" timestamptz null, "payment_collection_id" text not null, "session_id" text not null, constraint "payment_pkey" primary key ("id"));' + ) + this.addSql( + 'create index "IDX_payment_deleted_at" on "payment" ("deleted_at");' + ) + this.addSql( + 'alter table "payment" add constraint "payment_session_id_unique" unique ("session_id");' + ) + + this.addSql( + 'create table if not exists "capture" ("id" text not null, "amount" numeric not null, "payment_id" text not null, "created_at" timestamptz not null default now(), "created_by" varchar(255) null, constraint "capture_pkey" primary key ("id"));' + ) + + this.addSql( + 'create table if not exists "refund" ("id" text not null, "amount" numeric not null, "payment_id" text not null, "created_at" timestamptz not null default now(), "created_by" varchar(255) null, constraint "refund_pkey" primary key ("id"));' + ) + + this.addSql( + 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_coll_6b015_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_provider_id_foreign" foreign key ("payment_provider_id") references "payment-provider" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "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 "payment" add constraint "payment_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade;' + ) + this.addSql( + 'alter table "payment" add constraint "payment_session_id_foreign" foreign key ("session_id") references "payment-session" ("id") on update cascade;' + ) + + this.addSql( + 'alter table "capture" add constraint "capture_payment_id_foreign" foreign key ("payment_id") references "payment" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "refund" add constraint "refund_payment_id_foreign" foreign key ("payment_id") references "payment" ("id") on update cascade on delete cascade;' + ) + } +} diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 366a5a542c..f3fe6581ae 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -31,9 +31,10 @@ export default class Payment { @Property({ columnType: "numeric", + nullable: true, serializer: Number, }) - authorized_amount: number + authorized_amount: number | null @Property() provider_id: string