前言
支付寶對(duì) .Net 的支持還是比較充分的,在每個(gè)接口文檔中都有關(guān)于 C# 語(yǔ)言的示例,這樣就大大降低了對(duì)接的難度,很容易上手。
官方接口文檔地址:退款-alipay.trade.refund 退款狀態(tài)查詢-alipay.trade.fastpay.refund.query
本文就簡(jiǎn)單實(shí)現(xiàn)下這兩個(gè)接口,并順便記錄特別注意的點(diǎn)。
一、接入準(zhǔn)備
1.1 引入 SDK
安裝支付寶開(kāi)放平臺(tái)官方 SDK:AlipaySDKNet.Standard。基于.Net Standard 2.0開(kāi)發(fā),支持.Net Framework 4.6.1、.Net Core 2.0及其以上版本。
NuGet 簡(jiǎn)介地址:https://www.nuget.org/packages/AlipaySDKNet.Standard/
1.2 必要賬戶基礎(chǔ)信息
名稱 示例
應(yīng)用 ID 2024 ... ... (15 位純數(shù)字)
應(yīng)用公鑰 MIIBIjANBgkqhk ... ... (392 位)
應(yīng)用私鑰 MIIEvgIBADANBgk ... ... (PKCS1 格式的 1592 位)
注意:通過(guò)支付寶密鑰工具生成的應(yīng)用私鑰默認(rèn)是 PKCS8 格式的,僅適用于 Java,此時(shí)必須手動(dòng)轉(zhuǎn)換成 PKCS1 格式,適用于其他全部語(yǔ)言。
密鑰工具下載地址:https://opendocs.alipay.com/common/02kipk
如下格式轉(zhuǎn)換,下邊為轉(zhuǎn)換后的格式以及標(biāo)注:
二、接口調(diào)用
2.1 公用配置方法
將固定的應(yīng)用信息填入,此方法可在不同接口中公用:
private static AlipayConfig GetAlipayConfig()
{
string privateKey = "<-- 請(qǐng)?zhí)顚懩膽?yīng)用私鑰,例如:MIIEvQIBADANB ... ... -->";
string alipayPublicKey = "<-- 請(qǐng)?zhí)顚懩闹Ц秾毠€,例如:MIIBIjANBg... -->";
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.ServerUrl = "https://openapi.alipay.com/gateway.do";
alipayConfig.AppId = "<-- 請(qǐng)?zhí)顚懩腁ppId,例如:2019091767145019 -->";
alipayConfig.PrivateKey = privateKey;
alipayConfig.Format = "json";
alipayConfig.AlipayPublicKey = alipayPublicKey;
alipayConfig.Charset = "UTF-8";
alipayConfig.SignType = "RSA2";
return alipayConfig;
}
2.2 退款接口
以下是退款接口的代碼,其中入?yún)H示例了必要的字段,其他詳情見(jiàn)官方文檔。
// 初始化SDK
IAopClient alipayClient = new DefaultAopClient(GetAlipayConfig());
// 構(gòu)造請(qǐng)求參數(shù)以調(diào)用接口
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
// 設(shè)置查詢選項(xiàng)
List<String> queryOptions = new List<String>();
queryOptions.Add("refund_detail_item_list");
model.QueryOptions = queryOptions;
//// 設(shè)置商戶訂單號(hào)(與支付寶交易號(hào),配置任一即可)
//model.OutTradeNo = "20150320010101001";
// 設(shè)置支付寶交易號(hào)
model.TradeNo = "2024072900000000000000000001";
// 設(shè)置退款金額,單位為:元
model.RefundAmount = "0.01";
// 設(shè)置退款原因說(shuō)明
model.RefundReason = "正常退款0729";
// 設(shè)置退款請(qǐng)求號(hào)
model.OutRequestNo = $"ZFB{DateTime.Now.ToString("yyyyMMddHHmmssffffff")}";
request.SetBizModel(model);
AlipayTradeRefundResponse response = alipayClient.Execute(request);
if (!response.IsError)
{
Console.WriteLine("調(diào)用成功");
}
else
{
Console.WriteLine("調(diào)用失敗");
}
成功返回:
{
"alipay_trade_refund_response": {
"code": "10000",
"msg": "Success",
"buyer_logon_id": "188******10",
"fund_change": "Y",
"gmt_refund_pay": "2024-07-29 13:50:04",
"out_trade_no": "*****",
"refund_detail_item_list": [
{
"amount": "0.01",
"fund_channel": "COUPON"
}
],
"refund_fee": "0.01",
"send_back_fee": "0.01",
"trade_no": "*****",
"buyer_open_id": "*****"
},
"sign": "*****"
}
2.3 退款狀態(tài)查詢接口
以下是退款狀態(tài)查詢接口的代碼,其中入?yún)H示例了必要的字段,其他詳情見(jiàn)官方文檔。
// 初始化SDK
IAopClient alipayClient = new DefaultAopClient(GetAlipayConfig());
// 構(gòu)造請(qǐng)求參數(shù)以調(diào)用接口
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
// 設(shè)置查詢選項(xiàng)
List<String> queryOptions = new List<String>();
queryOptions.Add("refund_detail_item_list");
model.QueryOptions = queryOptions;
//// 設(shè)置商戶訂單號(hào)(與支付寶交易號(hào),配置任一即可)
//model.OutTradeNo = "2014112611001004680073956707";
// 設(shè)置支付寶交易號(hào)
model.TradeNo = "2024072900000000000000000001";
// 設(shè)置退款請(qǐng)求號(hào),來(lái)自退款接口中生成的編號(hào)
model.OutRequestNo = "ZFB20240729135000591024";
request.SetBizModel(model);
AlipayTradeFastpayRefundQueryResponse response = alipayClient.Execute(request);
if (!response.IsError)
{
Console.WriteLine("調(diào)用成功");
}
else
{
Console.WriteLine("調(diào)用失敗");
}
成功返回:
{
"alipay_trade_fastpay_refund_query_response": {
"code": "10000",
"msg": "Success",
"out_request_no": "ZFB20240729135000591024",
"out_trade_no": "*****",
"refund_amount": "0.01",
"refund_detail_item_list": [
{
"amount": "0.01",
"fund_channel": "COUPON"
}
],
"refund_status": "REFUND_SUCCESS",
"send_back_fee": "0.01",
"total_amount": "1.00",
"trade_no": "*****"
},
"sign": "*****"
}
轉(zhuǎn)載本文請(qǐng)注明原文鏈接:https://www.cnblogs.com/hnzhengfy/p/18330730/alipay_refunds
該文章在 2024/7/30 11:01:08 編輯過(guò)