sdk:37sdk发起支付接口

This commit is contained in:
luying
2021-11-09 11:53:00 +08:00
parent fc653b37e6
commit 173481a60c
9 changed files with 129 additions and 70 deletions

View File

@@ -6,6 +6,7 @@ import { UserOrderModel } from '../../../db/UserOrder';
import _ = require('underscore');
import { applyOrderWX, checkOrderWX } from '../../../services/pay/weixinPay';
import { applyOrderALI, checkOrderALI } from '../../../services/pay/aliPay';
import { applyOrder37 } from '../../../services/pay/37Pay';
import { makeOrder } from '../../../services/orderService';
import { addRechargeMoney } from '../../../services/activity/rechargeMoneyService';
import { ActivityModel, ActivityModelType } from '../../../db/Activity';
@@ -55,10 +56,21 @@ export class orderHandler {
let orderID = '';//平台订单号
let sdkOrderInfo = null;//客户端需要的平台订单信息
switch (payType) {
case PAY_TYPE.THREE_SEVEN:
{
let pay37Order = await applyOrder37(localOrderID, roleId, productInfo);
if (pay37Order.code == -1) {
console.log('37下单失败')
return pay37Order.resData;
}
sdkOrderInfo = pay37Order.data;
orderID = ''
break;
}
case PAY_TYPE.WX:
{
let weixinOrder = await applyOrderWX(localOrderID, price * 100, message);
if (weixinOrder.code == 0) {
if (weixinOrder.code == -1) {
console.log('微信下单失败')
return resResult(STATUS.APPLY_ORDER_ERROR);
}
@@ -69,7 +81,7 @@ export class orderHandler {
case PAY_TYPE.ALI:
{
let aliOrder = await applyOrderALI(localOrderID, price, message);
if (aliOrder.code == 0) {
if (aliOrder.code == -1) {
console.log('支付宝下单失败')
return resResult(STATUS.APPLY_ORDER_ERROR);
}

View File

@@ -0,0 +1,49 @@
import { SDK_37_CONST, STATUS } from "../../consts";
import { RoleModel, RoleType } from "../../db/Role";
import { UserModel } from "../../db/User";
import { LoginValidateData37 } from "../../domain/sdk";
import { DicRMB } from "../../pubUtils/dictionary/DicRMB";
import { nowSeconds } from "../../pubUtils/timeUtil";
import { resResult } from "../../pubUtils/util";
import { get37Md5Sign } from "../../pubUtils/sdkUtil";
/**
* 37支付下单
*
* @param {number} price 价格订单金额(分)
* @param {string} message 商品信息
* @param {string} localOrderID 本地订单号
*
*/
export async function applyOrder37(localOrderID: string, roleId: string, productInfo: DicRMB) {
let role = await RoleModel.findByRoleId(roleId);
let user = await UserModel.findUserByUid(role.userInfo.uid);
if(!user || !user.channelInfo || !(<LoginValidateData37>user.channelInfo).uid)
return {
code: -1, resData: resResult(STATUS.CHANNEL_INFO_NOT_FOUND)
}
let ext = '';
let body = {
game_id: SDK_37_CONST.GAME_ID,
uid: user.uid,
sid: role.serverId,
actor_id: role.roleId,
order_no: localOrderID,
money: productInfo.price.toFixed(2).toString(),
game_coin: role.gold,
product_id: productInfo.productID,
subject: productInfo.message,
time: nowSeconds(),
ext
}
let sign = get37Md5Sign(body, SDK_37_CONST.PAY_KEY);
return { code: 0, data: {
timestamp: nowSeconds(),
ext,
sign: sign
}};
}

View File

@@ -44,9 +44,9 @@ export async function applyOrderALI(localOrderID: string, price: number, message
// }
console.log('applyOrderALI result: ', result)
if (result.code == 0) {
return { code: 1, data: result.data };
return { code: 0, data: result.data };
}
return { code: 0, data: JSON.stringify(result) };
return { code: -1, data: JSON.stringify(result) };
}

View File

@@ -53,9 +53,9 @@ export async function applyOrderWX(localOrderID: string, price: number, message:
// 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: result };
}
return { code: 0, data: JSON.stringify(result) };
return { code: -1, data: JSON.stringify(result) };
}

View File

@@ -104,9 +104,10 @@ export enum FIRST_GIFT_STATE {
*/
export enum PAY_TYPE {
TEST = 0, // 测试
WX = 1, // 微信
ALI = 2, // 阿里
APPLE = 3, // 苹果
THREE_SEVEN = 1, // 37
WX = 2, // 微信
ALI = 3, // 阿里
APPLE = 4, // 苹果
}
/**

View File

@@ -445,5 +445,6 @@ export const STATUS = {
ACTIVITY_NO_PRODUCT: { code: 70011, simStr: '活动数据错误' },
ERROR_TYPE: { code: 70012, simStr: '未知活动类型' },
ORDER_PARAM_ERROR: { code: 70013, simStr: '订单参数错误' },
CHANNEL_INFO_NOT_FOUND: { code: 70014, simStr: '未找到玩家渠道数据' },
}

View File

@@ -259,7 +259,7 @@ export default class User extends BaseModel {
}
public static async findUserByUid(uid: number) {
const user: UserType = await UserModel.findOne({ uid }).select('uid tel').lean({ getters: true });
const user: UserType = await UserModel.findOne({ uid }).select('uid tel channelInfo').lean({ getters: true });
return user;
}

View File

@@ -1,6 +1,6 @@
import * as request from "request-promise";
import * as crypto from 'crypto'
import { BANTU_VID_ADDR, BANTU_VID_APP_KEY, HTTP_METHOD } from '../consts';
import { checkVidObjSign, get37Md5Sign, getVidObjSign } from "./sdkUtil";
// 通用请求http
export async function httpRequest(url: string, method: string, headers: any, body: any) {
@@ -25,38 +25,6 @@ export async function httpRequest(url: string, method: string, headers: any, bod
}
}
export function md5(str: string, treatStr?: (str: string) => string) {
if(treatStr) str = treatStr(str);
console.log('*****str', str)
return crypto.createHash('md5').update(str, 'utf8').digest("hex");
};
export function getMd5ObjSign(obj: any, joinMark: string, treatStr?: (str: string) => string) {
const str = [];
Object.keys(obj).sort().forEach((key) => {
if (key == 'sign') {
} else {
str.push(key + '=' + obj[key]);
}
});
if (str.length == 0) {
console.error('check obj', obj);
}
var strs = str.join(joinMark);
return md5(strs, treatStr);
}
export async function checkMd5Sign(obj: any, treatStr?: (str: string) => string) {
if (getMd5ObjSign(obj, '', treatStr) === obj.sign) {
return true;
}
console.warn('correct sign:', getMd5ObjSign(obj, '', treatStr), obj.sign);
return false;
}
/************** 厚土防沉迷接口 **************/
/**
* 在线报告 暂时不使用
@@ -129,36 +97,10 @@ export async function vidHttpRequest(addr: string, body: any) {
}
}
function getVidObjSign(body: any) {
return getMd5ObjSign(body, '', treatVid);
}
function checkVidObjSign(obj: any) {
return checkMd5Sign(obj, treatVid);
}
function treatVid(str) {
return 'vId' + str;
}
// 37sdk请求
function get37ObjSign(body: any, key: string) {
return getMd5ObjSign(body, '&', (str) => `${str}&${key}`);
}
export async function request37(url: string, body: any, key: string) {
// body = {
// clientid: 'abscddsssssss',
// pid: '37h5',
// game_id: 30,
// pst: 'MWNmY2pweWZ6dFRhZmdnZ1k4aHhINVUyZnFra1',
// time: 1583821808,
// c_game_id: 'com.a.test'
// }
// key = 'FkM619)t,P7E3yK#44q85)!Sm5cv8j'
body['sign'] = get37ObjSign(body, key);
body['sign'] = get37Md5Sign(body, key);
let result = await httpRequest(url, HTTP_METHOD.POST, {}, body);
console.log('******result', result)

View File

@@ -2,9 +2,47 @@ import { SDK_37_ADDR, SDK_37_CONST } from '../consts';
import { request37 } from './httpUtil';
import { nowSeconds } from './timeUtil';
import { LoginValidataReturn37 } from '../domain/sdk';
import * as crypto from 'crypto'
// 通用加密方法
export function md5(str: string, treatStr?: (str: string) => string) {
if(treatStr) str = treatStr(str);
console.log('*****str', str)
return crypto.createHash('md5').update(str, 'utf8').digest("hex");
};
export function getMd5ObjSign(obj: any, joinMark: string, treatStr?: (str: string) => string) {
const str = [];
Object.keys(obj).sort().forEach((key) => {
if (key == 'sign') {
} else {
str.push(key + '=' + obj[key]);
}
});
if (str.length == 0) {
console.error('check obj', obj);
}
var strs = str.join(joinMark);
return md5(strs, treatStr);
}
export async function checkMd5Sign(obj: any, treatStr?: (str: string) => string) {
if (getMd5ObjSign(obj, '', treatStr) === obj.sign) {
return true;
}
console.warn('correct sign:', getMd5ObjSign(obj, '', treatStr), obj.sign);
return false;
}
/************** 37sdk **************/
export function get37Md5Sign(body: any, key: string) {
return getMd5ObjSign(body, '&', (str) => `${str}&${key}`);
}
export async function loginValidate37(clientId: string, pst: string) {
let result: LoginValidataReturn37 = await request37(SDK_37_ADDR.LOGIN, {
clientid: clientId,
@@ -31,4 +69,20 @@ export async function loginValidata(channelType: string, clientId: string, pst:
export function getChannelId(channelType: string, uid: number|string) {
return `${channelType}: ${uid}`;
}
/********* 厚土防沉迷 *********/
export function getVidObjSign(body: any) {
return getMd5ObjSign(body, '', treatVid);
}
export function checkVidObjSign(obj: any) {
return checkMd5Sign(obj, treatVid);
}
function treatVid(str) {
return 'vId' + str;
}