Location>code7788 >text

Alipay refund and result query interface simple implementation (.Net 7.0)

Popularity:156 ℃/2024-07-29 18:49:16

X. Preamble

Net support for Alipay is still relatively full, there are examples of C# language in each interface document, which greatly reduces the difficulty of docking, it is easy to get started.

Official interface documentation address:Refunds -  Refund Status Inquiry - 

In this article, we will simply implement these two interfaces, and by the way, record the points of special attention.

I. Access preparation

1.1 Introducing the SDK

Install the official Alipay Open Platform SDK:. Developed based on .Net Standard 2.0, supports .Net Framework 4.6.1, .Net Core 2.0 and above.

NuGet Profile Address:/packages//

1.2 Required account base information

name (of a thing) typical example  
Application ID 2024 ... ... (15 digits pure)
application public key MIIBIjANBgkqhk ... ... (392)
application private key MIIEvgIBADANBgk ... ... (1592 bits in PKCS1 format)

Note: The application private key generated by Alipay Key Tool is in PKCS8 format by default, which is only applicable to Java, and must be converted to PKCS1 format manually, which is applicable to all other languages.

Key Tool Download Address:/common/02kipk

The following format conversion, the following is the converted format and labeling:

II. Interface calls

2.1 Public Configuration Methods

Fill in the fixed application information, this method can be common across different interfaces:

private static AlipayConfig GetAlipayConfig()
{
    string privateKey = "<-- Please fill in your application private key, e.g. MIIEvQIBADANB ... ... -->";
    string alipayPublicKey = "<-- Please fill in your alipay public key, e.g. MIIBIjANBg... -->";
    AlipayConfig alipayConfig = new AlipayConfig();
     = "/";
     = "<-- Please fill in your AppId, e.g. 2019091767145019 -->";";
     = privateKey;
     = "json";
     = alipayPublicKey;
     = "UTF-8";
     = "RSA2";
    return alipayConfig.
}

2.2 Refund interface

Here is the code for the refund interface, where the entry parameterOnly the required fields are exemplified, see the official documentation for additional details.

// Initialize the SDK
IAopClient alipayClient = new DefaultAopClient(GetAlipayConfig());
// Construct the request parameters to invoke the interface
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
AlipayTradeRefundModel model = new AlipayTradeRefundModel();

// Set the query options
List<String> queryOptions = new List<String> ();
("refund_detail_item_list").
 = queryOptions.

//// Set the merchant order number (with the Alipay transaction number, configure either one)
// = "20150320010101001";
// Set the Alipay transaction number
 = "202407290000000000000000000000000001";

// Set the refund amount in dollars
 = "0.01";

// Set the refund reason description
 = "Normal Refund 0729"; // Set the reason for the refund.

// Set the refund request number
 = $"ZFB{("yyyyyMMddHHmmssffffffff")}";

(model);
AlipayTradeRefundResponse response = (request);
if (!)
{
    ("The call was successful");
}
else
{
    ("Failed"); } else {
}

Successful return:

{
	"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 Refund Status Inquiry Interface

The following is the code for the refund status query interface, where the input parameterOnly the required fields are exemplified, see the official documentation for additional details.

// Initialize the SDK
IAopClient alipayClient = new DefaultAopClient(GetAlipayConfig());
// Construct the request parameters to invoke the interface
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();

// Set the query options
List<String> queryOptions = new List<String>();
("refund_detail_item_list").
 = queryOptions.

//// Set the merchant order number (with the Alipay transaction number, configure either one)
// = "2014112611001004680073956707";
// Set the Alipay transaction number
 = "20240729000000000000000000000001"; // Set the Alipay transaction number.

// Set the refund request number, from the number generated in the refund interface
 = "ZFB20240729135000591024"; // Set the refund request number, from the number generated in the refund interface.

(model).
AlipayTradeFastpayRefundQueryResponse response = (request);

if (!)
{
    ("The call was successful");
}
else
{
    ("Failed"); } else {
}

Successful return:

{
	"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": "*****"
}