feat: add client self-service purchases and integration kit
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 17:33:52 +08:00
parent 68a2b4d892
commit cfcc9b386b
33 changed files with 2598 additions and 44 deletions

View File

@@ -0,0 +1,57 @@
-- Operator-applied only. Fixed days start at successful payment issuance (UTC, 24 hours/day).
CREATE TABLE payment_plans (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
product_id BIGINT UNSIGNED NOT NULL,
code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
name VARCHAR(190) NOT NULL,
amount_cents BIGINT UNSIGNED NOT NULL,
currency CHAR(3) NOT NULL DEFAULT 'CNY',
license_days INT UNSIGNED NOT NULL,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
version BIGINT UNSIGNED NOT NULL DEFAULT 1,
UNIQUE KEY uq_plan_code(product_id,code),
FOREIGN KEY(product_id) REFERENCES products(id)
) ENGINE=InnoDB;
CREATE TABLE payment_plan_versions (
plan_id BIGINT UNSIGNED NOT NULL,
version BIGINT UNSIGNED NOT NULL,
product_id BIGINT UNSIGNED NOT NULL,
code VARCHAR(64) NOT NULL,
name VARCHAR(190) NOT NULL,
amount_cents BIGINT UNSIGNED NOT NULL,
currency CHAR(3) NOT NULL,
license_days INT UNSIGNED NOT NULL,
enabled BOOLEAN NOT NULL,
created_by BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(plan_id,version),
FOREIGN KEY(plan_id) REFERENCES payment_plans(id),
FOREIGN KEY(created_by) REFERENCES admins(id)
) ENGINE=InnoDB;
CREATE TABLE purchase_credentials (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
token_hash BINARY(32) NOT NULL UNIQUE,
customer_id BIGINT UNSIGNED NOT NULL,
product_id BIGINT UNSIGNED NOT NULL,
label VARCHAR(190) NOT NULL,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
created_by BIGINT UNSIGNED NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
FOREIGN KEY(customer_id) REFERENCES customers(id),
FOREIGN KEY(product_id) REFERENCES products(id),
FOREIGN KEY(created_by) REFERENCES admins(id)
) ENGINE=InnoDB;
ALTER TABLE payment_orders
ADD COLUMN plan_id BIGINT UNSIGNED NULL,
ADD COLUMN plan_version BIGINT UNSIGNED NULL,
ADD CONSTRAINT fk_payment_plan_version FOREIGN KEY(plan_id,plan_version) REFERENCES payment_plan_versions(plan_id,version);
CREATE TABLE client_purchases (
order_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
credential_id BIGINT UNSIGNED NOT NULL,
installation_id VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
request_fingerprint BINARY(32) NOT NULL,
instance_id CHAR(36) NULL,
UNIQUE KEY uq_client_purchase_key(credential_id,idempotency_key),
FOREIGN KEY(order_id) REFERENCES offline_orders(id),
FOREIGN KEY(credential_id) REFERENCES purchase_credentials(id)
) ENGINE=InnoDB;