微信小程序支付微信小程序+node服务支付爬坑v2

微信小程序支付微信小程序+node服务支付爬坑v2

2023年7月2日发(作者:)

微信⼩程序⽀付微信⼩程序+node服务⽀付爬坑v2⼀、前端⼩程序登录及⽀付请求和唤起⽀付界⾯// t {request} = require('./assets/js/utils')// ({ onLaunch() { // 展⽰本地存储能⼒ const logs = rageSync('logs') || [] t(()) rageSync('logs', logs) // 登录 ({ success: res => { (this) // 发送 到后台换取 openId, sessionKey, unionId if () { request('/wx/login', {code: }, 'POST').then(result => { (result) Data = {...Data, ...} }) } } }) }, globalData: { userInfo: null }})wxml 请使⽤1.4.4及以上版本基础库 payment/// pages/payment/// 获取应⽤实例const app = getApp()const {request} = require('../../assets/js/utils')Page({ /** * 页⾯的初始数据 */ data: { hasUserInfo: false, canIUse: e('rInfo'), canIUseGetUserProfile: false, canIUseGetUserProfile: false, userInfo: '' }, // 第⼀种获取⽤户信息⽅法 getUserProfile(e) { // 推荐使⽤rProfile获取⽤户信息,开发者每次通过该接⼝获取⽤户个⼈信息均需⽤户确认,开发者妥善保管⽤户快速填写的头像昵称,避免重复弹窗 rProfile({ desc: '展⽰⽤户信息', // 声明获取⽤户个⼈信息后的⽤途,后续会展⽰在弹窗中,请谨慎填写 success: (res) => { (res) // 存储⽤户信息 rageSync('userInfo', ify(fo)); a({ userInfo: fo, hasUserInfo: true }) } }) }, // 第⼆种获取⽤户信息⽅法 getUserInfo(e) { // 不推荐使⽤getUserInfo获取⽤户信息,预计⾃2021年4⽉13⽇起,getUserInfo将不再弹出弹窗,并直接返回匿名的⽤户个⼈信息 (e) // 存储⽤户信息 rageSync('userInfo', ify(fo)); a({ userInfo: fo, hasUserInfo: true }) }, // ⽀付事件 payment() { const params = { openid: , total: 0.01, orderCode: new Date().toLocaleString().replace(/[^d]/g, '') } request('/wx/pay/unifiedorder', params, "POST").then(res => { const {nonceStr, timeStamp, signType, paySign} = const pack = e tPayment({ nonceStr, timeStamp, package: pack, signType, paySign, success(res) { (res) }, fail(res) { (res) }, complete(res){ (res) } }) }) }, pageInit() { if (rProfile) { a({ canIUseGetUserProfile: true }) } // 如果缓存不存在⽤户信息时暂时获取⽤户信息按钮 if (fo || rageSync('userInfo')) { const userInfo = fo || (rageSync('userInfo')) a({ hasUserInfo: true, userInfo }) } } request封装t = function (requestMapping, data, requestWay, contentType) { ading({ title: '请稍后', }) return new Promise(function (resolve, reject) { ('请求中。。。。。') t({ url: baseUrl + requestMapping, data: data, header: { 'content-type': contentType || "application/x-www-form-urlencoded" // 默认值 }, timeout: 3000, method: requestWay, success(res) { //(res) if (s == false || Code == 404) { reject(res) } else { resolve(res) } }, fail: (e) => { ast({ title: '连接失败', icon: 'none' }) }, complete: () => { ading() } }) })}⼆、node服务端我这⾥使⽤的是eggjs,使⽤koa,express同理'use strict';/** * @param {ation} app - egg application */s = app => { const { router, controller } = app; ('/wx/login', ); ('/wx/pay/unifiedorder', dorder);};Controller'use strict';const { Controller } = require('egg');class WxController extends Controller { // 登录 async login() { const { ctx } = this; = await () } // 统⼀下单 async unifiedorder(){ const { ctx } = this; = await dorder() }}s = WxController;Service'use strict';const { Service } = require('egg');const appid = 'wx5591*****e47'; // ⼩程序idconst secret = '670c7*****1a7de971c3'; // ⼩程序secretconst mch_id = '1607****01'; // 商户idconst mch_key = '52052*****2522ghi52352'; // 商户keyclass Wx extends Service {// 登录获取openid,session_key并返回给前端 async login() { const { ctx } = this; const { code } = ; const url = `/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`; const res = await (url, { method: 'POST', dataType: 'json', }); (res, ); return s({ ctx, res: }); } async unifiedorder() { const { ctx } = this; // ⾃⼰封装的微信⽀付⼯具 const wxpayUitls = itls; const { openid, total, orderCode } = ; const url = '/pay/unifiedorder'; // params 参数参考地址:/wiki/doc/api/wxa/wxa_?chapter=9_1&index=1 // 所有能使⽤到到的参数都罗列了,根据实际需求⾃⼰选择

const params = { appid, mch_id, // ⾃定义参数,可以为终端设备号(门店号或收银设备ID),PC⽹页或公众号内⽀付可以传"WEB" // device_info: '', // 随机字符串,长度要求在32位以内。推荐随机数⽣成算法 nonce_str: NonceStr(), // 通过签名算法计算得出的签名值,详见签名⽣成算法 sign: '', // 签名类型,默认为MD5,⽀持HMAC-SHA256和MD5。 // sign_type: 'MD5', // 商品简单描述,该字段请按照规范传递,具体请见参数规定 body: '⽀付测试', // 商品详细描述,对于使⽤单品优惠的商户,该字段必须按照规范上传,详见“单品优惠参数说明” // detail: '', // 附加数据,在查询API和⽀付通知中原样返回,可作为⾃定义参数使⽤。 // attach: '', // 商户系统内部订单号,要求32个字符内,只能是数字、⼤⼩写字母_-|*且在同⼀个商户号下唯⼀。详见商户订单号 out_trade_no: orderCode, // 符合ISO 4217标准的三位字母代码,默认⼈民币:CNY,详细列表请参见货币类型 // 符合ISO 4217标准的三位字母代码,默认⼈民币:CNY,详细列表请参见货币类型 fee_type: 'CNY', // 订单总⾦额,单位为分,详见⽀付⾦额 total_fee: ey(total), // ⽀持IPV4和IPV6两种格式的IP地址。调⽤微信⽀付API的机器IP spbill_create_ip: '192.168.43.187' || , // 订单⽣成时间,格式为yyyyMMddHHmmss,如2009年12⽉25⽇9点10分10秒表⽰为20。其他详见时间规则 // time_start: '', // 订单失效时间,格式为yyyyMMddHHmmss,如2009年12⽉27⽇9点10分10秒表⽰为20。 // 订单失效时间是针对订单号⽽⾔的,由于在请求⽀付的时候有⼀个必传参数prepay_id只有两⼩时的有效期, // 所以在重⼊时间超过2⼩时的时候需要重新请求下单接⼝获取新的prepay_id。其他详见时间规则 // 建议:最短失效时间间隔⼤于1分钟 // time_expire: '', // 订单优惠标记,使⽤代⾦券或⽴减优惠功能时需要的参数,说明详见代⾦券或⽴减优惠 // goods_tag: '', // 异步接收微信⽀付结果通知的回调地址,通知url必须为外⽹可访问的url,不能携带参数。 notify_url: '', // ⼩程序取值如下:JSAPI,详细说明见参数规定 trade_type: 'JSAPI', // trade_type=NATIVE时,此参数必传。此参数为⼆维码中包含的商品ID,商户⾃⾏定义。 // product_id: '', // 上传此参数no_credit--可限制⽤户不能使⽤信⽤卡⽀付 // limit_pay: 'no_credit', // trade_type=JSAPI,此参数必传,⽤户在商户appid下的唯⼀标识。openid如何获取,可参考【获取openid】。 openid, // Y,传⼊Y时,⽀付成功消息和⽀付详情页将出现开票⼊⼝。 // 需要在微信⽀付商户平台或微信公众平台开通电⼦发票功能,传此字段才可⽣效 // receipt: '', // Y-是,需要分账 // N-否,不分账 // 字母要求⼤写,不传默认不分账 // profit_sharing: '', // 该字段常⽤于线下活动时的场景信息上报,⽀持上报实际门店信息, // 商户也可以按需求⾃⼰上报相关信息。该字段为JSON对象数据, // 对象格式为{"store_info":{"id": "门店ID","name": "名称","area_code": "编码","address": "地址" }} , // 字段详细说明请点击⾏前的+展开 // scene_info: '' }; //动态⽣成统⼀下单需要的签名MD5字符串 = Sign(params, mch_key); const res = await (url, { method: 'POST', dataType: 'text/xml', // 注意接⼝数据类型 data: XML(params) //动态⽣成的xml }); // ('统⼀下单结果:', res, 'data:', ng()); // 解析统⼀下单后返回的xml const {xml} = await XML(ng()) const r={ appId:appid, timeStamp:().toString(), //注意类型String nonceStr:_str, package:'prepay_id='+_id, signType:'MD5' } // paySign:Sign(r,mch_key) 动态⽣成tPayment使⽤的签名paySign MD5字符串 return s({ctx,res:{...r,paySign:Sign(r,mch_key)}}) }}s = Wx;注意事项统⼀下单 签名规则将统⼀下单所需的所有的参数进⾏签名计算(空值,sgin除外)tPayment 签名规则参与签名计算的参数有:appId,timeStamp,nonceStr,package,signType,注意 timeStamp转字符串。wxpayUitls⼯具const crypto = require('crypto');const Xml2 = require('xml2js');const MD5 = require('md5');itls = { //把⾦额转为分 getmoney: function(money) { return parseFloat(money) * 100; }, // 随机字符串产⽣函数 createNonceStr: function() { return () .toString(36) .substr(2, 15) .toUpperCase(); }, // 时间戳产⽣函数 createTimeStamp: function() { return parseInt(new Date().getTime() / 1000) + ''; }, // 动态⽣成签名⽅法 createSign: function(params, mchkey) { let str = raw(params); str += `&key=${mchkey}`; ('str=====', str); // 第⼀种⽅法 // return MD5(str).toUpperCase() // 第⼆种⽅法 return Hash('md5') .update(str, 'utf8') .digest('hex') .toUpperCase(); }, // 根据对象⽣成xml createXML: function(params) { // let xml = ``; // (params) // .map(key => { // xml += `<${key}>${params[ key ]}`; // }); // xml += ``; // return xml; const builder = new r(); return bject(params); }, // 解析xml parserXML: function(xml) { const Parser = new ({ explicitArray: false, ignoreAttrs: false }); return new Promise((resolve, reject) => { tring(xml, function(err, result) { if (err) reject(err); resolve(result); }); }); }};订单查询,关闭,申退,查询退款等后续流程 等爬完之后再更新!

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688278858a112427.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信