UNPKG

softbankjs

Version:
434 lines (433 loc) 22 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SoftbankCreditCard = exports.CardBrand = exports.CardBrandReturnFlag = exports.CustomerInfoReturnFlag = exports.ResponseInfoType = exports.EncryptedFlag = void 0; var softbank_1 = require("./softbank"); var Encoding = require("encoding-japanese"); /** * @enum {string} * @property {string} NONE 0 * @property {string} ENCRYPTED 1 */ var EncryptedFlag; (function (EncryptedFlag) { EncryptedFlag["NONE"] = "0"; EncryptedFlag["ENCRYPTED"] = "1"; })(EncryptedFlag = exports.EncryptedFlag || (exports.EncryptedFlag = {})); /** * @enum {string} * @property {string} NONE 0 * @property {string} RETURN_ALL_MARK 1 * @property {string} RETURN_4_DIGITS 2 */ var ResponseInfoType; (function (ResponseInfoType) { ResponseInfoType["NONE"] = "0"; ResponseInfoType["RETURN_ALL_MARK"] = "1"; ResponseInfoType["RETURN_4_DIGITS"] = "2"; })(ResponseInfoType = exports.ResponseInfoType || (exports.ResponseInfoType = {})); /** * @enum {string} * @property {string} NONE 0 * @property {string} RETURNED 1 */ var CustomerInfoReturnFlag; (function (CustomerInfoReturnFlag) { CustomerInfoReturnFlag["NONE"] = "0"; CustomerInfoReturnFlag["RETURNED"] = "1"; })(CustomerInfoReturnFlag = exports.CustomerInfoReturnFlag || (exports.CustomerInfoReturnFlag = {})); /** * @enum {string} * @property {string} NONE 0 * @property {string} RETURNED 1 */ var CardBrandReturnFlag; (function (CardBrandReturnFlag) { CardBrandReturnFlag["NONE"] = "0"; CardBrandReturnFlag["RETURNED"] = "1"; })(CardBrandReturnFlag = exports.CardBrandReturnFlag || (exports.CardBrandReturnFlag = {})); /** * @constant {string} * @property {string} J JCB * @property {string} M MasterCard * @property {string} V Visa * @property {string} A American Express * @property {string} D Diners Club * @property {string} X その他 */ exports.CardBrand = { J: "JCB", V: "Visa", M: "MasterCard", A: "American Express", D: "Diners Club", X: "その他", }; /** * @constructor * @param {string} endpoint Softbank API endpoint * @param {string} merchantId Softbank API merchant ID * @param {string} serviceId Softbank API service ID * @param {string} hashKey Softbank API hash key * @param {string} locale Softbank API locale */ var SoftbankCreditCard = /** @class */ (function (_super) { __extends(SoftbankCreditCard, _super); function SoftbankCreditCard(endpoint, merchantId, serviceId, hashKey, locale, debug) { if (locale === void 0) { locale = softbank_1.Locale.EN; } if (debug === void 0) { debug = false; } return _super.call(this, endpoint, merchantId, serviceId, hashKey, locale, debug) || this; } /** * @function createUpdateCustomer * @memberof SoftbankCreditCard * @param {boolean} isCreate true: create, false: update * @param {string} customerId is required and must be unique * @param {string} encryptedFlg is required * @param {string} requestDate format: YYYYMMddHHmmss * @param {string} ccNumber is required * @param {string} ccExpiration is required, format: YYYYMM * @param {string} securityCode * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.createUpdateCustomer = function (isCreate, customerId, encryptedFlg, requestDate, ccNumber, ccExpiration, securityCode) { if (isCreate === void 0) { isCreate = true; } if (encryptedFlg === void 0) { encryptedFlg = EncryptedFlag.NONE; } return __awaiter(this, void 0, void 0, function () { var payload; return __generator(this, function (_a) { payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: isCreate ? this.requestId.CREATE_CUSTOMER_REQUEST : this.requestId.UPDATE_CUSTOMER_REQUEST, }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, cust_code: { _text: customerId }, encrypted_flg: { _text: encryptedFlg }, request_date: { _text: requestDate }, pay_method_info: { cc_number: { _text: ccNumber }, cc_expiration: { _text: ccExpiration }, security_code: { _text: securityCode }, }, sps_hashcode: { _text: this.generateHashCode(customerId, ccNumber, ccExpiration, securityCode, encryptedFlg, requestDate), }, }, }; return [2 /*return*/, this.request(payload)]; }); }); }; /** * @public * @function createUpdateCustomerWithToken * @memberof SoftbankCreditCard * @param {boolean} isCreate mark true: create, false: update * @param {string} customerId is required and must be unique * @param {string} customerInfoReturnFlg is required * @param {EncryptedFlag} encryptedFlg is required * @param {string} requestDate format: YYYYMMddHHmmss * @param {string} token generate from web client * @param {string} tokenKey generate from web client * @param {CardBrandReturnFlag} cardbrandReturnFlg is required * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.createUpdateCustomerWithToken = function (isCreate, customerId, customerInfoReturnFlg, encryptedFlg, requestDate, token, tokenKey, cardbrandReturnFlg) { if (isCreate === void 0) { isCreate = true; } if (customerInfoReturnFlg === void 0) { customerInfoReturnFlg = CustomerInfoReturnFlag.RETURNED; } if (encryptedFlg === void 0) { encryptedFlg = EncryptedFlag.NONE; } if (cardbrandReturnFlg === void 0) { cardbrandReturnFlg = CardBrandReturnFlag.RETURNED; } return __awaiter(this, void 0, void 0, function () { var payload; return __generator(this, function (_a) { payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: isCreate ? this.requestId.CREATE_CUSTOMER_TOKEN_REQUEST : this.requestId.UPDATE_CUSTOMER_TOKEN_REQUEST, }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, cust_code: { _text: customerId }, sps_cust_info_return_flg: { _text: customerInfoReturnFlg }, encrypted_flg: { _text: encryptedFlg }, request_date: { _text: requestDate }, pay_option_manage: { token: { _text: token }, token_key: { _text: tokenKey }, cardbrand_return_flg: { _text: cardbrandReturnFlg }, }, sps_hashcode: { _text: this.generateHashCode(customerId, customerInfoReturnFlg, token, tokenKey, cardbrandReturnFlg, encryptedFlg, requestDate), }, }, }; return [2 /*return*/, this.request(payload)]; }); }); }; /** * @function getCustomer * @memberof SoftbankCreditCard * @param {string} customerId customerId of createUpdateCustomer or createUpdateCustomerWithToken * @param {CustomerInfoReturnFlag} customerReturnFlg is required * @param {ResponseInfoType} responseInfoType is required * @param {CardBrandReturnFlag} cardbrandReturnFlg is required * @param {EncryptedFlag} encryptedFlg is required * @param {string} requestDate format is YYYYMMDDHHmmss * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.getCustomer = function (customerId, customerReturnFlg, responseInfoType, cardbrandReturnFlg, encryptedFlg, requestDate) { if (customerReturnFlg === void 0) { customerReturnFlg = CustomerInfoReturnFlag.NONE; } if (responseInfoType === void 0) { responseInfoType = ResponseInfoType.NONE; } if (cardbrandReturnFlg === void 0) { cardbrandReturnFlg = CardBrandReturnFlag.RETURNED; } if (encryptedFlg === void 0) { encryptedFlg = EncryptedFlag.NONE; } return __awaiter(this, void 0, void 0, function () { var payload; return __generator(this, function (_a) { payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: this.requestId.GET_CUSTOMER_REQUEST }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, cust_code: { _text: customerId }, sps_cust_info_return_flg: { _text: customerReturnFlg }, response_info_type: { _text: responseInfoType }, pay_option_manage: { cardbrand_return_flg: { _text: cardbrandReturnFlg }, }, encrypted_flg: { _text: encryptedFlg }, request_date: { _text: requestDate }, sps_hashcode: { _text: this.generateHashCode(customerId, customerReturnFlg, responseInfoType, cardbrandReturnFlg, encryptedFlg, requestDate), }, }, }; return [2 /*return*/, this.request(payload)]; }); }); }; /** * @function createTransaction * @memberof SoftbankCreditCard * @param {string} customerId customerId from createUpdateCustomer or createUpdateCustomerWithToken * @param {string} orderId is required * @param {string} itemId is required * @param {string} amount is required * @param {CustomerInfoReturnFlag} customerReturnFlg is required * @param {EncryptedFlag} encryptedFlg is required * @param {string} requestDate format is YYYYMMDDHHmmss * @param {string} cardbrandReturnFlg is required * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.createTransaction = function (customerId, orderId, itemId, amount, customerReturnFlg, encryptedFlg, requestDate, itemName) { if (encryptedFlg === void 0) { encryptedFlg = EncryptedFlag.NONE; } if (itemName === void 0) { itemName = ""; } return __awaiter(this, void 0, void 0, function () { var unicodeArray, itemNameBase64, uCode, payload; return __generator(this, function (_a) { unicodeArray = Encoding.stringToCode(itemName); itemNameBase64 = Buffer.from(itemName).toString("base64"); if (this.locale === softbank_1.Locale.JA) { uCode = Encoding.convert(unicodeArray, { to: "SJIS", from: "UNICODE", }); itemNameBase64 = Encoding.base64Encode(uCode); } payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: this.requestId.CREATE_TRANSACTION_REQUEST }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, cust_code: { _text: customerId }, order_id: { _text: orderId }, item_id: { _text: itemId }, amount: { _text: amount }, sps_cust_info_return_flg: { _text: customerReturnFlg }, encrypted_flg: { _text: encryptedFlg }, request_date: { _text: requestDate }, sps_hashcode: { _text: this.generateHashCode(customerId, orderId, itemId, itemName, amount, customerReturnFlg, encryptedFlg, requestDate), }, }, }; if (itemName) { payload["sps-api-request"]["item_name"] = { _text: itemNameBase64, }; } return [2 /*return*/, this.request(payload)]; }); }); }; /** * @function confirmTransaction * @memberof SoftbankCreditCard * @param {string} transactionId transactionId from createTransaction * @param {string} trackingId trackingId from createTransaction * @param {string} requestDate requestDate format YYYYMMDDHHmmss * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.confirmTransaction = function (transactionId, trackingId, requestDate) { return __awaiter(this, void 0, void 0, function () { var payload; return __generator(this, function (_a) { payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: this.requestId.CONFIRM_TRANSACTION_REQUEST }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, request_date: { _text: requestDate }, sps_hashcode: { _text: this.generateHashCode(transactionId, trackingId, requestDate), }, sps_transaction_id: { _text: transactionId }, tracking_id: { _text: trackingId }, }, }; return [2 /*return*/, this.request(payload)]; }); }); }; /** * @function requestPurchase * @memberof SoftbankCreditCard * @param {string} transactionId transactionId from createTransaction request * @param {string} trackingId trackingId from createTransaction request * @param {string} processDate processDate from createTransaction request * @param {string} requestDate requestDate format YYYYMMDDHHmmss * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.requestPurchase = function (transactionId, trackingId, processDate, requestDate) { return __awaiter(this, void 0, void 0, function () { var payload; return __generator(this, function (_a) { payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: this.requestId.PURCHASE_REQUEST }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, request_date: { _text: requestDate }, sps_hashcode: { _text: this.generateHashCode(transactionId, trackingId, processDate, requestDate), }, sps_transaction_id: { _text: transactionId }, tracking_id: { _text: trackingId }, processing_datetime: { _text: processDate }, }, }; return [2 /*return*/, this.request(payload)]; }); }); }; /** * @function refundPurchase * @memberof SoftbankCreditCard * @param {string} transactionId transactionId from createTransaction request * @param {string} trackingId trackingId from createTransaction request * @param {string} processDate processDate from createTransaction request * @param {string} requestDate requestDate format YYYYMMDDHHmmss * @returns {Promise<SoftbankTransactionResponse>} */ SoftbankCreditCard.prototype.refundPurchase = function (transactionId, trackingId, processDate, requestDate) { return __awaiter(this, void 0, void 0, function () { var payload; return __generator(this, function (_a) { payload = { _declaration: { _attributes: { version: "1.0", encoding: "Shift_JIS" }, }, "sps-api-request": { _attributes: { id: this.requestId.REFUND_REQUEST }, merchant_id: { _text: this.merchantId }, service_id: { _text: this.serviceId }, request_date: { _text: requestDate }, sps_hashcode: { _text: this.generateHashCode(transactionId, trackingId, processDate, requestDate), }, sps_transaction_id: { _text: transactionId }, tracking_id: { _text: trackingId }, processing_datetime: { _text: processDate }, }, }; return [2 /*return*/, this.request(payload)]; }); }); }; return SoftbankCreditCard; }(softbank_1.SoftbankService)); exports.SoftbankCreditCard = SoftbankCreditCard;