支付寶-單筆轉賬介面
阿新 • • 發佈:2020-08-06
前言:支付寶的這個介面有什麼用呢?主要是方便支付寶商家轉賬給其他使用者,因為都是支付寶內部自己資料庫轉來轉去,所以現在都是免費的,不用手續費。這樣對商家來說省錢又便捷。
1.申請企業商戶。
2.簽約支付寶單筆轉賬產品介面。(1)登入alipay.com選擇支付寶商家->(2)頂部導航選產品中心->(3)拉到底部找到資金服務->(4)選擇轉賬到支付寶->(5)立即開通->(6)等待官方稽核
(1)(2)
(3)(4)
(5)
3.先看官方介面文件:https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer
4.申請(1)應用公鑰證書,(2)支付寶公鑰證書,(3)支付寶根證書。要獲取這3個證書先下載個支付寶開放平臺開發助手,下載地址https://opendocs.alipay.com/open/291/introduce
下載完用支付寶商戶掃碼登入進去。
(1)應用公鑰證書獲取:如下圖(1)紅色圈1,2,3,4,5,6步驟選擇。紅色圈3是因為我用.net core開發。圖2的紅色圈2一定要選擇對了,不然到時候對接會顯示RSA私鑰有問題。圖(1)、圖(2)都弄完會生成拓展名為csr檔案,點選圖(3)的紅色圈1上傳上去,彈出登入介面和簡訊驗證碼介面,驗證通過後到如圖(4)介面點選紅色圈1彈出如圖(5)介面。在如圖(5)介面上傳前面生成的csr檔案即可得到:(1)應用公鑰證書、(2)支付寶公鑰證書、(3)支付寶根證書。
圖(1)
圖(2)
圖(3)
圖(4)
如圖(5)
5.上對接程式碼。
1 public class AlipayResponse 2 { 3 /// <summary> 4 /// 閘道器返回碼,10000代表成功,其他都是失敗。API公共錯誤碼對照地址:https://opendocs.alipay.com/open/common/105806 5 /// </summary> 6 public string code { get; set; } 7 /// <summary>8 /// 閘道器返回碼描述 9 /// </summary> 10 public string msg { get; set; } 11 /// <summary> 12 /// 業務返回碼 13 /// </summary> 14 public string sub_code { get; set; } 15 /// <summary> 16 /// 業務返回碼描述 17 /// </summary> 18 public string sub_msg { get; set; } 19 /// <summary> 20 /// 簽名 21 /// </summary> 22 public string sign { get; set; } 23 /// <summary> 24 /// 商戶訂單號,示例值:201808080001 25 /// </summary> 26 public string out_biz_no { get; set; } 27 /// <summary> 28 /// 支付寶轉賬訂單號,示例值:20190801110070000006380000250621 29 /// </summary> 30 public string order_id { get; set; } 31 /// <summary> 32 /// 支付寶支付資金流水號,示例值:20190801110070001506380000251556 33 /// </summary> 34 public string pay_fund_order_id { get; set; } 35 /// <summary> 36 /// 轉賬單據狀態。 37 /// SUCCESS:成功(對轉賬到銀行卡的單據, 該狀態可能變為退票[REFUND] 狀態); 38 /// FAIL:失敗(具體失敗原因請參見error_code以及fail_reason返回值); 39 /// DEALING:處理中; 40 /// REFUND:退票; 41 /// </summary> 42 public string status { get; set; } 43 /// <summary> 44 /// 訂單支付時間,格式為yyyy-MM-dd HH:mm:ss,示例值:2019-08-21 00:00:00 45 /// </summary> 46 public string trans_date { get; set; } 47 }
1 public class PaymentService 2 { 3 /// <summary> 4 /// 單筆轉賬介面 5 /// </summary> 6 /// <param name="out_biz_no">訂單號</param> 7 /// <param name="trans_amount">訂單金額</param> 8 /// <param name="order_title">交易描述</param> 9 /// <param name="alipay_account">支付賬號</param> 10 /// <param name="real_name">支付寶真實姓名</param> 11 /// <param name="remark">描述</param> 12 /// <returns></returns>14 public AlipayResponse Payment(string out_biz_no, decimal trans_amount, string order_title, string alipay_account, string real_name, string remark) 15 { 16 var SIGN_TYPE = "RSA2";//簽名方式 17 var CHARSET = "UTF-8";//編碼格式 18 var APP_ID = "xxxxxxxxx";//應用appid 19 var PRIVATE_KEY = "xxxxxxxx";//這個是生成csr檔案時候一起生成的私鑰 20 var ALIPAY_GATEWAY = "https://openapi.alipay.com/gateway.do";//請求閘道器地址 21 var certParams = new CertParams 22 { 23 AlipayPublicCertPath = Environment.CurrentDirectory + "\\Crt\\alipayCertPublicKey_RSA2.crt",//支付寶公鑰證書絕對路徑 24 AppCertPath = Environment.CurrentDirectory + "\\Crt\\appCertPublicKey_2021001173604283.crt",//應用公鑰證書絕對路徑 25 RootCertPath = Environment.CurrentDirectory + "\\Crt\\alipayRootCert.crt"//支付寶根證書 26 }; 27 var client = new DefaultAopClient(ALIPAY_GATEWAY, APP_ID, PRIVATE_KEY, "json", "1.0", SIGN_TYPE, CHARSET, "false", certParams); 28 var request = new AlipayFundTransUniTransferRequest 29 { 30 BizContent = "{" + 31 //商戶端的唯一訂單號,對於同一筆轉賬請求,商戶需保證該訂單號唯一 32 "\"out_biz_no\":\"" + out_biz_no + "\"," + 33 "\"trans_amount\":" + trans_amount + "," + 34 "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," + 35 "\"biz_scene\":\"DIRECT_TRANSFER\"," + 36 "\"order_title\":\"" + order_title + "\"," + 37 "\"payee_info\":{" + 38 "\"identity\":\"" + alipay_account + "\"," + 39 "\"identity_type\":\"ALIPAY_LOGON_ID\"," + 40 "\"name\":\"" + real_name + "\"" + 41 "}," + 42 "\"remark\":\"" + remark + "\"" + 43 "}" 44 }; 45 46 var response = client.CertificateExecute(request); 47 AlipayResponse alipayResponse = new AlipayResponse(); 48 if (response.Code == "10000") 49 { 50 alipayResponse.code = response.Code;//閘道器返回碼 51 alipayResponse.msg = response.Msg;//閘道器返回碼描述 52 alipayResponse.sub_code = "";//業務返回碼 53 alipayResponse.sub_msg = "";//業務返回碼描述 54 alipayResponse.out_biz_no = response.OutBizNo;//商戶訂單號 55 alipayResponse.order_id = response.OrderId;//支付寶轉賬訂單號 56 alipayResponse.pay_fund_order_id = response.PayFundOrderId;//支付寶支付資金流水號 57 alipayResponse.status = response.Status;//轉賬單據狀態 58 alipayResponse.trans_date = response.TransDate;//訂單支付時間 59 } 60 else 61 { 62 alipayResponse.code = response.Code; 63 alipayResponse.msg = response.Msg; 64 alipayResponse.sub_code = response.SubCode; 65 alipayResponse.sub_msg = response.SubMsg; 66 } 67 return alipayResponse; 68 } 69 }
結束語:有什麼問題可以給我留言。