最近刚完成一个商场小程序(http://market.zhenzikj.com/detail/121.html), 使用到了微信支付功能,其中遇到了很多的抗,所以,我把支付这块摘出来,以免大家少走弯路。

demo小程序端很简单,就是一个页面:

js代码:

//支付
pay: function(e){
var that = this;
if(that.data.number == ''){
wx.showToast({
title: '请填写支付金额!',
icon: 'none',
duration: 2000
})
return;
}
util.getOpenid(function(openid){
wx.request({
url: app.globalData.baseUrl + '/pay/pay.html',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'POST',
data: {
totalFee: that.data.number,
openid: openid
},
success (res) {
var data = res.data.data;
wx.requestPayment(
{
'timeStamp': data.timeStamp,
'nonceStr': data.nonce_str,
'package': 'prepay_id='+data.prepay_id,
'signType': 'MD5',
'paySign': data.sign,
'success':function(res){
wx.showToast({
title: '支付成功',
icon: 'none',
duration: 2000
})
},
'fail':function(res){
wx.showToast({
title: '支付失败',
icon: 'none',
duration: 2000
})
}
})
}
})
});

  

java代码:

统一下单接口

/**
* 支付(api)
*/
@RequestMapping(value="/pay")
@ResponseBody
public ResultInfo<Object> pay(
HttpServletRequest request,
double totalFee,
String openid){
try {
final String appId = CustomPropertyConfigurer.getProperty("weixin.mini.appid");
final String key = CustomPropertyConfigurer.getProperty("weixin.mini.key");
final String mch_id = CustomPropertyConfigurer.getProperty("weixin.mini.mch_id");
final String notify_url = CustomPropertyConfigurer.getProperty("weixin.mini.notify_url");
final String ip = CustomPropertyConfigurer.getProperty("ip");
//out_trade_no是自定义的系统内部订单号
String out_trade_no = new DateTime().toString("yyyyMMddHHmmss") + String.valueOf((int)((Math.random()*9+1)*1000));
Order order = new Order();
order.setAppid(appId);
order.setMch_id(mch_id);
order.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));
order.setBody("下单-支付");
order.setOut_trade_no(out_trade_no);
order.setTotal_fee((int)(totalFee * 100));//支付金额(分)
order.setSpbill_create_ip(ip);
order.setNotify_url(notify_url);
order.setTrade_type("JSAPI");
order.setSign_type("MD5");
order.setOpenid(openid);
//生成签名
String sign = Signature.getSign(order, key);
order.setSign(sign); String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order, false, null, null);
System.out.println(result);
L.info("---------下单返回:"+result);
XStream xStream = new XStream();
xStream.ignoreUnknownElements();
xStream.alias("xml", OrderReturnInfo.class);
OrderReturnInfo returnInfo = (OrderReturnInfo)xStream.fromXML(result);
if(returnInfo.getResult_code().equals("SUCCESS") && returnInfo.getReturn_code().equals("SUCCESS")){
//下单成功,此处是你的业务代码
System.out.print("下单成功");
} //生成签名
final String timeStamp = String.valueOf(System.currentTimeMillis()/1000);
sign = "appId="+appId+"&nonceStr="+order.getNonce_str()+"&package=prepay_id="+returnInfo.getPrepay_id()+"&signType=MD5&timeStamp="+timeStamp+"&key="+key;
sign = MD5.MD5Encode(sign).toUpperCase();
JSONObject jsonObject = new JSONObject();
jsonObject.put("nonce_str", order.getNonce_str());
jsonObject.put("prepay_id", returnInfo.getPrepay_id());
jsonObject.put("sign", sign);
jsonObject.put("timeStamp", timeStamp);
return new ResultInfo<Object>(0, jsonObject);
} catch (Exception e) {
e.printStackTrace();
L.error("-------------", e);
}
return new ResultInfo<Object>(1000, "未知错误");
}

  

申请退款接口

/**
* 申请退款
*/
@RequestMapping(value="/apply")
@ResponseBody
public Object apply(
HttpServletRequest request,
double refundFee,
String out_trade_no){
try {
final String appId = CustomPropertyConfigurer.getProperty("weixin.mini.appid");
final String mch_id = CustomPropertyConfigurer.getProperty("weixin.mini.mch_id");
final String refund_notify_url = CustomPropertyConfigurer.getProperty("weixin.mini.refund_notify_url");
final String key = CustomPropertyConfigurer.getProperty("weixin.mini.key");
WxRefund wxRefund = new WxRefund();
wxRefund.setAppid(appId); wxRefund.setMch_id(mch_id);
wxRefund.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));
wxRefund.setOut_trade_no(out_trade_no);
wxRefund.setOut_refund_no(out_trade_no);
wxRefund.setRefund_fee((int)(refundFee*100));
wxRefund.setTotal_fee((int)(refundFee*100));
wxRefund.setNotify_url(refund_notify_url);
//生成签名
String sign = Signature.getSign(wxRefund, key);
wxRefund.setSign(sign); //获取证书
String certFile = request.getServletContext().getRealPath("/") + "cert/apiclient_cert.p12";
String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/secapi/pay/refund", wxRefund, true, mch_id, certFile);
L.info("---------退款返回:"+result);
System.out.println(result);
XStream xStream = new XStream();
xStream.ignoreUnknownElements();
xStream.alias("xml", RefundReturnInfo.class);
RefundReturnInfo returnInfo = (RefundReturnInfo)xStream.fromXML(result);
if(returnInfo.getResult_code().equals("SUCCESS") && returnInfo.getReturn_code().equals("SUCCESS")){
return true;
}
return new ResultInfo<Object>(0, null);
} catch (Exception e) {
e.printStackTrace();
L.error("------------------------", e);
}
return new ResultInfo<Object>(1000, "未知错误");
}

  

完整代码下载: http://market.zhenzikj.com/detail/123.html

小程序微信支付完整demo,包含退款的更多相关文章

  1. 微信小程序 微信支付

    微信小程序前端自处理: //时间戳 timeStamp() { return parseInt(new Date().getTime() / 1000) + '' }, //随机数 randomStr ...

  2. 微信小程序------微信支付模块

    最近项目涉及到小程序开发:需要进行微信支付模块,接下来通过叙述,记录一下微信小程序中微信支付模块的开发,以便日后翻阅和使用. 学习指南----------微信支付开发文档:https://pay.we ...

  3. TP5调用小程序微信支付,回调,在待支付中再次调用微信支付

    1,必须要有 $mch_id $key $appid这三个值,是需要去申请的,我是直接用公司的2,购买商品订单号用户openid统一下单名称商品价格(必须以分为单位,调起微信支付)服务器的ip地址(没 ...

  4. 微信小程序微信支付的一些坑

    使用的是Node.js作为后端 统一下单: appid:这里的appid是调起微信支付的appid mch_id:商户号,需要注意的是商户号要与appid对应 nonce_str:Math.rando ...

  5. 小程序微信支付(UNIAPP+第三方SDK:binarywang)

    小程序支付流程图说明(UNIAPP+第三方SDK:binarywang) 说明:小程序为UNI-APP开发,使用的第三方微信支付SDK为binarywang提供的,此SDK对微信公众号.小程序.微信各 ...

  6. 微信小程序——微信支付

    这个讲起来也就比较麻烦一点,因为需要的不仅仅是咱们代码上的技术,嘿嘿! 先整理一下思路.如果想做微信支付: 1.现有一个公司账户(非个人账户),并且实名认证过的. 2.微信号 必须开通微信支付功能. ...

  7. 微信小程序微信支付流程

    1.小程序调用wx.login获取登录凭证code wx.login(无请求参数)返回code(有效期5分钟) wx.login({ success:function(res){ //get res. ...

  8. 【微信小程序】调起微信支付完整demo

    微信小程序调用微信支付接口 https://blog.csdn.net/u012667477/article/details/80940578

  9. .NET开发微信小程序-微信支付

    前台MD5加密代码 /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algor ...

  10. Taro/JS/H5/小程序:纯前端解决小程序微信支付统一下单和调起支付

    这个文章不会说具体0到1的代码流程,我会着重讲几个问题的解决 准备以下依赖 "md5": "^2.2.1", "xml-js": " ...

随机推荐

  1. python线程池等待全部任务结束再继续

    import json import time from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED impor ...

  2. 用Redis实现延迟队列,我研究了两种方案,发现并不简单

    大家好,我是三友~~ 背景 前段时间有个小项目需要使用延迟任务,谈到延迟任务,我脑子第一时间一闪而过的就是使用消息队列来做,比如RabbitMQ的死信队列又或者RocketMQ的延迟队列,但是奈何这是 ...

  3. 联邦GNN综述与经典算法介绍

    作者:京东科技 李杰 联邦学习和GNN都是当前AI领域的研究热点.联邦学习的多个参与方可以在不泄露原始数据的情况下,安全合规地联合训练业务模型,目前已在诸多领域取得了较好的结果.GNN在应对非欧数据结 ...

  4. ECharts 饼图切换数据源bug 开始没数据显示 切换或刷新后显示

    1.出现问题原因 一个饼图,右上方两个按钮分别为今天和本月,分别调用不同接口控制,点击则调用不同接口同时饼图绑定数据源刷新:出现此问题原因点击今日按钮有一个饼图区域形没有数据不显示,对应数据值比例都没 ...

  5. ES6的Promise用法

    一.是什么: promise是异步编程的一种解决方案,它是一个对象,可以获取异步操作的信息,它的出现改善了异步编程,避免了地狱回调,它比传统的解决方案回调函数和事件更合理和更强大 二.promise的 ...

  6. ctfshow_web入门 sql注入(web171~248)

    sql注入 这是算是学习+做题+记录的一个笔记吧,而且基本都是看着Y4师傅的博客做的 由于是做过sqli靶场,所以这个就记录快点了.如果靶场没遇到的,也会做笔记. union 联合注入 web171 ...

  7. 音频处理库:pydub与ffmpeg

    一句话简介:pydub--音频处理库:ffmpeg--音视频编解码工具. 一.  安装 安装pydub pip install pydub pip install ffprobe 安装ffmpeg m ...

  8. 51nod 1675.序列变换

    序列变换 题目描述 \(lyk\) 有两序列 \(a\) 和 \(b\). \(lyk\) 想知道存在多少对 \(x,y\),满足以下两个条件. \(1:\gcd(x,y)=1\). \(2:a_{b ...

  9. JZOJ 2937. 【NOIP2012模拟8.9】监听还原

    题面 分析 注意读题 然后显然字符串哈希 \(Code\) #include<cstdio> #include<cstring> using namespace std; ty ...

  10. C#/.net程序调用python

    C#/.net程序调用python C#的优势在于window下的开发,不仅功能强大而且开发周期短.而python则有众多的第三方库,可以避免自己造轮子,利用C#来做界面,而具体实现使用python来 ...