@chevre/domain
Version:
Chevre Domain Library for Node.js
162 lines (161 loc) • 8.75 kB
JavaScript
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.payCreditCard = payCreditCard;
const GMO = require("@motionpicture/gmo-service");
const factory = require("../../../factory");
const onPayActionCompleted_1 = require("../any/onPayActionCompleted");
const factory_1 = require("./factory");
const getGMOInfoFromSeller_1 = require("./getGMOInfoFromSeller");
/**
* クレジットカード決済
*/
function payCreditCard(params) {
return (repos, settings) => __awaiter(this, void 0, void 0, function* () {
var _a;
const payObject = params.object;
// CreditCard系統の決済方法タイプは動的
const paymentMethodType = payObject[0].paymentMethod.typeOf;
if (typeof paymentMethodType !== 'string') {
throw new factory.errors.ArgumentNull('object.paymentMethod.typeOf');
}
const paymentServiceId = payObject[0].id;
const availableChannel = yield repos.paymentService.findAvailableChannelCreditCard({
project: params.project,
// typeOf: factory.service.paymentService.PaymentServiceType.CreditCard,
id: paymentServiceId
});
const sellerId = (_a = params.recipient) === null || _a === void 0 ? void 0 : _a.id;
if (typeof sellerId !== 'string') {
throw new factory.errors.ArgumentNull('recipient.id');
}
const requirePaymentAccepted = false;
const { shopId, shopPass } = yield (0, getGMOInfoFromSeller_1.getGMOInfoFromSeller)({
paymentMethodType, seller: { id: sellerId }, paymentServiceId,
requirePaymentAccepted
})(repos);
const action = yield repos.action.start(params);
const alterTranResults = [];
const processAlterTranResults = [];
let recipe;
try {
const creditService = new GMO.service.Credit({
endpoint: String(availableChannel.serviceUrl),
useFetch: settings.gmo.useFetch
}, { timeout: settings.gmo.timeout });
yield Promise.all(payObject.map((paymentMethod) => __awaiter(this, void 0, void 0, function* () {
const processAlterTranResult = yield processAlterTran({
shopId, shopPass, availableChannel, paymentMethod
})({ creditService });
processAlterTranResults.push(processAlterTranResult);
if (processAlterTranResult.processAlterTranError === undefined) {
if (processAlterTranResult.alterTranResult !== undefined) {
alterTranResults.push(processAlterTranResult.alterTranResult);
}
}
else {
// 代行決済連携中にエラーがあればそのままthrow
throw processAlterTranResult.processAlterTranError;
}
})));
// add recipe(2024-06-03~)
recipe = (0, factory_1.processAlterTranResult2payRecipe)({ processAlterTranResults, project: { id: params.project.id } });
}
catch (error) {
try {
// add recipe(2024-06-29~)
recipe = (0, factory_1.processAlterTranResult2payRecipe)({ processAlterTranResults, project: { id: params.project.id } });
yield repos.action.giveUp({ typeOf: action.typeOf, id: action.id, error, recipe });
}
catch (__) {
// 失敗したら仕方ない
}
throw error;
}
const actionResult = {
// creditCardSales: alterTranResults // discontinue(2024-06-10~)
};
yield repos.action.completeWithVoid({ typeOf: action.typeOf, id: action.id, result: actionResult, recipe });
yield (0, onPayActionCompleted_1.onPayActionCompleted)({
// actionStatus: factory.actionStatusType.CompletedActionStatus,
id: action.id,
object: params.object,
potentialActions: params.potentialActions,
project: params.project,
purpose: params.purpose,
typeOf: action.typeOf
})(repos);
// return action;
});
}
function processAlterTran(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g;
let searchTradeArgs;
let searchTradeResult;
let alterTranArgs;
let alterTranResult;
let processAlterTranError;
try {
const { shopId, shopPass } = params;
const paymentMethod = params.paymentMethod;
const orderId = paymentMethod.paymentMethod.paymentMethodId;
// 取引状態参照
searchTradeArgs = {
shopId, shopPass, orderId,
siteId: (_a = params.availableChannel.credentials) === null || _a === void 0 ? void 0 : _a.siteId,
sitePass: (_b = params.availableChannel.credentials) === null || _b === void 0 ? void 0 : _b.sitePass
};
searchTradeResult = yield repos.creditService.searchTrade(searchTradeArgs);
const { accessId, accessPass, forward, approve, tranId, jobCd } = searchTradeResult;
const amount = (_c = paymentMethod.paymentMethod.totalPaymentDue) === null || _c === void 0 ? void 0 : _c.value;
if (typeof amount !== 'number') {
throw new factory.errors.ArgumentNull('object.paymentMethod.totalPaymentDue?.value');
}
switch (jobCd) {
case GMO.utils.util.JobCd.Capture:
case GMO.utils.util.JobCd.Sales:
// すでに即時売上済み
alterTranResult = { accessId, accessPass, forward, approve, tranId, tranDate: '' };
break;
case GMO.utils.util.JobCd.Void:
// 返品手数料決済の場合を追加(状態が取消であれば即時売上)
alterTranArgs = {
shopId, shopPass, accessId, accessPass,
jobCd: GMO.utils.util.JobCd.Capture,
amount, // 手数料を指定
siteId: (_d = params.availableChannel.credentials) === null || _d === void 0 ? void 0 : _d.siteId,
sitePass: (_e = params.availableChannel.credentials) === null || _e === void 0 ? void 0 : _e.sitePass,
method: GMO.utils.util.Method.Lump // 再オーソリの場合、支払方法指定は必須
};
alterTranResult = yield repos.creditService.alterTran(alterTranArgs);
break;
case GMO.utils.util.JobCd.Auth:
alterTranArgs = {
shopId, shopPass,
accessId, accessPass,
jobCd: GMO.utils.util.JobCd.Sales,
amount: amount,
siteId: (_f = params.availableChannel.credentials) === null || _f === void 0 ? void 0 : _f.siteId,
sitePass: (_g = params.availableChannel.credentials) === null || _g === void 0 ? void 0 : _g.sitePass
};
alterTranResult = yield repos.creditService.alterTran(alterTranArgs);
break;
default:
throw new factory.errors.NotImplemented(`jobCd '${jobCd}' not implemented`);
}
}
catch (error) {
processAlterTranError = error;
}
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (searchTradeArgs !== undefined) ? { searchTradeArgs } : undefined), (searchTradeResult !== undefined) ? { searchTradeResult } : undefined), (alterTranArgs !== undefined) ? { alterTranArgs } : undefined), (alterTranResult !== undefined) ? { alterTranResult } : undefined), (processAlterTranError !== undefined) ? { processAlterTranError } : undefined);
});
}
;