UNPKG

typed-douyin-openapi

Version:
58 lines 2.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PayBase = void 0; const api_base_1 = require("../../api/api_base"); const md5_1 = __importDefault(require("md5")); const errors_1 = require("../../errors"); class PayBase extends api_base_1.ApiBase { constructor(config, tokenStorage) { super(config.sandbox === true ? 'https://open-sandbox.douyin.com/' : 'https://developer.toutiao.com/', tokenStorage); this.appid = config.appid; this.salt = config.salt; this.token = config.token; this.sandbox = config.sandbox || false; } signAndGet(params) { // params 代表请求内容 const skip_arr = ['thirdparty_id', 'app_id', 'sign']; const paramArray = []; const signParams = {}; for (const k in params) { if (params[k] == '' || params[k] === undefined) { continue; } signParams[k] = params[k]; if (skip_arr.indexOf(k) != -1) { continue; } paramArray.push(params[k]); } paramArray.push(this.salt); paramArray.sort(); signParams['sign'] = (0, md5_1.default)(paramArray.join('&')); console.log(JSON.stringify(signParams), this.salt); return signParams; } verifyAndGetMsg(query) { const { msg_signature, timestamp, msg, nonce } = query; const strArr = [this.token, timestamp, nonce, msg].sort(); const str = strArr.join(''); const _signature = require('crypto') .createHash('sha1') .update(str) .digest('hex'); if (msg_signature == _signature) { return JSON.parse(msg); } else { throw new errors_1.DouyinAPIError('Invalid signature', -2); } } } exports.PayBase = PayBase; //# sourceMappingURL=pay_base.js.map