Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createOrder use original parameters #7

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 67 additions & 38 deletions app/service/mp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ const crypto = require('crypto');
const jscode2sessionUri = 'https://api.weixin.qq.com/sns/jscode2session'; // 微信临时授权码
const tokenUri = 'https://api.weixin.qq.com/cgi-bin/token'; // 微信凭据
const msgSecCheck = 'https://api.weixin.qq.com/wxa/msg_sec_check'; // 微信敏感词
const sendMsgUri =
'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send'; // 微信服务通知
const sendMsgUri = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send'; // 微信服务通知
const payUri = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; // 微信统一下单
const wxacodeunlimitUri = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'; // 获取小程序码,适用于需要的码数量极多的业务场景

class MPService extends Service {

/**
* 登录凭证校验
* @param {String} code 临时授权码
* @return {Object} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/api/code2Session.html?search-key=jscode2session
*/
* 登录凭证校验
* @param {String} code 临时授权码
* @return {Object} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/api/code2Session.html?search-key=jscode2session
*/
async login(code) {
const {
appId,
Expand All @@ -31,10 +31,10 @@ class MPService extends Service {
}

/**
* 获取Token
* @return {Object} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/api/getAccessToken.html
*/
* 获取Token
* @return {Object} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/api/getAccessToken.html
*/
async getToken() {
const {
appId,
Expand All @@ -48,13 +48,40 @@ class MPService extends Service {
}

/**
* 加密数据解密算法
* @param {String} sessionKey sessionKey
* @param {String} encryptedData encryptedData
* @param {String} iv iv
* @return {Object} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
*/
* 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。
* @returns {Buffer}
* @see https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
*/
async getwxacodeunlimit(data) {
const tda = await this.getToken();
if (tda.errcode !== 0) {
return tda;
}
const token = tda.access_token;
const url = `${wxacodeunlimitUri}?access_token=${token}`;
const resp = await this.ctx.curl(url, {
method: 'POST',
contentType: 'json',
data: {
scene: data.scene || '.',
page: data.page || '',
width: data.width || 430,
auto_color: data.auto_color || false,
line_color: data.line_color || { r: 0, g: 0, b: 0 },
is_hyaline: data.is_hyaline || false,
},
});
return resp;
}

/**
* 加密数据解密算法
* @param {String} sessionKey sessionKey
* @param {String} encryptedData encryptedData
* @param {String} iv iv
* @return {Object} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
*/
async decryptData(sessionKey, encryptedData, iv) {
const {
appId,
Expand All @@ -79,11 +106,11 @@ class MPService extends Service {
}

/**
* 是否含有敏感内容
* @param {String} content 文本内容
* @return {Boolean} 是否敏感内容
* @see https://developers.weixin.qq.com/miniprogram/dev/api/msgSecCheck.html?search-key=msg_sec_check
*/
* 是否含有敏感内容
* @param {String} content 文本内容
* @return {Boolean} 是否敏感内容
* @see https://developers.weixin.qq.com/miniprogram/dev/api/msgSecCheck.html?search-key=msg_sec_check
*/
async checkIsSensitive(content) {
const token = await this.getToken();
const access_token = token.access_token;
Expand All @@ -99,11 +126,11 @@ class MPService extends Service {
}

/**
* 推送模板消息
* @param {Object} params 推送消息
* @return {Boolean} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html
*/
* 推送模板消息
* @param {Object} params 推送消息
* @return {Boolean} 微信返回的数据
* @see https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html
*/
async pushMessage(params) {
const body = {
touser: params.openid,
Expand All @@ -125,12 +152,13 @@ class MPService extends Service {
}

/**
* 统一下单
* @param {String} openid 目标用户
* @param {Object} data 推送消息
* @return {Object} 用于小程序发起支付
* @see https://api.mch.weixin.qq.com/pay/unifiedorder
*/
* 统一下单
* @param {String} openid 目标用户
* @param {Object} data 推送消息
* @return {Object} 用于小程序发起支付
* @see https://api.mch.weixin.qq.com/pay/unifiedorder
* @see https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
*/
async createOrder(openid, data) {
const {
ctx,
Expand Down Expand Up @@ -162,15 +190,16 @@ class MPService extends Service {
mchId,
} = app.config.mp;
const params = {
...data,
openid,
appid: appId,
mch_id: mchId,
nonce_str: service.sign.createNonceStr(),
out_trade_no: data.tradeNo || new Date().getTime(), // 内部订单号
total_fee: data.totalFee || 1, // 单位为分的标价金额
body: data.body || '未知产品-测试商品', // 应用市场上的APP名字-商品概述
out_trade_no: data.tradeNo || data.outTradeNo || data.trade_no || data.out_trade_no || new Date().getTime(), // 内部订单号
total_fee: data.totalFee || data.total_fee || 1, // 单位为分的标价金额
body: data.body || '未知产品-测试商品', // 应用市场上的APP名字-商品概述
spbill_create_ip: ctx.ip, // 支付提交用户端ip
notify_url: data.notifyUrl || '', // 异步接收微信支付结果通知
notify_url: data.notifyUrl || data.notify_url || '', // 异步接收微信支付结果通知
trade_type: 'JSAPI',
};
params.sign = service.sign.getPaySign(params); // 首次签名,用于验证支付通知
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-mp",
"version": "1.0.21",
"version": "1.0.22",
"description": "egg plugin for wechat",
"types": "./index.d.ts",
"eggPlugin": {
Expand Down Expand Up @@ -61,4 +61,4 @@
"homepage": "https://github.com/unclexiao/egg-mp#readme",
"author": "unclexiao <[email protected]>",
"license": "MIT"
}
}