本文實例為大家分享了java微信退款接口和支付寶退款接口的具體代碼,供大家參考,具體內容如下
1、微信退款接口
相對來說我感覺微信的退款接口還是比較好調用的,直接發送httppost請求即可;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/** * * 微信退款 * @param transaction_id 微信支付訂單號 * @param out_refund_no 商戶訂單號 * @param total_fee 總金額 * @param refund_fee 退款金額 * @param op_user_id 操作人 * @return string * @exception */ public string wxpayrefundrequest(string transaction_id, string out_refund_no, int total_fee, int refund_fee, string op_user_id) { closeablehttpclient httpclient = null ; closeablehttpresponse response = null ; string strresponse = null ; try { httpclient = clientcustomssl.getcloseablehttpclient(); // 構造http請求 httppost httppost = new httppost(configure.pay_refund_api); // payrefundreqdata wxdata = new payrefundreqdata( // "1004720096201602263541023415", "16371", 30, 30, "19417"); payrefundreqdata wxdata = new payrefundreqdata(transaction_id, out_refund_no, total_fee, refund_fee, op_user_id); string requeststr = util.convertobj2xml(wxdata); stringentity se = new stringentity(requeststr.tostring()); httppost.setentity(se); // 發送請求 response = httpclient.execute(httppost); httpentity entity = response.getentity(); if (entity != null ) { saxreader saxreader = new saxreader(); document document = saxreader.read(entity.getcontent()); element rootelt = document.getrootelement(); // 結果碼 string returncode = rootelt.elementtext( "return_code" ); string resultcode = rootelt.elementtext( "result_code" ); if ( "success" .equals(returncode)&& "success" .equals(resultcode)) { strresponse=returncode; } else { strresponse=rootelt.elementtext( "err_code_des" ); } } entityutils.consume(entity); } catch (exception e) { logger.getlogger(getclass()).error( "payrefundrequest" , e); } finally { try { response.close(); httpclient.close(); } catch (ioexception e) { // todo auto-generated catch block logger.getlogger(getclass()).error( "payrefundrequest關閉異常:" , e); } } return strresponse; } |
報錯的話請檢查加密的sign是否正確,還有就是調用的接口地址是否正確
2、支付寶退款接口
支付寶直接導入支付寶封裝好的jar包直接調用即可,官網
調用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** * * 支付寶退款請求 * @param out_trade_no 訂單支付時傳入的商戶訂單號,不能和 trade_no同時為空。 * @param trade_no 支付寶交易號,和商戶訂單號不能同時為空 * @param refund_amount 需要退款的金額,該金額不能大于訂單金額,單位為元,支持兩位小數 * @return * string * @exception */ public string alipayrefundrequest(string out_trade_no,string trade_no, double refund_amount){ // 發送請求 string strresponse = null ; try { alipayclient alipayclient = new defaultalipayclient (alipayconfig.alipayurl,alipayconfig.appid, alipayconfig.private_key,alipayconfig.content_type,alipayconfig.input_charset,alipayconfig.ali_public_key); alipaytraderefundrequest request = new alipaytraderefundrequest(); alipayrefundinfo alidata= new alipayrefundinfo(); alidata.setout_trade_no(out_trade_no); alidata.setrefund_amount(refund_amount); alidata.settrade_no(trade_no); request.setbizcontent(jsonutils.converttostring(alidata)); alipaytraderefundresponse response = alipayclient.execute(request); strresponse=response.getcode(); if ( "10000" .equals(response.getcode())) { strresponse= "退款成功" ; } else { strresponse=response.getsubmsg(); } } catch (exception e) { logger.getlogger(getclass()).error( "alipayrefundrequest" , e); } return strresponse; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u011321758/article/details/80308950