@serge-pro-dev/payload-payment-yookassa 0.0.0-stage → 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +146 -2
- package/README.ru.md +148 -0
- package/dist/amount.d.ts +11 -0
- package/dist/amount.js +29 -0
- package/dist/amount.js.map +1 -0
- package/dist/api.d.ts +51 -0
- package/dist/api.js +58 -0
- package/dist/api.js.map +1 -0
- package/dist/client.d.ts +49 -0
- package/dist/client.js +61 -0
- package/dist/client.js.map +1 -0
- package/dist/confirm.d.ts +4 -0
- package/dist/confirm.js +95 -0
- package/dist/confirm.js.map +1 -0
- package/dist/fields.d.ts +13 -0
- package/dist/fields.js +68 -0
- package/dist/fields.js.map +1 -0
- package/dist/finalize.d.ts +45 -0
- package/dist/finalize.js +207 -0
- package/dist/finalize.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +164 -0
- package/dist/index.js.map +1 -0
- package/dist/initiate.d.ts +3 -0
- package/dist/initiate.js +221 -0
- package/dist/initiate.js.map +1 -0
- package/dist/messages.d.ts +3 -0
- package/dist/messages.js +77 -0
- package/dist/messages.js.map +1 -0
- package/dist/receipt.d.ts +57 -0
- package/dist/receipt.js +43 -0
- package/dist/receipt.js.map +1 -0
- package/dist/reconcile.d.ts +35 -0
- package/dist/reconcile.js +125 -0
- package/dist/reconcile.js.map +1 -0
- package/dist/settings.d.ts +29 -0
- package/dist/settings.js +207 -0
- package/dist/settings.js.map +1 -0
- package/dist/settlement.d.ts +25 -0
- package/dist/settlement.js +62 -0
- package/dist/settlement.js.map +1 -0
- package/dist/status.d.ts +13 -0
- package/dist/status.js +92 -0
- package/dist/status.js.map +1 -0
- package/dist/types.d.ts +84 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +25 -0
- package/dist/utils.js.map +1 -0
- package/dist/webhooks.d.ts +16 -0
- package/dist/webhooks.js +83 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +54 -3
package/dist/confirm.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { PaymentNotCompletedError, validateSettlement } from './settlement.js';
|
|
2
|
+
import { getDocumentID, isRecord, normalizeEmail } from './utils.js';
|
|
3
|
+
export const snapshotItems = (items)=>{
|
|
4
|
+
if (!Array.isArray(items) || items.length === 0) throw new Error('Transaction items are missing');
|
|
5
|
+
return items.map((item)=>{
|
|
6
|
+
if (!isRecord(item)) throw new Error('Transaction items are invalid');
|
|
7
|
+
const product = getDocumentID(item.product);
|
|
8
|
+
const variant = item.variant ? getDocumentID(item.variant) : undefined;
|
|
9
|
+
if (product === undefined || typeof item.quantity !== 'number') {
|
|
10
|
+
throw new Error('Transaction items are invalid');
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
product,
|
|
14
|
+
quantity: item.quantity,
|
|
15
|
+
...variant === undefined ? {} : {
|
|
16
|
+
variant
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
export const confirmOrder = (config)=>async ({ data, finalizeOrder, req, transactionsSlug = 'transactions' })=>{
|
|
22
|
+
const payload = req.payload;
|
|
23
|
+
const transactionID = getDocumentID(data.transactionID);
|
|
24
|
+
if (transactionID === undefined) throw new Error('Transaction ID is required');
|
|
25
|
+
if (typeof finalizeOrder !== 'function') throw new Error('Core order finalizer is required');
|
|
26
|
+
try {
|
|
27
|
+
const transaction = await payload.findByID({
|
|
28
|
+
collection: transactionsSlug,
|
|
29
|
+
id: transactionID,
|
|
30
|
+
depth: 0,
|
|
31
|
+
overrideAccess: true,
|
|
32
|
+
req
|
|
33
|
+
});
|
|
34
|
+
// The browser sends the id as a string; the relationship needs the stored id type (number in Postgres).
|
|
35
|
+
const canonicalTransactionID = getDocumentID(transaction.id);
|
|
36
|
+
if (canonicalTransactionID === undefined) throw new Error('Transaction ID is invalid');
|
|
37
|
+
const provider = isRecord(transaction.yookassa) ? transaction.yookassa : undefined;
|
|
38
|
+
const paymentID = provider?.paymentID;
|
|
39
|
+
if (typeof paymentID !== 'string' || !paymentID) {
|
|
40
|
+
throw new Error('Transaction has no YooKassa payment');
|
|
41
|
+
}
|
|
42
|
+
// The source of truth is the provider, not the browser.
|
|
43
|
+
const payment = await config.client.getPayment(paymentID);
|
|
44
|
+
validateSettlement({
|
|
45
|
+
transaction,
|
|
46
|
+
payment,
|
|
47
|
+
canonicalCartID: data.cartID,
|
|
48
|
+
user: req.user,
|
|
49
|
+
customerEmail: data.customerEmail,
|
|
50
|
+
decimals: config.decimals
|
|
51
|
+
});
|
|
52
|
+
const purchaser = req.user ? {
|
|
53
|
+
customer: req.user.id
|
|
54
|
+
} : {
|
|
55
|
+
customerEmail: normalizeEmail(data.customerEmail)
|
|
56
|
+
};
|
|
57
|
+
const order = await finalizeOrder({
|
|
58
|
+
orderData: {
|
|
59
|
+
amount: transaction.amount,
|
|
60
|
+
currency: transaction.currency,
|
|
61
|
+
...purchaser,
|
|
62
|
+
items: snapshotItems(transaction.items),
|
|
63
|
+
...isRecord(provider?.shippingAddress) ? {
|
|
64
|
+
shippingAddress: provider.shippingAddress
|
|
65
|
+
} : {},
|
|
66
|
+
...isRecord(provider?.delivery) ? {
|
|
67
|
+
delivery: provider.delivery
|
|
68
|
+
} : {},
|
|
69
|
+
status: 'processing'
|
|
70
|
+
},
|
|
71
|
+
transactionID: canonicalTransactionID
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
message: 'Order confirmed successfully',
|
|
75
|
+
orderID: order.id,
|
|
76
|
+
transactionID: canonicalTransactionID,
|
|
77
|
+
...typeof order.accessToken === 'string' ? {
|
|
78
|
+
accessToken: order.accessToken
|
|
79
|
+
} : {}
|
|
80
|
+
};
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (error instanceof PaymentNotCompletedError) {
|
|
83
|
+
// Normal when the buyer comes back before the payment is final: the client retries.
|
|
84
|
+
payload.logger.info(`YooKassa payment not completed yet (${error.status})`);
|
|
85
|
+
} else {
|
|
86
|
+
payload.logger.error({
|
|
87
|
+
err: error,
|
|
88
|
+
msg: 'Error confirming order with YooKassa'
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
throw new Error(error instanceof Error ? error.message : 'Unknown error confirming order');
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
//# sourceMappingURL=confirm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/confirm.ts"],"sourcesContent":["import type { PaymentAdapter } from '@payloadcms/plugin-ecommerce/types'\n\nimport { PaymentNotCompletedError, validateSettlement } from './settlement.js'\nimport type { ResolvedConfig } from './types.js'\nimport { getDocumentID, isRecord, normalizeEmail } from './utils.js'\n\nexport const snapshotItems = (items: unknown): Record<string, unknown>[] => {\n if (!Array.isArray(items) || items.length === 0) throw new Error('Transaction items are missing')\n\n return items.map((item) => {\n if (!isRecord(item)) throw new Error('Transaction items are invalid')\n\n const product = getDocumentID(item.product)\n const variant = item.variant ? getDocumentID(item.variant) : undefined\n\n if (product === undefined || typeof item.quantity !== 'number') {\n throw new Error('Transaction items are invalid')\n }\n\n return { product, quantity: item.quantity, ...(variant === undefined ? {} : { variant }) }\n })\n}\n\nexport const confirmOrder =\n (config: ResolvedConfig): PaymentAdapter['confirmOrder'] =>\n async ({ data, finalizeOrder, req, transactionsSlug = 'transactions' }) => {\n const payload = req.payload\n const transactionID = getDocumentID(data.transactionID)\n\n if (transactionID === undefined) throw new Error('Transaction ID is required')\n\n if (typeof finalizeOrder !== 'function') throw new Error('Core order finalizer is required')\n\n try {\n const transaction = (await payload.findByID({\n collection: transactionsSlug as never,\n id: transactionID,\n depth: 0,\n overrideAccess: true,\n req,\n })) as unknown as Record<string, unknown>\n\n // The browser sends the id as a string; the relationship needs the stored id type (number in Postgres).\n const canonicalTransactionID = getDocumentID(transaction.id)\n\n if (canonicalTransactionID === undefined) throw new Error('Transaction ID is invalid')\n\n const provider = isRecord(transaction.yookassa) ? transaction.yookassa : undefined\n const paymentID = provider?.paymentID\n\n if (typeof paymentID !== 'string' || !paymentID) {\n throw new Error('Transaction has no YooKassa payment')\n }\n\n // The source of truth is the provider, not the browser.\n const payment = await config.client.getPayment(paymentID)\n\n validateSettlement({\n transaction,\n payment,\n canonicalCartID: data.cartID,\n user: req.user,\n customerEmail: data.customerEmail,\n decimals: config.decimals,\n })\n\n const purchaser = req.user\n ? { customer: req.user.id }\n : { customerEmail: normalizeEmail(data.customerEmail) }\n\n const order = await finalizeOrder({\n orderData: {\n amount: transaction.amount,\n currency: transaction.currency,\n ...purchaser,\n items: snapshotItems(transaction.items),\n ...(isRecord(provider?.shippingAddress) ? { shippingAddress: provider.shippingAddress } : {}),\n ...(isRecord(provider?.delivery) ? { delivery: provider.delivery } : {}),\n status: 'processing',\n },\n transactionID: canonicalTransactionID,\n })\n\n return {\n message: 'Order confirmed successfully',\n orderID: order.id as number | string,\n transactionID: canonicalTransactionID,\n ...(typeof order.accessToken === 'string' ? { accessToken: order.accessToken } : {}),\n }\n } catch (error) {\n if (error instanceof PaymentNotCompletedError) {\n // Normal when the buyer comes back before the payment is final: the client retries.\n payload.logger.info(`YooKassa payment not completed yet (${error.status})`)\n } else {\n payload.logger.error({ err: error, msg: 'Error confirming order with YooKassa' })\n }\n\n throw new Error(error instanceof Error ? error.message : 'Unknown error confirming order')\n }\n }\n"],"names":["PaymentNotCompletedError","validateSettlement","getDocumentID","isRecord","normalizeEmail","snapshotItems","items","Array","isArray","length","Error","map","item","product","variant","undefined","quantity","confirmOrder","config","data","finalizeOrder","req","transactionsSlug","payload","transactionID","transaction","findByID","collection","id","depth","overrideAccess","canonicalTransactionID","provider","yookassa","paymentID","payment","client","getPayment","canonicalCartID","cartID","user","customerEmail","decimals","purchaser","customer","order","orderData","amount","currency","shippingAddress","delivery","status","message","orderID","accessToken","error","logger","info","err","msg"],"mappings":"AAEA,SAASA,wBAAw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EAAEF,MAAMJ,MAAM,CAAC,CAAC,CAAC;YAC5E,OAAO;gBACL5B,QAAQiC,MAAM,CAACD,KAAK,CAAC;oBAAEG,KAAKH;oBAAOI,KAAK;gBAAuC;YACjF;YAEA,MAAM,IAAIjD,MAAM6C,iBAAiB7C,QAAQ6C,MAAMH,OAAO,GAAG;QAC3D;IACF,EAAC"}
|
package/dist/fields.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SelectField } from 'payload';
|
|
2
|
+
/** `vat_code` values of the YooKassa receipt API. */
|
|
3
|
+
export declare const VAT_CODE_OPTIONS: {
|
|
4
|
+
value: string;
|
|
5
|
+
label: import("@serge-pro-dev/payload-plugins-shared/i18n").Localized;
|
|
6
|
+
}[];
|
|
7
|
+
/**
|
|
8
|
+
* Optional product (and variant) field: the VAT rate for the 54-FZ receipt.
|
|
9
|
+
* Add it to your products override; the adapter falls back to `receipt.defaultVatCode`.
|
|
10
|
+
*
|
|
11
|
+
* fields: [...defaultCollection.fields, vatCodeField()]
|
|
12
|
+
*/
|
|
13
|
+
export declare const vatCodeField: (overrides?: Partial<SelectField>) => SelectField;
|
package/dist/fields.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { l } from '@serge-pro-dev/payload-plugins-shared/i18n';
|
|
2
|
+
/** `vat_code` values of the YooKassa receipt API. */ export const VAT_CODE_OPTIONS = [
|
|
3
|
+
{
|
|
4
|
+
value: '1',
|
|
5
|
+
label: l('Without VAT', 'Без НДС')
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
value: '2',
|
|
9
|
+
label: l('VAT 0%', 'НДС 0%')
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
value: '3',
|
|
13
|
+
label: l('VAT 10%', 'НДС 10%')
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
value: '4',
|
|
17
|
+
label: l('VAT 20%', 'НДС 20%')
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
value: '5',
|
|
21
|
+
label: l('VAT 10/110', 'НДС 10/110')
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
value: '6',
|
|
25
|
+
label: l('VAT 20/120', 'НДС 20/120')
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
value: '7',
|
|
29
|
+
label: l('VAT 5%', 'НДС 5%')
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
value: '8',
|
|
33
|
+
label: l('VAT 7%', 'НДС 7%')
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
value: '9',
|
|
37
|
+
label: l('VAT 5/105', 'НДС 5/105')
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
value: '10',
|
|
41
|
+
label: l('VAT 7/107', 'НДС 7/107')
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
value: '11',
|
|
45
|
+
label: l('VAT 22%', 'НДС 22%')
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
value: '12',
|
|
49
|
+
label: l('VAT 22/122', 'НДС 22/122')
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
/**
|
|
53
|
+
* Optional product (and variant) field: the VAT rate for the 54-FZ receipt.
|
|
54
|
+
* Add it to your products override; the adapter falls back to `receipt.defaultVatCode`.
|
|
55
|
+
*
|
|
56
|
+
* fields: [...defaultCollection.fields, vatCodeField()]
|
|
57
|
+
*/ export const vatCodeField = (overrides = {})=>({
|
|
58
|
+
name: 'vatCode',
|
|
59
|
+
type: 'select',
|
|
60
|
+
label: l('VAT rate for receipts', 'Ставка НДС для чека'),
|
|
61
|
+
options: VAT_CODE_OPTIONS,
|
|
62
|
+
admin: {
|
|
63
|
+
description: l('Used in the receipt sent by YooKassa. Empty means the default of the payment settings.', 'Передаётся в чек ЮKassa. Если не выбрано, берётся ставка по умолчанию из настроек оплаты.')
|
|
64
|
+
},
|
|
65
|
+
...overrides
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=fields.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fields.ts"],"sourcesContent":["import { l } from '@serge-pro-dev/payload-plugins-shared/i18n'\nimport type { SelectField } from 'payload'\n\n/** `vat_code` values of the YooKassa receipt API. */\nexport const VAT_CODE_OPTIONS = [\n { value: '1', label: l('Without VAT', 'Без НДС') },\n { value: '2', label: l('VAT 0%', 'НДС 0%') },\n { value: '3', label: l('VAT 10%', 'НДС 10%') },\n { value: '4', label: l('VAT 20%', 'НДС 20%') },\n { value: '5', label: l('VAT 10/110', 'НДС 10/110') },\n { value: '6', label: l('VAT 20/120', 'НДС 20/120') },\n { value: '7', label: l('VAT 5%', 'НДС 5%') },\n { value: '8', label: l('VAT 7%', 'НДС 7%') },\n { value: '9', label: l('VAT 5/105', 'НДС 5/105') },\n { value: '10', label: l('VAT 7/107', 'НДС 7/107') },\n { value: '11', label: l('VAT 22%', 'НДС 22%') },\n { value: '12', label: l('VAT 22/122', 'НДС 22/122') },\n]\n\n/**\n * Optional product (and variant) field: the VAT rate for the 54-FZ receipt.\n * Add it to your products override; the adapter falls back to `receipt.defaultVatCode`.\n *\n * fields: [...defaultCollection.fields, vatCodeField()]\n */\nexport const vatCodeField = (overrides: Partial<SelectField> = {}): SelectField =>\n ({\n name: 'vatCode',\n type: 'select',\n label: l('VAT rate for receipts', 'Ставка НДС для чека'),\n options: VAT_CODE_OPTIONS,\n admin: {\n description: l(\n 'Used in the receipt sent by YooKassa. Empty means the default of the payment settings.',\n 'Передаётся в чек ЮKassa. Если не выбрано, берётся ставка по умолчанию из настроек оплаты.',\n ),\n },\n ...overrides,\n }) as SelectField\n"],"names":["l","VAT_CODE_OPTIONS","value","label","vatCodeField","overrides","name","type","options","admin","description"],"mappings":"AAAA,SAASA,CAAC,QAAQ,6CAA4C;AAG9D,mDAAmD,GACnD,OAAO,MAAMC,mBAAmB;IAC9B;QAAEC,OAAO;QAAKC,OAAOH,EAAE,eAAe;IAAW;IACjD;QAAEE,OAAO;QAAKC,OAAOH,EAAE,UAAU;IAAU;IAC3C;QAAEE,OAAO;QAAKC,OAAOH,EAAE,WAAW;IAAW;IAC7C;QAAEE,OAAO;QAAKC,OAAOH,EAAE,WAAW;IAAW;IAC7C;QAAEE,OAAO;QAAKC,OAAOH,EAAE,cAAc;IAAc;IACnD;QAAEE,OAAO;QAAKC,OAAOH,EAAE,cAAc;IAAc;IACnD;QAAEE,OAAO;QAAKC,OAAOH,EAAE,UAAU;IAAU;IAC3C;QAAEE,OAAO;QAAKC,OAAOH,EAAE,UAAU;IAAU;IAC3C;QAAEE,OAAO;QAAKC,OAAOH,EAAE,aAAa;IAAa;IACjD;QAAEE,OAAO;QAAMC,OAAOH,EAAE,aAAa;IAAa;IAClD;QAAEE,OAAO;QAAMC,OAAOH,EAAE,WAAW;IAAW;IAC9C;QAAEE,OAAO;QAAMC,OAAOH,EAAE,cAAc;IAAc;CACrD,CAAA;AAED;;;;;CAKC,GACD,OAAO,MAAMI,eAAe,CAACC,YAAkC,CAAC,CAAC,GAC9D,CAAA;QACCC,MAAM;QACNC,MAAM;QACNJ,OAAOH,EAAE,yBAAyB;QAClCQ,SAASP;QACTQ,OAAO;YACLC,aAAaV,EACX,0FACA;QAEJ;QACA,GAAGK,SAAS;IACd,CAAA,EAAiB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
import type { YooKassaPayment } from './api.js';
|
|
3
|
+
import type { ResolvedConfig } from './types.js';
|
|
4
|
+
export type SettleResult = 'settled' | 'skipped';
|
|
5
|
+
/**
|
|
6
|
+
* What `applyPayment` did with the transaction:
|
|
7
|
+
* - settled: the order was created now
|
|
8
|
+
* - in_progress: the order is being (or was) created by someone else
|
|
9
|
+
* - cancelled: the payment was canceled, the transaction is cancelled
|
|
10
|
+
* - pending: the buyer has not paid yet
|
|
11
|
+
* - inconsistent: paid, but the transaction cannot get an order (it is not pending any more)
|
|
12
|
+
*/
|
|
13
|
+
export type ApplyResult = 'cancelled' | 'in_progress' | 'inconsistent' | 'pending' | 'settled';
|
|
14
|
+
type SettleArgs = {
|
|
15
|
+
config: ResolvedConfig;
|
|
16
|
+
payment: YooKassaPayment;
|
|
17
|
+
req: PayloadRequest;
|
|
18
|
+
/** The transaction loaded from the database (depth 0). */
|
|
19
|
+
transaction: Record<string, unknown>;
|
|
20
|
+
transactionsSlug: string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Creates the order for a paid transaction without the buyer's browser: this is what the
|
|
24
|
+
* YooKassa notification does, so closing the payment page never loses an order.
|
|
25
|
+
*
|
|
26
|
+
* @payloadcms/plugin-ecommerce does not export its order finalizer (it is handed only to
|
|
27
|
+
* `confirmOrder`), so the same steps are repeated here: claim the pending transaction
|
|
28
|
+
* atomically, create the order, mark the cart purchased, decrement inventory, mark the
|
|
29
|
+
* transaction succeeded. All of it runs in one database transaction. The claim makes this
|
|
30
|
+
* safe against the buyer returning at the same moment: whoever loses the claim does nothing
|
|
31
|
+
* (the return page then simply finds the finished order).
|
|
32
|
+
*
|
|
33
|
+
* The identity of the buyer is taken from the transaction itself, and the payment is the
|
|
34
|
+
* one fetched from YooKassa by id: status, amount, currency and metadata are all checked.
|
|
35
|
+
*/
|
|
36
|
+
export declare const settleTransaction: ({ config, payment, req, transaction, transactionsSlug, }: SettleArgs) => Promise<SettleResult>;
|
|
37
|
+
type ApplyArgs = SettleArgs;
|
|
38
|
+
/**
|
|
39
|
+
* Brings a transaction in line with the payment as YooKassa reports it: records the status
|
|
40
|
+
* and the reason of a cancellation, cancels the transaction of a canceled payment and
|
|
41
|
+
* creates the order of a paid one. The one place used by the webhook, the status endpoint
|
|
42
|
+
* and the reconcile job, so all of them behave the same and are safe to run together.
|
|
43
|
+
*/
|
|
44
|
+
export declare const applyPayment: ({ config, payment, req, transaction, transactionsSlug, }: ApplyArgs) => Promise<ApplyResult>;
|
|
45
|
+
export {};
|
package/dist/finalize.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { commitTransaction, initTransaction, killTransaction } from 'payload';
|
|
2
|
+
import { snapshotItems } from './confirm.js';
|
|
3
|
+
import { validateSettlement } from './settlement.js';
|
|
4
|
+
import { getDocumentID, isRecord, normalizeEmail } from './utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates the order for a paid transaction without the buyer's browser: this is what the
|
|
7
|
+
* YooKassa notification does, so closing the payment page never loses an order.
|
|
8
|
+
*
|
|
9
|
+
* @payloadcms/plugin-ecommerce does not export its order finalizer (it is handed only to
|
|
10
|
+
* `confirmOrder`), so the same steps are repeated here: claim the pending transaction
|
|
11
|
+
* atomically, create the order, mark the cart purchased, decrement inventory, mark the
|
|
12
|
+
* transaction succeeded. All of it runs in one database transaction. The claim makes this
|
|
13
|
+
* safe against the buyer returning at the same moment: whoever loses the claim does nothing
|
|
14
|
+
* (the return page then simply finds the finished order).
|
|
15
|
+
*
|
|
16
|
+
* The identity of the buyer is taken from the transaction itself, and the payment is the
|
|
17
|
+
* one fetched from YooKassa by id: status, amount, currency and metadata are all checked.
|
|
18
|
+
*/ export const settleTransaction = async ({ config, payment, req, transaction, transactionsSlug })=>{
|
|
19
|
+
const payload = req.payload;
|
|
20
|
+
const transactionID = getDocumentID(transaction.id);
|
|
21
|
+
const cartID = getDocumentID(transaction.cart);
|
|
22
|
+
if (transactionID === undefined) throw new Error('Transaction ID is invalid');
|
|
23
|
+
if (cartID === undefined) throw new Error('Transaction has no cart');
|
|
24
|
+
const customerID = getDocumentID(transaction.customer);
|
|
25
|
+
validateSettlement({
|
|
26
|
+
transaction,
|
|
27
|
+
payment,
|
|
28
|
+
canonicalCartID: cartID,
|
|
29
|
+
user: customerID === undefined ? null : {
|
|
30
|
+
id: customerID
|
|
31
|
+
},
|
|
32
|
+
customerEmail: transaction.customerEmail,
|
|
33
|
+
decimals: config.decimals
|
|
34
|
+
});
|
|
35
|
+
const provider = isRecord(transaction.yookassa) ? transaction.yookassa : undefined;
|
|
36
|
+
const items = snapshotItems(transaction.items);
|
|
37
|
+
const shouldCommit = await initTransaction(req);
|
|
38
|
+
try {
|
|
39
|
+
const claimed = await payload.db.updateOne({
|
|
40
|
+
collection: transactionsSlug,
|
|
41
|
+
data: {
|
|
42
|
+
status: 'processing'
|
|
43
|
+
},
|
|
44
|
+
options: {
|
|
45
|
+
atomic: true
|
|
46
|
+
},
|
|
47
|
+
req,
|
|
48
|
+
where: {
|
|
49
|
+
and: [
|
|
50
|
+
{
|
|
51
|
+
id: {
|
|
52
|
+
equals: transactionID
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
status: {
|
|
57
|
+
equals: 'pending'
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
order: {
|
|
62
|
+
exists: false
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
// Someone else (the buyer's return page) is finishing or has finished this order.
|
|
69
|
+
if (!claimed) {
|
|
70
|
+
if (shouldCommit) await commitTransaction(req);
|
|
71
|
+
return 'skipped';
|
|
72
|
+
}
|
|
73
|
+
const referenced = await payload.find({
|
|
74
|
+
collection: config.ordersSlug,
|
|
75
|
+
depth: 0,
|
|
76
|
+
limit: 1,
|
|
77
|
+
overrideAccess: true,
|
|
78
|
+
pagination: false,
|
|
79
|
+
req,
|
|
80
|
+
where: {
|
|
81
|
+
transactions: {
|
|
82
|
+
equals: transactionID
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
if (referenced.totalDocs !== 0) throw new Error('An order already references the transaction');
|
|
87
|
+
const order = await payload.create({
|
|
88
|
+
collection: config.ordersSlug,
|
|
89
|
+
data: {
|
|
90
|
+
amount: transaction.amount,
|
|
91
|
+
currency: transaction.currency,
|
|
92
|
+
...customerID === undefined ? {
|
|
93
|
+
customerEmail: normalizeEmail(transaction.customerEmail)
|
|
94
|
+
} : {
|
|
95
|
+
customer: customerID
|
|
96
|
+
},
|
|
97
|
+
items,
|
|
98
|
+
...isRecord(provider?.shippingAddress) ? {
|
|
99
|
+
shippingAddress: provider.shippingAddress
|
|
100
|
+
} : {},
|
|
101
|
+
...isRecord(provider?.delivery) ? {
|
|
102
|
+
delivery: provider.delivery
|
|
103
|
+
} : {},
|
|
104
|
+
status: 'processing',
|
|
105
|
+
transactions: [
|
|
106
|
+
transactionID
|
|
107
|
+
]
|
|
108
|
+
},
|
|
109
|
+
overrideAccess: true,
|
|
110
|
+
req
|
|
111
|
+
});
|
|
112
|
+
const orderID = getDocumentID(order);
|
|
113
|
+
if (orderID === undefined) throw new Error('Order ID is invalid');
|
|
114
|
+
await payload.update({
|
|
115
|
+
collection: config.cartsSlug,
|
|
116
|
+
id: cartID,
|
|
117
|
+
data: {
|
|
118
|
+
purchasedAt: new Date().toISOString()
|
|
119
|
+
},
|
|
120
|
+
overrideAccess: true,
|
|
121
|
+
req
|
|
122
|
+
});
|
|
123
|
+
for (const item of items){
|
|
124
|
+
const hasVariant = item.variant !== undefined;
|
|
125
|
+
const updated = await payload.db.updateOne({
|
|
126
|
+
collection: hasVariant ? config.variantsSlug : config.productsSlug,
|
|
127
|
+
data: {
|
|
128
|
+
inventory: {
|
|
129
|
+
$inc: -item.quantity
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
id: hasVariant ? item.variant : item.product,
|
|
133
|
+
req
|
|
134
|
+
});
|
|
135
|
+
if (!updated) throw new Error('Inventory item could not be updated');
|
|
136
|
+
}
|
|
137
|
+
await payload.update({
|
|
138
|
+
collection: transactionsSlug,
|
|
139
|
+
id: transactionID,
|
|
140
|
+
data: {
|
|
141
|
+
order: orderID,
|
|
142
|
+
status: 'succeeded'
|
|
143
|
+
},
|
|
144
|
+
overrideAccess: true,
|
|
145
|
+
req
|
|
146
|
+
});
|
|
147
|
+
if (shouldCommit) await commitTransaction(req);
|
|
148
|
+
return 'settled';
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (shouldCommit) await killTransaction(req);
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Brings a transaction in line with the payment as YooKassa reports it: records the status
|
|
156
|
+
* and the reason of a cancellation, cancels the transaction of a canceled payment and
|
|
157
|
+
* creates the order of a paid one. The one place used by the webhook, the status endpoint
|
|
158
|
+
* and the reconcile job, so all of them behave the same and are safe to run together.
|
|
159
|
+
*/ export const applyPayment = async ({ config, payment, req, transaction, transactionsSlug })=>{
|
|
160
|
+
const payload = req.payload;
|
|
161
|
+
const provider = isRecord(transaction.yookassa) ? transaction.yookassa : {};
|
|
162
|
+
const cancellationDetails = payment.cancellation_details;
|
|
163
|
+
const cancel = payment.status === 'canceled' && transaction.status === 'pending';
|
|
164
|
+
const statusChanged = provider.status !== payment.status;
|
|
165
|
+
const reasonMissing = payment.status === 'canceled' && !isRecord(provider.cancellation);
|
|
166
|
+
if (statusChanged || reasonMissing || cancel) {
|
|
167
|
+
await payload.update({
|
|
168
|
+
collection: transactionsSlug,
|
|
169
|
+
id: transaction.id,
|
|
170
|
+
data: {
|
|
171
|
+
yookassa: {
|
|
172
|
+
...provider,
|
|
173
|
+
status: payment.status,
|
|
174
|
+
...cancellationDetails ? {
|
|
175
|
+
cancellation: {
|
|
176
|
+
party: cancellationDetails.party,
|
|
177
|
+
reason: cancellationDetails.reason
|
|
178
|
+
}
|
|
179
|
+
} : {}
|
|
180
|
+
},
|
|
181
|
+
...cancel ? {
|
|
182
|
+
status: 'cancelled'
|
|
183
|
+
} : {}
|
|
184
|
+
},
|
|
185
|
+
overrideAccess: true
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (payment.status === 'succeeded' && payment.paid && !transaction.order) {
|
|
189
|
+
if (transaction.status === 'pending') {
|
|
190
|
+
const result = await settleTransaction({
|
|
191
|
+
config,
|
|
192
|
+
payment,
|
|
193
|
+
req,
|
|
194
|
+
transaction,
|
|
195
|
+
transactionsSlug
|
|
196
|
+
});
|
|
197
|
+
return result === 'settled' ? 'settled' : 'in_progress';
|
|
198
|
+
}
|
|
199
|
+
if (transaction.status === 'processing') return 'in_progress';
|
|
200
|
+
payload.logger.warn(`YooKassa payment ${payment.id} succeeded but transaction ${String(transaction.id)} is ${String(transaction.status)}, no order was created.`);
|
|
201
|
+
return 'inconsistent';
|
|
202
|
+
}
|
|
203
|
+
if (payment.status === 'canceled') return 'cancelled';
|
|
204
|
+
return 'pending';
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
//# sourceMappingURL=finalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/finalize.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { commitTransaction, initTransaction, killTransaction } from 'payload'\n\nimport type { YooKassaPayment } from './api.js'\nimport { snapshotItems } from './confirm.js'\nimport { validateSettlement } from './settlement.js'\nimport type { ResolvedConfig } from './types.js'\nimport { getDocumentID, isRecord, normalizeEmail } from './utils.js'\n\nexport type SettleResult = 'settled' | 'skipped'\n\n/**\n * What `applyPayment` did with the transaction:\n * - settled: the order was created now\n * - in_progress: the order is being (or was) created by someone else\n * - cancelled: the payment was canceled, the transaction is cancelled\n * - pending: the buyer has not paid yet\n * - inconsistent: paid, but the transaction cannot get an order (it is not pending any more)\n */\nexport type ApplyResult = 'cancelled' | 'in_progress' | 'inconsistent' | 'pending' | 'settled'\n\ntype SettleArgs = {\n config: ResolvedConfig\n payment: YooKassaPayment\n req: PayloadRequest\n /** The transaction loaded from the database (depth 0). */\n transaction: Record<string, unknown>\n transactionsSlug: string\n}\n\n/**\n * Creates the order for a paid transaction without the buyer's browser: this is what the\n * YooKassa notification does, so closing the payment page never loses an order.\n *\n * @payloadcms/plugin-ecommerce does not export its order finalizer (it is handed only to\n * `confirmOrder`), so the same steps are repeated here: claim the pending transaction\n * atomically, create the order, mark the cart purchased, decrement inventory, mark the\n * transaction succeeded. All of it runs in one database transaction. The claim makes this\n * safe against the buyer returning at the same moment: whoever loses the claim does nothing\n * (the return page then simply finds the finished order).\n *\n * The identity of the buyer is taken from the transaction itself, and the payment is the\n * one fetched from YooKassa by id: status, amount, currency and metadata are all checked.\n */\nexport const settleTransaction = async ({\n config,\n payment,\n req,\n transaction,\n transactionsSlug,\n}: SettleArgs): Promise<SettleResult> => {\n const payload = req.payload\n const transactionID = getDocumentID(transaction.id)\n const cartID = getDocumentID(transaction.cart)\n\n if (transactionID === undefined) throw new Error('Transaction ID is invalid')\n\n if (cartID === undefined) throw new Error('Transaction has no cart')\n\n const customerID = getDocumentID(transaction.customer)\n\n validateSettlement({\n transaction,\n payment,\n canonicalCartID: cartID,\n user: customerID === undefined ? null : { id: customerID },\n customerEmail: transaction.customerEmail,\n decimals: config.decimals,\n })\n\n const provider = isRecord(transaction.yookassa) ? transaction.yookassa : undefined\n const items = snapshotItems(transaction.items)\n\n const shouldCommit = await initTransaction(req)\n\n try {\n const claimed = await payload.db.updateOne({\n collection: transactionsSlug,\n data: { status: 'processing' },\n options: { atomic: true },\n req,\n where: {\n and: [\n { id: { equals: transactionID } },\n { status: { equals: 'pending' } },\n { order: { exists: false } },\n ],\n },\n } as never)\n\n // Someone else (the buyer's return page) is finishing or has finished this order.\n if (!claimed) {\n if (shouldCommit) await commitTransaction(req)\n\n return 'skipped'\n }\n\n const referenced = await payload.find({\n collection: config.ordersSlug as never,\n depth: 0,\n limit: 1,\n overrideAccess: true,\n pagination: false,\n req,\n where: { transactions: { equals: transactionID } } as never,\n })\n\n if (referenced.totalDocs !== 0) throw new Error('An order already references the transaction')\n\n const order = await payload.create({\n collection: config.ordersSlug as never,\n data: {\n amount: transaction.amount,\n currency: transaction.currency,\n ...(customerID === undefined\n ? { customerEmail: normalizeEmail(transaction.customerEmail) }\n : { customer: customerID }),\n items,\n ...(isRecord(provider?.shippingAddress) ? { shippingAddress: provider.shippingAddress } : {}),\n ...(isRecord(provider?.delivery) ? { delivery: provider.delivery } : {}),\n status: 'processing',\n transactions: [transactionID],\n } as never,\n overrideAccess: true,\n req,\n })\n\n const orderID = getDocumentID(order)\n\n if (orderID === undefined) throw new Error('Order ID is invalid')\n\n await payload.update({\n collection: config.cartsSlug as never,\n id: cartID,\n data: { purchasedAt: new Date().toISOString() } as never,\n overrideAccess: true,\n req,\n })\n\n for (const item of items) {\n const hasVariant = item.variant !== undefined\n const updated = await payload.db.updateOne({\n collection: hasVariant ? config.variantsSlug : config.productsSlug,\n data: { inventory: { $inc: -(item.quantity as number) } },\n id: (hasVariant ? item.variant : item.product) as number | string,\n req,\n } as never)\n\n if (!updated) throw new Error('Inventory item could not be updated')\n }\n\n await payload.update({\n collection: transactionsSlug as never,\n id: transactionID,\n data: { order: orderID, status: 'succeeded' } as never,\n overrideAccess: true,\n req,\n })\n\n if (shouldCommit) await commitTransaction(req)\n\n return 'settled'\n } catch (error) {\n if (shouldCommit) await killTransaction(req)\n\n throw error\n }\n}\n\ntype ApplyArgs = SettleArgs\n\n/**\n * Brings a transaction in line with the payment as YooKassa reports it: records the status\n * and the reason of a cancellation, cancels the transaction of a canceled payment and\n * creates the order of a paid one. The one place used by the webhook, the status endpoint\n * and the reconcile job, so all of them behave the same and are safe to run together.\n */\nexport const applyPayment = async ({\n config,\n payment,\n req,\n transaction,\n transactionsSlug,\n}: ApplyArgs): Promise<ApplyResult> => {\n const payload = req.payload\n const provider = isRecord(transaction.yookassa) ? transaction.yookassa : {}\n const cancellationDetails = payment.cancellation_details\n const cancel = payment.status === 'canceled' && transaction.status === 'pending'\n\n const statusChanged = provider.status !== payment.status\n const reasonMissing = payment.status === 'canceled' && !isRecord(provider.cancellation)\n\n if (statusChanged || reasonMissing || cancel) {\n await payload.update({\n collection: transactionsSlug as never,\n id: transaction.id as number | string,\n data: {\n yookassa: {\n ...provider,\n status: payment.status,\n ...(cancellationDetails\n ? {\n cancellation: {\n party: cancellationDetails.party,\n reason: cancellationDetails.reason,\n },\n }\n : {}),\n },\n ...(cancel ? { status: 'cancelled' } : {}),\n } as never,\n overrideAccess: true,\n })\n }\n\n if (payment.status === 'succeeded' && payment.paid && !transaction.order) {\n if (transaction.status === 'pending') {\n const result = await settleTransaction({ config, payment, req, transaction, transactionsSlug })\n\n return result === 'settled' ? 'settled' : 'in_progress'\n }\n\n if (transaction.status === 'processing') return 'in_progress'\n\n payload.logger.warn(\n `YooKassa payment ${payment.id} succeeded but transaction ${String(transaction.id)} is ${String(transaction.status)}, no order was created.`,\n )\n\n return 'inconsistent'\n }\n\n if (payment.status === 'canceled') return 'cancelled'\n\n return 'pending'\n}\n"],"names":["commitTransaction","initTransaction","killTransaction","snapshotItems","validateSettlement","getDocumentID","isRecord","normalizeEmail","settleTransaction","config","payment","req","transaction","transactionsSlug","payload","transactionID","id","cartID","cart","undefined","Error","customerID","customer","canonicalCartID","user","customerEmail","decimals","provider","yookassa","items","shouldCommit","claimed","db","updateOne","collection","data","status","options","atomic","where","and","equals","order","exists","referenced","find","ordersSlug","depth","limit","overrideAccess","pagination","transactions","totalDocs","create","amount","currency","shippingAddress","delivery","orderID","update","cartsSlug","purchasedAt","Date","toISOString","item","hasVariant","variant","updated","variantsSlug","productsSlug","inventory","$inc","quantity","product","error","applyPayment","cancellationDetails","cancellation_details","cancel","statusChanged","reasonMissing","cancellation","party","reason","paid","result","logger","warn","String"],"mappings":"AACA,SAASA,iBAAiB,EAAEC,eAAe,EAAEC,eAAe,QAAQ,UAAS;AAG7E,SAASC,aAAa,QAAQ,eAAc;AAC5C,SAASC,kBAAkB,QAAQ,kBAAiB;AAEpD,SAASC,aAAa,EAAEC,QAAQ,EAAEC,cAAc,QAAQ,aAAY;AAuBpE;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMC,oBAAoB,OAAO,EACtCC,MAAM,EACNC,OAAO,EACPC,GAAG,EACHC,WAAW,EACXC,gBAAgB,EACL;IACX,MAAMC,UAAUH,IAAIG,OAAO;IAC3B,MAAMC,gBAAgBV,cAAcO,YAAYI,EAAE;IAClD,MAAMC,SAASZ,cAAcO,YAAYM,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AACxE,YAAY8B,KAAK,EAAE;QACxE,IAAI9B,YAAYwB,MAAM,KAAK,WAAW;YACpC,MAAMiD,SAAS,MAAM7E,kBAAkB;gBAAEC;gBAAQC;gBAASC;gBAAKC;gBAAaC;YAAiB;YAE7F,OAAOwE,WAAW,YAAY,YAAY;QAC5C;QAEA,IAAIzE,YAAYwB,MAAM,KAAK,cAAc,OAAO;QAEhDtB,QAAQwE,MAAM,CAACC,IAAI,CACjB,CAAC,iBAAiB,EAAE7E,QAAQM,EAAE,CAAC,2BAA2B,EAAEwE,OAAO5E,YAAYI,EAAE,EAAE,IAAI,EAAEwE,OAAO5E,YAAYwB,MAAM,EAAE,uBAAuB,CAAC;QAG9I,OAAO;IACT;IAEA,IAAI1B,QAAQ0B,MAAM,KAAK,YAAY,OAAO;IAE1C,OAAO;AACT,EAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PaymentAdapter } from '@payloadcms/plugin-ecommerce/types';
|
|
2
|
+
import type { TaskConfig } from 'payload';
|
|
3
|
+
import type { ReconcileOptions } from './reconcile.js';
|
|
4
|
+
import type { YooKassaAdapterProps } from './types.js';
|
|
5
|
+
export { formatAmount, parseAmount } from './amount.js';
|
|
6
|
+
export { cancellationMessage } from './messages.js';
|
|
7
|
+
export type { ReconcileOptions } from './reconcile.js';
|
|
8
|
+
export { VAT_CODE_OPTIONS, vatCodeField } from './fields.js';
|
|
9
|
+
export type { ReceiptOptions, TaxSystemCode } from './receipt.js';
|
|
10
|
+
export { receiptFromSettings, settingsToReceipt, yookassaSettingsGlobal } from './settings.js';
|
|
11
|
+
export type { DeliveryQuote, DeliveryResolver, ReceiptResolver, YooKassaAdapterProps, } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* YooKassa payment adapter for @payloadcms/plugin-ecommerce.
|
|
14
|
+
*
|
|
15
|
+
* ecommercePlugin({ payments: { paymentMethods: [yookassaAdapter({ ... })] } })
|
|
16
|
+
*
|
|
17
|
+
* The buyer is redirected to the YooKassa payment page. The order is created by the
|
|
18
|
+
* webhook (POST /api/payments/yookassa/webhooks) as soon as the payment succeeds, or by
|
|
19
|
+
* the return page at `returnPath`, whichever comes first.
|
|
20
|
+
*/
|
|
21
|
+
export declare const yookassaAdapter: (props: YooKassaAdapterProps) => PaymentAdapter;
|
|
22
|
+
/**
|
|
23
|
+
* The adapter plus the job that checks unpaid transactions. Both share one configuration:
|
|
24
|
+
*
|
|
25
|
+
* const yookassa = createYooKassa({ ... })
|
|
26
|
+
* ecommercePlugin({ payments: { paymentMethods: [yookassa.adapter] } })
|
|
27
|
+
* buildConfig({ jobs: { tasks: [yookassa.reconcileTask()] } })
|
|
28
|
+
*/
|
|
29
|
+
export declare const createYooKassa: (props: YooKassaAdapterProps) => {
|
|
30
|
+
adapter: PaymentAdapter;
|
|
31
|
+
reconcileTask: (options?: Omit<ReconcileOptions, "transactionsSlug">) => TaskConfig<any>;
|
|
32
|
+
};
|