需要下载微信和支付宝的sdk,从官网下载就行
微信
1.调起支付
public static String micropay(WechatAccount config,String auth_code,String body,String outTradeNo,String money){
WXPay wxPay = new WXPay(config);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("appid", config.getAppID());
parameters.put("mch_id", config.getMchID());
parameters.put("nonce_str", WXPayUtil.generateNonceStr());
if (StringUtils.isNotBlank(config.getSub_mchID())) {
parameters.put("sub_mch_id", config.getSub_mchID());
}
parameters.put("sign_type", WXPayConstants.MD5);
parameters.put("body", body);//购买支付信息
//生成订单号
parameters.put("out_trade_no", outTradeNo);//订单号
parameters.put("total_fee", money);// 总金额单位为分
parameters.put("spbill_create_ip", "127.0.0.1");//getIpAddr(request)
parameters.put("auth_code", auth_code);//微信授权码
String sign = null;
try {
sign = WXPayUtil.generateSignature(parameters, config.getKey(), SignType.MD5);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parameters.put("sign", sign);
String result = null;
try {
result = wxPay.requestWithoutCert(WXPayConstants.MICROPAY_URL,parameters,config.getHttpConnectTimeoutMs(),config.getHttpReadTimeoutMs());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
2.查询是否支付成功
public static String queryOrder(WechatAccount config,String outTradeNo){
WXPay wxPay = new WXPay(config);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("appid", config.getAppID());
parameters.put("mch_id", config.getMchID());
parameters.put("nonce_str", WXPayUtil.generateNonceStr());
if (StringUtils.isNotEmpty(config.getSub_mchID())) {
parameters.put("sub_mch_id", config.getSub_mchID());
}
parameters.put("sign_type", WXPayConstants.MD5);
parameters.put("out_trade_no", outTradeNo);//购买支付信息
String sign = null;
try {
sign = WXPayUtil.generateSignature(parameters, config.getKey(), SignType.MD5);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parameters.put("sign", sign);
String result = null;
try {
result = wxPay.requestWithoutCert(WXPayConstants.ORDERQUERY_URL,parameters,config.getHttpConnectTimeoutMs(),config.getHttpReadTimeoutMs());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
3退费
退费需要证书,跟其他类似,因为接入的其他的项目的退费接口,所以没有加。官网开发文档上有
4查询退费是否成功
public static String queryRefund(WechatAccount config,String outTradeNo){
WXPay wxPay = new WXPay(config);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("appid", config.getAppID());
parameters.put("mch_id", config.getMchID());
parameters.put("nonce_str", WXPayUtil.generateNonceStr());
if (StringUtils.isNotEmpty(config.getSub_mchID())) {
parameters.put("sub_mch_id", config.getSub_mchID());
}
parameters.put("out_trade_no", outTradeNo);
String sign = null;
try {
sign = WXPayUtil.generateSignature(parameters, config.getKey(), SignType.MD5);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parameters.put("sign", sign);
String result = null;
try {
result = wxPay.requestWithoutCert(WXPayConstants.REFUNDQUERY_URL,parameters,config.getHttpConnectTimeoutMs(),config.getHttpReadTimeoutMs());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
微信实体类
package com.jinyi.medicine.modules.pay.model;
import java.io.InputStream;
import com.github.wxpay.sdk.WXPayConfig;
public class WechatAccount implements WXPayConfig{
//账户类型
private String accountType;
private String appID;
private String mchID;
private String sub_mchID;
private String key;
private InputStream certStream;
private int httpConnectTimeoutMs = 10000;
private int httpReadTimeoutMs = 10000;
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public String getAppID() {
return appID;
}
public void setAppID(String appID) {
this.appID = appID;
}
public String getMchID() {
return mchID;
}
public void setMchID(String mchID) {
this.mchID = mchID;
}
public String getSub_mchID() {
return sub_mchID;
}
public void setSub_mchID(String sub_mchID) {
this.sub_mchID = sub_mchID;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public InputStream getCertStream() {
return certStream;
}
public void setCertStream(InputStream certStream) {
this.certStream = certStream;
}
public int getHttpConnectTimeoutMs() {
return httpConnectTimeoutMs;
}
public void setHttpConnectTimeoutMs(int httpConnectTimeoutMs) {
this.httpConnectTimeoutMs = httpConnectTimeoutMs;
}
public int getHttpReadTimeoutMs() {
return httpReadTimeoutMs;
}
public void setHttpReadTimeoutMs(int httpReadTimeoutMs) {
this.httpReadTimeoutMs = httpReadTimeoutMs;
}
}
支付宝
1.调起支付
public static String aliPay(AlipayAccount alipayAccount,String authCode,String body,String outTradeNo,String money ){
String appId = alipayAccount.getAppId();
String privateKey = alipayAccount.getPrivateKey();
String aliPublicKey = alipayAccount.getAliPublicKey();
AlipayTradePayRequest request = new AlipayTradePayRequest(); //创建API对应的request类
request.setBizContent("{" +
"\"out_trade_no\":\""+outTradeNo+"\"," +
"\"scene\":\"bar_code\"," +
"\"auth_code\":\""+authCode+"\"," +
"\"subject\":\""+body+"\"," +
"\"store_id\":\"jinyi\"," +
"\"timeout_express\":\"2m\"," +
"\"total_amount\":\""+money+"\"" +
"}"); //设置业务参数
try {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
AlipayTradePayResponse response = alipayClient.execute(request);
if (response!=null) {
return response.getBody();
}
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
try {
AlipayTradePayResponse response = alipayClient.execute(request);
if (response!=null ) {
return response.getBody();
}
} catch (AlipayApiException e2) {
// TODO: handle exception
e.printStackTrace();
}
}
return null;
}
2.查询是否支付成功
public static String queryAliOrder(AlipayAccount alipayAccount ,String outTradeNo){
String appId = alipayAccount.getAppId();
String privateKey = alipayAccount.getPrivateKey();
String aliPublicKey = alipayAccount.getAliPublicKey();
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizContent("{" +
"\"out_trade_no\":\""+outTradeNo+"\"," +
"\"trade_no\":\"\"" +
"}");
try {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
AlipayTradeQueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return response.getBody();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
AlipayTradeQueryResponse response = null;
try {
response = alipayClient.execute(request);
} catch (AlipayApiException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(response.isSuccess()){
return response.getBody();
}
}
return null;
}
3退费
public static String refundFee(AlipayAccount alipayAccount ,String outTradeNo,String money){
String appId = alipayAccount.getAppId();
String privateKey = alipayAccount.getPrivateKey();
String aliPublicKey = alipayAccount.getAliPublicKey();
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizContent("{" +
"\"out_trade_no\":\""+outTradeNo+"\"," +
"\"trade_no\":\"\"," +
"\"refund_amount\":"+money+"," +
"\"refund_reason\":\"退款\"," +
"\"out_request_no\":\""+outTradeNo+"\"," +
"\"operator_id\":\"OP001\"," +
"\"store_id\":\"jinyi001\"," +
"\"terminal_id\":\"jinyi001\"" +
" }");
try {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
AlipayTradeRefundResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getBody();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
logger.info("调用该支付宝加密方式报错,开始执行RSA2==="+outTradeNo);
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
try {
AlipayTradeRefundResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getBody();
}
} catch (AlipayApiException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
logger.info("调用该支付宝加密方式RSA2报错,返回null==="+outTradeNo);
}
}
return null;
}
4.查询是否退费成功
public static String queryRefund(AlipayAccount alipayAccount ,String outTradeNo ){
String appId = alipayAccount.getAppId();
String privateKey = alipayAccount.getPrivateKey();
String aliPublicKey = alipayAccount.getAliPublicKey();
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
request.setBizContent("{" +
"\"trade_no\":\"\"," +
"\"out_trade_no\":\""+outTradeNo+"\"," +
"\"out_request_no\":\""+outTradeNo+"\"" +
"}");
try {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return response.getBody();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
logger.info("查询订单退费状态报错,改用RSA2重新执行:"+outTradeNo);
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
try {
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return response.getBody();
}
} catch (AlipayApiException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
支付宝实体类
package com.jinyi.medicine.modules.pay.model;
public class AlipayAccount {
private Long id;
//hospital_id + "_" accountType 生成的唯一键
private String uniqueKey;
//开发者公钥
private String publicKey;
//支付宝公钥--下载支付宝对账单要用
private String aliPublicKey;
//开发者私钥
private String privateKey;
//appId
private String appId;
//合作伙伴身份pid
private String partnerId;
//apiKey
private String apiKey;
//退款私钥
private String refundKey;
//卖家的email
private String sellerId;
//账户类型
private String accountType;
private String encryption;//加密方式
public String getEncryption() {
return encryption;
}
public void setEncryption(String encryption) {
this.encryption = encryption;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUniqueKey() {
return uniqueKey;
}
public void setUniqueKey(String uniqueKey) {
this.uniqueKey = uniqueKey;
}
public String getPublicKey() {
return publicKey;
}
public void setPublicKey(String publicKey) {
this.publicKey = publicKey;
}
public String getAliPublicKey() {
return aliPublicKey;
}
public void setAliPublicKey(String aliPublicKey) {
this.aliPublicKey = aliPublicKey;
}
public String getPrivateKey() {
return privateKey;
}
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getPartnerId() {
return partnerId;
}
public void setPartnerId(String partnerId) {
this.partnerId = partnerId;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getRefundKey() {
return refundKey;
}
public void setRefundKey(String refundKey) {
this.refundKey = refundKey;
}
public String getSellerId() {
return sellerId;
}
public void setSellerId(String sellerId) {
this.sellerId = sellerId;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
}