feat: add online payment management and checkout
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-15 09:26:40 +08:00
parent 5353a4a128
commit dc2efcde08
41 changed files with 4855 additions and 21 deletions

View File

@@ -0,0 +1,6 @@
-- Destructive: back up and obtain operator approval before running.
-- Existing issued licenses remain; payment records and checkout links are removed.
DROP TABLE authorization_grant_logs;
DROP TABLE payment_notifications;
DROP TABLE payment_orders;
ALTER TABLE offline_orders DROP COLUMN payment_expires_at, DROP COLUMN payment_status, DROP COLUMN payment_channel;

View File

@@ -0,0 +1,66 @@
-- Explicit operator migration; startup never applies DDL.
ALTER TABLE offline_orders
ADD COLUMN payment_channel VARCHAR(16) NULL AFTER currency,
ADD COLUMN payment_status VARCHAR(32) NOT NULL DEFAULT 'unpaid' AFTER status,
ADD COLUMN payment_expires_at TIMESTAMP(6) NULL AFTER paid_at;
UPDATE offline_orders SET payment_status='paid' WHERE status IN ('paid','issued');
CREATE TABLE payment_orders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
order_id BIGINT UNSIGNED NOT NULL,
merchant_order_no CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
channel_trade_no VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NULL,
amount_cents BIGINT UNSIGNED NOT NULL,
currency CHAR(3) NOT NULL DEFAULT 'CNY',
license_days INT UNSIGNED NOT NULL,
checkout_token CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'created',
code_url TEXT NULL,
expires_at TIMESTAMP(6) NOT NULL,
paid_at TIMESTAMP(6) NULL,
closed_at TIMESTAMP(6) NULL,
last_error VARCHAR(255) NULL,
last_queried_at TIMESTAMP(6) NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_payment_orders_order (order_id),
UNIQUE KEY uq_payment_orders_merchant_no (merchant_order_no),
UNIQUE KEY uq_payment_orders_token (checkout_token),
UNIQUE KEY uq_payment_orders_channel_trade (channel, channel_trade_no),
KEY idx_payment_orders_status (status),
CONSTRAINT fk_payment_orders_order FOREIGN KEY (order_id) REFERENCES offline_orders(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE payment_notifications (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
event_id VARCHAR(190) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
merchant_order_no CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
channel_trade_no VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
payload_digest BINARY(32) NOT NULL,
verified TINYINT(1) NOT NULL DEFAULT 1,
processed TINYINT(1) NOT NULL DEFAULT 0,
received_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
processed_at TIMESTAMP(6) NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_payment_notifications_event (channel, event_id),
CONSTRAINT fk_payment_notifications_payment FOREIGN KEY (merchant_order_no) REFERENCES payment_orders(merchant_order_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE authorization_grant_logs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
order_id BIGINT UNSIGNED NOT NULL,
license_id BIGINT UNSIGNED NULL,
idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
status VARCHAR(32) NOT NULL,
error_message VARCHAR(255) NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_authorization_grant_order (order_id),
UNIQUE KEY uq_authorization_grant_key (idempotency_key),
CONSTRAINT fk_authorization_grant_order FOREIGN KEY (order_id) REFERENCES offline_orders(id),
CONSTRAINT fk_authorization_grant_license FOREIGN KEY (license_id) REFERENCES licenses(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1,4 @@
ALTER TABLE payment_orders DROP FOREIGN KEY fk_payment_order_config_version;
ALTER TABLE payment_orders DROP COLUMN provider_config_version;
DROP TABLE payment_channel_configs;
DROP TABLE payment_channel_config_versions;

View File

@@ -0,0 +1,24 @@
-- Explicit operator migration; startup never applies DDL.
-- Published versions are append-only: application code only inserts versions.
CREATE TABLE payment_channel_config_versions (
channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
version BIGINT UNSIGNED NOT NULL,
enabled TINYINT(1) NOT NULL DEFAULT 0,
config_encrypted MEDIUMBLOB NOT NULL,
created_by BIGINT UNSIGNED NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (channel, version),
CONSTRAINT fk_payment_config_version_admin FOREIGN KEY (created_by) REFERENCES admins(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE payment_channel_configs (
channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
active_version BIGINT UNSIGNED NULL,
PRIMARY KEY (channel),
CONSTRAINT fk_payment_config_active FOREIGN KEY (channel, active_version) REFERENCES payment_channel_config_versions(channel, version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO payment_channel_configs(channel) VALUES ('alipay'), ('wechat');
ALTER TABLE payment_orders
ADD COLUMN provider_config_version BIGINT UNSIGNED NULL AFTER channel,
ADD CONSTRAINT fk_payment_order_config_version FOREIGN KEY (channel, provider_config_version) REFERENCES payment_channel_config_versions(channel, version);

View File

@@ -0,0 +1,51 @@
-- Preserve the repaired schema-14 tables for current installations and restore
-- the retained early schema-14 table only when it can be renamed without loss.
SET @schema = DATABASE();
SET @sql = (
SELECT IF(
EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema=@schema AND table_name='payment_orders' AND constraint_name='fk_payment_order_config_version'),
'ALTER TABLE payment_orders DROP FOREIGN KEY fk_payment_order_config_version',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql = (
SELECT IF(
EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=@schema AND table_name='payment_orders' AND column_name='provider_config_version'),
'ALTER TABLE payment_orders DROP COLUMN provider_config_version',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql = (
SELECT IF(
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema=@schema AND table_name='payment_channel_configs_legacy_v14')
AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema=@schema AND table_name='payment_channel_configs'),
'DROP TABLE payment_channel_configs',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql = (
SELECT IF(
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema=@schema AND table_name='payment_channel_configs_legacy_v14')
AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema=@schema AND table_name='payment_channel_configs'),
'RENAME TABLE payment_channel_configs_legacy_v14 TO payment_channel_configs',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Never drop immutable configuration history during this repair rollback.

View File

@@ -0,0 +1,57 @@
-- Schema 15 repairs early, incompatible schema-14 payment configuration tables.
-- The legacy table is retained verbatim and is never interpreted as active config.
SET @schema = DATABASE();
SET @sql = (
SELECT IF(
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema=@schema AND table_name='payment_channel_configs')
AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=@schema AND table_name='payment_channel_configs' AND column_name='active_version'),
'RENAME TABLE payment_channel_configs TO payment_channel_configs_legacy_v14',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
CREATE TABLE IF NOT EXISTS payment_channel_config_versions (
channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
version BIGINT UNSIGNED NOT NULL,
enabled TINYINT(1) NOT NULL DEFAULT 0,
config_encrypted MEDIUMBLOB NOT NULL,
created_by BIGINT UNSIGNED NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (channel, version),
CONSTRAINT fk_payment_config_version_admin FOREIGN KEY (created_by) REFERENCES admins(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE IF NOT EXISTS payment_channel_configs (
channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
active_version BIGINT UNSIGNED NULL,
PRIMARY KEY (channel),
CONSTRAINT fk_payment_config_active FOREIGN KEY (channel, active_version) REFERENCES payment_channel_config_versions(channel, version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT IGNORE INTO payment_channel_configs(channel) VALUES ('alipay'), ('wechat');
SET @sql = (
SELECT IF(
NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=@schema AND table_name='payment_orders' AND column_name='provider_config_version'),
'ALTER TABLE payment_orders ADD COLUMN provider_config_version BIGINT UNSIGNED NULL AFTER channel',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql = (
SELECT IF(
NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema=@schema AND table_name='payment_orders' AND constraint_name='fk_payment_order_config_version'),
'ALTER TABLE payment_orders ADD CONSTRAINT fk_payment_order_config_version FOREIGN KEY (channel, provider_config_version) REFERENCES payment_channel_config_versions(channel, version)',
'SELECT 1'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

View File

@@ -1,5 +1,10 @@
# MySQL migration baseline
Schema 15 repairs installations that applied an early incompatible schema-14 `payment_channel_configs` table (`id/channel/enabled/config_json/secret_encrypted/updated_by`). It renames that table to `payment_channel_configs_legacy_v14` without interpreting or deleting its rows, then creates the immutable encrypted-version table, active pointer table, and payment-order version foreign key required by the current service. The retained legacy table is deliberately not migrated into active configuration because its values cannot be safely interpreted as the current encrypted format. Schema 15 uses metadata-guarded MySQL `PREPARE` branches so it is safe for both early schema-14 and final schema-14 installations. Its down migration removes only the repair-layer order column/foreign key and restores the retained legacy table name when possible; it does not delete the retained legacy rows or immutable version history. Apply 15 before starting a service that requires `RequiredSchemaVersion=15`, and rehearse both schema-14 shapes only on an isolated database.
Schema 14 adds encrypted immutable payment channel configuration versions, per-channel active pointers, and payment order version references. Apply schemas 13 and 14 explicitly before starting the payment configuration UI/API; startup never migrates. Provide PAYMENT_CONFIG_ENCRYPTION_KEY_FILE as an absolute path to an independent 32-byte raw random key, mounted read-only and backed up securely with the database. All API processes must use the same key. Missing key prevents configuration saves and new environment-backed payment snapshots. Retain historical versions and the startup environment merchant configuration for legacy NULL-version orders. Disabling a channel blocks new payment orders but keeps bound historical notification/query verification. Down 14 destroys configuration history and bindings: quiesce payment entry points, back up, obtain operator approval, and do not resume historical payments as environment-backed transactions. Isolated migration 14/13 down/up and configuration integration tests passed; no business database migration was performed.
Status: DEV schemas are implemented through version 8 (local releases and digest-object locking). Formal entitlement policy acceptance and seed data remain pending. Read-only verification on 2026-09-30: business license_platform is version=7 dirty=false; isolated license_download_260930 is version=8 dirty=false. No business database migration was executed during the deployment-delivery slice.
## Version 007 to 008