UNPKG

node-easywechat

Version:

EasyWechat SDK for Node.js (NOT OFFICIAL)

123 lines (122 loc) 4.85 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const ServerInterface_1 = __importDefault(require("../Core/Contracts/ServerInterface")); const Response_1 = __importDefault(require("../Core/Http/Response")); const Message_1 = __importDefault(require("./Message")); const AES_1 = require("../Core/Support/AES"); class Server extends ServerInterface_1.default { constructor(merchant = null, request = null) { super(); this.merchant = merchant; this.request = request; } /** * 服务端消息处理 * @returns */ serve() { return __awaiter(this, void 0, void 0, function* () { let message = yield this.getRequestMessage(); try { let defaultResponse = new Response_1.default(200, {}, JSON.stringify({ code: 'SUCCESS', message: '成功', })); let response = yield this.handle(defaultResponse, message); if (!(response instanceof Response_1.default)) { response = defaultResponse; } return response; } catch (e) { return new Response_1.default(200, {}, JSON.stringify({ code: 'ERROR', message: e.message, })); } }); } /** * 获取来自微信服务器的推送消息 * @param request 未设置该参数时,则从当前服务端收到的请求中获取 * @returns */ getRequestMessage(request = null) { return __awaiter(this, void 0, void 0, function* () { if (!request) { request = this.request; } let originContent = ''; let body = request.getBody(); if (body) { originContent = body.toString(); } let attributes = {}; try { attributes = JSON.parse(originContent); } catch (e) { } if (Object.keys(attributes).length === 0) { throw new Error('Invalid request body.'); } if (!attributes['resource']['ciphertext']) { throw new Error('Invalid request.'); } try { let decryptMessage = AES_1.AES_GCM.decrypt(attributes['resource']['ciphertext'], this.merchant.getSecretKey(), attributes['resource']['nonce'], attributes['resource']['associated_data']); attributes = JSON.parse(decryptMessage.toString()); } catch (e) { throw new Error('Failed to decrypt request message.'); } return new Message_1.default(attributes, originContent); }); } /** * 获取解密后的消息 * @param request * @returns */ getDecryptedMessage(request = null) { return this.getRequestMessage(request); } /** * 处理付款回调 * @param handler 消息处理器,需要接受两个参数,参数1是消息,参数2是下一个消息处理器 * @returns */ handlePaid(handler) { this.with((message, next) => __awaiter(this, void 0, void 0, function* () { return message.getEventType() === 'TRANSACTION.SUCCESS' && message.trade_state === 'SUCCESS' ? handler(message, next) : next(message); })); return this; } /** * 处理退款回调 * @param handler 消息处理器,需要接受两个参数,参数1是消息,参数2是下一个消息处理器 * @returns */ handleRefunded(handler) { this.with((message, next) => __awaiter(this, void 0, void 0, function* () { let eventType = message.getEventType(); return [ 'REFUND.SUCCESS', 'REFUND.ABNORMAL', 'REFUND.CLOSED', ].findIndex(o => o === eventType) > -1 ? handler(message, next) : next(message); })); return this; } } ; module.exports = Server;