nodejs+koa2实现微信小程序签名和请求支付(二)
废话不多说直接上代码:
const getTradeNo = function() {let date = new Date();let arr = [date.getFullYear(),((date.getMonth() + 1)>=10?(date.getMonth() + 1):'0'+(date.getMonth() + 1)),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds(),date.getMilliseconds(),Math.floor(Math.random() * 100000)]let orderNo = arr.join('');if (orderNo.length < 22) {let len = 22 - orderNo.length;orderNo = orderNo + Math.floor(Math.random() * len * 10).toString();}return orderNo;
}
生成随机字符串
// 随机字符
const getNonceStr = function() {let str = "";let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for (let i = 0; i< 32; i++) {str += possible.charAt(Math.floor(Math.random() * possible.length))}return str;
};
微信支付预签名
// 微信支付预签名
const getPrePaySign = function(appid, mchId, notifyUrl, productIntro, nonceStr, openId, tradeNo, ip, price, trade_type, apiKey) {let ret = {appid: appid,attach: tradeNo,body: productIntro,mch_id: mchId,nonce_str: nonceStr,notify_url: notifyUrl,openid: openId,out_trade_no: tradeNo,spbill_create_ip: ip,total_fee: price,trade_type: 'JSAPI'};let stringSignTemp = raw(ret, apiKey);let sign = md5(stringSignTemp).toUpperCase();return sign;
}
使用axios向微信发送支付请求
productIntro: 支付的产品信息,自定义就好
appid: 小程序appid
mchId::微信支付的商户号
apiKey::商户号的apikey
notifyUrl: 支付结果的通知地址,用来微信支付服务器向我们通知支付结果
const util = require('util')
const axios = require('axios')
const parser = require('fast-xml-parser');
let J2XParse = parser.j2xParser;
const { generateToken,getNonceStr,getPrePaySign,getTradeNo
} = require('../../../core/util')
const { V_User} = require('../../models/violin/v_user')
const { Order } = require('../../models/violin/v_order')
const j2xParser = new J2XParse();
class WXPlay {// 支付static async requestWXPay(wxInfo) {let tradeNo = getTradeNo();let nonceStr = getNonceStr();let productIntro = wxInfo.productIntro;let appid = global.config.violinWx.appid;let mchId = global.config.violinWx.mchId;let notifyUrl = global.config.violinWx.notifyUrl;let apiKey = global.config.violinWx.payApiKey;let signStr = getPrePaySign(appid, mchId, notifyUrl, productIntro, nonceStr, wxInfo.openId, tradeNo, wxInfo.ip, wxInfo.payAmount, 'JSAPI', apiKey);let xmlData = { xml: {appid: global.config.violinWx.appid,attach: tradeNo,body: productIntro,mch_id: global.config.violinWx.mchId,nonce_str: nonceStr,notify_url: global.config.violinWx.notifyUrl,openid: wxInfo.openId,out_trade_no: tradeNo,spbill_create_ip: wxInfo.ip,total_fee: wxInfo.payAmount,trade_type: 'JSAPI',sign: signStr}}let sendData = j2xParser.parse(xmlData);let wxUrl = global.config.violinWx.payUrlconst result = await axios({method: 'POST',url: wxUrl,headers: {'Content-Type': 'text/xml'},data: sendData});if (result.err) {return err;}if (result.data && parser.validate(result.data)) {let jsonObj = parser.parse(result.data);if (jsonObj.xml.return_code === 'SUCCESS' && jsonObj.xml.result_code === 'SUCCESS') {let order = {appid: global.config.violinWx.appid,orderNo: tradeNo,orderName: wxInfo.productIntro,orderType: wxInfo.orderType,userId: wxInfo.userId,mch_id: global.config.violinWx.mchId,payAmount: wxInfo.money,orderStatus: 0,prepay_id: jsonObj.xml.prepay_id,entriesId: wxInfo.entriesId}await Order.addOrder(order);}return jsonObj;}return result;}
}module.exports = {WXPlay
}
谢谢!
nodejs+koa2实现微信小程序签名和请求支付(二)相关推荐
- 基于Nodejs的心理咨询微信小程序的设计和实现
<基于Nodejs的心理咨询微信小程序的设计和实现>该项目采用技术Nodejs.mysql数据库 ,项目含有源码.论文.PPT.配套开发软件.软件安装教程.项目发布教程.核心代码介绍视频等 ...
- 微信小程序中使用JSAPI支付
微信小程序中使用JSAPI支付 在微信小程序中使用微信支付api[wx.requestPayment]需要传递以下字段 如何获取支付所需要的值 在微信小程序中使用微信支付api[wx.requestP ...
- 微信小程序服务商下子商户支付下单接口
微信小程序服务商下子商户支付下单流程 调用方法 <?php namespace app\index\controller; class WeixinPay extends Base { prot ...
- 微信小程序把玩(三十二)Image API
原文:微信小程序把玩(三十二)Image API 选择图片时可设置图片是否是原图,图片来源.这用的也挺常见的,比如个人中心中设置头像,可以与wx.upLoadFile()API使用 主要方法: wx. ...
- 【微信小程序提取公共请求数据】
[微信小程序提取公共请求数据] 在utils下设置一个http.js const url = 'https://api.shop.eduwork.cn'; const request = (path= ...
- 微信小程序的网络请求 —— 微信小程序教程系列(14)
网络请求,基本上是必须的环节之一. 小程序提供了wx.request(object),与开发者的服务器实现数据交互的一个很重要的api. 最简单的用法如下(以GET请求为例) <view bin ...
- 微信小程序从零开始开发步骤(二)创建小程序页面
上一章注册完小程序,添加新建的项目,大致的准备开发已经完成,本章要分享的是 要创建一个简单的页面了,创建小程序页面的具体几个步骤: 1. 在pages 中添加一个目录 选中page,右击鼠标,从硬盘打 ...
- 微信小程序开发POST请求
微信小程序开发POST请求 wx.request( { url: "http://op.juhe.cn/onebox/weather/query", header: { " ...
- 微信小程序云开发入门(二)-数据库详解
微信小程序云开发入门(二)-数据库详解 接上一篇:微信小程序云开发入门(一) 摘要: 因为微信小程序云数据库有点类似传统的关系型数据库,但又有所不同.所以刚入手的时候会有点困扰,经过一段时间的学习和摸 ...
最新文章
- python将scikit-learn自带数据集转换为pandas dataframe格式
- powershell创建iis站点、应用程序及应用程序池
- 台式机dp接口_台式机成就3471和战99,哪个更好?对比分析
- linux下文件描述符的介绍
- centos rpm 安装 perl_XtraBackup工具详解 Part 2 xtrabackup安装
- Angular platform-server.js 里动态创建 JavaScript 标签页的场景
- redis session java获取attribute_redis里的数据结构
- 利用数组实现栈java,用java编写出来:用数组实现一个栈
- (二)数据结构与算法-稀疏数组
- Java基础知识之变量与常量、数据类型、类型转换
- 对第三组博客的检查情况
- Silverlight socket组件
- 何謂 Raw Data ?
- C++中在堆区用new开辟空间
- 获取Google Advertising ID作为唯一识别码
- STM32实现按键控制继电器
- sosdp(高维前缀和)学习笔记
- Android安卓应用发布平台汇总
- ImmutableList hessian2序列化失败问题分析
- Java学习个人总结
热门文章
- XyplayerX4.0 影视解析源码+解析接口+安装步骤
- 阿里云查找或修改对应实例的远程连接密码
- 【操作篇】Excel中如何批量删除批注
- 《惢客创业日记》2019.04.25(周四)如何解决骚扰电话?
- 求和 矩阵迹的性质_怎么证明矩阵特征值的和等于矩阵的迹_
- centos7的scp命令_Linux命令-CentOS7安装scp命令,进行mac与Linux之间的文件上传下载...
- PC党福音,育碧五款游戏大作登场E3 2014
- 完美破解StartUML软件
- 6.lambda表达式
- html如何生成条形码,前端如何生成条形码---JsBarcode