最近刚完成一个商场小程序(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. 2023.1.13 [网络流24题] 餐巾计划问题 LuoguP1251

    2023.1.13 今日完成的[餐巾计划问题],是一道最小费用最大流的模板题,本人太弱在第一次使用dinic + spfa 完成此题时,也出现了许多问题,在此总结和提醒. 大致题意 一个餐厅在相继的 ...

  2. 【学习日志】@NotNull注解不生效问题

    后端API需要接受fe传过来的参数,就必然涉及到参数校验. Spring提供了使用注解进行非法判断的引用(需要主动引入),继承自 spring-boot-starter-parent <depe ...

  3. 重学SpringBoot. step6 SpringBoot高级技巧

    SpringBoot高级技术 博客地址: step6 SpringBoot高级技巧 异步线程池 书上讲的是什么像异步操作那样,然后不需要等待. 问题是,不需要等待,但数据在生成的时候的时间并不能省. ...

  4. JWT拦截器与跨域问题

    本文参考: https://blog.csdn.net/csdn_x_w/article/details/108027940 我发现走的都是OPTIONS协议,然后JWT 却把OPTIONS拦截了,于 ...

  5. 接入jira OAuth权限流程

    如果要在自己的系统中操作jira的api完成这些单据的创建.审批等操作,就不得不要先完成jira的第三方授权,才能在第三方系统去做这些jira的操作. 首先必须在jira系统配置客户端的相关信息,须配 ...

  6. Centos7系统编译Hadoop3.3.4

    1.背景 最近在学习hadoop,此篇文章简单记录一下通过源码来编译hadoop.为什么要重新编译hadoop源码,是因为为了匹配不同操作系统的本地库环境. 2.编译源码 2.1 下载并解压源码 [r ...

  7. 深度学习-LSTM

    目录 前言 神经网络的历史和背景 循环神经网络的出现及其作用 LSTM在处理序列数据中的应用 LSTM的基本原理 LSTM的结构和原理 遗忘门.输入门.输出门的作用 LSTM的训练方法 代码 LSTM ...

  8. Computed 和 Watch 的区别

    1.computed计算属性: 作用:(1)解决模板中放入过多的逻辑会让模板过重且难以维护的问题.例如两个数据的拼接或字体颜色的判断. (2)它支持缓存,只有依赖的数据发生了变化,才会重新计算.例如模 ...

  9. Java处理正则匹配卡死(正则回溯问题)

    目录 背景 项目现场问题 问题跟踪 优化方案 处理正则问题 使用子线程来匹配正则实现 监控线程实现 最优选择方案 参考文章 正则匹配卡死怎么来的? 背景 背景:这次问题的背景是项目上遇到了,在使用正则 ...

  10. Apinto 网关 V0.11.1 版本发布,多协议互转,新增编码转换器,接入 Prometheus...

    憋了那么久,Eolink 旗下 Apinto 开源网关再次更新啦~ 一起来看看是否有你期待的功能! 1.协议转换功能上线 之前发布的 Apinto v0.10.0 已经支持了多协议的基本功能,实现多协 ...