java实现微信企业付款到个人
时间:2020-10-13 13:21:37|栏目:JAVA代码|点击: 次
微信企业付款到个人的PHP实现代码,供大家参考,具体内容如下
企业付款实现企业向个人付款,实现付款到用户零钱。项目实现了企业付款到个人和企业付款个人账单查询。代码包括签名实现,双向证书验证,付款功能等
支付流程
付款功能
企业付款到授权用户的零钱
企业付款注意注意:
1、所有接口需要双向证书验证
2、需要设置接口秘钥,签名用
详细参考:微信企业付款开发文档
项目结构
和上一篇一样,需要配置证书以及商户id、appid等
支付功能
包含企业转账和企业转账查询
package org.andy.wxpay.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.andy.wxpay.model.JsonResult; import org.andy.wxpay.model.ResponseData; import org.andy.wxpay.utils.CollectionUtil; import org.andy.wxpay.utils.ConfigUtil; import org.andy.wxpay.utils.HttpUtils; import org.andy.wxpay.utils.PayUtil; import org.andy.wxpay.utils.SerializerFeatureUtil; import org.andy.wxpay.utils.StringUtil; import org.andy.wxpay.utils.WebUtil; import org.andy.wxpay.utils.XmlUtil; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.alibaba.fastjson.JSON; /** * 创建时间:2016年11月9日 下午5:49:00 * * @author andy * @version 2.2 */ @Controller @RequestMapping("/transfer") public class TransferController { private static final Logger LOG = Logger.getLogger(TransferController.class); private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; // 企业付款 private static final String TRANSFERS_PAY_QUERY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo"; // 企业付款查询 private static final String APP_ID = ConfigUtil.getProperty("wx.appid"); private static final String MCH_ID = ConfigUtil.getProperty("wx.mchid"); private static final String API_SECRET = ConfigUtil.getProperty("wx.api.secret"); /** * 企业向个人支付转账 * @param request * @param response * @param openid 用户openid * @param callback */ @RequestMapping(value = "/pay", method = RequestMethod.POST) public void transferPay(HttpServletRequest request, HttpServletResponse response, String openid, String callback) { LOG.info("[/transfer/pay]"); //业务判断 openid是否有收款资格 Map<String, String> restmap = null; try { Map<String, String> parm = new HashMap<String, String>(); parm.put("mch_appid", APP_ID); //公众账号appid parm.put("mchid", MCH_ID); //商户号 parm.put("nonce_str", PayUtil.getNonceStr()); //随机字符串 parm.put("partner_trade_no", PayUtil.getTransferNo()); //商户订单号 parm.put("openid", openid); //用户openid parm.put("check_name", "NO_CHECK"); //校验用户姓名选项 OPTION_CHECK //parm.put("re_user_name", "安迪"); //check_name设置为FORCE_CHECK或OPTION_CHECK,则必填 parm.put("amount", "100"); //转账金额 parm.put("desc", "测试转账到个人"); //企业付款描述信息 parm.put("spbill_create_ip", PayUtil.getLocalIp(request)); //服务器Ip地址 parm.put("sign", PayUtil.getSign(parm, API_SECRET)); String restxml = HttpUtils.posts(TRANSFERS_PAY, XmlUtil.xmlFormat(parm, false)); restmap = XmlUtil.xmlParse(restxml); } catch (Exception e) { LOG.error(e.getMessage(), e); } if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) { LOG.info("转账成功:" + restmap.get("err_code") + ":" + restmap.get("err_code_des")); Map<String, String> transferMap = new HashMap<>(); transferMap.put("partner_trade_no", restmap.get("partner_trade_no"));//商户转账订单号 transferMap.put("payment_no", restmap.get("payment_no")); //微信订单号 transferMap.put("payment_time", restmap.get("payment_time")); //微信支付成功时间 WebUtil.response(response, WebUtil.packJsonp(callback, JSON.toJSONString(new JsonResult(1, "转账成功", new ResponseData(null, transferMap)), SerializerFeatureUtil.FEATURES))); }else { if (CollectionUtil.isNotEmpty(restmap)) { LOG.info("转账失败:" + restmap.get("err_code") + ":" + restmap.get("err_code_des")); } WebUtil.response(response, WebUtil.packJsonp(callback, JSON .toJSONString(new JsonResult(-1, "转账失败", new ResponseData()), SerializerFeatureUtil.FEATURES))); } } /** * 企业向个人转账查询 * @param request * @param response * @param tradeno 商户转账订单号 * @param callback */ @RequestMapping(value = "/pay/query", method = RequestMethod.POST) public void orderPayQuery(HttpServletRequest request, HttpServletResponse response, String tradeno, String callback) { LOG.info("[/transfer/pay/query]"); if (StringUtil.isEmpty(tradeno)) { WebUtil.response(response, WebUtil.packJsonp(callback, JSON .toJSONString(new JsonResult(-1, "转账订单号不能为空", new ResponseData()), SerializerFeatureUtil.FEATURES))); } Map<String, String> restmap = null; try { Map<String, String> parm = new HashMap<String, String>(); parm.put("appid", APP_ID); parm.put("mch_id", MCH_ID); parm.put("partner_trade_no", tradeno); parm.put("nonce_str", PayUtil.getNonceStr()); parm.put("sign", PayUtil.getSign(parm, API_SECRET)); String restxml = HttpUtils.posts(TRANSFERS_PAY_QUERY, XmlUtil.xmlFormat(parm, true)); restmap = XmlUtil.xmlParse(restxml); } catch (Exception e) { LOG.error(e.getMessage(), e); } if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) { // 订单查询成功 处理业务逻辑 LOG.info("订单查询:订单" + restmap.get("partner_trade_no") + "支付成功"); Map<String, String> transferMap = new HashMap<>(); transferMap.put("partner_trade_no", restmap.get("partner_trade_no"));//商户转账订单号 transferMap.put("openid", restmap.get("openid")); //收款微信号 transferMap.put("payment_amount", restmap.get("payment_amount")); //转账金额 transferMap.put("transfer_time", restmap.get("transfer_time")); //转账时间 transferMap.put("desc", restmap.get("desc")); //转账描述 WebUtil.response(response, WebUtil.packJsonp(callback, JSON .toJSONString(new JsonResult(1, "订单转账成功", new ResponseData(null, transferMap)), SerializerFeatureUtil.FEATURES))); }else { if (CollectionUtil.isNotEmpty(restmap)) { LOG.info("订单转账失败:" + restmap.get("err_code") + ":" + restmap.get("err_code_des")); } WebUtil.response(response, WebUtil.packJsonp(callback, JSON .toJSONString(new JsonResult(-1, "订单转账失败", new ResponseData()), SerializerFeatureUtil.FEATURES))); } } }
其他代码参考上一篇 微信支付-App支付服务端详解
支付结果
支付成功后会将金额支付到用户余额中
功能实际很简单,需要自己看一下文档。
源代码地址:微信企业付款
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机