@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. session写入memcache

    1 <?php 2 class MemSession{ 3 private static $handler = null; 4 private static $lifetime = null; ...

  2. oracle 表查询(一)

    通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salgrade表结构进行解说. emp 雇员表字段名称   数据类型       是否为空   备注-------- ...

  3. 理解ios 11中webview的视口

    iOS 11在状态栏区域带来了一些新的,也许是不直观的行为,这对使用Apache Cordova或Ionic等工具的开发人员尤为重要.特别是,这种行为变化会影响任何基于Web的应用程序,这些应用程序在 ...

  4. jquery模板下载网站

    jquery模板下载网站 http://www.jqshare.com/

  5. django之快速分页

    本文介绍djanog两种分页,第一是普通分页,第二是使用haystack全文检索的分页. 1.django自带分页功能,这个功能非常好用.基本知识点:Django提供了数据分页的类,这些类被定义在dj ...

  6. HDFS概述(3)————HDFS Federation

    本指南概述了HDFS Federation功能以及如何配置和管理联合集群. 当前HDFS背景 HDFS主要有两层: 1.Namespace (1)包含目录,文件和块. (2)它支持所有命名空间相关的文 ...

  7. hdu2222 ac自动机入门

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

  9. Angular学习笔记(一)

    本文为原创文章,转载请标明出处 目录 架构 模块 组件 模板 元数据 数据绑定 指令 服务 依赖注入 模板与数据绑定 1. 架构 模块 Angular 应用是模块化的,并且 Angular 有自己的模 ...

  10. PHP程序员40点陋习

    1.不写注释 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行sql前不执行编码和安全检测 8.不使用测 ...