java实现微信企业付款到个人
程序员文章站
2024-02-26 11:18:40
微信企业付款到个人的php实现代码,供大家参考,具体内容如下
企业付款实现企业向个人付款,实现付款到用户零钱。项目实现了企业付款到个人和企业付款个人账单查询。代码包括签名...
微信企业付款到个人的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支付服务端详解
支付结果
支付成功后会将金额支付到用户余额中
功能实际很简单,需要自己看一下文档。
源代码地址:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。