JAVA实现微信App支付(二)
来源:互联网 发布:spark mac 安装 编辑:程序博客网 时间:2024/05/19 20:43
以前说了微信公众号支付 然而微信公众App支付和微信公众号类似如果你看了开发文档的话。
只需要调用这个方法就可以了
/** * 微信支付工具类 * @ClassName: WeixiPayUtil * @Description: 微信支付工具 * @author Administrator * @date 2017年4月28日 下午12:02:12 * */public class WeixinPayUtil { private static Logger logger = LoggerFactory.getLogger(WeixinPayUtil.class); public static final String RETURN_CODE_SUCC = "SUCCESS"; public static final String RETURN_CODE_FAIL = "FAIL"; protected static HttpServletRequest request = ServletActionContext.getRequest(); protected static HttpServletResponse response = ServletActionContext.getResponse(); public static SortedMap<String,String> getOrderInfoMap(Map<String, Object> mrsInfo, List<Map<String, Object>> productList){ SortedMap<String,String> orderInfo = new TreeMap<String, String>(); orderInfo.put("userId", mrsInfo.get("userId").toString()); orderInfo.put("orderNum", mrsInfo.get("orderNum").toString()); orderInfo.put("orderMoney", mrsInfo.get("allMoney").toString()); String subject = "",body=""; int bookCount = 0; if(productList!=null && productList.size()>0){ for (Map<String, Object> shopOrderDetail : productList) { subject = subject + shopOrderDetail.get("productName") + " , "; bookCount = bookCount + StrUtil.getIntValue(shopOrderDetail.get("productCount").toString()); } } if(subject.contains(",")){ if(subject.length()>25){ subject = subject.substring(0,25)+"......"; } subject = subject.substring(0,subject.length()-3);// 去掉最后一个,号或者最后一个.号 } body = "订单:"+mrsInfo.get("orderNum")+" 共包含"+bookCount+"件商品。"; orderInfo.put("body", body); orderInfo.put("detail", subject); return orderInfo; }/** * 获取APP预支付ID * @param request * @param response * @param orderInfo 包括字段orderNum,orderMoney(单位:分),myNotifyUrl(自定义回调地址时,可空),detail(可空),body * @return */ public static String getWxAppPrepayId(HttpServletRequest request, HttpServletResponse response, Map<String, String> orderInfo, TSysWxPayConfigCustom wxPayConfigCustom, String notifyUrl){ String methodName = "getWxAppPrepayId"; // 网页授权后获取传递的参数 String myNotifyUrl = orderInfo.get("myNotifyUrl"); String orderNum = orderInfo.get("orderNum"); String orderMoney = orderInfo.get("orderMoney"); // 商品描述 String body = orderInfo.get("body"); String detail = orderInfo.get("detail"); String prepayId = getAppPrepayIdRedis(orderNum); if (StringUtil.isNotEmpty(prepayId)) { return prepayId; } if(StringUtil.isEmptyAnyOf(orderNum, orderMoney, body)){ logger.error(getClassName() +"类"+ methodName +"方法中orderNum,orderMoney,body参数必须都不为空!"); return "-1"; } // 金额转化为分为单位 float sessionmoney = Float.parseFloat(orderMoney); String finalmoney = String.format("%.2f", sessionmoney); String total_fee = Integer.parseInt(finalmoney.replace(".", "")) + ""; // 商户相关资料 String appid = wxPayConfigCustom.getOpenAppId(); String appsecret = wxPayConfigCustom.getOpenSecret(); String partnerkey = wxPayConfigCustom.getOpenKey(); // 商户号 String mch_id = wxPayConfigCustom.getOpenMchId(); // 随机数 String nonce_str = Sha1Util.getNonceStr(); // 附加数据 logger.info(getClassName() +"类"+ methodName +"方法中myNotifyUrl="+ myNotifyUrl); String attach = getAttachKey(orderNum, myNotifyUrl); // 商户订单号 String out_trade_no = orderNum; // 订单生成的机器 IP String spbill_create_ip = request.getRemoteAddr(); // 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。 String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); String notify_url = url; if (StringUtil.isEmpty(notifyUrl)) { notify_url += WeixinConfig.notify_url; } else { notify_url += notifyUrl; } String trade_type = WeixinConfig.trade_type_app; SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put("appid", appid); packageParams.put("mch_id", mch_id); packageParams.put("nonce_str", nonce_str); packageParams.put("body", body); if (StringUtil.isNotEmpty(detail)) { packageParams.put("detail", detail); } packageParams.put("attach", attach); packageParams.put("out_trade_no", out_trade_no); packageParams.put("total_fee", total_fee); packageParams.put("spbill_create_ip", spbill_create_ip); packageParams.put("notify_url", notify_url); packageParams.put("trade_type", trade_type); RequestHandler reqHandler = new RequestHandler(request, response); reqHandler.init(appid, appsecret, partnerkey); String sign = reqHandler.createSign(packageParams); String xml = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>" + mch_id + "</mch_id>" + "<nonce_str>" + nonce_str + "</nonce_str>" + "<sign>" + sign + "</sign>" + "<body><![CDATA[" + body + "]]></body>"; if (StringUtil.isNotEmpty(detail)) { xml += "<detail><![CDATA[" + detail + "]]></detail>"; } xml += "<attach>" + attach + "</attach>" + "<out_trade_no>" + out_trade_no + "</out_trade_no>" + "<total_fee>" + total_fee + "</total_fee>" + "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>" + "<notify_url>" + notify_url + "</notify_url>" + "<trade_type>" + trade_type + "</trade_type>" + "</xml>"; logger.info(getClassName()+"类 "+ methodName +"方法,xml="+ xml); String createOrderURL = WeixinConfig.prepay_url; try { prepayId = GetWxOrderno.getPayNo(createOrderURL, xml); setAppPrepayIdRedis(orderNum, prepayId); logger.info(getClassName()+"类 "+ methodName +"方法中生成 prepayId="+prepayId+"||orderNum="+orderNum); } catch (Exception e) { logger.error(getClassName()+"类 "+ methodName +"方法中生成 prepayId 出错了", e); e.printStackTrace(); } return prepayId; } }package com.shopn.util;import java.io.IOException;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.SortedMap;import java.util.TreeMap;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.HttpProtocolParams;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import net.sf.json.JSONObject;public class weixiPayUtil {public static Map<String,String> createWxAppSignByPrepayId(HttpServletRequest request, HttpServletResponse response, String prepayId, TSysWxPayConfigCustom wxPayConfigCustom){ SortedMap<String, String> finalpackage = new TreeMap<String, String>(); String appid = WeixinpayConfig.wxappappid; String appsecret = WeixinpayConfig.wxappappsecret; String partner = WeixinpayConfig.wxapppartner; String partnerkey = WeixinpayConfig.wxapppartnerkey; if(wxPayConfigCustom!=null){ if(!StringUtil.isEmptyOrNullStr(wxPayConfigCustom.getOpenAppId())){ appid = wxPayConfigCustom.getOpenAppId(); appsecret = wxPayConfigCustom.getOpenSecret(); partner = wxPayConfigCustom.getOpenMchId(); partnerkey = wxPayConfigCustom.getOpenKey(); } } logger.info("createWxAppSignByPrepayId 方法中生成 appid="+appid+"|appsecret="+appsecret+"|partner"+partner+"||partnerkey="+partnerkey); String nonce_str = Sha1Util.getNonceStr(); String timestamp = Sha1Util.getTimeStamp(); String packages = "Sign=WXPay"; finalpackage.put("appid", appid); finalpackage.put("partnerid", partner); finalpackage.put("prepayid", prepayId); finalpackage.put("noncestr", nonce_str); finalpackage.put("timestamp", timestamp); finalpackage.put("package", packages); //finalpackage.put("signType", "MD5"); RequestHandler reqHandler = new RequestHandler(request, response); reqHandler.init(appid, appsecret, partnerkey); String finalSign = reqHandler.createSign(finalpackage); logger.info("createSignByPrepayId方法中生成 finalSign "+finalSign); Map<String,String> map = new HashMap<String, String>(); map.put("partnerId", partner); map.put("prepayId", prepayId); map.put("sign", finalSign); map.put("nonceStr", nonce_str); map.put("timeStamp", timestamp); map.put("appid", appid); return map; }}
希望对开发微信支付的朋友有帮助。
版权声明:未经本人允许不得转载。
阅读全文
0 0
- JAVA实现微信App支付(二)
- 微信APP支付-Java后台实现
- 微信APP支付-Java后台实现
- 微信APP支付-Java后台实现
- Java实现微信支付之APP支付
- 微信APP支付服务端(JAVA)
- 【手机app微信支付】app微信支付服务端(java)的实现
- 微信支付app支付服务器端(Java)
- 微信支付 APP支付 Java 服务器端
- 微信支付 APP支付 Java 服务器端
- C#+Hybrid App(Android)实现微信APP支付
- java--spring boot微信支付服务端实现含代码(app支付、扫码支付等)
- 三方 app微信支付 java后端实现
- 基于PHP的APP微信支付开发(二)
- java微信支付宝支付(二)
- Java服务端支付功能模块--(二)微信支付
- APP微信支付的后台实现
- PHP实现APP微信支付
- 如何在cmd java main方法 传入含有中文的参数乱码问题解决
- python: set函数
- iOS App的加固保护原理
- 8大排序算法
- linux下查看所有用户及所有用户组
- JAVA实现微信App支付(二)
- 「MICCAI 2017」Reading Notes
- 无效的DNS配置,导致防火墙服务启动失败
- centos常用命令总结
- 戒
- VirtualBox中的网络连接方式详解
- Windows7下安装与破解IntelliJ IDEA2017
- Permutation in String问题及解法
- 「MICCAI 2016」Reading Note