Skip to content

Commit fdd883e

Browse files
authored
support commerce's new partial refund feature (#14)
1 parent ea08305 commit fdd883e

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
</ItemGroup>
1212
<ItemGroup>
1313
<PackageVersion Include="Flurl.Http" Version="[4.0.2, 5)" />
14-
<PackageVersion Include="Umbraco.Commerce.Core" Version="[15.0.0, 15.999.999)" />
14+
<PackageVersion Include="Umbraco.Commerce.Core" Version="[15.3.0, 15.999.999)" />
1515
</ItemGroup>
1616
</Project>

src/Umbraco.Commerce.PaymentProviders.Quickpay/QuickpayCheckoutPaymentProvider.cs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Net.Http;
65
using System.Security.Cryptography;
76
using System.Text;
87
using System.Text.Json;
@@ -31,16 +30,17 @@ public QuickpayCheckoutPaymentProvider(UmbracoCommerceContext ctx, ILogger<Quick
3130
public override bool CanCancelPayments => true;
3231
public override bool CanCapturePayments => true;
3332
public override bool CanRefundPayments => true;
33+
public override bool CanPartiallyRefundPayments => true;
3434
public override bool CanFetchPaymentStatus => true;
3535

3636
public override bool FinalizeAtContinueUrl => false;
3737

38-
public override IEnumerable<TransactionMetaDataDefinition> TransactionMetaDataDefinitions => new[]
39-
{
40-
new TransactionMetaDataDefinition("quickpayOrderId", "Quickpay Order ID"),
41-
new TransactionMetaDataDefinition("quickpayPaymentId", "Quickpay Payment ID"),
42-
new TransactionMetaDataDefinition("quickpayPaymentHash", "Quickpay Payment Hash")
43-
};
38+
public override IEnumerable<TransactionMetaDataDefinition> TransactionMetaDataDefinitions =>
39+
[
40+
new TransactionMetaDataDefinition("quickpayOrderId"),
41+
new TransactionMetaDataDefinition("quickpayPaymentId"),
42+
new TransactionMetaDataDefinition("quickpayPaymentHash")
43+
];
4444

4545
public override async Task<PaymentFormResult> GenerateFormAsync(PaymentProviderContext<QuickpayCheckoutSettings> ctx, CancellationToken cancellationToken = default)
4646
{
@@ -383,35 +383,55 @@ public override async Task<ApiResult> CapturePaymentAsync(PaymentProviderContext
383383
return ApiResult.Empty;
384384
}
385385

386-
public override async Task<ApiResult> RefundPaymentAsync(PaymentProviderContext<QuickpayCheckoutSettings> ctx, CancellationToken cancellationToken = default)
386+
[Obsolete("Will be removed in v17. Use the overload that takes an order refund request instead.")]
387+
public override async Task<ApiResult?> RefundPaymentAsync(PaymentProviderContext<QuickpayCheckoutSettings> context, CancellationToken cancellationToken = default)
387388
{
388-
// POST: /payments/{id}/refund
389+
ArgumentNullException.ThrowIfNull(context);
390+
391+
StoreReadOnly store = await Context.Services.StoreService.GetStoreAsync(context.Order.StoreId);
392+
Amount refundAmount = store.CanRefundTransactionFee ? context.Order.TransactionInfo.AmountAuthorized + context.Order.TransactionInfo.TransactionFee : context.Order.TransactionInfo.AmountAuthorized;
393+
return await this.RefundPaymentAsync(
394+
context,
395+
new PaymentProviderOrderRefundRequest
396+
{
397+
RefundAmount = refundAmount,
398+
Orderlines = context.Order.OrderLines.Select(x => new PaymentProviderOrderlineRefundRequest
399+
{
400+
OrderLineId = x.Id,
401+
Quantity = x.Quantity,
402+
}),
403+
},
404+
cancellationToken);
405+
}
389406

407+
public override async Task<ApiResult?> RefundPaymentAsync(PaymentProviderContext<QuickpayCheckoutSettings> context, PaymentProviderOrderRefundRequest refundRequest, CancellationToken cancellationToken = default)
408+
{
409+
ArgumentNullException.ThrowIfNull(context);
410+
ArgumentNullException.ThrowIfNull(refundRequest);
390411
try
391412
{
392-
var id = ctx.Order.TransactionInfo.TransactionId;
413+
string id = context.Order.TransactionInfo.TransactionId;
393414

394-
var clientConfig = GetQuickpayClientConfig(ctx.Settings);
395-
var client = new QuickpayClient(clientConfig);
415+
QuickpayClientConfig clientConfig = GetQuickpayClientConfig(context.Settings);
416+
QuickpayClient client = new(clientConfig);
396417

397-
var payment = await client.RefundPaymentAsync(id, new
398-
{
399-
amount = AmountToMinorUnits(ctx.Order.TransactionInfo.AmountAuthorized.Value)
400-
},
401-
cancellationToken);
418+
QuickpayPayment payment = await client.RefundPaymentAsync(
419+
id,
420+
new { amount = AmountToMinorUnits(refundRequest.RefundAmount) },
421+
cancellationToken);
402422

403-
Operation lastCompletedOperation = payment.Operations.LastOrDefault(o => !o.Pending && o.QuickpayStatusCode == "20000");
423+
Operation? lastCompletedOperation = payment.Operations.LastOrDefault(o => !o.Pending && o.QuickpayStatusCode == "20000");
404424

405425
if (lastCompletedOperation != null)
406426
{
407-
var paymentStatus = GetPaymentStatus(lastCompletedOperation);
427+
PaymentStatus paymentStatus = GetPaymentStatus(lastCompletedOperation);
408428

409429
return new ApiResult()
410430
{
411431
TransactionInfo = new TransactionInfoUpdate()
412432
{
413433
TransactionId = GetTransactionId(payment),
414-
PaymentStatus = paymentStatus
434+
PaymentStatus = paymentStatus,
415435
}
416436
};
417437
}

src/Umbraco.Commerce.PaymentProviders.Quickpay/Umbraco.Commerce.PaymentProviders.Quickpay.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<Title>Umbraco Commerce Quickpay Payment Provider</Title>
55
<Description>Quickpay payment provider for Umbraco Commerce</Description>
66
<StaticWebAssetBasePath>App_Plugins/UmbracoCommerceQuickpayPaymentProvider</StaticWebAssetBasePath>
7+
<NoWarn>IDE0290;IDE0058;CA2007;IDE0021;IDE0022;</NoWarn>
8+
<Nullable>enable</Nullable>
79
</PropertyGroup>
810

911
<ItemGroup>

0 commit comments

Comments
 (0)