微信小程序支付
@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+
"¬ify_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;
}
}
//以上工具类请在个人博客上寻找
请发表评论