fix: enable sdk payment checkout providers
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Aop.Api;
|
using Aop.Api;
|
||||||
|
using Aop.Api.Domain;
|
||||||
|
using Aop.Api.Request;
|
||||||
using Aop.Api.Util;
|
using Aop.Api.Util;
|
||||||
using Tiku.Application.Commerce;
|
using Tiku.Application.Commerce;
|
||||||
|
|
||||||
@@ -14,10 +16,47 @@ internal sealed class AlipayProvider : IPaymentProvider
|
|||||||
CreatePaymentProviderRequest request,
|
CreatePaymentProviderRequest request,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
_ = BuildClient(account);
|
_ = cancellationToken;
|
||||||
throw new PaymentProviderException(
|
var client = BuildClient(account);
|
||||||
"Alipay SDK is configured; checkout call is enabled in the commerce checkout batch.",
|
var alipayRequest = new AlipayTradeWapPayRequest();
|
||||||
"alipay_checkout_not_enabled");
|
alipayRequest.SetNotifyUrl(request.NotifyUrl);
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.ReturnUrl))
|
||||||
|
{
|
||||||
|
alipayRequest.SetReturnUrl(request.ReturnUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
alipayRequest.SetBizModel(new AlipayTradeWapPayModel
|
||||||
|
{
|
||||||
|
OutTradeNo = request.OrderNo,
|
||||||
|
Subject = request.Subject,
|
||||||
|
TotalAmount = FormatYuan(request.AmountCents),
|
||||||
|
ProductCode = "QUICK_WAP_WAY",
|
||||||
|
QuitUrl = request.QuitUrl
|
||||||
|
});
|
||||||
|
var response = client.pageExecute(alipayRequest);
|
||||||
|
if (string.IsNullOrWhiteSpace(response.Body))
|
||||||
|
{
|
||||||
|
throw new PaymentProviderException("Alipay create payment returned an empty body.", "alipay_create_failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientPayload = JsonSerializer.SerializeToElement(new
|
||||||
|
{
|
||||||
|
formHtml = response.Body
|
||||||
|
});
|
||||||
|
var rawPayload = JsonSerializer.SerializeToElement(new
|
||||||
|
{
|
||||||
|
response.Body,
|
||||||
|
request.OrderNo,
|
||||||
|
request.AmountCents
|
||||||
|
});
|
||||||
|
|
||||||
|
return Task.FromResult(new CreatePaymentProviderResult(
|
||||||
|
Provider,
|
||||||
|
request.Method,
|
||||||
|
"pending",
|
||||||
|
null,
|
||||||
|
clientPayload,
|
||||||
|
rawPayload));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<PaymentNotificationResult> ParsePaymentNotificationAsync(
|
public Task<PaymentNotificationResult> ParsePaymentNotificationAsync(
|
||||||
@@ -138,4 +177,7 @@ internal sealed class AlipayProvider : IPaymentProvider
|
|||||||
? (int)Math.Round(amount * 100, MidpointRounding.AwayFromZero)
|
? (int)Math.Round(amount * 100, MidpointRounding.AwayFromZero)
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string FormatYuan(int cents) =>
|
||||||
|
(cents / 100m).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,7 @@ internal sealed class WechatPayProvider : IPaymentProvider
|
|||||||
CreatePaymentProviderRequest request,
|
CreatePaymentProviderRequest request,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
_ = BuildClient(account);
|
return CreatePaymentCoreAsync(account, request, cancellationToken);
|
||||||
throw new PaymentProviderException(
|
|
||||||
"WeChat Pay SDK is configured; checkout call is enabled in the commerce checkout batch.",
|
|
||||||
"wechat_pay_checkout_not_enabled");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<PaymentNotificationResult> ParsePaymentNotificationAsync(
|
public Task<PaymentNotificationResult> ParsePaymentNotificationAsync(
|
||||||
@@ -73,6 +70,59 @@ internal sealed class WechatPayProvider : IPaymentProvider
|
|||||||
return WechatTenpayClientBuilder.Create(options).Build();
|
return WechatTenpayClientBuilder.Create(options).Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<CreatePaymentProviderResult> CreatePaymentCoreAsync(
|
||||||
|
PaymentProviderAccount account,
|
||||||
|
CreatePaymentProviderRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(request.OpenId))
|
||||||
|
{
|
||||||
|
throw new PaymentProviderException("WeChat Pay JSAPI requires openId.", "wechat_pay_openid_required");
|
||||||
|
}
|
||||||
|
|
||||||
|
var client = BuildClient(account);
|
||||||
|
var appId = Required(account.ConfigPublic, "appId");
|
||||||
|
var wxRequest = new CreatePayTransactionJsapiRequest
|
||||||
|
{
|
||||||
|
AppId = appId,
|
||||||
|
Description = request.Subject,
|
||||||
|
OutTradeNumber = request.OrderNo,
|
||||||
|
NotifyUrl = request.NotifyUrl,
|
||||||
|
Amount = new CreatePayTransactionJsapiRequest.Types.Amount
|
||||||
|
{
|
||||||
|
Total = request.AmountCents
|
||||||
|
},
|
||||||
|
Payer = new CreatePayTransactionJsapiRequest.Types.Payer
|
||||||
|
{
|
||||||
|
OpenId = request.OpenId
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var response = await client.ExecuteCreatePayTransactionJsapiAsync(wxRequest, cancellationToken);
|
||||||
|
if (!response.IsSuccessful())
|
||||||
|
{
|
||||||
|
throw new PaymentProviderException(
|
||||||
|
$"WeChat Pay create transaction failed: {response.ErrorCode}",
|
||||||
|
"wechat_pay_create_failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientPayload = JsonSerializer.SerializeToElement(
|
||||||
|
client.GenerateParametersForJsapiPayRequest(appId, response.PrepayId));
|
||||||
|
var rawPayload = JsonSerializer.SerializeToElement(new
|
||||||
|
{
|
||||||
|
response.PrepayId,
|
||||||
|
request.OrderNo,
|
||||||
|
request.AmountCents
|
||||||
|
});
|
||||||
|
|
||||||
|
return new CreatePaymentProviderResult(
|
||||||
|
Provider,
|
||||||
|
request.Method,
|
||||||
|
"pending",
|
||||||
|
null,
|
||||||
|
clientPayload,
|
||||||
|
rawPayload);
|
||||||
|
}
|
||||||
|
|
||||||
private static string Required(JsonElement element, params string[] keys)
|
private static string Required(JsonElement element, params string[] keys)
|
||||||
{
|
{
|
||||||
if (element.ValueKind == JsonValueKind.Object)
|
if (element.ValueKind == JsonValueKind.Object)
|
||||||
|
|||||||
Reference in New Issue
Block a user