當前位置:
首頁 > 知識 > 關於微信和支付寶自動退款介面的接入總結

關於微信和支付寶自動退款介面的接入總結

一:支付寶的自動退款介面接入

支付寶官方api:https://doc.open.alipay.com/doc2/apiDetail.htm?spm=a219a.7629065.0.0.PlTwKb&apiId=759&docType=4

支付寶的自動退款按照官方文檔開發都非常明了,不做太多的累贅:

有一點不太清楚的地方是,在退款成功後沒有返回支付寶自己的退款單號

官方文檔截圖如下:

關於微信和支付寶自動退款介面的接入總結

部分代碼如下:

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_4

微信退款比較麻煩的是需要證書的雙向認證,並且有簽名演算法做簽名認證。

官方文檔截圖如下:

關於微信和支付寶自動退款介面的接入總結

對於證書的使用以及簽名生成演算法可以在官方文檔中詳細去閱讀,部分代碼如下:

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;
//
// wx2421b1c4370ec43b
// 10000100
// 6cefdb308e1e2e8aabd48cf79e546a02
// 10000100
// 1415701182
// 1415757673
// 1
// 1
// // FE56DD4AA85C0EECA82C35595A69E153
//

//上面注掉的就是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);
}
}

簡單的一個總結,希望能對大家有所幫助或者啟發,當然最直接的東西還是看官方文檔。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

Amazing ASP.NET Core 2.0
多線程編程——part 2 線程的生命周期和優先順序
Swift和Javascript的神奇魔法

TAG:達人科技 |

您可能感興趣

《遊戲王 決鬥者的遺產:鏈接進化》開始接受預購退款
使用信用卡消費退貨退款後, 信用卡賬單應該怎麼還?
eBay退貨流程中將正式上線「自動接受退貨」和「自動退款」功能
「無限重啟」起訴結案 LG將給用戶提供退款或抵用金
賓士車主維權讓金融服務費擺上檯面,將引發「退款潮」?
網易博客會員怎麼退款 退款流程分享
網友付費推廣馬蓉專訪視頻卻遭秒退款,被拒理由:負面信息!
共享汽車途歌發退押金提醒:按順序退款
微軟智能手環業務將在五月底結束,忠實用戶可獲退款
特斯拉要求供應商對已交付的零部件降價退款以幫助公司扭虧為盈
亞馬遜工具推薦:賣家必備工具之——退款索賠、產品發布和反饋服務
任天堂或免費提供NS手柄維修服務 已付費可申請退款
良心發現?蘋果為全款更換電池用戶退款
6元錢的良心國產遊戲被主播通關後惡意退款,揚言還要繼續!
阿里客服聯合菜鳥裹裹打造「退貨極速退款」服務
監管部門對「女車主哭訴維權」立案調查 責成退車退款
華帝啟動退全款 「退全款」到底要退多少錢?退款流程公布
市場監管部門已對「女車主哭訴維權」立案調查 並責成儘快退車退款
華帝「法國隊奪冠全額退款」結果退款變退卡,消費者集體聲討「你這把誰當傻子呢!」
共享汽車途歌發布退押金提醒:審核完畢後按順序退款