Skip to content

Commit

Permalink
Merge pull request #46 from pajohns/master
Browse files Browse the repository at this point in the history
Resolve issue #45 - PinchApiOptions to be readonly
  • Loading branch information
dkarzon authored Dec 19, 2023
2 parents b742ae0 + 46e0e06 commit afa2fe6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,6 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

# JetBrains Rider files
.idea/
31 changes: 15 additions & 16 deletions src/Pinch.SDK.WebSample/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,23 @@ protected PinchApi GetApi()

if (token != null)
{
return new PinchApi(_settings.MerchantId, _settings.SecretKey, new PinchApiOptions(){
IsLive = _settings.IsLive,
BaseUri = _settings.BaseUri,
AuthUri = _settings.AuthUri,
ApplicationId = _settings.ApplicationId,
AccessToken = token.AccessToken,
RefreshToken = token.RefreshToken,
ImpersonateMerchantId = ImpersonatedMerchantId
});
return new PinchApi(_settings.MerchantId, _settings.SecretKey, new PinchApiOptions(
isLive: _settings.IsLive,
baseUri: _settings.BaseUri,
authUri: _settings.AuthUri,
applicationId: _settings.ApplicationId,
accessToken: token.AccessToken,
refreshToken: token.RefreshToken,
impersonateMerchantId: ImpersonatedMerchantId
));
}

return new PinchApi(_settings.MerchantId, _settings.SecretKey, new PinchApiOptions()
{
IsLive = _settings.IsLive,
BaseUri = _settings.BaseUri,
AuthUri = _settings.AuthUri,
ImpersonateMerchantId = ImpersonatedMerchantId
});
return new PinchApi(_settings.MerchantId, _settings.SecretKey, new PinchApiOptions(
isLive: _settings.IsLive,
baseUri: _settings.BaseUri,
authUri: _settings.AuthUri,
impersonateMerchantId: ImpersonatedMerchantId
));
}
}
}
22 changes: 1 addition & 21 deletions src/Pinch.SDK/PinchApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Pinch.SDK.Agreements;
Expand Down Expand Up @@ -77,9 +75,7 @@ public class PinchApi
/// <param name="secretKey">Your Secret Key</param>
/// <param name="isLive">Set to false to use the sandbox test system. Set to true to perform live production transactions.</param>
public PinchApi(string merchantId, string secretKey, bool isLive)
:this(merchantId, secretKey, new PinchApiOptions() {
IsLive = isLive
})
:this(merchantId, secretKey, new PinchApiOptions(isLive: isLive))
{
}

Expand All @@ -93,22 +89,6 @@ public PinchApi(string merchantId, string secretKey, bool isLive)
public PinchApi(string merchantId, string secretKey, PinchApiOptions options, Func<HttpClient> httpClientFactory = null)
{
_httpClientFactory = httpClientFactory;

options.ApiVersion = !string.IsNullOrEmpty(options.ApiVersion)
? options.ApiVersion
: Settings.LatestApiVersion;

if (!string.IsNullOrEmpty(options.BaseUri))
{
options.BaseUri = $"{options.BaseUri.TrimEnd('/')}/{(options.IsLive ? "live" : "test")}/";
}
else
{
options.BaseUri = options.IsLive ? Settings.ApiBaseUri_Production : Settings.ApiBaseUri_Test;
}

options.AuthUri = !string.IsNullOrEmpty(options.AuthUri) ? options.AuthUri : Settings.AuthBaseUri_Production;

_secretKey = secretKey;
_clientId = merchantId;
_accessToken = options.AccessToken;
Expand Down
60 changes: 46 additions & 14 deletions src/Pinch.SDK/PinchApiOptions.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Pinch.SDK
{
public class PinchApiOptions
{
public bool IsLive { get; set; }
public string BaseUri { get; set; }
public string AuthUri { get; set; }
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public string ApplicationId { get; set; }
public PinchApiOptions(
bool? isLive = null,
string baseUri = null,
string authUri = null,
string accessToken = null,
string refreshToken = null,
string applicationId = null,
string apiVersion = null,
string impersonateMerchantId = null,
int? webhookVerificationClockSkewThreshold = null,
List<string> additionalScopes = null)
{
IsLive = isLive ?? false;
ApiVersion = !string.IsNullOrEmpty(apiVersion)
? apiVersion
: Settings.LatestApiVersion;

if (!string.IsNullOrEmpty(baseUri))
{
BaseUri = $"{baseUri.TrimEnd('/')}/{(IsLive ? "live" : "test")}/";
}
else
{
BaseUri = IsLive ? Settings.ApiBaseUri_Production : Settings.ApiBaseUri_Test;
}

AuthUri = !string.IsNullOrEmpty(authUri) ? authUri : Settings.AuthBaseUri_Production;
AccessToken = accessToken;
RefreshToken = refreshToken;
ApplicationId = applicationId;
ImpersonateMerchantId = impersonateMerchantId;
WebhookVerificationClockSkewThreshold = webhookVerificationClockSkewThreshold ?? 300; // Defaults to 5 minutes
AdditionalScopes = additionalScopes;
}

public bool IsLive { get; }
public string BaseUri { get; }
public string AuthUri { get; }
public string AccessToken { get; }
public string RefreshToken { get; }
public string ApplicationId { get; }

/// <summary>
/// Set this to specify which version of the API you have coded against. Omitting this in the SDK is fine as we'll
/// use the most recent version. Used for backwards compatibility.
/// </summary>
public string ApiVersion { get; set; }
public string ApiVersion { get; }

/// <summary>
/// Set this Merchant ID to impersonate a different merchant.
/// </summary>
public string ImpersonateMerchantId { get; set; }
public string ImpersonateMerchantId { get; }

/// <summary>
/// The maximum difference in seconds between the current time and the webhook timestamp.
/// Used to verify authenticity of webhook requests and to avoid replay attacks.
/// </summary>
public int WebhookVerificationClockSkewThreshold { get; set; } = 300; // 5 minutes
public int WebhookVerificationClockSkewThreshold { get; }

/// <summary>
/// List of additional scopes to request
/// </summary>
public List<string> AdditionalScopes { get; set; }
public List<string> AdditionalScopes { get; }
}
}

0 comments on commit afa2fe6

Please sign in to comment.