|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
5 | | -using System.Net.Http; |
6 | 5 | using System.Security.Cryptography; |
7 | 6 | using System.Text; |
8 | 7 | using System.Text.Json; |
@@ -31,16 +30,17 @@ public QuickpayCheckoutPaymentProvider(UmbracoCommerceContext ctx, ILogger<Quick |
31 | 30 | public override bool CanCancelPayments => true; |
32 | 31 | public override bool CanCapturePayments => true; |
33 | 32 | public override bool CanRefundPayments => true; |
| 33 | + public override bool CanPartiallyRefundPayments => true; |
34 | 34 | public override bool CanFetchPaymentStatus => true; |
35 | 35 |
|
36 | 36 | public override bool FinalizeAtContinueUrl => false; |
37 | 37 |
|
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 | + ]; |
44 | 44 |
|
45 | 45 | public override async Task<PaymentFormResult> GenerateFormAsync(PaymentProviderContext<QuickpayCheckoutSettings> ctx, CancellationToken cancellationToken = default) |
46 | 46 | { |
@@ -383,35 +383,55 @@ public override async Task<ApiResult> CapturePaymentAsync(PaymentProviderContext |
383 | 383 | return ApiResult.Empty; |
384 | 384 | } |
385 | 385 |
|
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) |
387 | 388 | { |
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 | + } |
389 | 406 |
|
| 407 | + public override async Task<ApiResult?> RefundPaymentAsync(PaymentProviderContext<QuickpayCheckoutSettings> context, PaymentProviderOrderRefundRequest refundRequest, CancellationToken cancellationToken = default) |
| 408 | + { |
| 409 | + ArgumentNullException.ThrowIfNull(context); |
| 410 | + ArgumentNullException.ThrowIfNull(refundRequest); |
390 | 411 | try |
391 | 412 | { |
392 | | - var id = ctx.Order.TransactionInfo.TransactionId; |
| 413 | + string id = context.Order.TransactionInfo.TransactionId; |
393 | 414 |
|
394 | | - var clientConfig = GetQuickpayClientConfig(ctx.Settings); |
395 | | - var client = new QuickpayClient(clientConfig); |
| 415 | + QuickpayClientConfig clientConfig = GetQuickpayClientConfig(context.Settings); |
| 416 | + QuickpayClient client = new(clientConfig); |
396 | 417 |
|
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); |
402 | 422 |
|
403 | | - Operation lastCompletedOperation = payment.Operations.LastOrDefault(o => !o.Pending && o.QuickpayStatusCode == "20000"); |
| 423 | + Operation? lastCompletedOperation = payment.Operations.LastOrDefault(o => !o.Pending && o.QuickpayStatusCode == "20000"); |
404 | 424 |
|
405 | 425 | if (lastCompletedOperation != null) |
406 | 426 | { |
407 | | - var paymentStatus = GetPaymentStatus(lastCompletedOperation); |
| 427 | + PaymentStatus paymentStatus = GetPaymentStatus(lastCompletedOperation); |
408 | 428 |
|
409 | 429 | return new ApiResult() |
410 | 430 | { |
411 | 431 | TransactionInfo = new TransactionInfoUpdate() |
412 | 432 | { |
413 | 433 | TransactionId = GetTransactionId(payment), |
414 | | - PaymentStatus = paymentStatus |
| 434 | + PaymentStatus = paymentStatus, |
415 | 435 | } |
416 | 436 | }; |
417 | 437 | } |
|
0 commit comments