提供企业向用户付款的功能,支持企业通过API接口付款,或通过微信支付商户平台网页功能操作付款。
企业付款有两种方式
开通条件:
付款之前需要充值: 在调用API接口付款或通过微信支付商户平台网页功能操作付款之前需要登录微信支付商户平台,通过网页充值功能充值(商户平台-交易中心)
充值
、商户平台网页功能操作付款
、其他限制条件
详细介绍请参考官方文档
下面就来介绍下如何通过API接口付款
参考安全规范-3.商户证书
下载:微信商户平台(pay.weixin.qq.com)–>账户中心–>账户设置–>API安全–>证书下载
付款到零钱需要openId
如何获取openId呢?参考授权获取用户信息
https://gitee.com/Javen205/IJPay https://github.com/Javen205/IJPay
/**
* 企业付款到零钱
* @param params
* 请求参数
* @param certPath
* 证书文件目录
* @param certPassword
* 证书密码
* @return {String}
*/
public static String transfers(Map<String, String> params, String certPath, String certPassword) {
return WxPayApi.doPostSSL(TRANSFERS_URL, params, certPath, certPassword);
}
/**
* 查询企业付款到零钱
* @param params
* 请求参数
* @param certPath
* 证书文件目录
* @param certPassword
* 证书密码
* @return {String}
*/
public static String getTransferInfo(Map<String, String> params, String certPath, String certPassword) {
return WxPayApi.doPostSSL(GETTRANSFERINFO_URL, params, certPath, certPassword);
}
https://github.com/Javen205/IJPay-Demo
/**
* 企业付款到零钱
*/
public void transfers() {
String openId = getSessionAttr("openId");
Map<String, String> params = new HashMap<String, String>();
params.put("mch_appid", appid);
params.put("mchid", mch_id);
String nonceStr = String.valueOf(System.currentTimeMillis());
params.put("nonce_str", nonceStr);
String partnerTradeNo = String.valueOf(System.currentTimeMillis());
params.put("partner_trade_no", partnerTradeNo);
params.put("openid", openId);
params.put("check_name", "NO_CHECK");
params.put("amount", "100");
params.put("desc", "IJPay提现测试-By Javen");
String ip = IpKit.getRealIp(getRequest());
if (StrKit.isBlank(ip)) {
ip = "127.0.0.1";
}
params.put("spbill_create_ip", ip);
params.put("sign", PaymentKit.createSign(params, partnerKey));
System.out.println("certPath>"+certPath);
// 提现
String transfers = WxPayApi.transfers(params, certPath, mch_id);
log.info("提现结果:" + transfers);
System.out.println("提现结果:" + transfers);
Map<String, String> map = PaymentKit.xmlToMap(transfers);
String return_code = map.get("return_code");
String result_code = null;
if (("SUCCESS").equals(return_code)) {
result_code = map.get("result_code");
if (("SUCCESS").equals(result_code)) {
//提现成功
} else {
//提现失败
}
}
renderText(transfers);
}
/**
* 查询企业付款到零钱
*/
public void transferInfo() {
try {
String partner_trade_no = getPara("partner_trade_no");
Map<String, String> params = new HashMap<String, String>();
params.put("nonce_str", System.currentTimeMillis()+"");
params.put("partner_trade_no", partner_trade_no);
params.put("mch_id", mch_id);
params.put("appid", appid);
params.put("sign", PaymentKit.createSign(params, partnerKey));
String transferInfo = WxPayApi.getTransferInfo(params , certPath, mch_id);
renderText(transferInfo);
} catch (Exception e) {
e.printStackTrace();
renderText(e.getMessage());
}
}
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
<result_code><![CDATA[SUCCESS]]></result_code>
<mch_id><![CDATA[1262075601]]></mch_id>
<pub_key><![CDATA[-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA6Bl76IwSvBTiibZ+CNRUA6BfahMshZ0WJpHD1GpmvcQjeN6Yrv6c
9eIl6gB4nU3isN7bn+LmoVTpH1gHViaV2YyG/zXj4z4h7r+V+ezesMqqorEg38BC
NUHNmhnw4/C0I4gBAQ4x0SJOGnfKGZKR9yzvbkJtvEn732JcEZCbdTZmaxkwlenX
vM+mStcJaxBCB/h5xJ5VOF5nDbTPzLphIpzddr3zx/Jxjna9QB1v/YSKYXn+iuwr
uNUXGCvvxBWaBGKrjOdRTRy9adWOgNmtuYDQJ2YOfG8PtPe06ELKjmr2CfaAGrKK
UroyaGvy3qxAV0PlT+UQ4ADSXWt/zl0o5wIDAQAB
-----END RSA PUBLIC KEY-----
]]></pub_key>
</xml>
将获取到的
pub_key
保存到文件public.pem
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA6Bl76IwSvBTiibZ+CNRUA6BfahMshZ0WJpHD1GpmvcQjeN6Yrv6c
9eIl6gB4nU3isN7bn+LmoVTpH1gHViaV2YyG/zXj4z4h7r+V+ezesMqqorEg38BC
NUHNmhnw4/C0I4gBAQ4x0SJOGnfKGZKR9yzvbkJtvEn732JcEZCbdTZmaxkwlenX
vM+mStcJaxBCB/h5xJ5VOF5nDbTPzLphIpzddr3zx/Jxjna9QB1v/YSKYXn+iuwr
uNUXGCvvxBWaBGKrjOdRTRy9adWOgNmtuYDQJ2YOfG8PtPe06ELKjmr2CfaAGrKK
UroyaGvy3qxAV0PlT+UQ4ADSXWt/zl0o5wIDAQAB
-----END RSA PUBLIC KEY-----
PKCS#1格式的公钥转成PKCS#8格式的公钥
PKCS#1格式的公钥转PKCS#8
/**
* 企业付款到银行
* @param params
* 请求参数
* @param certPath
* 证书文件目录
* @param certPassword
* 证书密码
* @return {String}
*/
public static String payBank(Map<String, String> params, String certPath, String certPassword) {
return WxPayApi.doPostSSL(PAY_BANK_URL, params, certPath, certPassword);
}
/**
* 查询企业付款到银行
* @param params
* 请求参数
* @param certPath
* 证书文件目录
* @param certPassword
* 证书密码
* @return {String}
*/
public static String queryBank(Map<String, String> params, String certPath, String certPassword) {
return WxPayApi.doPostSSL(QUERY_BANK_URL, params, certPath, certPassword);
}
/**
* 获取RSA加密公钥
* @param params
* 请求参数
* @param certPath
* 证书文件目录
* @param certPassword
* 证书密码
* @return {String}
*/
public static String getPublicKey(Map<String, String> params, String certPath, String certPassword) {
return WxPayApi.doPostSSL(GETPUBLICKEY_URL, params, certPath, certPassword);
}
/**
* 获取RSA加密公钥
* 接口默认输出PKCS#1格式的公钥,商户需根据自己开发的语言选择公钥格式
* Java RSA加密需要使用PKCS#8 不然会出现异常algid parse error, not a sequence
* PKCS#1 转 PKCS#8:
* openssl rsa -RSAPublicKey_in -in <filename> -pubout
*/
public void getPublicKey(){
try {
Map<String, String> params = new HashMap<String, String>();
params.put("mch_id", mch_id);
params.put("nonce_str", String.valueOf(System.currentTimeMillis()));
params.put("sign_type", "MD5");
String createSign = PaymentKit.createSign(params, partnerKey);
params.put("sign", createSign);
String publicKeyStr = WxPayApi.getPublicKey(params , certPath, mch_id);
renderText(publicKeyStr);
} catch (Exception e) {
e.printStackTrace();
renderText(e.getMessage());
}
}
/**
* 企业付款到银行卡
*/
public void payBank() {
try {
//假设获取到的RSA加密公钥为PUBLIC_KEY(PKCS#8格式)
final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6Bl76IwSvBTiibZ+CNRUA6BfahMshZ0WJpHD1GpmvcQjeN6Yrv6c9eIl6gB4nU3isN7bn+LmoVTpH1gHViaV2YyG/zXj4z4h7r+V+ezesMqqorEg38BCNUHNmhnw4/C0I4gBAQ4x0SJOGnfKGZKR9yzvbkJtvEn732JcEZCbdTZmaxkwlenXvM+mStcJaxBCB/h5xJ5VOF5nDbTPzLphIpzddr3zx/Jxjna9QB1v/YSKYXn+iuwruNUXGCvvxBWaBGKrjOdRTRy9adWOgNmtuYDQJ2YOfG8PtPe06ELKjmr2CfaAGrKKUroyaGvy3qxAV0PlT+UQ4ADSXWt/zl0o5wIDAQAB";
Map<String, String> params = new HashMap<String, String>();
params.put("mch_id", mch_id);
params.put("partner_trade_no", System.currentTimeMillis()+"");
params.put("nonce_str", System.currentTimeMillis()+"");
params.put("enc_bank_no", RSAUtils.encryptByPublicKeyByWx("6214837805417833", PUBLIC_KEY));//收款方银行卡号
params.put("enc_true_name", RSAUtils.encryptByPublicKeyByWx("周业文", PUBLIC_KEY));//收款方用户名
params.put("bank_code", "1001");//收款方开户行
params.put("amount", "1");
params.put("desc", "IJPay 测试付款到银行卡-By Javen");
params.put("sign", PaymentKit.createSign(params, partnerKey));
String payBank = WxPayApi.payBank(params , certPath, mch_id);
renderText(payBank);
} catch (Exception e) {
e.printStackTrace();
renderText(e.getMessage());
}
}
/**
* 查询企业付款到银行
*/
public void queryBank() {
try {
String partner_trade_no = getPara("partner_trade_no");
Map<String, String> params = new HashMap<String, String>();
params.put("mch_id", mch_id);
params.put("partner_trade_no", partner_trade_no);
params.put("nonce_str", System.currentTimeMillis()+"");
params.put("sign", PaymentKit.createSign(params, partnerKey));
String queryBank = WxPayApi.queryBank(params, certPath, mch_id);
renderText(queryBank);
} catch (Exception e) {
e.printStackTrace();
renderText(e.getMessage());
}
}
IJPay 让支付触手可及接入文档
如有疑问欢迎留言交流讨论