@Controller
public class UserPayToMerchantController {

public static final String appid="*********";//开发者appid

public static final String mch_id="********";//商户号

public static final String key="*****";//设置的支付密钥

public static final String secret="*********";

public static final String payUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";//统一下单的url

@Autowired
private UserPayToMerchantService userPayToMerchantService;

@Autowired
private UserService userService;

public static Logger log=Logger.getLogger(UserPayToMerchantController.class);

@RequestMapping("getPrepayId")
@ResponseBody
public Result getPrepayId(PrepayIdPoJo prepayIdPoJo,HttpServletRequest request) throws IOException, NoSuchAlgorithmException, DocumentException{
log.info("有充值数据其中,用户的名称="+prepayIdPoJo.getNickName()+"用户的openid="+prepayIdPoJo.getOpenid()+"用户的充值的金额="+prepayIdPoJo.getMoney());
//关键参数判断
if(prepayIdPoJo.getOpenid()==null||prepayIdPoJo.getOpenid().equals("")){
return new Result(500,"openid这个关键参数为空",null,null,null);
}
if(prepayIdPoJo.getNickName()==null||prepayIdPoJo.getNickName().equals("")){
return new Result(500,"nickName这个关键参数为空",null,null,null);
}else{
prepayIdPoJo.setNickName(new String(prepayIdPoJo.getNickName().getBytes("ISO8859-1"),"utf-8"));
}
if(prepayIdPoJo.getMoney()==null||prepayIdPoJo.getMoney().equals("")){
return new Result(500,"money这个关键参数为空",null,null,null);
}
try{
String spbill_create_ip=request.getRemoteAddr();
StringBuilder sb=new StringBuilder();
sb.append("<xml>");
sb.append("<appid>"+appid+"</appid>");
sb.append("<mch_id>"+mch_id+"</mch_id>");
sb.append("<device_info>WEB</device_info>");
String timeStamp=String.valueOf(new Date().getTime());
sb.append("<nonce_str>"+timeStamp+"</nonce_str>");
sb.append("<body>百度-红包</body>");
String out_trade_no=String.valueOf(new Date().getTime());
sb.append("<out_trade_no>"+out_trade_no+"</out_trade_no>");
sb.append("<total_fee>"+(int)((double)prepayIdPoJo.getMoney())*100+"</total_fee>");
sb.append("<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>");
sb.append("<notify_url><![CDATA[https://wx.ricksha.net/wxgetredpacket/userReceiver]]></notify_url>");
sb.append("<trade_type>JSAPI</trade_type>");
sb.append("<openid>"+prepayIdPoJo.getOpenid()+"</openid>");
String stringA="appid="+appid+"&body=百度-红包&device_info=WEB"+"&mch_id="+mch_id+"&nonce_str="+timeStamp+
"&notify_url=https://wx.ricksha.net/wxgetredpacket/userReceiver"+"&openid="+prepayIdPoJo.getOpenid()+"&out_trade_no="+out_trade_no+
"&spbill_create_ip="+spbill_create_ip+"&total_fee="+(int)((double)prepayIdPoJo.getMoney())*100+"&trade_type=JSAPI";
String stringSignTemp=stringA+"&key="+key;
String sign=MD5Util.MD5Encode(stringSignTemp, "utf-8").toUpperCase();
sb.append("<sign>"+sign+"</sign>");
sb.append("</xml>");
String str=sb.toString();
//执行统一下单

//这里需要一个工具类HttpClientUtil,字符串需要是utf-8的否则会出现微信的官方校验器通过,但是返回结果是----签名错误
String resultXml=HttpClientUtil.doPosts(payUrl, str);

//这里需要一个工具类XmlUtil
Map<String,String> map=XmlUtil.parseXmlText(resultXml);

//这里需要一个工具类JsonUtils
String mapStr=JsonUtils.objectToJson(map);
PrepayIdPoJo pojo=(PrepayIdPoJo) JsonUtils.jsonToPojo(mapStr, PrepayIdPoJo.class);
String stringB="appId="+appid+"&nonceStr="+timeStamp
+"&package=prepay_id="+pojo.getPrepay_id()
+"&signType=MD5&timeStamp="+timeStamp+"&key="+key;

//md5的工具类,这里是需要utf-8格式
String paySign=MD5Util.MD5Encode(stringB, "utf-8").toUpperCase();

//这是一个返回数据封装的一个对象
PayPoJo pay=new PayPoJo();
pay.setTimeStamp(timeStamp);
pay.setNonceStr(timeStamp);
pay.setPack("prepay_id="+pojo.getPrepay_id());
pay.setSignType("MD5");
pay.setPaySign(paySign);
String payStr=JsonUtils.objectToJson(pay);
return new Result(200,"成功",payStr,null,null);
}catch(Exception e){
e.printStackTrace();
log.info("充值数据异常,用户的名称="+prepayIdPoJo.getNickName()+"用户的openid="+prepayIdPoJo.getOpenid()+"用户的充值的金额="+prepayIdPoJo.getMoney());
}
return null;
}
}

//以上工具类请在个人博客上寻找

微信小程序支付的更多相关文章

  1. 微信小程序支付步骤

    http://blog.csdn.net/wangsf789/article/details/53419781 最近开发微信小程序进入到支付阶段,一直以来从事App开发,所以支付流程还是熟记于心的.但 ...

  2. 微信小程序支付及退款流程详解

    微信小程序的支付和退款流程 近期在做微信小程序时,涉及到了小程序的支付和退款流程,所以也大概的将这方面的东西看了一个遍,就在这篇博客里总结一下. 首先说明一下,微信小程序支付的主要逻辑集中在后端,前端 ...

  3. Java 后端微信小程序支付demo (网上说的坑里面基本上都有)

    Java 后端微信小程序支付 一.遇到的问题 1. 商户号该产品权限未开通,请前往商户平台>产品中心检查后重试 2.签名错误 3.已经调起微信统一下单接口,可以拿到预支付ID,但是前端支付的时候 ...

  4. php对接微信小程序支付

    前言:这里我就假装你已经注册了微信小程序,并且基本的配置都已经好了.注: 个人注册小程序不支持微信支付,所以我还是假装你是企业或者个体工商户的微信小程序,其他的商户号注册,二者绑定,授权,支付开通,就 ...

  5. 微信小程序支付遇到的坑

    1,微信公众号支付和微信小程序支付有差异 微信公众号:可以直接跳转走h5的微信支付 微信小程序:在测试环境.沙箱环境使用微信公众号的跳转支付没有问题,在线上存在支付异常 最后商讨的解决方法 openi ...

  6. 微信小程序支付接入注意点

    一.微信支付后台服务器部署 服务器采用ubuntu16.04 + php7.0 + apache2.0. 微信支付后台服务使用了curl 和 samplexml ,因此php.ini配置中必须开启这两 ...

  7. 微信小程序支付开发之申请退款

    微信小程序支付跟微信公众号支付类似,这里不另做记录,如果没有开发过支付,可以查看我关于微信支付的文章 重点记录微信小程序申请退款开发过程中遇到一些坑. 退款接口比支付接口接口多了一个 双向证书 证书介 ...

  8. 微信小程序支付接入实战

    1. 微信小程序支付接入实战 1.1. 需求   最近接到一个小程序微信支付的需求,需要我写后台支持,本着能不自己写就不自己写的cv原则,在网上找到了些第三方程序,经过尝试后,最后决定了这不要脸作者的 ...

  9. 微信小程序支付异常:requestPayment:fail no permission

    今天在调试微信小程序支付时碰到了这个问题,支付参数都正常生成了,在调用 wx.requestPayment 进行支付时遇到了这个报错,查了一下发现是开发者工具中 AppID 写错了,用的 AppID ...

  10. SpringBoot2.0微信小程序支付多次回调问题

    SpringBoot2.0微信小程序支付多次回调问题 WxJava - 微信开发 Java SDK(开发工具包); 支持包括微信支付.开放平台.公众号.企业微信/企业号.小程序等微信功能的后端开发. ...

随机推荐

  1. Linux第一篇【介绍、安装Ubuntu、基本目录结构】

    Linux介绍 Linux:不管是不是我们这些学编程的都肯定会听说过这么一个系统,一般地,我们在PC端都是用Windows操作系统,那我们学习Linux操作系统有什么用呢??? 由于我们的JAVAEE ...

  2. python实例编写(6)--引入unittest测试框架,构造测试集批量测试(以微信统一管理平台为例)

    ---恢复内容开始--- 一.python单元测试实例介绍 unittest框架又叫PyUnit框架,是python的单元测试框架. 先介绍一个普通的单元测试(不用unittest框架)的实例: 首先 ...

  3. Hibernate @Embeddable注释

    在hibernate中实现自定义类型,只要实现UserType接口即可或者以Component的形式提供.JPA的@Embedded注释可以在你的Entity中使用一般的Java对象,此对象需要用@E ...

  4. junit initializationError和找不到或无法加载主类

    今天在做Junit测试的时候 出现了initialzationError , 在网上查找,一般都是因为Jar 包冲突或者缺少Jar包导致的, 但是我的其他方法是可以使用Junit 的, 所以感觉应该跟 ...

  5. [转]IOS开发中的CGFloat、CGPoint、CGSize和CGRect

    http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference ...

  6. unity3D写一个hello world

    unity3D写一个hello world 打开unity并且在assets建立一个新的文件,新的文件命名为hello world.unity.接着创建一个新的C#Sript脚本文件,命名为hello ...

  7. 【个人笔记】《知了堂》MySQL中的数据类型

    MySQL中的数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) ...

  8. 均值滤波去除图像噪声的matlab程序

    所谓均值滤波实际上就是用均值替代原图像中的各个像素值. 均值滤波的方法是:对待处理的当前像素,选择一个模板,该模板为其近邻的若干像素组成,用模板中的像素的均值来替代原像素. 优点:算法简单,计算速度快 ...

  9. hdu1760博弈SG

    A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. .Neter玩转Linux系列之五:crontab使用详解和Linux的进程管理以及网络状态监控

    一.crontab使用详解 概述:任务调度:是指系统在某个时间执行的特定的命令或程序. 任务调度分类: (1)系统工作:有些重要的工作必须周而 复始地执行. (2)个别用户工作:个别用户可能希望执 行 ...