活动:微信支付
This commit is contained in:
@@ -6,6 +6,7 @@ import { addReward, stringToRewardParam, useGiftPackage } from '../../../service
|
||||
import { dicRMB } from '../../../pubUtils/dictionary/DicRMB';
|
||||
import { UserOrderModel } from '../../../db/UserOrder';
|
||||
import _ = require('underscore');
|
||||
import { applyOrderWX, checkOrderWX } from '../../../services/pay/weixinPay';
|
||||
|
||||
export default function (app: Application) {
|
||||
return new orderHandler(app);
|
||||
@@ -40,13 +41,43 @@ export class orderHandler {
|
||||
|
||||
let price = productInfo.price;//价格
|
||||
let productType = productInfo.type;//类型
|
||||
let message = productInfo.message;//商品信息
|
||||
let localOrderID = genCode(32);//本地订单号
|
||||
let orderID = '';//平台订单号
|
||||
|
||||
//生成订单号
|
||||
let orderInfo = await UserOrderModel.applyOrder(serverId, roleId, productID, localOrderID, orderID, price, payType);
|
||||
|
||||
return resResult(STATUS.SUCCESS, { orderInfo, productInfo });
|
||||
let sdkOrderInfo = null;//客户端需要的平台订单信息
|
||||
switch (payType) {
|
||||
case PAY_TYPE.WX:
|
||||
{
|
||||
//生成订单号
|
||||
let weixinOrder = await applyOrderWX(localOrderID, price * 100, message);
|
||||
if (weixinOrder.code == 0) {
|
||||
console.log('微信下单失败')
|
||||
return resResult(STATUS.APPLY_ORDER_ERROR);
|
||||
}
|
||||
sdkOrderInfo = weixinOrder.data;
|
||||
orderID = weixinOrder.data.prepayid;//微信订单号
|
||||
break;
|
||||
}
|
||||
case PAY_TYPE.ALI:
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
case PAY_TYPE.APPLE:
|
||||
{
|
||||
break;
|
||||
}
|
||||
default:
|
||||
console.log('未知支付类型');
|
||||
return resResult(STATUS.NO_PAY_TYPE);
|
||||
}
|
||||
|
||||
await UserOrderModel.applyOrder(serverId, roleId, productID, localOrderID, orderID, price, payType);
|
||||
return resResult(STATUS.SUCCESS, { orderInfo: sdkOrderInfo, productInfo, localOrderID });
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +135,20 @@ export class orderHandler {
|
||||
switch (payType) {
|
||||
case PAY_TYPE.WX:
|
||||
{
|
||||
let weixinResult = await checkOrderWX(localOrderID);
|
||||
if (weixinResult.code == 0) {
|
||||
console.log('微信订单查询失败')
|
||||
return resResult(STATUS.ORDER_ERROR);
|
||||
}
|
||||
if (parseInt(weixinResult.data.total_fee) / 100 != price) {
|
||||
console.log('订单价格错误')
|
||||
return resResult(STATUS.PRICE_ERROR);
|
||||
}
|
||||
if (weixinResult.data.trade_state != "SUCCESS" ||
|
||||
weixinResult.data.result_code != "SUCCESS" ||
|
||||
weixinResult.data.return_code != "SUCCESS") {
|
||||
return resResult(STATUS.NO_PAY);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PAY_TYPE.ALI:
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
|
||||
import tenpay = require('tenpay');
|
||||
import path = require('path');
|
||||
|
||||
/*
|
||||
包名:com.bantu.sgzzyz
|
||||
APPID:wx1d3cd5cd2f065358
|
||||
AppSecret:ebf099c41da57edf8693b5bb234d9b31
|
||||
商户号:1272966801
|
||||
APISECRET:ABCabc328BantuPKU726BANTUMPRCghb
|
||||
*/
|
||||
|
||||
const config = {
|
||||
appid: 'wx1d3cd5cd2f065358',
|
||||
mchid: '1272966801',
|
||||
partnerKey: 'ABCabc328BantuPKU726BANTUMPRCghb',
|
||||
pfx: require('fs').readFileSync(path.resolve(__dirname, '../../../../../key/weixinpay/apiclient_cert.p12')),
|
||||
notify_url: "https://" + "10.29.176.166" + "/order/weixinpaycallback",//支付回调网址
|
||||
spbill_create_ip: '127.0.0.1'
|
||||
};
|
||||
// 方式一
|
||||
const api = new tenpay(config);
|
||||
// 调试模式
|
||||
// const api = new tenpay(config, true);
|
||||
// 沙盒模式(用于微信支付验收)
|
||||
// const sandboxAPI = await tenpay.sandbox(config);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 微信下单
|
||||
*
|
||||
* @param {number} price 价格订单金额(分)
|
||||
* @param {string} message 商品信息
|
||||
* @param {string} localOrderID 本地订单号
|
||||
*
|
||||
*/
|
||||
export async function applyOrderWX(localOrderID: string, price: number, message: string) {
|
||||
let data = {
|
||||
out_trade_no: localOrderID,
|
||||
body: message,//'商品简单描述',
|
||||
total_fee: price,//'订单金额(分)',
|
||||
}
|
||||
console.log('applyOrderWX :', data)
|
||||
let result = await api.getAppParams(data)
|
||||
// { appid: 'wx1d3cd5cd2f065358',
|
||||
// partnerid: '1272966801',
|
||||
// prepayid: 'wx12171213153829021f7cfe9413f0a20000',//微信订单号
|
||||
// package: 'Sign=WXPay',
|
||||
// noncestr: 'OohMyaHNK0iP77uh',
|
||||
// timestamp: '1620810734',
|
||||
// sign: '7A1B41C577E78602344D03DE4A2F7B0A' }
|
||||
console.log('applyOrderWX result: ', result)
|
||||
if (result.appid == config.appid && result.partnerid == config.mchid) {
|
||||
return { code: 1, data: result };
|
||||
}
|
||||
return { code: 0, data: JSON.stringify(result) };
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 微信查询支付结果
|
||||
*
|
||||
* @param {number} serverId 区Id
|
||||
* @param {string} roleId 角色Id
|
||||
* @param {string} localOrderID 本地订单号
|
||||
*
|
||||
*/
|
||||
export async function checkOrderWX(localOrderID: string) {
|
||||
let data = {
|
||||
out_trade_no: localOrderID,
|
||||
}
|
||||
console.log('checkOrderWX :', data)
|
||||
let result = await api.orderQuery(data)
|
||||
// { return_code: 'SUCCESS',
|
||||
// return_msg: 'OK',
|
||||
// result_code: 'SUCCESS',
|
||||
// mch_id: '1272966801',
|
||||
// appid: 'wx1d3cd5cd2f065358',
|
||||
// device_info: '',
|
||||
// trade_state: 'NOTPAY',
|
||||
// total_fee: '6',
|
||||
// out_trade_no: 'nMeyvwjJSmNXPmfGcr6kzalTR3jlp7jW',
|
||||
// trade_state_desc: '订单未支付',
|
||||
// nonce_str: 'aJevQOtSDgvNDbtQ',
|
||||
// sign: '955CE5F8AA2F046DA66E0A5DA56B338A' }
|
||||
console.log('checkOrderWX result: ', result)
|
||||
if (result.appid == config.appid && result.mch_id == config.mchid) {
|
||||
return { code: 1, data: result };
|
||||
}
|
||||
return { code: 0, data: JSON.stringify(result) };
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
|
||||
import { dispatch} from './dispatcher';
|
||||
import { dispatch } from './dispatcher';
|
||||
import { Session, Application } from 'pinus';
|
||||
|
||||
export function chat(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {
|
||||
export function chat(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let chatServers = app.getServersByType('chat');
|
||||
|
||||
if(!chatServers || chatServers.length === 0) {
|
||||
if (!chatServers || chatServers.length === 0) {
|
||||
cb(new Error('can not find chat servers.'));
|
||||
return;
|
||||
}
|
||||
@@ -14,10 +14,10 @@ export function chat(session: Session, msg: any, app: Application, cb: (err: Err
|
||||
cb(null, res.id);
|
||||
}
|
||||
|
||||
export function battle(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {
|
||||
export function battle(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let battleServers = app.getServersByType('battle');
|
||||
|
||||
if(!battleServers || battleServers.length === 0) {
|
||||
if (!battleServers || battleServers.length === 0) {
|
||||
cb(new Error('can not find battle servers.'));
|
||||
return;
|
||||
}
|
||||
@@ -43,10 +43,10 @@ export function battle(session: Session, msg: any, app: Application, cb: (err: E
|
||||
}
|
||||
|
||||
|
||||
export function guild(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {
|
||||
export function guild(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let guildServers = app.getServersByType('guild');
|
||||
|
||||
if(!guildServers || guildServers.length === 0) {
|
||||
if (!guildServers || guildServers.length === 0) {
|
||||
cb(new Error('can not find guild servers.'));
|
||||
return;
|
||||
}
|
||||
@@ -56,36 +56,36 @@ export function guild(session: Session, msg: any, app: Application, cb: (err: Er
|
||||
if (msg.args && msg.args.length > 0) {
|
||||
for (let arg of msg.args) {
|
||||
if (!arg.route) continue;
|
||||
|
||||
|
||||
if (['guild.gateActivityHandler.getGateActivity',
|
||||
'guild.gateActivityHandler.checkBattle', // 军团活动蛮夷入侵路由,按军团路由
|
||||
'guild.gateActivityHandler.action',
|
||||
'guild.gateActivityHandler.hitGate',
|
||||
'guild.gateActivityHandler.battleEnd',
|
||||
'guild.gateActivityHandler.debugGetPrivate',
|
||||
'guild.gateActivityHandler.debugStartActivity',
|
||||
'guild.gateActivityHandler.debugEndActivity'
|
||||
].indexOf(arg.route) !== -1) {
|
||||
rid = session.get('guildCode');
|
||||
'guild.gateActivityHandler.checkBattle', // 军团活动蛮夷入侵路由,按军团路由
|
||||
'guild.gateActivityHandler.action',
|
||||
'guild.gateActivityHandler.hitGate',
|
||||
'guild.gateActivityHandler.battleEnd',
|
||||
'guild.gateActivityHandler.debugGetPrivate',
|
||||
'guild.gateActivityHandler.debugStartActivity',
|
||||
'guild.gateActivityHandler.debugEndActivity'
|
||||
].indexOf(arg.route) !== -1) {
|
||||
rid = session.get('guildCode');
|
||||
} else if (['guild.cityActivityHandler.getCityStatus',
|
||||
'guild.cityActivityHandler.declareCity',
|
||||
'guild.cityActivityHandler.checkBattle',
|
||||
'guild.cityActivityHandler.hitGate',
|
||||
'guild.cityActivityHandler.battleEnd'
|
||||
].indexOf(arg.route) !== -1) {
|
||||
|
||||
'guild.cityActivityHandler.declareCity',
|
||||
'guild.cityActivityHandler.checkBattle',
|
||||
'guild.cityActivityHandler.hitGate',
|
||||
'guild.cityActivityHandler.battleEnd'
|
||||
].indexOf(arg.route) !== -1) {
|
||||
|
||||
if (arg.body.cityId) {
|
||||
rid = arg.body.cityId.toString();
|
||||
}
|
||||
} else if (['guild.raceActivityHandler.getRaceActivity',
|
||||
'guild.raceActivityHandler.join',
|
||||
'guild.raceActivityHandler.getRace',
|
||||
'guild.raceActivityHandler.useItem',
|
||||
'guild.raceActivityHandler.battleEnd',
|
||||
].indexOf(arg.route) !== -1) {
|
||||
rid = session.get('serverId').toString();
|
||||
'guild.raceActivityHandler.join',
|
||||
'guild.raceActivityHandler.getRace',
|
||||
'guild.raceActivityHandler.useItem',
|
||||
'guild.raceActivityHandler.battleEnd',
|
||||
].indexOf(arg.route) !== -1) {
|
||||
rid = session.get('serverId').toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,10 +93,10 @@ export function guild(session: Session, msg: any, app: Application, cb: (err: Er
|
||||
cb(null, res.id);
|
||||
}
|
||||
|
||||
export function activity(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {
|
||||
export function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let activityServers = app.getServersByType('activity');
|
||||
|
||||
if(!activityServers || activityServers.length === 0) {
|
||||
if (!activityServers || activityServers.length === 0) {
|
||||
cb(new Error('can not find activity servers.'));
|
||||
return;
|
||||
}
|
||||
@@ -105,14 +105,26 @@ export function activity(session: Session, msg: any, app: Application, cb: (err:
|
||||
cb(null, res.id);
|
||||
}
|
||||
|
||||
export function gm(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {
|
||||
export function gm(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let gmServers = app.getServersByType('gm');
|
||||
|
||||
if(!gmServers || gmServers.length === 0) {
|
||||
if (!gmServers || gmServers.length === 0) {
|
||||
cb(new Error('can not find gm servers.'));
|
||||
return;
|
||||
}
|
||||
|
||||
let res = dispatch(session.get('roleId'), gmServers);
|
||||
cb(null, res.id);
|
||||
}
|
||||
|
||||
export function order(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let orderServers = app.getServersByType('order');
|
||||
|
||||
if (!orderServers || orderServers.length === 0) {
|
||||
cb(new Error('can not find activity servers.'));
|
||||
return;
|
||||
}
|
||||
|
||||
let res = dispatch(session.get('roleId'), orderServers);
|
||||
cb(null, res.id);
|
||||
}
|
||||
Reference in New Issue
Block a user