@tly-pay/vendure-plugin 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +192 -2
- package/assets/coins/arb.svg +10 -0
- package/assets/coins/base.svg +4 -0
- package/assets/coins/bln.svg +4 -0
- package/assets/coins/bnb.svg +13 -0
- package/assets/coins/btc.svg +1 -0
- package/assets/coins/busd.svg +7 -0
- package/assets/coins/coin.svg +1 -0
- package/assets/coins/dai.svg +1 -0
- package/assets/coins/doge.svg +109 -0
- package/assets/coins/eth.svg +10 -0
- package/assets/coins/jst.svg +11 -0
- package/assets/coins/link.svg +4 -0
- package/assets/coins/ltc.svg +4 -0
- package/assets/coins/op.svg +12 -0
- package/assets/coins/pol.svg +11 -0
- package/assets/coins/shib.svg +18 -0
- package/assets/coins/sol.svg +15 -0
- package/assets/coins/sun.svg +71 -0
- package/assets/coins/trx.svg +4 -0
- package/assets/coins/uni.svg +10 -0
- package/assets/coins/usdc.svg +6 -0
- package/assets/coins/usdd.svg +18 -0
- package/assets/coins/usdt.svg +1 -0
- package/assets/coins/wbtc.svg +16 -0
- package/assets/coins/weth.svg +17 -0
- package/assets/tly-pay-mark.svg +31 -0
- package/dashboard/api.ts +167 -0
- package/dashboard/appearance.tsx +253 -0
- package/dashboard/banner.tsx +72 -0
- package/dashboard/i18n/en.po +584 -0
- package/dashboard/index.tsx +33 -0
- package/dashboard/logic.ts +219 -0
- package/dashboard/messages.tsx +90 -0
- package/dashboard/notice.tsx +15 -0
- package/dashboard/order-block.tsx +21 -0
- package/dashboard/orders.tsx +246 -0
- package/dashboard/page.tsx +174 -0
- package/dashboard/settings.tsx +186 -0
- package/dashboard/use-connect.ts +46 -0
- package/dist/api.d.ts +169 -0
- package/dist/api.js +541 -0
- package/dist/client-ref.d.ts +75 -0
- package/dist/client-ref.js +165 -0
- package/dist/connect-flow.d.ts +65 -0
- package/dist/connect-flow.js +177 -0
- package/dist/connection-check.d.ts +28 -0
- package/dist/connection-check.js +49 -0
- package/dist/connection.d.ts +140 -0
- package/dist/connection.js +361 -0
- package/dist/contracts.d.ts +66 -0
- package/dist/contracts.js +151 -0
- package/dist/creation-service.d.ts +136 -0
- package/dist/creation-service.js +242 -0
- package/dist/handler.d.ts +13 -0
- package/dist/handler.js +45 -0
- package/dist/hosted-checkout.d.ts +3 -0
- package/dist/hosted-checkout.js +25 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +32 -0
- package/dist/ownership.d.ts +38 -0
- package/dist/ownership.js +38 -0
- package/dist/payment-sync.d.ts +45 -0
- package/dist/payment-sync.js +111 -0
- package/dist/persistence.d.ts +104 -0
- package/dist/persistence.js +405 -0
- package/dist/plugin.d.ts +26 -0
- package/dist/plugin.js +112 -0
- package/dist/public-api.d.ts +56 -0
- package/dist/public-api.js +221 -0
- package/dist/records.d.ts +53 -0
- package/dist/records.js +109 -0
- package/dist/release-config.d.ts +55 -0
- package/dist/release-config.js +107 -0
- package/dist/release-config.json +15 -0
- package/dist/secret-box.d.ts +19 -0
- package/dist/secret-box.js +65 -0
- package/dist/service.d.ts +267 -0
- package/dist/service.js +788 -0
- package/dist/signed-webhook.d.ts +67 -0
- package/dist/signed-webhook.js +119 -0
- package/dist/storefront/checkout.d.mts +86 -0
- package/dist/storefront/checkout.mjs +149 -0
- package/package.json +97 -4
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PaymentSync = void 0;
|
|
4
|
+
const contracts_1 = require("./contracts");
|
|
5
|
+
const public_api_1 = require("./public-api");
|
|
6
|
+
/** Codes that end the processing of a Payment's stored Events; retrying the same data cannot succeed. */
|
|
7
|
+
const FINAL = new Set(["payment_association_missing", "payment_authority_mismatch", "payment_amount_mismatch"]);
|
|
8
|
+
const PAID = new Set(["paid", "overpaid"]);
|
|
9
|
+
const NOTED = new Set(["partially_paid", "overpaid", "review_required", "expired", "cancelled"]);
|
|
10
|
+
/**
|
|
11
|
+
* Applies the authoritative status of one local Payment: reads `GET /v1/payments/{id}` with the saved
|
|
12
|
+
* connection, checks it against the stored intent (Order code, `client_ref`, exact source amount),
|
|
13
|
+
* records it only when its version is newer and settles the Vendure Order once for `paid` and
|
|
14
|
+
* `overpaid`. `partially_paid` and `review_required` stay out of fulfillment with an Order note.
|
|
15
|
+
* A transient failure throws so the job or the scheduled sweep retries; a definitive mismatch marks
|
|
16
|
+
* the stored Events with its code.
|
|
17
|
+
*/
|
|
18
|
+
class PaymentSync {
|
|
19
|
+
store;
|
|
20
|
+
inbox;
|
|
21
|
+
orders;
|
|
22
|
+
connection;
|
|
23
|
+
api;
|
|
24
|
+
clientRef;
|
|
25
|
+
claimSecs;
|
|
26
|
+
constructor(store, inbox, orders, connection, api, clientRef, claimSecs) {
|
|
27
|
+
this.store = store;
|
|
28
|
+
this.inbox = inbox;
|
|
29
|
+
this.orders = orders;
|
|
30
|
+
this.connection = connection;
|
|
31
|
+
this.api = api;
|
|
32
|
+
this.clientRef = clientRef;
|
|
33
|
+
this.claimSecs = claimSecs;
|
|
34
|
+
}
|
|
35
|
+
async sync(paymentId, now = new Date()) {
|
|
36
|
+
const intent = await this.store.findByPaymentId(paymentId);
|
|
37
|
+
if (intent === null)
|
|
38
|
+
return this.reject(paymentId, "payment_association_missing");
|
|
39
|
+
const connection = await this.connection();
|
|
40
|
+
// Without a connection of the same Store the Events wait for a reconnect.
|
|
41
|
+
if (connection === null || connection.storeId !== intent.storeId)
|
|
42
|
+
return "connection_unavailable";
|
|
43
|
+
let view;
|
|
44
|
+
try {
|
|
45
|
+
view = await (await this.api(connection.apiKey)).getPayment(paymentId);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error instanceof public_api_1.TlyApiError && error.rejected)
|
|
49
|
+
return this.reject(paymentId, `api_${error.code}`);
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
const mismatch = this.mismatch(intent, view, await this.clientRef());
|
|
53
|
+
if (mismatch !== null)
|
|
54
|
+
return this.reject(paymentId, mismatch);
|
|
55
|
+
const before = intent.statusVersion;
|
|
56
|
+
const current = await this.store.recordAuthority(intent.id, view);
|
|
57
|
+
if (current.statusVersion > before && NOTED.has(view.status))
|
|
58
|
+
await this.orders.note(current, view.status);
|
|
59
|
+
let outcome = "applied";
|
|
60
|
+
if (current.status !== null && PAID.has(current.status))
|
|
61
|
+
outcome = await this.settle(current, now);
|
|
62
|
+
if (outcome === "applied" || outcome === "settled" || outcome === "already_settled") {
|
|
63
|
+
await this.inbox.finish(paymentId, current.statusVersion, "applied");
|
|
64
|
+
}
|
|
65
|
+
else if (outcome !== "settle_busy") {
|
|
66
|
+
await this.inbox.finish(paymentId, current.statusVersion, outcome);
|
|
67
|
+
}
|
|
68
|
+
return outcome;
|
|
69
|
+
}
|
|
70
|
+
async settle(intent, now) {
|
|
71
|
+
const claim = await this.store.claimSettlement(intent.id, now, new Date(now.getTime() + this.claimSecs * 1000));
|
|
72
|
+
if (claim === "settled")
|
|
73
|
+
return "already_settled";
|
|
74
|
+
if (claim === "busy")
|
|
75
|
+
return "settle_busy";
|
|
76
|
+
let outcome;
|
|
77
|
+
try {
|
|
78
|
+
outcome = await this.orders.settle(intent);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
await this.store.finishSettlement(intent.id, false, null);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
const settled = outcome === "settled" || outcome === "already_settled";
|
|
85
|
+
await this.store.finishSettlement(intent.id, settled, settled ? null : outcome);
|
|
86
|
+
return outcome;
|
|
87
|
+
}
|
|
88
|
+
mismatch(intent, view, clientRef) {
|
|
89
|
+
if (view.orderRef !== intent.orderCode || view.clientRef !== null && view.clientRef !== clientRef) {
|
|
90
|
+
return "payment_authority_mismatch";
|
|
91
|
+
}
|
|
92
|
+
let source;
|
|
93
|
+
try {
|
|
94
|
+
source = JSON.parse(intent.requestBody).source_amount;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
source = null;
|
|
98
|
+
}
|
|
99
|
+
if (!(0, contracts_1.isRecord)(source) || source.currency !== view.sourceAmount.currency
|
|
100
|
+
|| source.amount_raw !== view.sourceAmount.amountRaw) {
|
|
101
|
+
return "payment_amount_mismatch";
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
async reject(paymentId, code) {
|
|
106
|
+
if (FINAL.has(code) || code.startsWith("api_"))
|
|
107
|
+
await this.inbox.reject(paymentId, code);
|
|
108
|
+
return code;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.PaymentSync = PaymentSync;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { TransactionalConnection } from "@vendure/core";
|
|
2
|
+
import { PaymentView, TlyStatus } from "./contracts";
|
|
3
|
+
import { AssociationInput, AssociationStore, PaymentAssociation } from "./creation-service";
|
|
4
|
+
/** One Payment creation intent and the Payment it created; see `PaymentAssociation`. */
|
|
5
|
+
export declare class TlyPayPaymentIntent {
|
|
6
|
+
id: string;
|
|
7
|
+
channelToken: string;
|
|
8
|
+
orderCode: string;
|
|
9
|
+
paymentMethodCode: string;
|
|
10
|
+
attemptNo: number;
|
|
11
|
+
storeId: string;
|
|
12
|
+
idempotencyKey: string;
|
|
13
|
+
/** Hash of Store, Channel, Order, method, attempt and body: one intent per Order attempt. */
|
|
14
|
+
requestHash: string;
|
|
15
|
+
requestBody: string;
|
|
16
|
+
state: "creating" | "bound" | "failed";
|
|
17
|
+
err: string | null;
|
|
18
|
+
createdAt: Date;
|
|
19
|
+
updatedAt: Date;
|
|
20
|
+
lastAttemptAt: Date | null;
|
|
21
|
+
paymentId: string | null;
|
|
22
|
+
hostedUrl: string | null;
|
|
23
|
+
status: TlyStatus | null;
|
|
24
|
+
statusVersion: number;
|
|
25
|
+
/** Settlement of the Vendure Order: claim lease, completion time and the last anomaly code. */
|
|
26
|
+
settleClaimUntil: Date | null;
|
|
27
|
+
settledAt: Date | null;
|
|
28
|
+
settleErr: string | null;
|
|
29
|
+
}
|
|
30
|
+
/** One verified Webhook Event of a local Payment, stored before it is acknowledged. */
|
|
31
|
+
export declare class TlyPayEvent {
|
|
32
|
+
eventId: string;
|
|
33
|
+
paymentId: string;
|
|
34
|
+
status: string;
|
|
35
|
+
statusVersion: number;
|
|
36
|
+
occurredAt: string;
|
|
37
|
+
bodyHash: string;
|
|
38
|
+
receivedAt: Date;
|
|
39
|
+
/** Null until processed: `applied` or a stable code why the Event was not applied. */
|
|
40
|
+
outcome: string | null;
|
|
41
|
+
processedAt: Date | null;
|
|
42
|
+
lastAttemptAt: Date | null;
|
|
43
|
+
}
|
|
44
|
+
export type StoredEvent = Pick<TlyPayEvent, "eventId" | "paymentId" | "status" | "statusVersion" | "occurredAt" | "bodyHash" | "receivedAt" | "outcome" | "processedAt">;
|
|
45
|
+
/** Status filters of the Orders view, as in the WooCommerce plugin. */
|
|
46
|
+
export declare const ORDER_FILTERS: Record<string, string[]>;
|
|
47
|
+
export type IntentRow = PaymentAssociation & {
|
|
48
|
+
updatedAt: Date;
|
|
49
|
+
settledAt: Date | null;
|
|
50
|
+
settleErr: string | null;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Intents on the raw autocommit connection: `reserve` commits before `POST /v1/payments`, outside
|
|
54
|
+
* the host request transaction, so a rolled back request cannot lose the idempotency key.
|
|
55
|
+
*/
|
|
56
|
+
export declare class TypeOrmAssociationStore implements AssociationStore {
|
|
57
|
+
private readonly connection;
|
|
58
|
+
private readonly batchSize;
|
|
59
|
+
constructor(connection: TransactionalConnection, batchSize: number);
|
|
60
|
+
private repository;
|
|
61
|
+
private one;
|
|
62
|
+
reserve(input: AssociationInput): Promise<PaymentAssociation>;
|
|
63
|
+
bindPayment(id: string, paymentId: string, hostedUrl: string): Promise<PaymentAssociation>;
|
|
64
|
+
markFailed(id: string, err: string): Promise<void>;
|
|
65
|
+
recordAttempt(id: string, at: Date): Promise<void>;
|
|
66
|
+
recordAuthority(id: string, view: PaymentView): Promise<PaymentAssociation>;
|
|
67
|
+
findByPaymentId(paymentId: string): Promise<IntentRow | null>;
|
|
68
|
+
findById(id: string): Promise<IntentRow | null>;
|
|
69
|
+
findOrderIntents(channelToken: string, orderCode: string): Promise<PaymentAssociation[]>;
|
|
70
|
+
findCreatingIntents(orderCode: string): Promise<PaymentAssociation[]>;
|
|
71
|
+
findRecoverable(createdBefore: Date): Promise<PaymentAssociation[]>;
|
|
72
|
+
/** Claims settlement of one intent until `until`; see `SyncStore.claimSettlement`. */
|
|
73
|
+
claimSettlement(id: string, now: Date, until: Date): Promise<"claimed" | "busy" | "settled">;
|
|
74
|
+
finishSettlement(id: string, settled: boolean, err: string | null): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* One page of the Orders view, newest first, with counts per status filter. `channelToken` null
|
|
77
|
+
* lists every Channel.
|
|
78
|
+
*/
|
|
79
|
+
list(filter: string, skip: number, take: number, channelToken: string | null): Promise<{
|
|
80
|
+
items: IntentRow[];
|
|
81
|
+
totalItems: number;
|
|
82
|
+
counts: Record<string, number>;
|
|
83
|
+
}>;
|
|
84
|
+
private require;
|
|
85
|
+
}
|
|
86
|
+
/** Verified Events of local Payments, written on the raw autocommit connection before acknowledging. */
|
|
87
|
+
export declare class TypeOrmEventInbox {
|
|
88
|
+
private readonly connection;
|
|
89
|
+
private readonly batchSize;
|
|
90
|
+
constructor(connection: TransactionalConnection, batchSize: number);
|
|
91
|
+
private repository;
|
|
92
|
+
/**
|
|
93
|
+
* Stores one Event: `stored` (new, or the same Event ID with the same content) or `conflict` (same
|
|
94
|
+
* Event ID with other content; the stored Event is kept).
|
|
95
|
+
*/
|
|
96
|
+
store(event: Omit<StoredEvent, "outcome" | "processedAt">): Promise<"stored" | "conflict">;
|
|
97
|
+
forPayment(paymentId: string): Promise<StoredEvent[]>;
|
|
98
|
+
/** Marks the unprocessed Events of a Payment up to `version` with their outcome. */
|
|
99
|
+
finish(paymentId: string, version: number, outcome: string): Promise<void>;
|
|
100
|
+
/** Marks every unprocessed Event of a Payment that cannot be applied, with the reason. */
|
|
101
|
+
reject(paymentId: string, outcome: string): Promise<void>;
|
|
102
|
+
/** Payments with unprocessed Events received before `before`, least recently attempted first. */
|
|
103
|
+
duePayments(before: Date, now: Date): Promise<string[]>;
|
|
104
|
+
}
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TypeOrmEventInbox = exports.TypeOrmAssociationStore = exports.ORDER_FILTERS = exports.TlyPayEvent = exports.TlyPayPaymentIntent = void 0;
|
|
13
|
+
const node_crypto_1 = require("node:crypto");
|
|
14
|
+
const typeorm_1 = require("typeorm");
|
|
15
|
+
/** One Payment creation intent and the Payment it created; see `PaymentAssociation`. */
|
|
16
|
+
let TlyPayPaymentIntent = class TlyPayPaymentIntent {
|
|
17
|
+
id;
|
|
18
|
+
channelToken;
|
|
19
|
+
orderCode;
|
|
20
|
+
paymentMethodCode;
|
|
21
|
+
attemptNo;
|
|
22
|
+
storeId;
|
|
23
|
+
idempotencyKey;
|
|
24
|
+
/** Hash of Store, Channel, Order, method, attempt and body: one intent per Order attempt. */
|
|
25
|
+
requestHash;
|
|
26
|
+
requestBody;
|
|
27
|
+
state;
|
|
28
|
+
err;
|
|
29
|
+
createdAt;
|
|
30
|
+
updatedAt;
|
|
31
|
+
lastAttemptAt;
|
|
32
|
+
paymentId;
|
|
33
|
+
hostedUrl;
|
|
34
|
+
status;
|
|
35
|
+
statusVersion;
|
|
36
|
+
/** Settlement of the Vendure Order: claim lease, completion time and the last anomaly code. */
|
|
37
|
+
settleClaimUntil;
|
|
38
|
+
settledAt;
|
|
39
|
+
settleErr;
|
|
40
|
+
};
|
|
41
|
+
exports.TlyPayPaymentIntent = TlyPayPaymentIntent;
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, typeorm_1.PrimaryColumn)({ type: "varchar", length: 36 }),
|
|
44
|
+
__metadata("design:type", String)
|
|
45
|
+
], TlyPayPaymentIntent.prototype, "id", void 0);
|
|
46
|
+
__decorate([
|
|
47
|
+
(0, typeorm_1.Index)(),
|
|
48
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 128 }),
|
|
49
|
+
__metadata("design:type", String)
|
|
50
|
+
], TlyPayPaymentIntent.prototype, "channelToken", void 0);
|
|
51
|
+
__decorate([
|
|
52
|
+
(0, typeorm_1.Index)(),
|
|
53
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 128 }),
|
|
54
|
+
__metadata("design:type", String)
|
|
55
|
+
], TlyPayPaymentIntent.prototype, "orderCode", void 0);
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 64 }),
|
|
58
|
+
__metadata("design:type", String)
|
|
59
|
+
], TlyPayPaymentIntent.prototype, "paymentMethodCode", void 0);
|
|
60
|
+
__decorate([
|
|
61
|
+
(0, typeorm_1.Column)({ type: "int" }),
|
|
62
|
+
__metadata("design:type", Number)
|
|
63
|
+
], TlyPayPaymentIntent.prototype, "attemptNo", void 0);
|
|
64
|
+
__decorate([
|
|
65
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 32 }),
|
|
66
|
+
__metadata("design:type", String)
|
|
67
|
+
], TlyPayPaymentIntent.prototype, "storeId", void 0);
|
|
68
|
+
__decorate([
|
|
69
|
+
(0, typeorm_1.Index)({ unique: true }),
|
|
70
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 255 }),
|
|
71
|
+
__metadata("design:type", String)
|
|
72
|
+
], TlyPayPaymentIntent.prototype, "idempotencyKey", void 0);
|
|
73
|
+
__decorate([
|
|
74
|
+
(0, typeorm_1.Index)({ unique: true }),
|
|
75
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 64 }),
|
|
76
|
+
__metadata("design:type", String)
|
|
77
|
+
], TlyPayPaymentIntent.prototype, "requestHash", void 0);
|
|
78
|
+
__decorate([
|
|
79
|
+
(0, typeorm_1.Column)({ type: "text" }),
|
|
80
|
+
__metadata("design:type", String)
|
|
81
|
+
], TlyPayPaymentIntent.prototype, "requestBody", void 0);
|
|
82
|
+
__decorate([
|
|
83
|
+
(0, typeorm_1.Index)(),
|
|
84
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 16 }),
|
|
85
|
+
__metadata("design:type", String)
|
|
86
|
+
], TlyPayPaymentIntent.prototype, "state", void 0);
|
|
87
|
+
__decorate([
|
|
88
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 64, nullable: true }),
|
|
89
|
+
__metadata("design:type", Object)
|
|
90
|
+
], TlyPayPaymentIntent.prototype, "err", void 0);
|
|
91
|
+
__decorate([
|
|
92
|
+
(0, typeorm_1.Column)({ type: Date }),
|
|
93
|
+
__metadata("design:type", Date)
|
|
94
|
+
], TlyPayPaymentIntent.prototype, "createdAt", void 0);
|
|
95
|
+
__decorate([
|
|
96
|
+
(0, typeorm_1.Column)({ type: Date }),
|
|
97
|
+
__metadata("design:type", Date)
|
|
98
|
+
], TlyPayPaymentIntent.prototype, "updatedAt", void 0);
|
|
99
|
+
__decorate([
|
|
100
|
+
(0, typeorm_1.Column)({ type: Date, nullable: true }),
|
|
101
|
+
__metadata("design:type", Object)
|
|
102
|
+
], TlyPayPaymentIntent.prototype, "lastAttemptAt", void 0);
|
|
103
|
+
__decorate([
|
|
104
|
+
(0, typeorm_1.Index)({ unique: true }),
|
|
105
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 22, nullable: true }),
|
|
106
|
+
__metadata("design:type", Object)
|
|
107
|
+
], TlyPayPaymentIntent.prototype, "paymentId", void 0);
|
|
108
|
+
__decorate([
|
|
109
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 512, nullable: true }),
|
|
110
|
+
__metadata("design:type", Object)
|
|
111
|
+
], TlyPayPaymentIntent.prototype, "hostedUrl", void 0);
|
|
112
|
+
__decorate([
|
|
113
|
+
(0, typeorm_1.Index)(),
|
|
114
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 32, nullable: true }),
|
|
115
|
+
__metadata("design:type", Object)
|
|
116
|
+
], TlyPayPaymentIntent.prototype, "status", void 0);
|
|
117
|
+
__decorate([
|
|
118
|
+
(0, typeorm_1.Column)({ type: "int", default: 0 }),
|
|
119
|
+
__metadata("design:type", Number)
|
|
120
|
+
], TlyPayPaymentIntent.prototype, "statusVersion", void 0);
|
|
121
|
+
__decorate([
|
|
122
|
+
(0, typeorm_1.Column)({ type: Date, nullable: true }),
|
|
123
|
+
__metadata("design:type", Object)
|
|
124
|
+
], TlyPayPaymentIntent.prototype, "settleClaimUntil", void 0);
|
|
125
|
+
__decorate([
|
|
126
|
+
(0, typeorm_1.Column)({ type: Date, nullable: true }),
|
|
127
|
+
__metadata("design:type", Object)
|
|
128
|
+
], TlyPayPaymentIntent.prototype, "settledAt", void 0);
|
|
129
|
+
__decorate([
|
|
130
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 64, nullable: true }),
|
|
131
|
+
__metadata("design:type", Object)
|
|
132
|
+
], TlyPayPaymentIntent.prototype, "settleErr", void 0);
|
|
133
|
+
exports.TlyPayPaymentIntent = TlyPayPaymentIntent = __decorate([
|
|
134
|
+
(0, typeorm_1.Entity)("tlypay_vd_payment_intent")
|
|
135
|
+
], TlyPayPaymentIntent);
|
|
136
|
+
/** One verified Webhook Event of a local Payment, stored before it is acknowledged. */
|
|
137
|
+
let TlyPayEvent = class TlyPayEvent {
|
|
138
|
+
eventId;
|
|
139
|
+
paymentId;
|
|
140
|
+
status;
|
|
141
|
+
statusVersion;
|
|
142
|
+
occurredAt;
|
|
143
|
+
bodyHash;
|
|
144
|
+
receivedAt;
|
|
145
|
+
/** Null until processed: `applied` or a stable code why the Event was not applied. */
|
|
146
|
+
outcome;
|
|
147
|
+
processedAt;
|
|
148
|
+
lastAttemptAt;
|
|
149
|
+
};
|
|
150
|
+
exports.TlyPayEvent = TlyPayEvent;
|
|
151
|
+
__decorate([
|
|
152
|
+
(0, typeorm_1.PrimaryColumn)({ type: "varchar", length: 22 }),
|
|
153
|
+
__metadata("design:type", String)
|
|
154
|
+
], TlyPayEvent.prototype, "eventId", void 0);
|
|
155
|
+
__decorate([
|
|
156
|
+
(0, typeorm_1.Index)(),
|
|
157
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 22 }),
|
|
158
|
+
__metadata("design:type", String)
|
|
159
|
+
], TlyPayEvent.prototype, "paymentId", void 0);
|
|
160
|
+
__decorate([
|
|
161
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 32 }),
|
|
162
|
+
__metadata("design:type", String)
|
|
163
|
+
], TlyPayEvent.prototype, "status", void 0);
|
|
164
|
+
__decorate([
|
|
165
|
+
(0, typeorm_1.Column)({ type: "int" }),
|
|
166
|
+
__metadata("design:type", Number)
|
|
167
|
+
], TlyPayEvent.prototype, "statusVersion", void 0);
|
|
168
|
+
__decorate([
|
|
169
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 40 }),
|
|
170
|
+
__metadata("design:type", String)
|
|
171
|
+
], TlyPayEvent.prototype, "occurredAt", void 0);
|
|
172
|
+
__decorate([
|
|
173
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 64 }),
|
|
174
|
+
__metadata("design:type", String)
|
|
175
|
+
], TlyPayEvent.prototype, "bodyHash", void 0);
|
|
176
|
+
__decorate([
|
|
177
|
+
(0, typeorm_1.Column)({ type: Date }),
|
|
178
|
+
__metadata("design:type", Date
|
|
179
|
+
/** Null until processed: `applied` or a stable code why the Event was not applied. */
|
|
180
|
+
)
|
|
181
|
+
], TlyPayEvent.prototype, "receivedAt", void 0);
|
|
182
|
+
__decorate([
|
|
183
|
+
(0, typeorm_1.Index)(),
|
|
184
|
+
(0, typeorm_1.Column)({ type: "varchar", length: 64, nullable: true }),
|
|
185
|
+
__metadata("design:type", Object)
|
|
186
|
+
], TlyPayEvent.prototype, "outcome", void 0);
|
|
187
|
+
__decorate([
|
|
188
|
+
(0, typeorm_1.Column)({ type: Date, nullable: true }),
|
|
189
|
+
__metadata("design:type", Object)
|
|
190
|
+
], TlyPayEvent.prototype, "processedAt", void 0);
|
|
191
|
+
__decorate([
|
|
192
|
+
(0, typeorm_1.Column)({ type: Date, nullable: true }),
|
|
193
|
+
__metadata("design:type", Object)
|
|
194
|
+
], TlyPayEvent.prototype, "lastAttemptAt", void 0);
|
|
195
|
+
exports.TlyPayEvent = TlyPayEvent = __decorate([
|
|
196
|
+
(0, typeorm_1.Entity)("tlypay_vd_event")
|
|
197
|
+
], TlyPayEvent);
|
|
198
|
+
/** Status filters of the Orders view, as in the WooCommerce plugin. */
|
|
199
|
+
exports.ORDER_FILTERS = {
|
|
200
|
+
all: [],
|
|
201
|
+
open: ["open"],
|
|
202
|
+
paid: ["paid"],
|
|
203
|
+
attention: ["partially_paid", "overpaid", "review_required"],
|
|
204
|
+
closed: ["expired", "cancelled"],
|
|
205
|
+
creating: ["creating"],
|
|
206
|
+
};
|
|
207
|
+
function toAssociation(row) {
|
|
208
|
+
return {
|
|
209
|
+
id: row.id, channelToken: row.channelToken, orderCode: row.orderCode, paymentMethodCode: row.paymentMethodCode,
|
|
210
|
+
attemptNo: row.attemptNo, storeId: row.storeId, idempotencyKey: row.idempotencyKey, requestHash: row.requestHash,
|
|
211
|
+
requestBody: row.requestBody, state: row.state, err: row.err, createdAt: row.createdAt,
|
|
212
|
+
lastAttemptAt: row.lastAttemptAt, paymentId: row.paymentId, hostedUrl: row.hostedUrl, status: row.status,
|
|
213
|
+
statusVersion: row.statusVersion, updatedAt: row.updatedAt, settledAt: row.settledAt, settleErr: row.settleErr,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Intents on the raw autocommit connection: `reserve` commits before `POST /v1/payments`, outside
|
|
218
|
+
* the host request transaction, so a rolled back request cannot lose the idempotency key.
|
|
219
|
+
*/
|
|
220
|
+
class TypeOrmAssociationStore {
|
|
221
|
+
connection;
|
|
222
|
+
batchSize;
|
|
223
|
+
constructor(connection, batchSize) {
|
|
224
|
+
this.connection = connection;
|
|
225
|
+
this.batchSize = batchSize;
|
|
226
|
+
}
|
|
227
|
+
repository() {
|
|
228
|
+
return this.connection.rawConnection.getRepository(TlyPayPaymentIntent);
|
|
229
|
+
}
|
|
230
|
+
async one(where) {
|
|
231
|
+
const row = await this.repository().findOne({ where });
|
|
232
|
+
return row === null ? null : toAssociation(row);
|
|
233
|
+
}
|
|
234
|
+
async reserve(input) {
|
|
235
|
+
const now = new Date();
|
|
236
|
+
await this.repository().createQueryBuilder().insert().into(TlyPayPaymentIntent).values({
|
|
237
|
+
...input, id: (0, node_crypto_1.randomUUID)(), state: "creating", err: null, createdAt: now, updatedAt: now, lastAttemptAt: null,
|
|
238
|
+
paymentId: null, hostedUrl: null, status: null, statusVersion: 0, settleClaimUntil: null, settledAt: null,
|
|
239
|
+
settleErr: null,
|
|
240
|
+
}).orIgnore().execute();
|
|
241
|
+
const row = await this.one({ requestHash: input.requestHash });
|
|
242
|
+
if (row === null)
|
|
243
|
+
throw new TypeError("Vendure Payment association was not persisted");
|
|
244
|
+
return row;
|
|
245
|
+
}
|
|
246
|
+
async bindPayment(id, paymentId, hostedUrl) {
|
|
247
|
+
await this.repository().createQueryBuilder().update(TlyPayPaymentIntent)
|
|
248
|
+
.set({ paymentId, hostedUrl, state: "bound", err: null, updatedAt: new Date() })
|
|
249
|
+
.where("id = :id", { id })
|
|
250
|
+
.andWhere(new typeorm_1.Brackets((qb) => qb.where("paymentId IS NULL").orWhere("paymentId = :paymentId", { paymentId })))
|
|
251
|
+
.execute();
|
|
252
|
+
return this.require(id);
|
|
253
|
+
}
|
|
254
|
+
async markFailed(id, err) {
|
|
255
|
+
await this.repository().update({ id, state: "creating" }, { state: "failed", err, updatedAt: new Date() });
|
|
256
|
+
}
|
|
257
|
+
async recordAttempt(id, at) {
|
|
258
|
+
await this.repository().update({ id, state: "creating" }, { lastAttemptAt: at });
|
|
259
|
+
}
|
|
260
|
+
async recordAuthority(id, view) {
|
|
261
|
+
await this.repository().createQueryBuilder().update(TlyPayPaymentIntent)
|
|
262
|
+
.set({ status: view.status, statusVersion: view.statusVersion, updatedAt: new Date() })
|
|
263
|
+
.where("id = :id AND paymentId = :paymentId AND statusVersion < :version", { id, paymentId: view.paymentId, version: view.statusVersion })
|
|
264
|
+
.execute();
|
|
265
|
+
return this.require(id);
|
|
266
|
+
}
|
|
267
|
+
findByPaymentId(paymentId) {
|
|
268
|
+
return this.one({ paymentId });
|
|
269
|
+
}
|
|
270
|
+
findById(id) {
|
|
271
|
+
return this.one({ id });
|
|
272
|
+
}
|
|
273
|
+
async findOrderIntents(channelToken, orderCode) {
|
|
274
|
+
const rows = await this.repository().find({ where: { channelToken, orderCode }, order: { createdAt: "DESC" } });
|
|
275
|
+
return rows.map(toAssociation);
|
|
276
|
+
}
|
|
277
|
+
async findCreatingIntents(orderCode) {
|
|
278
|
+
const rows = await this.repository().find({ where: { orderCode, state: "creating" }, order: { createdAt: "DESC" } });
|
|
279
|
+
return rows.map(toAssociation);
|
|
280
|
+
}
|
|
281
|
+
async findRecoverable(createdBefore) {
|
|
282
|
+
const rows = await this.repository().createQueryBuilder("intent")
|
|
283
|
+
.addSelect("CASE WHEN intent.lastAttemptAt IS NULL THEN 0 ELSE 1 END", "attempted")
|
|
284
|
+
.where("intent.state = :state AND intent.createdAt < :before", { state: "creating", before: createdBefore })
|
|
285
|
+
.orderBy("attempted", "ASC")
|
|
286
|
+
.addOrderBy("intent.lastAttemptAt", "ASC")
|
|
287
|
+
.addOrderBy("intent.createdAt", "ASC")
|
|
288
|
+
.limit(this.batchSize)
|
|
289
|
+
.getMany();
|
|
290
|
+
return rows.map(toAssociation);
|
|
291
|
+
}
|
|
292
|
+
/** Claims settlement of one intent until `until`; see `SyncStore.claimSettlement`. */
|
|
293
|
+
async claimSettlement(id, now, until) {
|
|
294
|
+
const result = await this.repository().createQueryBuilder().update(TlyPayPaymentIntent)
|
|
295
|
+
.set({ settleClaimUntil: until })
|
|
296
|
+
.where("id = :id AND settledAt IS NULL", { id })
|
|
297
|
+
.andWhere(new typeorm_1.Brackets((qb) => qb.where("settleClaimUntil IS NULL").orWhere("settleClaimUntil < :now", { now })))
|
|
298
|
+
.execute();
|
|
299
|
+
if (result.affected === 1)
|
|
300
|
+
return "claimed";
|
|
301
|
+
const row = await this.repository().findOne({ where: { id } });
|
|
302
|
+
return row?.settledAt != null ? "settled" : "busy";
|
|
303
|
+
}
|
|
304
|
+
async finishSettlement(id, settled, err) {
|
|
305
|
+
await this.repository().update({ id }, {
|
|
306
|
+
settleClaimUntil: null, settledAt: settled ? new Date() : null, settleErr: err, updatedAt: new Date(),
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* One page of the Orders view, newest first, with counts per status filter. `channelToken` null
|
|
311
|
+
* lists every Channel.
|
|
312
|
+
*/
|
|
313
|
+
async list(filter, skip, take, channelToken) {
|
|
314
|
+
const scoped = () => {
|
|
315
|
+
const qb = this.repository().createQueryBuilder("intent");
|
|
316
|
+
// One row per Order: its latest intent.
|
|
317
|
+
const latest = qb.subQuery().select("MAX(latest.attemptNo)").from(TlyPayPaymentIntent, "latest")
|
|
318
|
+
.where("latest.orderCode = intent.orderCode AND latest.channelToken = intent.channelToken").getQuery();
|
|
319
|
+
qb.where(`intent.attemptNo = ${latest}`);
|
|
320
|
+
if (channelToken !== null)
|
|
321
|
+
qb.andWhere("intent.channelToken = :channelToken", { channelToken });
|
|
322
|
+
return qb;
|
|
323
|
+
};
|
|
324
|
+
const grouped = await scoped()
|
|
325
|
+
.select("intent.state", "state").addSelect("intent.status", "status").addSelect("COUNT(*)", "count")
|
|
326
|
+
.groupBy("intent.state").addGroupBy("intent.status").getRawMany();
|
|
327
|
+
const counts = Object.fromEntries(Object.keys(exports.ORDER_FILTERS).map((key) => [key, 0]));
|
|
328
|
+
for (const group of grouped) {
|
|
329
|
+
const key = group.state === "bound" ? group.status ?? "open" : group.state;
|
|
330
|
+
const amount = Number(group.count);
|
|
331
|
+
counts.all = (counts.all ?? 0) + amount;
|
|
332
|
+
for (const [name, statuses] of Object.entries(exports.ORDER_FILTERS)) {
|
|
333
|
+
if (statuses.includes(key))
|
|
334
|
+
counts[name] = (counts[name] ?? 0) + amount;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const statuses = exports.ORDER_FILTERS[filter] ?? [];
|
|
338
|
+
const qb = scoped();
|
|
339
|
+
if (statuses.includes("creating"))
|
|
340
|
+
qb.andWhere("intent.state = 'creating'");
|
|
341
|
+
else if (statuses.length > 0)
|
|
342
|
+
qb.andWhere("intent.state = 'bound' AND intent.status IN (:...statuses)", { statuses });
|
|
343
|
+
const [rows, totalItems] = await qb.orderBy("intent.createdAt", "DESC").offset(skip).limit(take).getManyAndCount();
|
|
344
|
+
return { items: rows.map(toAssociation), totalItems, counts };
|
|
345
|
+
}
|
|
346
|
+
async require(id) {
|
|
347
|
+
const row = await this.one({ id });
|
|
348
|
+
if (row === null)
|
|
349
|
+
throw new TypeError("Vendure Payment association was not persisted");
|
|
350
|
+
return row;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
exports.TypeOrmAssociationStore = TypeOrmAssociationStore;
|
|
354
|
+
/** Verified Events of local Payments, written on the raw autocommit connection before acknowledging. */
|
|
355
|
+
class TypeOrmEventInbox {
|
|
356
|
+
connection;
|
|
357
|
+
batchSize;
|
|
358
|
+
constructor(connection, batchSize) {
|
|
359
|
+
this.connection = connection;
|
|
360
|
+
this.batchSize = batchSize;
|
|
361
|
+
}
|
|
362
|
+
repository() {
|
|
363
|
+
return this.connection.rawConnection.getRepository(TlyPayEvent);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Stores one Event: `stored` (new, or the same Event ID with the same content) or `conflict` (same
|
|
367
|
+
* Event ID with other content; the stored Event is kept).
|
|
368
|
+
*/
|
|
369
|
+
async store(event) {
|
|
370
|
+
await this.repository().createQueryBuilder().insert().into(TlyPayEvent)
|
|
371
|
+
.values({ ...event, outcome: null, processedAt: null, lastAttemptAt: null }).orIgnore().execute();
|
|
372
|
+
const stored = await this.repository().findOne({ where: { eventId: event.eventId } });
|
|
373
|
+
if (stored === null)
|
|
374
|
+
throw new TypeError("TLY Pay Event was not persisted");
|
|
375
|
+
return stored.bodyHash === event.bodyHash ? "stored" : "conflict";
|
|
376
|
+
}
|
|
377
|
+
async forPayment(paymentId) {
|
|
378
|
+
return this.repository().find({ where: { paymentId }, order: { statusVersion: "DESC", receivedAt: "DESC" } });
|
|
379
|
+
}
|
|
380
|
+
/** Marks the unprocessed Events of a Payment up to `version` with their outcome. */
|
|
381
|
+
async finish(paymentId, version, outcome) {
|
|
382
|
+
await this.repository().createQueryBuilder().update(TlyPayEvent)
|
|
383
|
+
.set({ outcome, processedAt: new Date() })
|
|
384
|
+
.where("paymentId = :paymentId AND outcome IS NULL AND statusVersion <= :version", { paymentId, version })
|
|
385
|
+
.execute();
|
|
386
|
+
}
|
|
387
|
+
/** Marks every unprocessed Event of a Payment that cannot be applied, with the reason. */
|
|
388
|
+
async reject(paymentId, outcome) {
|
|
389
|
+
await this.repository().update({ paymentId, outcome: (0, typeorm_1.IsNull)() }, { outcome, processedAt: new Date() });
|
|
390
|
+
}
|
|
391
|
+
/** Payments with unprocessed Events received before `before`, least recently attempted first. */
|
|
392
|
+
async duePayments(before, now) {
|
|
393
|
+
const rows = await this.repository().find({
|
|
394
|
+
where: { outcome: (0, typeorm_1.IsNull)(), receivedAt: (0, typeorm_1.LessThan)(before) },
|
|
395
|
+
order: { lastAttemptAt: "ASC", receivedAt: "ASC" }, take: this.batchSize,
|
|
396
|
+
});
|
|
397
|
+
const ids = [...new Set(rows.map((row) => row.paymentId))];
|
|
398
|
+
if (ids.length > 0) {
|
|
399
|
+
await this.repository().createQueryBuilder().update(TlyPayEvent).set({ lastAttemptAt: now })
|
|
400
|
+
.where("paymentId IN (:...ids) AND outcome IS NULL", { ids }).execute();
|
|
401
|
+
}
|
|
402
|
+
return ids;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
exports.TypeOrmEventInbox = TypeOrmEventInbox;
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ScheduledTask, Type } from "@vendure/core";
|
|
2
|
+
import { TlyPayPluginOptions } from "./service";
|
|
3
|
+
/** Hourly recheck of the saved connection, like the WooCommerce plugin's hourly check. */
|
|
4
|
+
export declare const connectionCheckTask: ScheduledTask<Record<string, any>>;
|
|
5
|
+
/**
|
|
6
|
+
* Runs the early connection check after a transient failure, replays due Payment creation intents and
|
|
7
|
+
* dispatches stored Events that are still unprocessed.
|
|
8
|
+
*/
|
|
9
|
+
export declare const recoveryTask: ScheduledTask<Record<string, any>>;
|
|
10
|
+
/**
|
|
11
|
+
* TLY Pay crypto payments for Vendure 3.8.
|
|
12
|
+
*
|
|
13
|
+
* Registers the "TLY Pay" payment method handler and its readiness eligibility checker, the Shop API
|
|
14
|
+
* `tlyPayCheckout` query and `createTlyPayPayment` mutation, the Admin API used by the Dashboard
|
|
15
|
+
* extension, the REST routes `/tly-pay/webhook` and `/tly-pay/connect/return`, the encrypted connection
|
|
16
|
+
* storage, the Payment and Event tables, the sync job queue and the scheduled connection check and
|
|
17
|
+
* recovery tasks.
|
|
18
|
+
*/
|
|
19
|
+
export declare class TlyPayPlugin {
|
|
20
|
+
static options: TlyPayPluginOptions;
|
|
21
|
+
/**
|
|
22
|
+
* Configures the plugin with the host's site-private values. `publicUrl` is the public HTTPS URL of
|
|
23
|
+
* this Vendure server's REST routes; `secretKey` encrypts the saved connection at rest.
|
|
24
|
+
*/
|
|
25
|
+
static init(options: TlyPayPluginOptions): Type<TlyPayPlugin>;
|
|
26
|
+
}
|