Skip to content

Commit

Permalink
Amazon Pay API SDK (.NET) 2.4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Shangamesh T committed Jun 1, 2021
1 parent fd7e331 commit ec45b7e
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 9 deletions.
46 changes: 46 additions & 0 deletions Amazon.Pay.API.SDK.Tests/ApiConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,51 @@ public void ThrowFileNotFoundException(string privateKey)
privateKey: privateKey
);
}

[Test]
public void CanInstantiateApiConfigurationForEnvironmentSpecificPublicKeyId()
{
// Fake Public keys
const string livePublicKeyId = "LIVE-XXXXXXXXXXXXXXXXXXXXXXXX";
const string sandboxPublicKeyId = "SANDBOX-XXXXXXXXXXXXXXXXXXXXXXXX";

// Testing Live PublicKeyId for region UnitedStates
AssertEnvironmentSpecificPublicKeyId(Region.UnitedStates, livePublicKeyId);

// Testing Live PublicKeyId for region Europe
AssertEnvironmentSpecificPublicKeyId(Region.Europe, livePublicKeyId);

// Testing Live PublicKeyId for region Japan
AssertEnvironmentSpecificPublicKeyId(Region.Japan, livePublicKeyId);

// Testing Sandbox PublicKeyId for region UnitedStates
AssertEnvironmentSpecificPublicKeyId(Region.UnitedStates, sandboxPublicKeyId);

// Testing Sandbox PublicKeyId for region Europe
AssertEnvironmentSpecificPublicKeyId(Region.Europe, sandboxPublicKeyId);

// Testing Sandbox PublicKeyId for region Japan
AssertEnvironmentSpecificPublicKeyId(Region.Japan, sandboxPublicKeyId);
}

// Generic methods to assert environment specific publicKeyId
private void AssertEnvironmentSpecificPublicKeyId(Region region, string publicKeyId)
{
// Configuration
var payConfig = new ApiConfiguration
(
region: region,
publicKeyId: publicKeyId,
privateKey: "-----BEGIN RSA PRIVATE KEY-----" // Fake Private key
);

// Assertion
Assert.NotNull(payConfig);
Assert.AreEqual(region, payConfig.Region);
Assert.AreEqual(publicKeyId, payConfig.PublicKeyId);
Assert.AreEqual(3, payConfig.MaxRetries);
Assert.AreEqual(Constants.ApiVersion, payConfig.ApiVersion);
payConfig.PrivateKey.Should().StartWith("-----BEGIN RSA");
}
}
}
155 changes: 155 additions & 0 deletions Amazon.Pay.API.SDK.Tests/ApiUrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class ApiUrlBuilderTests
{
private ApiUrlBuilder apiUrlBuilder;
private ApiConfiguration payConfig;
// Fake Public keys
private const string livePublicKeyId = "LIVE-XXXXXXXXXXXXXXXXXXXXXXXX";
private const string sandboxPublicKeyId = "SANDBOX-XXXXXXXXXXXXXXXXXXXXXXXX";

[SetUp]
public void SetUp()
Expand Down Expand Up @@ -135,5 +138,157 @@ public void GetFullPathForTokenExchangeApi()
// assert
Assert.AreEqual(expectedURL, actualURL);
}

[Test]
public void GetApiUnifiedEndPointBaseUrlForUnitedStates()
{
string expectedURL = "https://pay-api.amazon.com/";

// Scenario 1 : Testing Unified endpoint base URL by passing Live specific PublicKeyId for UnitedStates
VerifyUnifiedEndpointBaseURL(Region.UnitedStates, livePublicKeyId, expectedURL);

// Scenario 2 : Testing Unified endpoint base URL by passing Sandbox specific PublicKeyId for UnitedStates
VerifyUnifiedEndpointBaseURL(Region.UnitedStates, sandboxPublicKeyId, expectedURL);
}

[Test]
public void GetApiUnifiedEndPointBaseUrlForForEurope()
{
string expectedURL = "https://pay-api.amazon.eu/";

// Scenario 1 : Testing Unified endpoint base URL by passing Live specific PublicKeyId for Europe
VerifyUnifiedEndpointBaseURL(Region.Europe, livePublicKeyId, expectedURL);

// Scenario 2 : Testing Unified endpoint base URL by passing Sandbox specific PublicKeyId for Europe
VerifyUnifiedEndpointBaseURL(Region.Europe, sandboxPublicKeyId, expectedURL);
}

[Test]
public void GetApiUnifiedEndPointBaseUrlForForJapan()
{
string expectedURL = "https://pay-api.amazon.jp/";

// Scenario 1 : Testing Unified endpoint base URL by passing Live specific PublicKeyId for Japan
VerifyUnifiedEndpointBaseURL(Region.Japan, livePublicKeyId, expectedURL);

// Scenario 2 : Testing Unified endpoint base URL by passing Sandbox specific PublicKeyId for Japan
VerifyUnifiedEndpointBaseURL(Region.Japan, sandboxPublicKeyId, expectedURL);
}

// Generic method used to verify Unified Endpoint Base URL
public void VerifyUnifiedEndpointBaseURL(Region region, string publicKeyId, string url)
{
// Configuration
payConfig.Region = region;
payConfig.PublicKeyId = publicKeyId;
apiUrlBuilder = new ApiUrlBuilder(payConfig);

// Building URL
Uri expectedURL = new Uri(url);
Uri actualURL = apiUrlBuilder.GetApiEndPointBaseUrl();

// Assertion
Assert.AreEqual(expectedURL, actualURL);
}

[Test]
public void GetUnifiedEndpointFullPathForInStoreApiService()
{
string expectedUnitedStatesURL = "https://pay-api.amazon.com/v2/in-store/merchantScan/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for UnitedStates
VerifyUnifiedEndpointFullPath(Region.UnitedStates, livePublicKeyId, expectedUnitedStatesURL,
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
VerifyUnifiedEndpointFullPath(Region.UnitedStates, sandboxPublicKeyId, expectedUnitedStatesURL,
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);

string expectedEuropeURL = "https://pay-api.amazon.eu/v2/in-store/merchantScan/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for Europe
VerifyUnifiedEndpointFullPath(Region.Europe, livePublicKeyId, expectedEuropeURL,
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
VerifyUnifiedEndpointFullPath(Region.Europe, sandboxPublicKeyId, expectedEuropeURL,
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);

string expectedJapanURL = "https://pay-api.amazon.jp/v2/in-store/merchantScan/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for Japan
VerifyUnifiedEndpointFullPath(Region.Japan, livePublicKeyId, expectedJapanURL,
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
VerifyUnifiedEndpointFullPath(Region.Japan, sandboxPublicKeyId, expectedJapanURL,
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
}

[Test]
public void GetUnifiedEndpointFullPathForDeliveryTrackerApiService()
{
string expectedUnitedStatesURL = "https://pay-api.amazon.com/v2/deliveryTrackers/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for UnitedStates
VerifyUnifiedEndpointFullPath(Region.UnitedStates, livePublicKeyId, expectedUnitedStatesURL,
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
VerifyUnifiedEndpointFullPath(Region.UnitedStates, sandboxPublicKeyId, expectedUnitedStatesURL,
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);

string expectedEuropeURL = "https://pay-api.amazon.eu/v2/deliveryTrackers/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for Europe
VerifyUnifiedEndpointFullPath(Region.Europe, livePublicKeyId, expectedEuropeURL,
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
VerifyUnifiedEndpointFullPath(Region.Europe, sandboxPublicKeyId, expectedEuropeURL,
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);

string expectedJapanURL = "https://pay-api.amazon.jp/v2/deliveryTrackers/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for Japan
VerifyUnifiedEndpointFullPath(Region.Japan, livePublicKeyId, expectedJapanURL,
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
VerifyUnifiedEndpointFullPath(Region.Japan, sandboxPublicKeyId, expectedJapanURL,
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
}

[Test]
public void GetUnifiedEndpointFullPathForTokenExchangeApi()
{
string expectedUnitedStatesURL = "https://pay-api.amazon.com/v2/authorizationTokens/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for UnitedStates
VerifyUnifiedEndpointFullPath(Region.UnitedStates, livePublicKeyId, expectedUnitedStatesURL,
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
VerifyUnifiedEndpointFullPath(Region.UnitedStates, sandboxPublicKeyId, expectedUnitedStatesURL,
Constants.ApiServices.Default, Constants.Resources.TokenExchange);

string expectedEuropeURL = "https://pay-api.amazon.eu/v2/authorizationTokens/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for Europe
VerifyUnifiedEndpointFullPath(Region.Europe, livePublicKeyId, expectedEuropeURL,
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
VerifyUnifiedEndpointFullPath(Region.Europe, sandboxPublicKeyId, expectedEuropeURL,
Constants.ApiServices.Default, Constants.Resources.TokenExchange);

string expectedJapanURL = "https://pay-api.amazon.jp/v2/authorizationTokens/";

// Testing Unified endpoint full path by passing environment specific PublicKeyId for Japan
VerifyUnifiedEndpointFullPath(Region.Japan, livePublicKeyId, expectedJapanURL,
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
VerifyUnifiedEndpointFullPath(Region.Japan, sandboxPublicKeyId, expectedJapanURL,
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
}

// Generic method used to verify Unified Endpoint Full Path
public void VerifyUnifiedEndpointFullPath(Region region, string publicKeyId, string url, string apiService, string resource)
{
// Configuration
payConfig.Region = region;
payConfig.PublicKeyId = publicKeyId;
apiUrlBuilder = new ApiUrlBuilder(payConfig);

// Building URL
Uri expectedURL = new Uri(url);
Uri actualURL = apiUrlBuilder.BuildFullApiPath(apiService, resource);

// Assertion
Assert.AreEqual(expectedURL, actualURL);
}
}
}
4 changes: 2 additions & 2 deletions Amazon.Pay.API.SDK/Amazon.Pay.API.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>2.4.7</Version>
<AssemblyVersion>2.4.7.0</AssemblyVersion>
<Version>2.4.8</Version>
<AssemblyVersion>2.4.8.0</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Amazon Pay</Authors>
<Company>Amazon</Company>
Expand Down
11 changes: 9 additions & 2 deletions Amazon.Pay.API.SDK/ApiUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ public Uri GetApiEndPointBaseUrl()
{
string regionDomain = config.Region.ToDomain();
string environment = config.Environment.ToString().ToLower();
string serviceURL;

string serviceURL = $"https://pay-api.amazon.{regionDomain}/{environment}/";

if (config.PublicKeyId.ToUpper().StartsWith("LIVE") || config.PublicKeyId.ToUpper().StartsWith("SANDBOX"))
{
serviceURL = $"https://pay-api.amazon.{regionDomain}/";
}
else
{
serviceURL = $"https://pay-api.amazon.{regionDomain}/{environment}/";
}
return new Uri(serviceURL);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Amazon.Pay.API.SDK/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Amazon.Pay.API
{
public class Constants
{
public const string SdkVersion = "2.4.7.0";
public const string SdkVersion = "2.4.8.0";
public const string SdkName = "amazon-pay-api-sdk-dotnet";
public const int ApiVersion = 2;

Expand Down
14 changes: 14 additions & 0 deletions Amazon.Pay.API.SDK/Types/ApiConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ public ApiConfiguration(Region region, Environment environment, string publicKey
PrivateKey = privateKey;
}

/// <summary>
/// Initializes a new instance of the ApiConfiguration class without Enviroment.
/// Use this initialization for having environment specific publicKeyId (i.e PublicKeyId starts with prefix LIVE or SANDBOX)
/// </summary>
/// <param name="region">The payment region the Amazon Pay merchant account is registered in.</param>
/// <param name="publicKeyId">The identifier for the registered key pair.</param>
/// <param name="privateKey">The private key in form of a file path, or directly as a string.</param>
public ApiConfiguration(Region region, string publicKeyId, string privateKey)
{
Region = region;
PublicKeyId = publicKeyId;
PrivateKey = privateKey;
}

/// <summary>
/// The payment region the Amazon Pay merchant account is registered in.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Amazon.Pay.API.SDK/WebStore/Types/PaymentDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,11 @@ internal void OnSerialized(StreamingContext content)
/// </summary>
[JsonProperty(PropertyName = "softDescriptor")]
public string SoftDescriptor { get; set; }

/// <summary>
/// Estimate Order Total. TODO : Replace this with the version from the guide, once available
/// </summary>
[JsonProperty(PropertyName = "estimateOrderTotal")]
public Price EstimateOrderTotal { get; set; }
}
}
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version 2.4.8 - May 2021
* Enabled support for environment specific keys (i.e Public key & Private key). The changes are fully backwards-compatible, where merchants can also use non environment specific keys
* Added "EstimateOrderTotal" to PaymentDetails

### Version 2.4.7 - May 2021
* Added "recurringMetadata" to UpdateChargePermissionRequest for ChargePermission API
* Added "HasPrimeMembershipType" to BuyerResponse for Buyer API
Expand Down
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ This SDK is compatible with .NET Standard 2.0 (including .NET Core 2.0), as well

## SDK Installation

This SDK can be downloaded from NuGet [here](https://www.nuget.org/packages/Amazon.Pay.API.SDK) or GitHub [here](https://github.com/amzn/amazon-pay-api-sdk-dotnet/releases/download/2.4.7/Amazon.Pay.API.SDK.2.4.7.nupkg).
This SDK can be downloaded from NuGet [here](https://www.nuget.org/packages/Amazon.Pay.API.SDK) or GitHub [here](https://github.com/amzn/amazon-pay-api-sdk-dotnet/releases/download/2.4.8/Amazon.Pay.API.SDK.2.4.8.nupkg).

NuGet install from Package Manager:
```
Install-Package Amazon.Pay.API.SDK -Version 2.4.7
Install-Package Amazon.Pay.API.SDK -Version 2.4.8
```

NuGet install from .NET CLI:
Expand All @@ -42,12 +42,12 @@ Alternatively, to manually install after a GitHub download, use one of the follo

Visual Studio Package Manager Console
```
Install-Package Amazon.Pay.API.SDK -Version 2.4.7 -Source %USERPROFILE%\Downloads
Install-Package Amazon.Pay.API.SDK -Version 2.4.8 -Source %USERPROFILE%\Downloads
```

.NET Core CLI
```
dotnet add package Amazon.Pay.API.SDK -v 2.4.7 -s %USERPROFILE%\Downloads\
dotnet add package Amazon.Pay.API.SDK -v 2.4.8 -s %USERPROFILE%\Downloads\
```


Expand Down Expand Up @@ -120,6 +120,31 @@ public class Sample
}
}
```

If you have created envrionment specific keys (i.e Public Key Starts with LIVE or SANDBOX) in Seller Central, then use those PublicKeyId & PrivateKey. In this case, there is no need to pass the Environment parameter to the ApiConfiguration.
```csharp
using Amazon.Pay.API.Types;
using Amazon.Pay.API.WebStore;

public class Sample
{
public WebStoreClient InitiateClient()
{
// set up config
var payConfiguration = new ApiConfiguration
(
region: Region.Europe,
publicKeyId: "MY_PUBLIC_KEY_ID", // LIVE-XXXXX or SANDBOX-XXXXX
privateKey: "PATH_OR_CONTENT_OF_MY_PRIVATE_KEY"
);

// init API client
var client = new WebStoreClient(payConfiguration);

return client;
}
}
```
### Generate Button Signature

The signatures generated by the `GenerateButtonSignature` helper method are only valid for the Checkout v2 front-end buttons. Unlike API signing, no timestamps are involved, so the result of this function can be considered a static signature that can safely be placed in your website JS source files and used repeatedly (as long as your payload does not change).
Expand Down

0 comments on commit ec45b7e

Please sign in to comment.