在微信公眾號支付的api中沒有這個接口,如果企業(yè)需要給用戶轉賬,或者讓用戶提現或者給用戶發(fā)紅包等需要再商戶平臺中的產品中心分別開通。
一、開通功能
開通就是點擊一下,很簡單。但需要注意的是支持向用戶轉賬的賬戶和收到用戶付款的賬戶不是同一個,而為了滿足此功能,你需要先用財付通進行充值(交易中心--資金管理--充值)。
二、下載證書
證書下載在賬戶中心--api安全,現在需要手機驗證碼和商戶平臺登錄密碼。下載之后再window上進行安裝,安裝的密碼是商戶號。
安裝之后并將證書放在網站目錄下,用于下一步在代碼中進行驗證。
三、轉賬
微信現在提供的demo中沒有這一塊,下面就根據官方的demo做一些修改。和之前的例子類似,我們都需要用wxpaydata對象來操作我們的參數。定義一個transferspay對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class transferspay { public string openid { get ; set ; } public int amount { get ; set ; } public string partner_trade_no { get ; set ; } public string re_user_name { get ; set ; } public string spbill_create_ip { get ; set ; } public wxpaydata gettransfersapiparameters() { wxpaydata apiparam = new wxpaydata(); apiparam.setvalue( "partner_trade_no" , partner_trade_no); apiparam.setvalue( "openid" , openid); apiparam.setvalue( "check_name" , "no_check" ); apiparam.setvalue( "amount" , amount); apiparam.setvalue( "desc" , "提現" ); apiparam.setvalue( "spbill_create_ip" , spbill_create_ip); apiparam.setvalue( "re_user_name" , re_user_name); return apiparam; } } |
在官方demo中的wxpayapi中已經包含了公眾號支付的相關方法。再增加一個transfers的方法用來轉賬:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static wxpaydata transfers(wxpaydata inputdata, int timeout = 6) { var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers" ; inputdata.setvalue( "mch_appid" , wxpayconfig.appid); //公眾賬號id inputdata.setvalue( "mchid" , wxpayconfig.mchid); //商戶號 inputdata.setvalue( "nonce_str" , wxpayapi.generatenoncestr()); //隨機字符串 inputdata.setvalue( "sign" , inputdata.makesign()); //簽名 string xml = inputdata.toxml(); var start = datetime.now; string response = httpservice.post(xml, url, true , timeout); // portal.mvc.logger.info("wxpayapi"+ "unfiedorder response : " + response); var end = datetime.now; int timecost = ( int )((end - start).totalmilliseconds); wxpaydata result = new wxpaydata(); result.fromxml(response); reportcosttime(url, timecost, result); //測速上報 return result; } |
稍微需要注意下的地方就是幾個默認參數的名字和別的方法不一樣,比如appid和mch_id。轉賬中是mch_appid和mchid,紅包中又叫wxappid和mch_id。然后注意到httpservice.post方法第三個參數是帶true的。也就是會使用到證書。進入post方法中我們可以看到:
1
2
3
4
5
6
7
8
|
//是否使用證書 if (isusecert) { string path = httpcontext.current.request.physicalapplicationpath; x509certificate2 cert = new x509certificate2(path + wxpayconfig.sslcert_path, wxpayconfig.sslcert_password); request.clientcertificates.add(cert); log.debug( "wxpayapi" , "postxml used cert" ); } |
這里使用到了證書的路徑和密碼,密碼即商戶號。這一切準備好之后就可以controller中進行轉賬了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[loginvalid] public actionresult cashtransfers( string ordernumber) { //var order = new order(){amount = 1}; // var openid = "obsbmwqjqwjfzqlksfnjxflsixxx"; var user = _workcontext.currentuser; var order = _paymentservice.getorderbyordernumber(ordernumber); var transfer = new transferspay { openid = user.openid, amount = ( int ) order.amount*100, partner_trade_no = order.ordernumber, re_user_name = "stoneniqiu" , spbill_create_ip = _webhelper.getcurrentipaddress() }; var data = transfer.gettransfersapiparameters(); var result = wxpayapi.transfers(data); return content(result.toprintstr()); } |
得到結果
這樣就實現了轉賬/提現的功能。
發(fā)布
在正式的環(huán)境中,我們需要先創(chuàng)建自己的訂單,然后向微信請求轉賬,成功之后對自己的訂單進行處理。cashtransfers方法稍作調整。
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
|
[loginvalid] public actionresult cashtransfers( string ordernumber) { var user = _workcontext.currentuser; var order = _paymentservice.getorderbyordernumber(ordernumber); if ( string .isnullorempty(user.openid)) { return json( new portalresult( "請用微信登錄!" )); } if (order == null || order.orderstate != orderstate.padding) { return json( new portalresult( "訂單有誤!" )); } var transfer = new transferspay { openid = user.openid, amount = ( int ) order.amount*100, partner_trade_no = order.ordernumber, re_user_name = "stoneniqiu" , spbill_create_ip = _webhelper.getcurrentipaddress() }; var data = transfer.gettransfersapiparameters(); var result = wxpayapi.transfers(data); if (result.getvalue( "result_code" ).tostring() == "success" ) { return json( new portalresult( true , "提現成功" )); } return json( new portalresult( false , result.getvalue( "return_msg" ).tostring())); } |
另外一個要注意的是,發(fā)布之后老是出現操作超時的錯誤,建議就是修改超時時間為30秒。默認的6秒容易超時。支付的時候也是。
public static wxpaydata transfers(wxpaydata inputdata, int timeout = 30)
如果企業(yè)賬戶的錢沒了,會出現以下提示:
開發(fā)文檔:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/stoneniqiu/p/6337525.html