🐛 Bug fixes in restore purchase with afdian

This commit is contained in:
LittleSheep 2025-06-23 02:59:41 +08:00
parent 1b45f07419
commit 5ce3598cc9

View File

@ -40,8 +40,10 @@ public class AfdianPaymentHandler(
{ {
try try
{ {
var userId = "abc"; // Replace with your actual USER_ID var token = _configuration["Payment:Auth:Afdian"] ?? "_:_";
var token = _configuration["Payment:Auth:Afdian"] ?? "<token here>"; var tokenParts = token.Split(':');
var userId = tokenParts[0];
token = tokenParts[1];
var paramsJson = JsonSerializer.Serialize(new { page }, JsonOptions); var paramsJson = JsonSerializer.Serialize(new { page }, JsonOptions);
var ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)) var ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1))
.TotalSeconds; // Current timestamp in seconds .TotalSeconds; // Current timestamp in seconds
@ -94,8 +96,10 @@ public class AfdianPaymentHandler(
try try
{ {
var userId = "abc"; // Replace with your actual USER_ID var token = _configuration["Payment:Auth:Afdian"] ?? "_:_";
var token = _configuration["Payment:Auth:Afdian"] ?? "<token here>"; var tokenParts = token.Split(':');
var userId = tokenParts[0];
token = tokenParts[1];
var paramsJson = JsonSerializer.Serialize(new { out_trade_no = orderId }, JsonOptions); var paramsJson = JsonSerializer.Serialize(new { out_trade_no = orderId }, JsonOptions);
var ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)) var ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1))
.TotalSeconds; // Current timestamp in seconds .TotalSeconds; // Current timestamp in seconds
@ -126,7 +130,7 @@ public class AfdianPaymentHandler(
await response.Content.ReadAsStreamAsync(), JsonOptions); await response.Content.ReadAsStreamAsync(), JsonOptions);
// Check if we have a valid response and orders in the list // Check if we have a valid response and orders in the list
if (result?.Data?.Orders == null || result.Data.Orders.Count == 0) if (result?.Data.Orders == null || result.Data.Orders.Count == 0)
{ {
_logger.LogWarning($"No order found with ID: {orderId}"); _logger.LogWarning($"No order found with ID: {orderId}");
return null; return null;
@ -147,21 +151,24 @@ public class AfdianPaymentHandler(
/// </summary> /// </summary>
/// <param name="orderIds">A collection of order IDs to query</param> /// <param name="orderIds">A collection of order IDs to query</param>
/// <returns>A list of found order items</returns> /// <returns>A list of found order items</returns>
public async Task<List<OrderItem>> GetOrders(IEnumerable<string> orderIds) public async Task<List<OrderItem>> GetOrderBatchAsync(IEnumerable<string> orderIds)
{ {
if (orderIds == null || !orderIds.Any()) var orders = orderIds.ToList();
if (orders.Count == 0)
{ {
_logger.LogWarning("Order IDs cannot be null or empty"); _logger.LogWarning("Order IDs cannot be null or empty");
return new List<OrderItem>(); return [];
} }
try try
{ {
// Join the order IDs with commas as specified in the API documentation // Join the order IDs with commas as specified in the API documentation
var orderIdsParam = string.Join(",", orderIds); var orderIdsParam = string.Join(",", orders);
var userId = "abc"; // Replace with your actual USER_ID var token = _configuration["Payment:Auth:Afdian"] ?? "_:_";
var token = _configuration["Payment:Auth:Afdian"] ?? "<token here>"; var tokenParts = token.Split(':');
var userId = tokenParts[0];
token = tokenParts[1];
var paramsJson = JsonSerializer.Serialize(new { out_trade_no = orderIdsParam }, JsonOptions); var paramsJson = JsonSerializer.Serialize(new { out_trade_no = orderIdsParam }, JsonOptions);
var ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)) var ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1))
.TotalSeconds; // Current timestamp in seconds .TotalSeconds; // Current timestamp in seconds
@ -192,13 +199,9 @@ public class AfdianPaymentHandler(
await response.Content.ReadAsStreamAsync(), JsonOptions); await response.Content.ReadAsStreamAsync(), JsonOptions);
// Check if we have a valid response and orders in the list // Check if we have a valid response and orders in the list
if (result?.Data?.Orders == null || result.Data.Orders.Count == 0) if (result?.Data?.Orders != null && result.Data.Orders.Count != 0) return result.Data.Orders;
{
_logger.LogWarning($"No orders found with IDs: {orderIdsParam}"); _logger.LogWarning($"No orders found with IDs: {orderIdsParam}");
return new List<OrderItem>(); return [];
}
return result.Data.Orders;
} }
catch (Exception ex) catch (Exception ex)
{ {