时间:2021-05-19
微信支付的具体实现方法,供大家参考,具体内容如下
工具类(用于在微信支付服务后台生成预支付交易单)
package com.fengdi.lianmeng.zhifu.weixin;import com.fengdi.lianmeng.util.MD5;import java.lang.reflect.Field;import java.util.HashMap;import java.util.Map;/** * * 类说明:除被扫支付场景以外,店铺系统先调用该接口在微信支付服务后台生成预支付交易单, * 返回正确的预支付交易回话标识后再按扫码、JSAPI、APP等不同场景生成交易串调起支付。 * @author 聂枫 * 2016年1月8日上午11:33:21 */public class UnifiedorderReq{ /** * 公众账号ID */ private String appid; /** * 店铺号 */ private String mch_id; /** * 设备号 */ private String device_info; /** * 随机字符串 */ private String nonce_str; /** * 签名 */ private String sign; /** * 商品描述 */ private String body; /** * 商品详情 */ private String detail; /** * 附加数据 */ private String attach; /** * 店铺订单号 */ private String out_trade_no; /** * 货币类型 */ private String fee_type; /** * 总金额 */ private Integer total_fee; /** * 终端IP */ private String spbill_create_ip; /** * 交易起始时间 */ private String time_start; /** * 交易结束时间 */ private String time_expire; /** * 商品标记 */ private String goods_tag; /** * 通知地址 */ private String notify_url; /** * 交易类型 */ private String trade_type; /** * 商品ID */ private String product_id; /** * 指定支付方式 */ private String limit_pay; /** * 用户标识 */ private String openid; public String getAppid() { return appid; } public void setAppid(String appid) { this.appid = appid; } public String getMch_id() { return mch_id; } public void setMch_id(String mch_id) { this.mch_id = mch_id; } public String getDevice_info() { return device_info; } public void setDevice_info(String device_info) { this.device_info = device_info; } public String getNonce_str() { return nonce_str; } public void setNonce_str(String nonce_str) { this.nonce_str = nonce_str; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } public String getBody() { return body; } public void setBody(String body) { this.body = body.length()>40?body.substring(0, 40):body; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getAttach() { return attach; } public void setAttach(String attach) { this.attach = attach; } public String getOut_trade_no() { return out_trade_no; } public void setOut_trade_no(String out_trade_no) { this.out_trade_no = out_trade_no; } public String getFee_type() { return fee_type; } public void setFee_type(String fee_type) { this.fee_type = fee_type; } public Integer getTotal_fee() { return total_fee; } public void setTotal_fee(Integer total_fee) { this.total_fee = total_fee; } public String getSpbill_create_ip() { return spbill_create_ip; } public void setSpbill_create_ip(String spbill_create_ip) { this.spbill_create_ip = spbill_create_ip; } public String getTime_start() { return time_start; } public void setTime_start(String time_start) { this.time_start = time_start; } public String getTime_expire() { return time_expire; } public void setTime_expire(String time_expire) { this.time_expire = time_expire; } public String getGoods_tag() { return goods_tag; } public void setGoods_tag(String goods_tag) { this.goods_tag = goods_tag; } public String getNotify_url() { return notify_url; } public void setNotify_url(String notify_url) { this.notify_url = notify_url; } public String getTrade_type() { return trade_type; } public void setTrade_type(String trade_type) { this.trade_type = trade_type; } public String getProduct_id() { return product_id; } public void setProduct_id(String product_id) { this.product_id = product_id; } public String getLimit_pay() { return limit_pay; } public void setLimit_pay(String limit_pay) { this.limit_pay = limit_pay; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("UnifiedorderReq [appid="); builder.append(appid); builder.append(", mch_id="); builder.append(mch_id); builder.append(", device_info="); builder.append(device_info); builder.append(", nonce_str="); builder.append(nonce_str); builder.append(", sign="); builder.append(sign); builder.append(", body="); builder.append(body); builder.append(", detail="); builder.append(detail); builder.append(", attach="); builder.append(attach); builder.append(", out_trade_no="); builder.append(out_trade_no); builder.append(", fee_type="); builder.append(fee_type); builder.append(", total_fee="); builder.append(total_fee); builder.append(", spbill_create_ip="); builder.append(spbill_create_ip); builder.append(", time_start="); builder.append(time_start); builder.append(", time_expire="); builder.append(time_expire); builder.append(", goods_tag="); builder.append(goods_tag); builder.append(", notify_url="); builder.append(notify_url); builder.append(", trade_type="); builder.append(trade_type); builder.append(", product_id="); builder.append(product_id); builder.append(", limit_pay="); builder.append(limit_pay); builder.append(", openid="); builder.append(openid); builder.append("]"); return builder.toString(); } public String toSignString(){ Class<?> clz = this.getClass(); Field[] fields = clz.getDeclaredFields(); StringBuilder builder = new StringBuilder(); for (Field field : fields) { builder.append(field.getName()); } return builder.toString(); } public String signString(){ StringBuilder builder = new StringBuilder(); builder.append("appid="+appid); if(attach!=null){ builder.append("&attach="+attach); } builder.append("&body="+body); builder.append("&mch_id="+mch_id); builder.append("&nonce_str="+nonce_str); builder.append("¬ify_url="+notify_url); if(!trade_type.equals("APP")) { builder.append("&openid="+openid); } builder.append("&out_trade_no="+out_trade_no); builder.append("&spbill_create_ip="+spbill_create_ip); builder.append("&total_fee="+total_fee); builder.append("&trade_type="+trade_type); builder.append("&key="+FgWxConfig.mobilekey); return builder.toString(); } public String wapSignString(){ StringBuilder builder = new StringBuilder(); builder.append("appid="+appid); if(attach!=null){ builder.append("&attach="+attach); } builder.append("&body="+body); builder.append("&mch_id="+mch_id); builder.append("&nonce_str="+nonce_str); builder.append("¬ify_url="+notify_url); if(!trade_type.equals("APP")) { builder.append("&openid="+openid); } builder.append("&out_trade_no="+out_trade_no); builder.append("&spbill_create_ip="+spbill_create_ip); builder.append("&total_fee="+total_fee); builder.append("&trade_type="+trade_type); builder.append("&key="+FgWxConfig.key); return builder.toString(); } public String toXml(){ return "<xml>" + "<appid>"+appid+"</appid>"+ (attach != null? "<attach>"+attach+"</attach>" : "")+ "<body>"+body+"</body>"+ "<mch_id>"+mch_id+"</mch_id>"+ "<nonce_str>"+nonce_str+"</nonce_str>"+ "<notify_url>"+notify_url+"</notify_url>"+ (trade_type.equals("APP") == true?"":"<openid>"+openid+"</openid>")+ "<out_trade_no>"+out_trade_no+"</out_trade_no>"+ "<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"+ "<total_fee>"+total_fee+"</total_fee>"+ "<trade_type>"+trade_type+"</trade_type>"+ "<sign>"+MD5.encryptByMD5(signString()).toUpperCase()+"</sign>"+ "</xml>"; } public String toWapXml(){ return "<xml>" + "<appid>"+appid+"</appid>"+ (attach != null? "<attach>"+attach+"</attach>" : "")+ "<body>"+body+"</body>"+ "<mch_id>"+mch_id+"</mch_id>"+ "<nonce_str>"+nonce_str+"</nonce_str>"+ "<notify_url>"+notify_url+"</notify_url>"+ (trade_type.equals("APP") == true?"":"<openid>"+openid+"</openid>")+ "<out_trade_no>"+out_trade_no+"</out_trade_no>"+ "<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"+ "<total_fee>"+total_fee+"</total_fee>"+ "<trade_type>"+trade_type+"</trade_type>"+ "<sign>"+MD5.encryptByMD5(wapSignString()).toUpperCase()+"</sign>"+ "</xml>"; } public String signQueryString(String transaction_id){ StringBuilder builder = new StringBuilder(); builder.append("appid="+appid); builder.append("&body="+body); builder.append("&mch_id="+mch_id); builder.append("&nonce_str="+nonce_str); builder.append("&transaction_id="+transaction_id); builder.append("&key="+FgWxConfig.mobilekey); return builder.toString(); } public String toQueryXml(String transaction_id){ StringBuilder sBuilder=new StringBuilder(); sBuilder.append("<xml>"); sBuilder.append("<appid>"+appid+"</appid>"); sBuilder.append("<body>"+body+"</body>"); sBuilder.append("<mch_id>"+mch_id+"</mch_id>"); sBuilder.append("<nonce_str>"+nonce_str+"</nonce_str>"); sBuilder.append("<transaction_id>"+transaction_id+"</transaction_id>"); sBuilder.append("<sign>"+MD5.encryptByMD5(signQueryString(transaction_id)).toUpperCase()+"</sign>"); sBuilder.append("</xml>"); return sBuilder.toString(); } public Map<String,Object> toMap(){ Map<String, Object> params = new HashMap<String, Object>(); params.put("appid", appid); if(attach!=null){ params.put("attach", attach); } params.put("body", body); params.put("mch_id", mch_id); params.put("nonce_str", nonce_str); params.put("notify_url", notify_url); if(!trade_type.equals("APP")) { params.put("openid", openid); } params.put("out_trade_no", out_trade_no); params.put("spbill_create_ip", spbill_create_ip); params.put("total_fee", total_fee); params.put("trade_type", trade_type); params.put("sign", MD5.encryptByMD5(signString()).toUpperCase()); return params; }组装工具类数据
private AppWeixinPayResponse appUnifiedorderReq(Order order, Member member, HttpServletRequest request) { // 获取支付参数 UnifiedorderReq unifiedorderReq = new UnifiedorderReq(); unifiedorderReq.setBody(order.getOrderName().length() > 50 ? order.getOrderName().substring(0, 50) : order.getOrderName()); log.error(unifiedorderReq.getBody().length() + "--" + unifiedorderReq.getBody()); unifiedorderReq.setAttach(order.getOrderNo()); unifiedorderReq.setTotal_fee(order.getRealAmt().intValue()); unifiedorderReq.setTrade_type("APP");//此处可修改为JSAPI unifiedorderReq.setOut_trade_no(order.getOrderNo() + RandomUtils.nextInt(100, 1000)); unifiedorderReq.setSpbill_create_ip(getIpAddr(request)); unifiedorderReq.setOpenid(member.getOpenid()); return payService.appUnifiedorderReq(unifiedorderReq, PayCategory.order.toString()); }微信统一下单
public AppWeixinPayResponse appUnifiedorderReq(UnifiedorderReq unifiedorderReq, String payCategory) throws FdServiceException { unifiedorderReq.setAppid(FgWxConfig.appid_app); unifiedorderReq.setMch_id(FgWxConfig.mchId); unifiedorderReq.setNonce_str(Random2Utils.generateString(20)); if (payCategory.equals(PayCategory.order.toString())) { unifiedorderReq.setNotify_url(fgProperties.getDomainUrl() + FgWxConfig.weixinOrderNotifyCallBack); } else if (payCategory.equals(PayCategory.recharge.toString())) { unifiedorderReq.setNotify_url(fgProperties.getDomainUrl() + FgWxConfig.weixinRechargeNotifyCallBack); } else { throw new FdServiceException(ErrorCode.ILLEGAL_ARGUMENT, "微信参数类型[" + payCategory + "]异常"); } unifiedorderReq.setSign(MD5.encryptByMD5(unifiedorderReq.wapSignString()).toUpperCase()); logger.info("unifiedorder返回数据unifiedorderReq:" + unifiedorderReq); String unifiedorder = FgHttp.sendPost(FgWxConfig.unifiedorderUrl, unifiedorderReq.toWapXml()); logger.info("unifiedorder返回数据unifiedorder:" + unifiedorder); AppWeixinPayResponse appWeixinPayResponse = new AppWeixinPayResponse(); try { Map<String, Object> map = XmlHelper.parseResXML(unifiedorder); if ("SUCCESS".equals(map.get("return_code")) && "SUCCESS".equals(map.get("result_code"))) { logger.info("微信统一下单返回成功:" + map); appWeixinPayResponse.setAppid(FgWxConfig.appid_app); appWeixinPayResponse.setNoncestr((String) map.get("nonce_str")); appWeixinPayResponse.setPartnerid(FgWxConfig.mchId); appWeixinPayResponse.setPrepayid((String) map.get("prepay_id")); appWeixinPayResponse.setTimestamp((System.currentTimeMillis() + "").substring(0, 10)); appWeixinPayResponse.setSign(MD5.encryptByMD5(appWeixinPayResponse.wapSignString()).toUpperCase()); } else { logger.info("微信统一下单返回失败:" + map); throw new FdServiceException(ErrorCode.ILLEGAL_ARGUMENT, "微信统一下单返回失败:" + map.get("return_msg")); } } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return appWeixinPayResponse;//直接将参数返回给前端,发起支付 }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
微信支付密码怎么取消,想要取消微信支付密码,微信支付密码怎么取消的呢?微信支付密码怎么取消需要先进行一项功能操作,才能实现微信支付密码怎么取消绑定的问题。微
前面讲了怎么实现微信支付,详见博文:PHP实现微信支付(jsapi支付)流程和ThinkPHP中实现微信支付(jsapi支付)流程。由于业务需求,还需要有微信退
微信小程序在线支付功能最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支
之前有写过几篇文章将微信支付和退款:1.PHP实现微信支付(jsapi支付)流程2.ThinkPHP中实现微信支付(jsapi支付)流程3.PHP实现微信申请退
开微店,就是使用微商城,也就是利用微信公众号进行商品或者服务买卖的功能。公众号开设微商城,是使用高级功能实现的一种功能,这种功能的实现如果加入了微信支付,就闭环