Skip to content

Commit 0a742d9

Browse files
authored
Upgrade to commerce v14 (#6)
* Upgrade to commerce v14 * Remove redundant client build process * Remove redundant build process * Migrate from context.Request to context.HttpContext.Request * Delete src/Umbraco.Commerce.PaymentProviders.Buckaroo/umbraco-package-schema.json * Clean up translation keys; upgrade to commerce 14-alpha3 * update commerce version * update packages.lock.json * Create an error order when the payment is rejected from buckaroo side. * Remove packages.lock.json * update localization file
1 parent 81f42a1 commit 0a742d9

File tree

11 files changed

+99
-33
lines changed

11 files changed

+99
-33
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,5 @@ $RECYCLE.BIN/
473473
##
474474
/local_pack.bat
475475
**/packages.lock.json
476-
476+
src/Umbraco.Commerce.PaymentProviders.Buckaroo/packages.lock.json
477+
src/**/packages.lock.json

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ItemGroup>
1313
<PackageVersion Include="BuckarooSdk" Version="[1.4.1, 2)" />
1414
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="[2.2.0, 3)" />
15-
<PackageVersion Include="Umbraco.Commerce.Core" Version="[13.0.0, 14)" />
15+
<PackageVersion Include="Umbraco.Commerce.Core" Version="[14.0.0--beta2.preview.501.gc4c8b29, 15)" />
1616
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="[6.0.0, 7)" />
1717
</ItemGroup>
18-
</Project>
18+
</Project>

src/Umbraco.Commerce.PaymentProviders.Buckaroo/BuckarooOneTimePaymentProvider.cs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
5-
using System.Net.Http;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using BuckarooSdk.Connection;
98
using BuckarooSdk.DataTypes;
109
using BuckarooSdk.DataTypes.RequestBases;
1110
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Http.Extensions;
12+
using Microsoft.Extensions.Primitives;
1213
using Umbraco.Commerce.Common.Logging;
1314
using Umbraco.Commerce.Core.Api;
1415
using Umbraco.Commerce.Core.Models;
@@ -19,7 +20,7 @@
1920

2021
namespace Umbraco.Commerce.PaymentProviders.Buckaroo
2122
{
22-
[PaymentProvider("buckaroo-onetime-payment", "Buckaroo One Time Payment", "Buckaroo one time payment provider")]
23+
[PaymentProvider("buckaroo-onetime-payment")]
2324
public class BuckarooOneTimePaymentProvider : BuckarooPaymentProviderBase<BuckarooOneTimePaymentProvider, BuckarooOneTimeSettings>
2425
{
2526
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -102,16 +103,19 @@ public override async Task<CallbackResult> ProcessCallbackAsync(PaymentProviderC
102103

103104
try
104105
{
105-
BuckarooWebhookTransaction? buckarooEvent = await ParseWebhookDataAsync(context, cancellationToken).ConfigureAwait(false);
106-
if (buckarooEvent == null || !buckarooEvent.IsSuccess)
106+
BuckarooWebhookTransaction buckarooEvent = ParseWebhookData(context);
107+
if (!buckarooEvent.IsSuccess)
107108
{
108-
// Just returns OK without finalizing the order
109-
return CallbackResult.Empty;
109+
return CallbackResult.Ok(new TransactionInfo
110+
{
111+
TransactionId = buckarooEvent.Key,
112+
PaymentStatus = buckarooEvent.Status.Code.Code.ToPaymentStatus(),
113+
});
110114
}
111115

112116
OrderReadOnly order = context.Order;
113117

114-
var transactionInfo = new TransactionInfo
118+
TransactionInfo transactionInfo = new()
115119
{
116120
TransactionId = buckarooEvent.Key,
117121
PaymentStatus = buckarooEvent.Status.Code.Code.ToPaymentStatus(),
@@ -128,31 +132,33 @@ public override async Task<CallbackResult> ProcessCallbackAsync(PaymentProviderC
128132
return CallbackResult.BadRequest();
129133
}
130134

131-
private async Task<BuckarooWebhookTransaction?> ParseWebhookDataAsync(PaymentProviderContext<BuckarooOneTimeSettings> context, CancellationToken cancellationToken)
135+
private BuckarooWebhookTransaction ParseWebhookData(PaymentProviderContext<BuckarooOneTimeSettings> context)
132136
{
133-
HttpRequestMessage request = context.Request;
134-
request.Headers.TryGetValues("Authorization", out IEnumerable<string>? headers);
135-
if (headers == null || string.IsNullOrEmpty(headers.FirstOrDefault()))
137+
Logger.Info("Begin parsing buckaroo callback data.");
138+
139+
HttpRequest request = context.HttpContext.Request;
140+
request.Headers.TryGetValue("Authorization", out StringValues header);
141+
string?[] authorizationHeaders = header.ToArray();
142+
if (authorizationHeaders == null || string.IsNullOrEmpty(authorizationHeaders.FirstOrDefault()))
136143
{
137-
throw new BuckarooWebhookInvalidAuthorizationHeaderException(context.Order.OrderNumber, context.Order.CartNumber, request.RequestUri!);
144+
throw new BuckarooWebhookInvalidAuthorizationHeaderException(context.Order.OrderNumber, context.Order.CartNumber, request.GetDisplayUrl());
138145
}
139146

140-
if (request.Content == null)
147+
if (request.Body == null)
141148
{
142-
throw new BuckarooWebhookEmptyBodyException(context.Order.OrderNumber, context.Order.CartNumber, request.RequestUri!);
149+
throw new BuckarooWebhookEmptyBodyException(context.Order.OrderNumber, context.Order.CartNumber, request.GetDisplayUrl());
143150
}
144151

145152
#pragma warning disable CA1308 // Do not normalize strings to uppercase because Buckaroo asks for lowercase string ¯\_(ツ)_/¯
146-
string webhookHostname = !string.IsNullOrWhiteSpace(context.Settings.WebhookHostnameOverwrite) ? context.Settings.WebhookHostnameOverwrite : request.RequestUri!.Authority;
147-
string encodedUri = WebUtility.UrlEncode(webhookHostname + request.RequestUri!.PathAndQuery).ToLowerInvariant();
153+
string webhookHostname = !string.IsNullOrWhiteSpace(context.Settings.WebhookHostnameOverwrite) ? context.Settings.WebhookHostnameOverwrite : request.Host.Host;
154+
string encodedUri = WebUtility.UrlEncode(webhookHostname + request.GetEncodedPathAndQuery()).ToLowerInvariant();
148155
#pragma warning restore CA1308 // Normalize strings to uppercase
149156

150-
byte[] requestBody = await request.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
151-
string authorizationHeader = headers.First();
157+
byte[] requestBody = request.Body.ToByteArray();
152158
BuckarooApiCredentials apiCredentials = context.Settings.GetApiCredentials();
153159

154160
SignatureCalculationService signatureService = new();
155-
bool signatureVerified = signatureService.VerifySignature(requestBody, request.Method.Method.ToUpperInvariant(), encodedUri, apiCredentials.SecretKey, authorizationHeader);
161+
bool signatureVerified = signatureService.VerifySignature(requestBody, request.Method.ToUpperInvariant(), encodedUri, apiCredentials.SecretKey, authorizationHeaders.First());
156162
if (!signatureVerified)
157163
{
158164
throw new BuckarooWebhookInvalidSignatureException(context.Order.OrderNumber, context.Order.CartNumber, encodedUri);

src/Umbraco.Commerce.PaymentProviders.Buckaroo/BuckarooSettingsBase.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ namespace Umbraco.Commerce.PaymentProviders.Buckaroo
44
{
55
public class BuckarooSettingsBase
66
{
7-
[PaymentProviderSetting(Name = "Continue URL", Description = "The URL to continue to after this provider has done processing. eg: /continue/", SortOrder = 100)]
7+
[PaymentProviderSetting]
88
public string ContinueUrl { get; set; } = string.Empty;
99

10-
[PaymentProviderSetting(Name = "Cancel URL", Description = "The URL to return to if the payment attempt is canceled. eg: /cart/", SortOrder = 200)]
10+
[PaymentProviderSetting]
1111
public string CancelUrl { get; set; } = string.Empty;
1212

13-
[PaymentProviderSetting(Name = "Error URL", Description = "The URL to return to if the payment attempt errors. eg: /error/", SortOrder = 300)]
13+
[PaymentProviderSetting]
1414
public string ErrorUrl { get; set; } = string.Empty;
1515

16-
[PaymentProviderSetting(Name = "Website key", Description = "The website key, which can be found here: https://plaza.buckaroo.nl/Configuration/WebSite/Index/", SortOrder = 400)]
16+
[PaymentProviderSetting]
1717
public string WebsiteKey { get; set; } = string.Empty;
1818

1919
/// <summary>
2020
/// Gets or sets secret key.
2121
/// </summary>
22-
[PaymentProviderSetting(Name = "Secret key", Description = "The secret key, which can be found here: https://plaza.buckaroo.nl/Configuration/Merchant/SecretKey", SortOrder = 500)]
22+
[PaymentProviderSetting]
2323
public string ApiKey { get; set; } = string.Empty;
2424

25-
[PaymentProviderSetting(Name = "Webhook hostname overwrite", Description = "If you rewrite incoming host headers to a different value, set this to the hostname where the buyer does the checkout action. Enter hostname only eg: 'umbraco.com'", SortOrder = 600)]
25+
[PaymentProviderSetting]
2626
public string WebhookHostnameOverwrite { get; set; } = string.Empty;
2727

28-
[PaymentProviderSetting(Name = "Enable test mode", Description = "Set whether to process payments in test mode", SortOrder = 10000)]
28+
[PaymentProviderSetting]
2929
public bool IsTestMode { get; set; }
3030
}
3131
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
3+
namespace Umbraco.Commerce.PaymentProviders.Buckaroo.Extensions
4+
{
5+
internal static class StreamExtensions
6+
{
7+
public static byte[] ToByteArray(this Stream stream)
8+
{
9+
if (stream is MemoryStream memoryStream)
10+
{
11+
return memoryStream.ToArray();
12+
}
13+
14+
using (MemoryStream ms = new())
15+
{
16+
stream.CopyTo(ms);
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}

src/Umbraco.Commerce.PaymentProviders.Buckaroo/Umbraco.Commerce.PaymentProviders.Buckaroo.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
44
<Title>Umbraco Commerce Buckaroo Payment Provider</Title>
55
<Description>Buckaroo payment provider for Umbraco Commerce</Description>
66
<Nullable>enable</Nullable>
7+
<StaticWebAssetBasePath>App_Plugins/UmbracoCommerceBuckarooPaymentProvider</StaticWebAssetBasePath>
78
</PropertyGroup>
89

910
<ItemGroup>

src/Umbraco.Commerce.PaymentProviders.Buckaroo/Webhooks/Exceptions/BuckarooWebhookEmptyBodyException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class BuckarooWebhookEmptyBodyException : Exception
1111
public BuckarooWebhookEmptyBodyException(
1212
string orderNumber,
1313
string cartNumber,
14-
Uri webhookUri)
14+
string webhookUri)
1515
: base(string.Format(CultureInfo.InvariantCulture, _messageFormat, orderNumber, cartNumber, webhookUri))
1616
{
1717
}

src/Umbraco.Commerce.PaymentProviders.Buckaroo/Webhooks/Exceptions/BuckarooWebhookInvalidAuthorizationHeaderException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class BuckarooWebhookInvalidAuthorizationHeaderException : Exception
1111
public BuckarooWebhookInvalidAuthorizationHeaderException(
1212
string orderNumber,
1313
string cartNumber,
14-
Uri webhookUri)
14+
string webhookUri)
1515
: base(string.Format(CultureInfo.InvariantCulture, _messageFormat, orderNumber, cartNumber, webhookUri))
1616
{
1717
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default {
2+
ucPaymentProviders: {
3+
'buckarooOnetimePaymentLabel': 'Buckaroo One Time Payment',
4+
'buckarooOnetimePaymentDescription': 'Buckaroo payment provider for one time checkout',
5+
'buckarooOnetimePaymentSettingsContinueUrlLabel': 'Continue URL',
6+
'buckarooOnetimePaymentSettingsContinueUrlDescription': 'The URL to continue to after this provider has done processing. eg: /continue/',
7+
'buckarooOnetimePaymentSettingsCancelUrlLabel': 'Cancel URL',
8+
'buckarooOnetimePaymentSettingsCancelUrlDescription': 'The URL to return to if the payment attempt is canceled. eg: /cart/',
9+
'buckarooOnetimePaymentSettingsErrorUrlLabel': 'Error URL',
10+
'buckarooOnetimePaymentSettingsErrorUrlDescription': 'The URL to return to if the payment attempt errors. eg: /error/',
11+
'buckarooOnetimePaymentSettingsWebsiteKeyLabel': 'Website key',
12+
'buckarooOnetimePaymentSettingsWebsiteKeyDescription': 'The website key, which can be found here: https://plaza.buckaroo.nl/Configuration/WebSite/Index/',
13+
'buckarooOnetimePaymentSettingsApiKeyLabel': 'Secret key',
14+
'buckarooOnetimePaymentSettingsApiKeyDescription': 'The secret key, which can be found here: https://plaza.buckaroo.nl/Configuration/Merchant/SecretKey',
15+
'buckarooOnetimePaymentSettingsWebhookHostnameOverwriteLabel': 'Webhook hostname overwrite',
16+
'buckarooOnetimePaymentSettingsWebhookHostnameOverwriteDescription': 'If you rewrite incoming host headers to a different value, set this to the hostname where the buyer does the checkout action. Enter hostname only eg: \'umbraco.com\'',
17+
'buckarooOnetimePaymentSettingsIsTestModeLabel': 'Enable test mode',
18+
'buckarooOnetimePaymentSettingsIsTestModeDescription': 'Set whether to process payments in test mode',
19+
},
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Umbraco Commerce Buckaroo Payment Provider",
3+
"$schema": "../umbraco-package-schema.json",
4+
"extensions": [
5+
{
6+
"type": "localization",
7+
"alias": "UmbracoCommerce.PaymentProviders.Buckaroo.Localization.En_US",
8+
"weight": -100,
9+
"name": "English (US)",
10+
"meta": {
11+
"culture": "en-us"
12+
},
13+
"js": "/App_Plugins/UmbracoCommerceBuckarooPaymentProvider/lang/en-us.js"
14+
}
15+
],
16+
"version": ""
17+
}

0 commit comments

Comments
 (0)