🐛 Fix restore purchase in afdian
This commit is contained in:
@ -2,11 +2,13 @@ using System.Security.Cryptography;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
|
|
||||||
namespace DysonNetwork.Sphere.Wallet.PaymentHandlers;
|
namespace DysonNetwork.Sphere.Wallet.PaymentHandlers;
|
||||||
|
|
||||||
public class AfdianPaymentHandler(
|
public class AfdianPaymentHandler(
|
||||||
|
AppDatabase db,
|
||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
ILogger<AfdianPaymentHandler> logger,
|
ILogger<AfdianPaymentHandler> logger,
|
||||||
IConfiguration configuration
|
IConfiguration configuration
|
||||||
@ -86,7 +88,7 @@ public class AfdianPaymentHandler(
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="orderId">The order ID to query</param>
|
/// <param name="orderId">The order ID to query</param>
|
||||||
/// <returns>The order item if found, otherwise null</returns>
|
/// <returns>The order item if found, otherwise null</returns>
|
||||||
public async Task<OrderItem?> GetOrderAsync(string orderId)
|
public async Task<OrderItem?> GetOrderAsync(string orderId, Guid accountId)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(orderId))
|
if (string.IsNullOrEmpty(orderId))
|
||||||
{
|
{
|
||||||
@ -94,11 +96,16 @@ public class AfdianPaymentHandler(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var connection = await db.AccountConnections
|
||||||
|
.Where(c => c.AccountId == accountId && c.Provider == "afdian")
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
if (connection is null) throw new InvalidOperationException("Account need to link an afdian account first.");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var token = _configuration["Payment:Auth:Afdian"] ?? "_:_";
|
var token = _configuration["Payment:Auth:Afdian"] ?? "_:_";
|
||||||
var tokenParts = token.Split(':');
|
var tokenParts = token.Split(':');
|
||||||
var userId = tokenParts[0];
|
var userId = connection.ProvidedIdentifier;
|
||||||
token = tokenParts[1];
|
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))
|
||||||
@ -442,4 +449,4 @@ public class SkuDetailItem
|
|||||||
[JsonPropertyName("album_id")] public string AlbumId { get; set; } = null!;
|
[JsonPropertyName("album_id")] public string AlbumId { get; set; } = null!;
|
||||||
|
|
||||||
[JsonPropertyName("pic")] public string Picture { get; set; } = null!;
|
[JsonPropertyName("pic")] public string Picture { get; set; } = null!;
|
||||||
}
|
}
|
||||||
|
@ -180,11 +180,14 @@ public class SubscriptionController(SubscriptionService subscriptions, AfdianPay
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("order/restore/afdian")]
|
[HttpPost("order/restore/afdian")]
|
||||||
|
[Authorize]
|
||||||
public async Task<IActionResult> RestorePurchaseFromAfdian([FromBody] RestorePurchaseRequest request)
|
public async Task<IActionResult> RestorePurchaseFromAfdian([FromBody] RestorePurchaseRequest request)
|
||||||
{
|
{
|
||||||
var order = await afdian.GetOrderAsync(request.OrderId);
|
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
||||||
|
|
||||||
|
var order = await afdian.GetOrderAsync(request.OrderId, currentUser.Id);
|
||||||
if (order is null) return NotFound($"Order with ID {request.OrderId} was not found.");
|
if (order is null) return NotFound($"Order with ID {request.OrderId} was not found.");
|
||||||
|
|
||||||
var subscription = await subscriptions.CreateSubscriptionFromOrder(order);
|
var subscription = await subscriptions.CreateSubscriptionFromOrder(order);
|
||||||
return Ok(subscription);
|
return Ok(subscription);
|
||||||
}
|
}
|
||||||
@ -200,4 +203,4 @@ public class SubscriptionController(SubscriptionService subscriptions, AfdianPay
|
|||||||
|
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user