@chevre/domain 21.33.0-alpha.17 → 21.33.0-alpha.18

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.
@@ -0,0 +1,24 @@
1
+ import * as factory from '../../../factory';
2
+ import type { MongoRepository as ActionRepo } from '../../../repo/action';
3
+ import type { ActionRecipeRepo } from '../../../repo/actionRecipe';
4
+ import type { ITransactionInProgress, MongoRepository as TransactionRepo } from '../../../repo/transaction';
5
+ import * as PayTransactionService from '../../assetTransaction/pay';
6
+ type IObjectWithoutDetail = factory.action.authorize.paymentMethod.any.IObjectWithoutDetail;
7
+ declare function handlePrePublishedPaymentMethodIdOnAuthorizing(params: {
8
+ object: Pick<IObjectWithoutDetail, 'amount' | 'issuedThrough' | 'paymentMethod'>;
9
+ prePublishedPaymentMethodId: string;
10
+ transaction: Pick<ITransactionInProgress<factory.transactionType.PlaceOrder>, 'agent' | 'expires' | 'id' | 'typeOf' | 'project' | 'seller'>;
11
+ }): (repos: {
12
+ action: ActionRepo;
13
+ actionRecipe?: ActionRecipeRepo;
14
+ transaction: TransactionRepo;
15
+ }) => Promise<{
16
+ authorizeParams?: {
17
+ paymentMethodByTransaction: factory.transaction.placeOrder.IPaymentMethodByPaymentUrl;
18
+ pendingPaymentAgencyTransaction: PayTransactionService.IPaymentAgencyTransaction;
19
+ } | undefined;
20
+ existingCompletedAuthorizeAction?: {
21
+ id: string;
22
+ } | undefined;
23
+ }>;
24
+ export { handlePrePublishedPaymentMethodIdOnAuthorizing };
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.handlePrePublishedPaymentMethodIdOnAuthorizing = void 0;
13
+ const createDebug = require("debug");
14
+ const factory = require("../../../factory");
15
+ const debug = createDebug('chevre-domain:service:payment');
16
+ function recipe2paymentAgencyTransaction(actionRecipe) {
17
+ var _a;
18
+ let entryTranArgs;
19
+ let entryTranResult;
20
+ let execTranArgs;
21
+ let execTranResult;
22
+ if (actionRecipe.recipeCategory !== factory.recipe.RecipeCategory.publishPaymentUrl) {
23
+ throw new factory.errors.NotFound('', `Recipe with category ${factory.recipe.RecipeCategory.publishPaymentUrl} not found`);
24
+ }
25
+ const howToSection = actionRecipe.step;
26
+ const howToSteps = (_a = howToSection.at(0)) === null || _a === void 0 ? void 0 : _a.itemListElement;
27
+ if (Array.isArray(howToSteps)) {
28
+ howToSteps.forEach((howToStep) => {
29
+ var _a, _b, _c, _d;
30
+ const stepIdentifier = howToStep.identifier;
31
+ if (stepIdentifier === factory.recipe.StepIdentifier.entryTran) {
32
+ entryTranArgs = (_a = howToStep.itemListElement.at(0)) === null || _a === void 0 ? void 0 : _a.beforeMedia;
33
+ entryTranResult = (_b = howToStep.itemListElement.at(0)) === null || _b === void 0 ? void 0 : _b.afterMedia;
34
+ }
35
+ else if (stepIdentifier === factory.recipe.StepIdentifier.execTran) {
36
+ execTranArgs = (_c = howToStep.itemListElement.at(0)) === null || _c === void 0 ? void 0 : _c.beforeMedia;
37
+ execTranResult = (_d = howToStep.itemListElement.at(0)) === null || _d === void 0 ? void 0 : _d.afterMedia;
38
+ }
39
+ else {
40
+ throw new factory.errors.NotImplemented(`howToStep.identifier '${stepIdentifier}' not implemented`);
41
+ }
42
+ });
43
+ }
44
+ if (entryTranArgs === undefined || entryTranResult === undefined
45
+ || execTranArgs === undefined || execTranResult === undefined) {
46
+ throw new factory.errors.NotFound(`beforeMedia or afterMedia of actionRecipe`);
47
+ }
48
+ return { entryTranArgs, entryTranResult, execTranArgs, execTranResult };
49
+ }
50
+ function handlePrePublishedPaymentMethodIdOnAuthorizing(params) {
51
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
52
+ var _a;
53
+ let pendingPaymentAgencyTransaction;
54
+ let existingCompletedAuthorizeAction;
55
+ // transaction.objectへのアクセス回避(2024-05-30~)
56
+ // const paymentMethodByTransaction = transaction.object.paymentMethods;
57
+ const paymentMethodByTransaction = yield repos.transaction.findInProgressPaymentMethodId({ id: params.transaction.id });
58
+ if (params.prePublishedPaymentMethodId === (paymentMethodByTransaction === null || paymentMethodByTransaction === void 0 ? void 0 : paymentMethodByTransaction.paymentMethodId)) {
59
+ // check existence of acceptAction when authorizing payment(2024-06-01~)
60
+ const acceptPayAction = (yield repos.action.search({
61
+ limit: 1,
62
+ page: 1,
63
+ project: { id: { $eq: params.transaction.project.id } },
64
+ typeOf: { $eq: factory.actionType.AcceptAction },
65
+ actionStatus: { $in: [factory.actionStatusType.CompletedActionStatus] },
66
+ purpose: { id: { $in: [params.transaction.id] } },
67
+ object: {
68
+ transactionNumber: { $eq: params.prePublishedPaymentMethodId },
69
+ typeOf: { $eq: factory.assetTransactionType.Pay }
70
+ }
71
+ }, ['result'], [])).shift();
72
+ if (acceptPayAction === undefined) {
73
+ throw new factory.errors.NotFound(factory.actionType.AcceptAction);
74
+ }
75
+ debug('acceptPayAction found:', acceptPayAction.id);
76
+ // find recipe(2024-06-02~)
77
+ const actionRecipe = yield ((_a = repos.actionRecipe) === null || _a === void 0 ? void 0 : _a.findByAction({
78
+ project: { id: params.transaction.project.id },
79
+ recipeFor: { id: acceptPayAction.id }
80
+ }));
81
+ debug('actionRecipe found:', actionRecipe === null || actionRecipe === void 0 ? void 0 : actionRecipe.recipeCategory);
82
+ // 検証強化(2024-01-04~)
83
+ validatePaymentMethodByTransaction({
84
+ object: params.object,
85
+ paymentMethodByTransaction
86
+ });
87
+ // use actionRecipe(2024-06-02~)
88
+ if (typeof (actionRecipe === null || actionRecipe === void 0 ? void 0 : actionRecipe.recipeCategory) === 'string') {
89
+ pendingPaymentAgencyTransaction = recipe2paymentAgencyTransaction(actionRecipe);
90
+ debug('handlePrePublishedPaymentMethodIdOnAuthorizing: pendingPaymentAgencyTransaction from recipe:', JSON.stringify(pendingPaymentAgencyTransaction));
91
+ }
92
+ else {
93
+ const { entryTranArgs, entryTranResult, execTranArgs, execTranResult } = paymentMethodByTransaction;
94
+ if (entryTranArgs !== undefined && entryTranResult !== undefined
95
+ && execTranArgs !== undefined && execTranResult !== undefined) {
96
+ pendingPaymentAgencyTransaction = { entryTranArgs, entryTranResult, execTranArgs, execTranResult };
97
+ }
98
+ }
99
+ if (pendingPaymentAgencyTransaction === undefined) {
100
+ throw new factory.errors.Argument('paymentMethodId', 'pendingPaymentAgencyTransaction not found');
101
+ }
102
+ // 既に承認済であれば何もしない(2023-05-15~)
103
+ const existingCompletedAuthorizeActions = yield repos.action.searchByPurpose({
104
+ typeOf: factory.actionType.AuthorizeAction,
105
+ purpose: { id: params.transaction.id, typeOf: params.transaction.typeOf },
106
+ actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus },
107
+ object: {
108
+ paymentMethodId: { $eq: params.prePublishedPaymentMethodId },
109
+ typeOf: { $eq: factory.action.authorize.paymentMethod.any.ResultType.Payment }
110
+ },
111
+ sort: { startDate: factory.sortType.Ascending }
112
+ });
113
+ existingCompletedAuthorizeAction = existingCompletedAuthorizeActions.shift();
114
+ if (existingCompletedAuthorizeAction !== undefined) {
115
+ return { existingCompletedAuthorizeAction: { id: existingCompletedAuthorizeAction.id } };
116
+ }
117
+ return {
118
+ authorizeParams: { pendingPaymentAgencyTransaction, paymentMethodByTransaction }
119
+ };
120
+ }
121
+ else {
122
+ // 取引にないpaymentMethodIdの指定はクライアントエラー
123
+ throw new factory.errors.Argument('paymentMethodId', 'invalid paymentMethodId for the transaction');
124
+ }
125
+ });
126
+ }
127
+ exports.handlePrePublishedPaymentMethodIdOnAuthorizing = handlePrePublishedPaymentMethodIdOnAuthorizing;
128
+ /**
129
+ * 注文取引に保管された決済情報を承認しようとしている決済の整合性を検証する
130
+ */
131
+ function validatePaymentMethodByTransaction(params) {
132
+ var _a, _b;
133
+ const paymentServiceIdByObject = params.object.issuedThrough.id;
134
+ const amountByObject = params.object.amount;
135
+ const paymentMethodTypeByObject = params.object.paymentMethod;
136
+ const paymentServiceIdByTransaction = params.paymentMethodByTransaction.issuedThrough.id;
137
+ const amountByTransaction = (_a = params.paymentMethodByTransaction.paymentMethod) === null || _a === void 0 ? void 0 : _a.amount;
138
+ const paymentMethodTypeByTransaction = (_b = params.paymentMethodByTransaction.paymentMethod) === null || _b === void 0 ? void 0 : _b.identifier;
139
+ // 決済サービスID検証
140
+ if (paymentServiceIdByObject !== paymentServiceIdByTransaction) {
141
+ throw new factory.errors.Argument('object.issuedThrough.id', 'issuedThrough.id must match the target of the paymentUrl');
142
+ }
143
+ // 金額検証
144
+ if (amountByObject !== amountByTransaction) {
145
+ throw new factory.errors.Argument('object.amount', 'amount must match the target of the paymentUrl');
146
+ }
147
+ // 決済方法区分検証
148
+ if (paymentMethodTypeByObject !== paymentMethodTypeByTransaction) {
149
+ throw new factory.errors.Argument('object.paymentMethod', 'paymentMethod must match the target of the paymentUrl');
150
+ }
151
+ }
@@ -1,3 +1,6 @@
1
+ /**
2
+ * 汎用決済サービス
3
+ */
1
4
  import * as factory from '../../factory';
2
5
  import type { MongoRepository as AccountingReportRepo } from '../../repo/accountingReport';
3
6
  import type { MongoRepository as ActionRepo } from '../../repo/action';
@@ -13,17 +13,16 @@ exports.publishPaymentUrl = exports.processVoidPayTransaction = exports.person2u
13
13
  /**
14
14
  * 汎用決済サービス
15
15
  */
16
- const createDebug = require("debug");
17
16
  const factory = require("../../factory");
18
17
  const PayTransactionService = require("../assetTransaction/pay");
19
18
  const code_1 = require("../code");
20
19
  const publishConfirmationNumberIfNotExist_1 = require("../transaction/placeOrderInProgress/publishConfirmationNumberIfNotExist");
21
20
  const factory_1 = require("./any/factory");
21
+ const handlePrePublishedPaymentMethodIdOnAuthorizing_1 = require("./any/handlePrePublishedPaymentMethodIdOnAuthorizing");
22
22
  const onPaymentStatusChanged_1 = require("./any/onPaymentStatusChanged");
23
23
  Object.defineProperty(exports, "onPaymentStatusChanged", { enumerable: true, get: function () { return onPaymentStatusChanged_1.onPaymentStatusChanged; } });
24
24
  const person2username_1 = require("./any/person2username");
25
25
  Object.defineProperty(exports, "person2username", { enumerable: true, get: function () { return person2username_1.person2username; } });
26
- const debug = createDebug('chevre-domain:service:payment');
27
26
  /**
28
27
  * 決済承認中止
29
28
  * タスクから決済承認を取り消す
@@ -291,7 +290,7 @@ exports.publishPaymentUrl = publishPaymentUrl;
291
290
  function authorize(params) {
292
291
  // tslint:disable-next-line:cyclomatic-complexity max-func-body-length
293
292
  return (repos) => __awaiter(this, void 0, void 0, function* () {
294
- var _a, _b, _c, _d;
293
+ var _a, _b, _c;
295
294
  if (params.purpose.typeOf !== factory.transactionType.PlaceOrder) {
296
295
  throw new factory.errors.NotImplemented(`purpose.typeOf '${params.purpose.typeOf} not implemented'`);
297
296
  }
@@ -315,84 +314,38 @@ function authorize(params) {
315
314
  let creditCard = params.object.creditCard;
316
315
  // リクエストでpaymentMethodIdを指定された場合、取引に保管されたpaymentMethodIdに一致すればそちらを適用(外部サイト決済対応)
317
316
  if (typeof params.object.paymentMethodId === 'string' && params.object.paymentMethodId.length > 0) {
318
- // transaction.objectへのアクセス回避(2024-05-30~)
319
- // const paymentMethodByTransaction = transaction.object.paymentMethods;
320
- const paymentMethodByTransaction = yield repos.transaction.findInProgressPaymentMethodId({ id: params.purpose.id });
321
- if (params.object.paymentMethodId === (paymentMethodByTransaction === null || paymentMethodByTransaction === void 0 ? void 0 : paymentMethodByTransaction.paymentMethodId)) {
322
- // check existence of acceptAction when authorizing payment(2024-06-01~)
323
- const acceptPayAction = (yield repos.action.search({
324
- limit: 1,
325
- page: 1,
326
- project: { id: { $eq: transaction.project.id } },
327
- typeOf: { $eq: factory.actionType.AcceptAction },
328
- actionStatus: { $in: [factory.actionStatusType.CompletedActionStatus] },
329
- purpose: { id: { $in: [transaction.id] } },
330
- object: {
331
- transactionNumber: { $eq: params.object.paymentMethodId },
332
- typeOf: { $eq: factory.assetTransactionType.Pay }
333
- }
334
- }, ['result'], [])).shift();
335
- if (acceptPayAction === undefined) {
336
- throw new factory.errors.NotFound(factory.actionType.AcceptAction);
337
- }
338
- debug('acceptPayAction found:', JSON.stringify(acceptPayAction));
339
- // find recipe(2024-06-02~)
340
- const actionRecipe = yield ((_a = repos.actionRecipe) === null || _a === void 0 ? void 0 : _a.findByAction({
341
- project: { id: transaction.project.id },
342
- recipeFor: { id: acceptPayAction.id }
343
- }));
344
- debug('actionRecipe found:', JSON.stringify(actionRecipe));
345
- // 検証強化(2024-01-04~)
346
- validatePaymentMethodByTransaction({
347
- object: params.object,
348
- paymentMethodByTransaction
349
- });
317
+ const { authorizeParams, existingCompletedAuthorizeAction } = yield (0, handlePrePublishedPaymentMethodIdOnAuthorizing_1.handlePrePublishedPaymentMethodIdOnAuthorizing)({
318
+ object: params.object,
319
+ prePublishedPaymentMethodId: params.object.paymentMethodId,
320
+ transaction
321
+ })(repos);
322
+ if (existingCompletedAuthorizeAction !== undefined) {
323
+ return { id: existingCompletedAuthorizeAction.id };
324
+ }
325
+ else if (authorizeParams !== undefined) {
350
326
  // creditCardを決済URL発行時の情報で上書き(2024-01-08~)
351
- creditCard = (_b = paymentMethodByTransaction.paymentMethod) === null || _b === void 0 ? void 0 : _b.creditCard;
327
+ creditCard = (_a = authorizeParams.paymentMethodByTransaction.paymentMethod) === null || _a === void 0 ? void 0 : _a.creditCard;
352
328
  transactionNumber = params.object.paymentMethodId;
353
- const { entryTranArgs, entryTranResult, execTranArgs, execTranResult } = paymentMethodByTransaction;
354
- if (entryTranArgs !== undefined && entryTranResult !== undefined
355
- && execTranArgs !== undefined && execTranResult !== undefined) {
356
- pendingPaymentAgencyTransaction = { entryTranArgs, entryTranResult, execTranArgs, execTranResult };
357
- }
358
- // 既に承認済であれば何もしない(2023-05-15~)
359
- const existingCompletedAuthorizeActions = yield repos.action.searchByPurpose({
360
- typeOf: factory.actionType.AuthorizeAction,
361
- purpose: { id: transaction.id, typeOf: transaction.typeOf },
362
- actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus },
363
- object: {
364
- paymentMethodId: { $eq: transactionNumber },
365
- typeOf: { $eq: factory.action.authorize.paymentMethod.any.ResultType.Payment }
366
- },
367
- sort: { startDate: factory.sortType.Ascending }
368
- });
369
- const existingCompletedAuthorizeAction = existingCompletedAuthorizeActions.shift();
370
- if (existingCompletedAuthorizeAction !== undefined) {
371
- return existingCompletedAuthorizeAction;
372
- }
329
+ pendingPaymentAgencyTransaction = authorizeParams.pendingPaymentAgencyTransaction;
373
330
  }
374
331
  else {
375
- // 取引にないpaymentMethodIdの指定はクライアントエラー
376
- throw new factory.errors.Argument('paymentMethodId', 'invalid paymentMethodId for the transaction');
332
+ throw new factory.errors.NotImplemented('pendingPaymentAgencyTransaction requied on paymentMethodId specified');
377
333
  }
378
334
  }
335
+ // 取引番号発行済でなければ発行
379
336
  if (typeof transactionNumber !== 'string') {
380
337
  const publishTransactionNumberResult = yield repos.transactionNumber.publishByTimestamp({ startDate: new Date() });
381
338
  transactionNumber = publishTransactionNumberResult.transactionNumber;
382
339
  }
383
340
  const movieTickets = (Array.isArray(params.object.movieTickets)) ? params.object.movieTickets.map(factory_1.createMovieTicket) : undefined;
384
- const { accountId } = yield fixAccountIdIfPossible({
385
- object: params.object,
386
- project: { id: transaction.project.id }
387
- })(repos);
341
+ const { accountId } = yield fixAccountIdIfPossible({ object: params.object, project: { id: transaction.project.id } })(repos);
388
342
  const authorizeObject = Object.assign(Object.assign(Object.assign(Object.assign({}, params.object), { accountId, paymentMethodId: transactionNumber, typeOf: factory.action.authorize.paymentMethod.any.ResultType.Payment }), (creditCard !== undefined) ? { creditCard } : undefined), (Array.isArray(movieTickets)) ? { movieTickets } : undefined);
389
343
  // 承認アクションを開始する
390
- const taskId = (_c = params.sameAs) === null || _c === void 0 ? void 0 : _c.id;
344
+ const taskId = (_b = params.sameAs) === null || _b === void 0 ? void 0 : _b.id;
391
345
  const actionAttributes = Object.assign({ project: transaction.project, typeOf: factory.actionType.AuthorizeAction, object: authorizeObject, agent: {
392
346
  typeOf: transaction.agent.typeOf,
393
347
  id: transaction.agent.id
394
348
  }, instrument: {
395
- // typeOf: 'WebAPI',
396
349
  typeOf: factory.assetTransactionType.Pay,
397
350
  identifier: factory.action.authorize.paymentMethod.any.ServiceIdentifier.Chevre,
398
351
  transactionNumber
@@ -401,10 +354,9 @@ function authorize(params) {
401
354
  id: transaction.seller.id,
402
355
  name: (typeof transaction.seller.name === 'string')
403
356
  ? transaction.seller.name
404
- : String((_d = transaction.seller.name) === null || _d === void 0 ? void 0 : _d.ja)
405
- }, purpose: { typeOf: transaction.typeOf, id: transaction.id } }, (typeof taskId === 'string')
406
- ? { sameAs: { id: taskId, typeOf: 'Task' } } // タスク関連付け(2024-04-22~)
407
- : undefined);
357
+ : String((_c = transaction.seller.name) === null || _c === void 0 ? void 0 : _c.ja)
358
+ }, purpose: { typeOf: transaction.typeOf, id: transaction.id } }, (typeof taskId === 'string') ? { sameAs: { id: taskId, typeOf: 'Task' } } : undefined // タスク関連付け(2024-04-22~)
359
+ );
408
360
  const action = yield repos.action.start(actionAttributes);
409
361
  let payTransaction;
410
362
  try {
@@ -457,30 +409,6 @@ function authorize(params) {
457
409
  });
458
410
  }
459
411
  exports.authorize = authorize;
460
- /**
461
- * 注文取引に保管された決済情報を承認しようとしている決済の整合性を検証する
462
- */
463
- function validatePaymentMethodByTransaction(params) {
464
- var _a, _b;
465
- const paymentServiceIdByObject = params.object.issuedThrough.id;
466
- const amountByObject = params.object.amount;
467
- const paymentMethodTypeByObject = params.object.paymentMethod;
468
- const paymentServiceIdByTransaction = params.paymentMethodByTransaction.issuedThrough.id;
469
- const amountByTransaction = (_a = params.paymentMethodByTransaction.paymentMethod) === null || _a === void 0 ? void 0 : _a.amount;
470
- const paymentMethodTypeByTransaction = (_b = params.paymentMethodByTransaction.paymentMethod) === null || _b === void 0 ? void 0 : _b.identifier;
471
- // 決済サービスID検証
472
- if (paymentServiceIdByObject !== paymentServiceIdByTransaction) {
473
- throw new factory.errors.Argument('object.issuedThrough.id', 'issuedThrough.id must match the target of the paymentUrl');
474
- }
475
- // 金額検証
476
- if (amountByObject !== amountByTransaction) {
477
- throw new factory.errors.Argument('object.amount', 'amount must match the target of the paymentUrl');
478
- }
479
- // 決済方法区分検証
480
- if (paymentMethodTypeByObject !== paymentMethodTypeByTransaction) {
481
- throw new factory.errors.Argument('object.paymentMethod', 'paymentMethod must match the target of the paymentUrl');
482
- }
483
- }
484
412
  /**
485
413
  * 承認しようとしているobjectからaccountIdを決定する
486
414
  */
package/package.json CHANGED
@@ -110,5 +110,5 @@
110
110
  "postversion": "git push origin --tags",
111
111
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
112
112
  },
113
- "version": "21.33.0-alpha.17"
113
+ "version": "21.33.0-alpha.18"
114
114
  }