UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

155 lines (154 loc) 9.21 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handlePrePublishedPaymentMethodIdOnAuthorizing = handlePrePublishedPaymentMethodIdOnAuthorizing; const createDebug = require("debug"); const factory = require("../../../factory"); const debug = createDebug('chevre-domain:service:payment'); function recipe2paymentAgencyTransaction(actionRecipe) { var _a; let entryTranArgs; let entryTranResult; let execTranArgs; let execTranResult; if (actionRecipe.recipeCategory !== factory.recipe.RecipeCategory.publishPaymentUrl) { throw new factory.errors.NotFound('', `Recipe with category ${factory.recipe.RecipeCategory.publishPaymentUrl} not found`); } const howToSection = actionRecipe.step; const howToSteps = (_a = howToSection.at(0)) === null || _a === void 0 ? void 0 : _a.itemListElement; if (Array.isArray(howToSteps)) { howToSteps.forEach((howToStep) => { var _a, _b, _c, _d; const stepIdentifier = howToStep.identifier; if (stepIdentifier === factory.recipe.StepIdentifier.entryTran) { entryTranArgs = (_a = howToStep.itemListElement.at(0)) === null || _a === void 0 ? void 0 : _a.beforeMedia; entryTranResult = (_b = howToStep.itemListElement.at(0)) === null || _b === void 0 ? void 0 : _b.afterMedia; } else if (stepIdentifier === factory.recipe.StepIdentifier.execTran) { execTranArgs = (_c = howToStep.itemListElement.at(0)) === null || _c === void 0 ? void 0 : _c.beforeMedia; execTranResult = (_d = howToStep.itemListElement.at(0)) === null || _d === void 0 ? void 0 : _d.afterMedia; } else { throw new factory.errors.NotImplemented(`howToStep.identifier '${stepIdentifier}' not implemented`); } }); } if (entryTranArgs === undefined || entryTranResult === undefined || execTranArgs === undefined || execTranResult === undefined) { throw new factory.errors.NotFound(`beforeMedia or afterMedia of actionRecipe`); } return { entryTranArgs, entryTranResult, execTranArgs, execTranResult }; } function handlePrePublishedPaymentMethodIdOnAuthorizing(params) { return (repos) => __awaiter(this, void 0, void 0, function* () { let creditCard; let pendingPaymentAgencyTransaction; let existingCompletedAuthorizeAction; // transaction.objectへのアクセス回避(2024-05-30~) // const paymentMethodByTransaction = transaction.object.paymentMethods; const paymentMethodByTransaction = yield repos.transaction.findInProgressPaymentMethodId({ id: params.transaction.id }); if (params.prePublishedPaymentMethodId === (paymentMethodByTransaction === null || paymentMethodByTransaction === void 0 ? void 0 : paymentMethodByTransaction.paymentMethodId)) { // check existence of acceptAction when authorizing payment(2024-06-01~) const acceptPayAction = (yield repos.action.search({ limit: 1, page: 1, project: { id: { $eq: params.transaction.project.id } }, typeOf: { $eq: factory.actionType.AcceptAction }, actionStatus: { $in: [factory.actionStatusType.CompletedActionStatus] }, purpose: { id: { $in: [params.transaction.id] } }, object: { transactionNumber: { $eq: params.prePublishedPaymentMethodId }, typeOf: { $eq: factory.assetTransactionType.Pay } } }, ['object', 'result'])).shift(); if (acceptPayAction === undefined) { throw new factory.errors.NotFound(factory.actionType.AcceptAction); } debug('acceptPayAction found:', acceptPayAction.id); // find recipe(2024-06-02~) const actionRecipe = yield repos.action.findRecipeByAction({ project: { id: params.transaction.project.id }, recipeFor: { id: acceptPayAction.id } }); debug('actionRecipe found:', actionRecipe === null || actionRecipe === void 0 ? void 0 : actionRecipe.recipeCategory); // 検証強化(2024-01-04~) validatePaymentMethodByTransaction({ object: params.object, acceptObject: acceptPayAction.object }); // use actionRecipe(2024-06-02~) if (typeof (actionRecipe === null || actionRecipe === void 0 ? void 0 : actionRecipe.recipeCategory) === 'string') { pendingPaymentAgencyTransaction = recipe2paymentAgencyTransaction(actionRecipe); creditCard = acceptPayAction.object.object.paymentMethod.creditCard; } else { // discontinue(2024-06-05~) // const { entryTranArgs, entryTranResult, execTranArgs, execTranResult } = paymentMethodByTransaction; // pendingPaymentAgencyTransaction = { entryTranArgs, entryTranResult, execTranArgs, execTranResult }; } if (creditCard === undefined) { throw new factory.errors.Argument('paymentMethodId', 'creditCard not found'); } if (pendingPaymentAgencyTransaction === undefined) { throw new factory.errors.Argument('paymentMethodId', 'pendingPaymentAgencyTransaction not found'); } // 既に承認済であれば何もしない(2023-05-15~) const existingCompletedAuthorizeActions = yield repos.action.searchByPurpose({ typeOf: factory.actionType.AuthorizeAction, purpose: { id: params.transaction.id, typeOf: params.transaction.typeOf }, actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus }, object: { paymentMethodId: { $eq: params.prePublishedPaymentMethodId }, typeOf: { $eq: factory.action.authorize.paymentMethod.any.ResultType.Payment } }, sort: { startDate: factory.sortType.Ascending } }); existingCompletedAuthorizeAction = existingCompletedAuthorizeActions.shift(); if (existingCompletedAuthorizeAction !== undefined) { return { existingCompletedAuthorizeAction: { id: existingCompletedAuthorizeAction.id } }; } return { authorizeParams: { creditCard, pendingPaymentAgencyTransaction, paymentMethodByTransaction } }; } else { // 取引にないpaymentMethodIdの指定はクライアントエラー throw new factory.errors.Argument('paymentMethodId', 'invalid paymentMethodId for the transaction'); } }); } /** * 注文取引に保管された決済情報を承認しようとしている決済の整合性を検証する */ function validatePaymentMethodByTransaction(params) { var _a, _b; const paymentServiceIdByObject = params.object.issuedThrough.id; const amountByObject = params.object.amount; const paymentMethodTypeByObject = params.object.paymentMethod; // const paymentServiceIdByTransaction = params.paymentMethodByTransaction.issuedThrough.id; // const amountByTransaction = params.paymentMethodByTransaction.paymentMethod?.amount; // const paymentMethodTypeByTransaction = params.paymentMethodByTransaction.paymentMethod?.identifier; const paymentServiceIdByTransaction = params.acceptObject.object.id; const amountByTransaction = (_a = params.acceptObject.object.paymentMethod) === null || _a === void 0 ? void 0 : _a.amount; const paymentMethodTypeByTransaction = (_b = params.acceptObject.object.paymentMethod) === null || _b === void 0 ? void 0 : _b.identifier; // 決済サービスID検証 if (paymentServiceIdByObject !== paymentServiceIdByTransaction) { throw new factory.errors.Argument('object.issuedThrough.id', 'issuedThrough.id must match the target of the paymentUrl'); } // 金額検証 if (amountByObject !== amountByTransaction) { throw new factory.errors.Argument('object.amount', 'amount must match the target of the paymentUrl'); } // 決済方法区分検証 if (paymentMethodTypeByObject !== paymentMethodTypeByTransaction) { throw new factory.errors.Argument('object.paymentMethod', 'paymentMethod must match the target of the paymentUrl'); } }