支付寶官方api:https://doc.open.alipay.com/doc2/apiDetail.htm?spm=a219a.7629065.0.0.PlTwKb&apiId=759&docType=4php
支付寶的自動退款按照官方文檔開發都很是明瞭,不作太多的累贅:算法
有一點不太清楚的地方是,在退款成功後沒有返回支付寶本身的退款單號 json
官方文檔截圖以下:api
部分代碼以下:微信
public void orderPayRefund(OpsOrderRefundDetailModel refundDetailModel, String refundPrice) throws Exception{
String appId = DataRequest.resources.getProperty("ali.app.appId");
String publicKey = DataRequest.resources.getProperty("ali.public.key");
AlipayClient alipayClient = new DefaultAlipayClient(
"https://openapi.alipay.com/gateway.do",appId,PRIVATE_KEY,"json",
"utf-8",publicKey,AlipayConstants.SIGN_TYPE_RSA);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizContent("{" +
" \"out_trade_no\":\""+refundDetailModel.getOutTradeNo()+"\"," +
" \"refund_amount\":"+refundPrice+"," +
" \"refund_reason\":\"正常退款\"," +
" \"out_request_no\":\""+refundDetailModel.getId()+"\"" +
" }");
AlipayTradeRefundResponse response = alipayClient.execute(request);
if(response.isSuccess()){
dealRefundSuccess(refundDetailModel,refundPrice,response.getTradeNo());
} else {
logger.error(response.getMsg()+"退款失敗");
throw new UserHintException(response.getMsg());
}
}
微信官方api:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4app
微信退款比較麻煩的是須要證書的雙向認證,而且有簽名算法作簽名認證。post
官方文檔截圖以下:ui
對於證書的使用以及簽名生成算法能夠在官方文檔中詳細去閱讀,部分代碼以下:this
public void refundApp(OpsOrderRefundDetailModel refundDetailModel,String refundPrice) throws Exception{
// 1.證書的使用(這個證書使用能夠在官方文檔證書使用裏面有官方demo)
KeyStore keyStore = KeyStore.getInstance("PKCS12");
InputStream instream = this.getClass().getResourceAsStream(DataRequest.resources.getProperty("weixin.app.cert"));
try {
keyStore.load(instream, DataRequest.resources.getProperty("weixin.app.mchId").toCharArray());
} catch (Exception e){
e.printStackTrace();
}finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,DataRequest.resources.getProperty("weixin.app.mchId").toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
//http請求建立
HttpPost httppost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
UnifiedRefundRequest req = new UnifiedRefundRequest();
req.setAppid(DataRequest.resources.getProperty("weixin.app.appId"));
req.setMch_id(DataRequest.resources.getProperty("weixin.app.mchId"));
req.setOut_trade_no(refundDetailModel.getOutTradeNo());
req.setOut_refund_no(refundDetailModel.getId());
req.setTotal_fee(refundDetailModel.getOrderPayedPrice().multiply(new BigDecimal(100)).intValue());
req.setRefund_fee(new BigDecimal(refundPrice).multiply(new BigDecimal(100)).intValue());
req.setOp_user_id("admin");
// 這裏createXml 方法已經完成了簽名
String xml = req.createXml();
//<xml>
// <appid>wx2421b1c4370ec43b</appid>
// <mch_id>10000100</mch_id>
// <nonce_str>6cefdb308e1e2e8aabd48cf79e546a02</nonce_str>
// <op_user_id>10000100</op_user_id>
// <out_refund_no>1415701182</out_refund_no>
// <out_trade_no>1415757673</out_trade_no>
// <refund_fee>1</refund_fee>
// <total_fee>1</total_fee>
// <transaction_id></transaction_id>
// <sign>FE56DD4AA85C0EECA82C35595A69E153</sign>
//</xml>
//上面注掉的<xml>就是createXml的結果 紅色的地方就是簽名
StringEntity entity = new StringEntity(xml, "UTF-8");
httppost.setEntity(entity);
//退款接口請求結果接受和處理
CloseableHttpResponse responseEntry = httpclient.execute(httppost);
HttpEntity resEntity = responseEntry.getEntity();
String returnObj = (resEntity == null) ? null : EntityUtils.toString(resEntity, Consts.UTF_8);
UnifiedOrderResponse resp = AbstractWxResponse.toBean(returnObj, UnifiedOrderResponse.class);
resp.toVerify();
if("SUCCESS".equals(resp.getResult_code())){
dealRefundSuccess(refundDetailModel,refundPrice,resp.getRefund_id());
}else{
logger.error(resp.getErr_code_des()+"退款失敗");
throw new UserHintException(resp.getErr_code_des());
}
}
簡單的一個總結,但願能對你們有所幫助或者啓發,固然最直接的東西仍是看官方文檔。spa