@easy-cms/plugin-ecommerce 1.0.1 → 1.1.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.
|
@@ -783,7 +783,7 @@ var INFO = {
|
|
|
783
783
|
name: "@easy-cms/plugin-ecommerce",
|
|
784
784
|
// The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
|
|
785
785
|
apiVersion: 1,
|
|
786
|
-
...true ? { version: "1.0
|
|
786
|
+
...true ? { version: "1.1.0" } : {}
|
|
787
787
|
};
|
|
788
788
|
|
|
789
789
|
// src/shop.ts
|
|
@@ -821,6 +821,9 @@ async function scope(cms, collection, field, who) {
|
|
|
821
821
|
return parts;
|
|
822
822
|
}
|
|
823
823
|
var writeAs = (who) => ({ context: who.context, user: who.user });
|
|
824
|
+
var RECHECK_AFTER = 2 * 6e4;
|
|
825
|
+
var RECHECK_FOR = 864e5;
|
|
826
|
+
var RECHECK_FOR_AT_MOST = 7 * 864e5;
|
|
824
827
|
var Carts = class {
|
|
825
828
|
constructor(cms, shop) {
|
|
826
829
|
this.cms = cms;
|
|
@@ -1495,10 +1498,6 @@ var Orders = class {
|
|
|
1495
1498
|
for (const item of order.items ?? [])
|
|
1496
1499
|
await this.take({ product: item.product, variant: item.variant }, Number(item.quantity));
|
|
1497
1500
|
}
|
|
1498
|
-
/**
|
|
1499
|
-
* Upkeep: transactions left `processing` by a stopped process go back to `pending`, so the
|
|
1500
|
-
* next confirmation or webhook makes the order.
|
|
1501
|
-
*/
|
|
1502
1501
|
/** Cancels unpaid orders past their `expiresAt`, putting their stock back. */
|
|
1503
1502
|
async expire(now) {
|
|
1504
1503
|
const due = await this.cms.find(ORDERS, {
|
|
@@ -1526,6 +1525,10 @@ var Orders = class {
|
|
|
1526
1525
|
}
|
|
1527
1526
|
return cancelled;
|
|
1528
1527
|
}
|
|
1528
|
+
/**
|
|
1529
|
+
* Upkeep: transactions left `processing` by a stopped process go back to `pending`, so the
|
|
1530
|
+
* next confirmation or webhook makes the order.
|
|
1531
|
+
*/
|
|
1529
1532
|
async release(now) {
|
|
1530
1533
|
const stale = new Date(now.getTime() - 10 * 6e4).toISOString();
|
|
1531
1534
|
const stuck = await this.cms.find(TRANSACTIONS, {
|
|
@@ -1550,6 +1553,53 @@ var Orders = class {
|
|
|
1550
1553
|
);
|
|
1551
1554
|
return stuck.docs.length;
|
|
1552
1555
|
}
|
|
1556
|
+
/**
|
|
1557
|
+
* Upkeep: asks the payment provider again about payments still `pending` after a few minutes,
|
|
1558
|
+
* for when neither the page nor a webhook said they went through (a closed tab, a webhook not
|
|
1559
|
+
* set up, a provider that doesn't retry webhooks). A payment that went through gets its order.
|
|
1560
|
+
* Past the method's `expiresIn`, a payment that didn't is given up (`failed`, "expired"); if it
|
|
1561
|
+
* goes through after all, the order is still made. Without `expiresIn`, payments are asked
|
|
1562
|
+
* about for a day.
|
|
1563
|
+
*/
|
|
1564
|
+
async recheck(now) {
|
|
1565
|
+
const settled = new Date(now.getTime() - RECHECK_AFTER).toISOString();
|
|
1566
|
+
const stale = await this.cms.find(TRANSACTIONS, {
|
|
1567
|
+
where: {
|
|
1568
|
+
and: [
|
|
1569
|
+
{ status: { equals: "pending" } },
|
|
1570
|
+
{ order: { exists: false } },
|
|
1571
|
+
{ createdAt: { lt: settled } },
|
|
1572
|
+
{ createdAt: { gt: new Date(now.getTime() - RECHECK_FOR_AT_MOST).toISOString() } }
|
|
1573
|
+
]
|
|
1574
|
+
},
|
|
1575
|
+
sort: "createdAt",
|
|
1576
|
+
limit: 100,
|
|
1577
|
+
depth: 0
|
|
1578
|
+
});
|
|
1579
|
+
let paid = 0;
|
|
1580
|
+
let expired = 0;
|
|
1581
|
+
for (const tx of stale.docs) {
|
|
1582
|
+
const adapter = this.shop.methods.get(String(tx.method));
|
|
1583
|
+
if (!adapter || adapter.manual || !adapter.confirm) continue;
|
|
1584
|
+
const age = now.getTime() - new Date(String(tx.createdAt)).getTime();
|
|
1585
|
+
const expiresIn = typeof adapter.expiresIn === "number" ? adapter.expiresIn * 1e3 : null;
|
|
1586
|
+
if (expiresIn === null && age > RECHECK_FOR) continue;
|
|
1587
|
+
const outcome = await adapter.confirm({ cms: this.cms, transaction: toPayment(tx), input: {} }).catch(() => "processing");
|
|
1588
|
+
if (outcome === "succeeded") {
|
|
1589
|
+
const order = await this.finalize(tx.id, "paid").catch(() => null);
|
|
1590
|
+
if (order) paid++;
|
|
1591
|
+
} else if (expiresIn !== null && age > expiresIn) {
|
|
1592
|
+
const given = await this.cms.update(
|
|
1593
|
+
TRANSACTIONS,
|
|
1594
|
+
tx.id,
|
|
1595
|
+
{ status: "failed", error: "expired" },
|
|
1596
|
+
{ where: { status: { equals: "pending" } }, depth: 0 }
|
|
1597
|
+
);
|
|
1598
|
+
if (given) expired++;
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
return { paid, expired };
|
|
1602
|
+
}
|
|
1553
1603
|
};
|
|
1554
1604
|
|
|
1555
1605
|
// src/plugin.ts
|
|
@@ -1898,6 +1948,7 @@ function ecommercePlugin(options) {
|
|
|
1898
1948
|
run: async ({ cms, now }) => {
|
|
1899
1949
|
await runtime(cms).orders.release(now);
|
|
1900
1950
|
await runtime(cms).orders.expire(now);
|
|
1951
|
+
await runtime(cms).orders.recheck(now);
|
|
1901
1952
|
}
|
|
1902
1953
|
},
|
|
1903
1954
|
{
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Label, Field, FieldTypeDefinition, ID, AuthUser, RequestContext, EasyCMS, MembersSignup, MembersConfig, CollectionConfig, TypedPlugin } from '@easy-cms/core';
|
|
2
|
-
import { O as ORDER_STATUSES, C as CartView, a as Currency, A as Address, b as Adjustment, c as OrderStatus, P as PaymentAdapter } from './payments-
|
|
3
|
-
export { d as CURRENCIES, e as CartLine, f as CheckoutResult, g as ConfirmResult, I as InitiateResult, M as ManualAdapterOptions, h as PaymentTransaction, S as ShopSettings, T as TRANSACTION_STATUSES, i as TransactionStatus, j as definePaymentAdapter, k as formatPrice, m as manualAdapter, p as priceField } from './payments-
|
|
2
|
+
import { O as ORDER_STATUSES, C as CartView, a as Currency, A as Address, b as Adjustment, c as OrderStatus, P as PaymentAdapter } from './payments-CyvXJbyX.js';
|
|
3
|
+
export { d as CURRENCIES, e as CartLine, f as CheckoutResult, g as ConfirmResult, I as InitiateResult, M as ManualAdapterOptions, h as PaymentTransaction, S as ShopSettings, T as TRANSACTION_STATUSES, i as TransactionStatus, j as definePaymentAdapter, k as formatPrice, m as manualAdapter, p as priceField } from './payments-CyvXJbyX.js';
|
|
4
4
|
|
|
5
5
|
declare module '@easy-cms/core' {
|
|
6
6
|
interface CustomFieldTypes {
|
package/dist/index.js
CHANGED
|
@@ -166,8 +166,11 @@ interface PaymentAdapter {
|
|
|
166
166
|
/** For `manual` ones: what to tell the customer, e.g. the bank account (page and email). */
|
|
167
167
|
readonly instructions?: Label;
|
|
168
168
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
169
|
+
* Seconds a payment may wait. For `manual` ones, then its order is cancelled and its stock put
|
|
170
|
+
* back; `false`: it waits until staff act on it. For the others, the shop asks the provider
|
|
171
|
+
* (`confirm`) about payments still pending after a few minutes, and past `expiresIn` gives up
|
|
172
|
+
* on those that didn't go through, e.g. an expired PromptPay QR code. Without it, they are
|
|
173
|
+
* asked about for a day and then left pending.
|
|
171
174
|
*/
|
|
172
175
|
readonly expiresIn?: number | false;
|
|
173
176
|
/** Starts the payment of a new transaction. */
|
package/dist/stripe.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Label } from '@easy-cms/core';
|
|
2
|
-
import { P as PaymentAdapter } from './payments-
|
|
2
|
+
import { P as PaymentAdapter } from './payments-CyvXJbyX.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The Stripe API version the adapter asks for and is tested with, whichever `stripe` package
|
package/dist/stripe.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-ecommerce",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A shop for Easy CMS: products and variants, prices in several currencies, carts (guests too), checkout with Stripe or bank transfer, orders, stock, customer accounts, and React and Vue hooks (experimental)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"react": ">=18.0.0",
|
|
37
37
|
"stripe": ">=17.0.0 <24.0.0",
|
|
38
38
|
"vue": "^3.4.0",
|
|
39
|
-
"@easy-cms/core": "^1.0
|
|
39
|
+
"@easy-cms/core": "^1.1.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"react": {
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
"stripe": "^23.0.0",
|
|
59
59
|
"stripe-17": "npm:stripe@^17.7.0",
|
|
60
60
|
"vue": "^3.5.43",
|
|
61
|
-
"@easy-cms/core": "^1.0
|
|
62
|
-
"@easy-cms/db-sqlite": "^1.0
|
|
63
|
-
"@easy-cms/plugin-multi-tenant": "^1.0
|
|
61
|
+
"@easy-cms/core": "^1.1.0",
|
|
62
|
+
"@easy-cms/db-sqlite": "^1.1.0",
|
|
63
|
+
"@easy-cms/plugin-multi-tenant": "^1.1.0"
|
|
64
64
|
},
|
|
65
65
|
"engines": {
|
|
66
66
|
"node": ">=22.12"
|