微信企业付款到个人的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支付服务端详解
支付结果
支付成功后会将金额支付到用户余额中
功能实际很简单,需要自己看一下文档。
源代码地址:微信企业付款
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。