最近刚完成一个商场小程序(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. Lodop打印小票

    使用Lodop打印小票 1.html页面 <head> <meta http-equiv="Content-Type" content="text/ht ...

  2. Seal 0.4 发布:软件供应链安全洞察更上一层楼!

    今天,我们很高兴宣布 Seal 0.4 已正式发布!在上一个版本中,Seal 完成了从单一产品到全链路平台的转变,通过全局视图帮助用户掌握软件开发生命周期各个环节的安全状况. 在 Seal 0.4 中 ...

  3. The Missing Semester - 第二讲 学习笔记

    第二讲 Shell 工具和脚本 课程视频地址: https://www.bilibili.com/video/BV1Vv411v7FR 本机学习使用平台:虚拟机ubuntu18.04.6 主题一:Sh ...

  4. Istio 升级后踩的坑

    背景 前段时间我们将 istio 版本升级到 1.12 后导致现有的应用监控有部分数据丢失(页面上显示不出来). 一个是应用基础信息丢失. 再一个是应用 JVM 数据丢失. 接口维度的监控数据丢失. ...

  5. jenkins简单安装及配置(Windows环境

    jenkins简单安装及配置(Windows环境) jenkins是一款跨平台的持续集成和持续交付.基于Java开发的开源软件,提供任务构建.持续集成监控的功能,可以使开发测试人员更方便的构建软件项目 ...

  6. JZOJ 3207.Orthogonal Anagram

    \(\text{Problem}\) 给出一个字符串,求经过重新排列的另一个字典序最小的字符串,满足:相同的位置上 原串与结果串的字符不同.不存在则输出空串. \(\text{Solution}\) ...

  7. JZOJ 2020.08.03【NOIP提高组】模拟 &&【NOIP2015模拟11.5】

    总结 又是一日爆炸 \(T1\) 不出所料报 \(0\) 了?! 题目 \(T1\) JZOJ 4315. Prime 暴力就好了?! 考场根本没想暴力 赛后发现暴力跑得贼快 只需二分一下组数的上界 ...

  8. IP地址后面/24/26/27/28/29/30网关数量分别是多少?如何计算?

    转载csdn: https://blog.csdn.net/jinfengyunIDC/article/details/112575286

  9. 跳板攻击之:SSH 隧道

    跳板攻击之:SSH 隧道 郑重声明: 本笔记编写目的只用于安全知识提升,并与更多人共享安全知识,切勿使用笔记中的技术进行违法活动,利用笔记中的技术造成的后果与作者本人无关.倡导维护网络安全人人有责,共 ...

  10. Word 设置脚注和尾注

    描述 脚注一般位于页面的底部,作为文档某处内容的注释.尾注一般位于文档的末尾,列出引文的出处等. 设置脚注和尾注 将光标移动到要插入脚注或尾注的地方,然后点击"引用"选项卡. 左边 ...