Skip to content

Commit ec45b7e

Browse files
author
Shangamesh T
committed
Amazon Pay API SDK (.NET) 2.4.8
1 parent fd7e331 commit ec45b7e

File tree

9 files changed

+266
-9
lines changed

9 files changed

+266
-9
lines changed

Amazon.Pay.API.SDK.Tests/ApiConfigurationTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,51 @@ public void ThrowFileNotFoundException(string privateKey)
7777
privateKey: privateKey
7878
);
7979
}
80+
81+
[Test]
82+
public void CanInstantiateApiConfigurationForEnvironmentSpecificPublicKeyId()
83+
{
84+
// Fake Public keys
85+
const string livePublicKeyId = "LIVE-XXXXXXXXXXXXXXXXXXXXXXXX";
86+
const string sandboxPublicKeyId = "SANDBOX-XXXXXXXXXXXXXXXXXXXXXXXX";
87+
88+
// Testing Live PublicKeyId for region UnitedStates
89+
AssertEnvironmentSpecificPublicKeyId(Region.UnitedStates, livePublicKeyId);
90+
91+
// Testing Live PublicKeyId for region Europe
92+
AssertEnvironmentSpecificPublicKeyId(Region.Europe, livePublicKeyId);
93+
94+
// Testing Live PublicKeyId for region Japan
95+
AssertEnvironmentSpecificPublicKeyId(Region.Japan, livePublicKeyId);
96+
97+
// Testing Sandbox PublicKeyId for region UnitedStates
98+
AssertEnvironmentSpecificPublicKeyId(Region.UnitedStates, sandboxPublicKeyId);
99+
100+
// Testing Sandbox PublicKeyId for region Europe
101+
AssertEnvironmentSpecificPublicKeyId(Region.Europe, sandboxPublicKeyId);
102+
103+
// Testing Sandbox PublicKeyId for region Japan
104+
AssertEnvironmentSpecificPublicKeyId(Region.Japan, sandboxPublicKeyId);
105+
}
106+
107+
// Generic methods to assert environment specific publicKeyId
108+
private void AssertEnvironmentSpecificPublicKeyId(Region region, string publicKeyId)
109+
{
110+
// Configuration
111+
var payConfig = new ApiConfiguration
112+
(
113+
region: region,
114+
publicKeyId: publicKeyId,
115+
privateKey: "-----BEGIN RSA PRIVATE KEY-----" // Fake Private key
116+
);
117+
118+
// Assertion
119+
Assert.NotNull(payConfig);
120+
Assert.AreEqual(region, payConfig.Region);
121+
Assert.AreEqual(publicKeyId, payConfig.PublicKeyId);
122+
Assert.AreEqual(3, payConfig.MaxRetries);
123+
Assert.AreEqual(Constants.ApiVersion, payConfig.ApiVersion);
124+
payConfig.PrivateKey.Should().StartWith("-----BEGIN RSA");
125+
}
80126
}
81127
}

Amazon.Pay.API.SDK.Tests/ApiUrlBuilderTests.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public class ApiUrlBuilderTests
1111
{
1212
private ApiUrlBuilder apiUrlBuilder;
1313
private ApiConfiguration payConfig;
14+
// Fake Public keys
15+
private const string livePublicKeyId = "LIVE-XXXXXXXXXXXXXXXXXXXXXXXX";
16+
private const string sandboxPublicKeyId = "SANDBOX-XXXXXXXXXXXXXXXXXXXXXXXX";
1417

1518
[SetUp]
1619
public void SetUp()
@@ -135,5 +138,157 @@ public void GetFullPathForTokenExchangeApi()
135138
// assert
136139
Assert.AreEqual(expectedURL, actualURL);
137140
}
141+
142+
[Test]
143+
public void GetApiUnifiedEndPointBaseUrlForUnitedStates()
144+
{
145+
string expectedURL = "https://pay-api.amazon.com/";
146+
147+
// Scenario 1 : Testing Unified endpoint base URL by passing Live specific PublicKeyId for UnitedStates
148+
VerifyUnifiedEndpointBaseURL(Region.UnitedStates, livePublicKeyId, expectedURL);
149+
150+
// Scenario 2 : Testing Unified endpoint base URL by passing Sandbox specific PublicKeyId for UnitedStates
151+
VerifyUnifiedEndpointBaseURL(Region.UnitedStates, sandboxPublicKeyId, expectedURL);
152+
}
153+
154+
[Test]
155+
public void GetApiUnifiedEndPointBaseUrlForForEurope()
156+
{
157+
string expectedURL = "https://pay-api.amazon.eu/";
158+
159+
// Scenario 1 : Testing Unified endpoint base URL by passing Live specific PublicKeyId for Europe
160+
VerifyUnifiedEndpointBaseURL(Region.Europe, livePublicKeyId, expectedURL);
161+
162+
// Scenario 2 : Testing Unified endpoint base URL by passing Sandbox specific PublicKeyId for Europe
163+
VerifyUnifiedEndpointBaseURL(Region.Europe, sandboxPublicKeyId, expectedURL);
164+
}
165+
166+
[Test]
167+
public void GetApiUnifiedEndPointBaseUrlForForJapan()
168+
{
169+
string expectedURL = "https://pay-api.amazon.jp/";
170+
171+
// Scenario 1 : Testing Unified endpoint base URL by passing Live specific PublicKeyId for Japan
172+
VerifyUnifiedEndpointBaseURL(Region.Japan, livePublicKeyId, expectedURL);
173+
174+
// Scenario 2 : Testing Unified endpoint base URL by passing Sandbox specific PublicKeyId for Japan
175+
VerifyUnifiedEndpointBaseURL(Region.Japan, sandboxPublicKeyId, expectedURL);
176+
}
177+
178+
// Generic method used to verify Unified Endpoint Base URL
179+
public void VerifyUnifiedEndpointBaseURL(Region region, string publicKeyId, string url)
180+
{
181+
// Configuration
182+
payConfig.Region = region;
183+
payConfig.PublicKeyId = publicKeyId;
184+
apiUrlBuilder = new ApiUrlBuilder(payConfig);
185+
186+
// Building URL
187+
Uri expectedURL = new Uri(url);
188+
Uri actualURL = apiUrlBuilder.GetApiEndPointBaseUrl();
189+
190+
// Assertion
191+
Assert.AreEqual(expectedURL, actualURL);
192+
}
193+
194+
[Test]
195+
public void GetUnifiedEndpointFullPathForInStoreApiService()
196+
{
197+
string expectedUnitedStatesURL = "https://pay-api.amazon.com/v2/in-store/merchantScan/";
198+
199+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for UnitedStates
200+
VerifyUnifiedEndpointFullPath(Region.UnitedStates, livePublicKeyId, expectedUnitedStatesURL,
201+
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
202+
VerifyUnifiedEndpointFullPath(Region.UnitedStates, sandboxPublicKeyId, expectedUnitedStatesURL,
203+
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
204+
205+
string expectedEuropeURL = "https://pay-api.amazon.eu/v2/in-store/merchantScan/";
206+
207+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for Europe
208+
VerifyUnifiedEndpointFullPath(Region.Europe, livePublicKeyId, expectedEuropeURL,
209+
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
210+
VerifyUnifiedEndpointFullPath(Region.Europe, sandboxPublicKeyId, expectedEuropeURL,
211+
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
212+
213+
string expectedJapanURL = "https://pay-api.amazon.jp/v2/in-store/merchantScan/";
214+
215+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for Japan
216+
VerifyUnifiedEndpointFullPath(Region.Japan, livePublicKeyId, expectedJapanURL,
217+
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
218+
VerifyUnifiedEndpointFullPath(Region.Japan, sandboxPublicKeyId, expectedJapanURL,
219+
Constants.ApiServices.InStore, Constants.Resources.InStore.MerchantScan);
220+
}
221+
222+
[Test]
223+
public void GetUnifiedEndpointFullPathForDeliveryTrackerApiService()
224+
{
225+
string expectedUnitedStatesURL = "https://pay-api.amazon.com/v2/deliveryTrackers/";
226+
227+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for UnitedStates
228+
VerifyUnifiedEndpointFullPath(Region.UnitedStates, livePublicKeyId, expectedUnitedStatesURL,
229+
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
230+
VerifyUnifiedEndpointFullPath(Region.UnitedStates, sandboxPublicKeyId, expectedUnitedStatesURL,
231+
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
232+
233+
string expectedEuropeURL = "https://pay-api.amazon.eu/v2/deliveryTrackers/";
234+
235+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for Europe
236+
VerifyUnifiedEndpointFullPath(Region.Europe, livePublicKeyId, expectedEuropeURL,
237+
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
238+
VerifyUnifiedEndpointFullPath(Region.Europe, sandboxPublicKeyId, expectedEuropeURL,
239+
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
240+
241+
string expectedJapanURL = "https://pay-api.amazon.jp/v2/deliveryTrackers/";
242+
243+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for Japan
244+
VerifyUnifiedEndpointFullPath(Region.Japan, livePublicKeyId, expectedJapanURL,
245+
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
246+
VerifyUnifiedEndpointFullPath(Region.Japan, sandboxPublicKeyId, expectedJapanURL,
247+
Constants.ApiServices.Default, Constants.Resources.DeliveryTracker);
248+
}
249+
250+
[Test]
251+
public void GetUnifiedEndpointFullPathForTokenExchangeApi()
252+
{
253+
string expectedUnitedStatesURL = "https://pay-api.amazon.com/v2/authorizationTokens/";
254+
255+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for UnitedStates
256+
VerifyUnifiedEndpointFullPath(Region.UnitedStates, livePublicKeyId, expectedUnitedStatesURL,
257+
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
258+
VerifyUnifiedEndpointFullPath(Region.UnitedStates, sandboxPublicKeyId, expectedUnitedStatesURL,
259+
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
260+
261+
string expectedEuropeURL = "https://pay-api.amazon.eu/v2/authorizationTokens/";
262+
263+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for Europe
264+
VerifyUnifiedEndpointFullPath(Region.Europe, livePublicKeyId, expectedEuropeURL,
265+
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
266+
VerifyUnifiedEndpointFullPath(Region.Europe, sandboxPublicKeyId, expectedEuropeURL,
267+
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
268+
269+
string expectedJapanURL = "https://pay-api.amazon.jp/v2/authorizationTokens/";
270+
271+
// Testing Unified endpoint full path by passing environment specific PublicKeyId for Japan
272+
VerifyUnifiedEndpointFullPath(Region.Japan, livePublicKeyId, expectedJapanURL,
273+
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
274+
VerifyUnifiedEndpointFullPath(Region.Japan, sandboxPublicKeyId, expectedJapanURL,
275+
Constants.ApiServices.Default, Constants.Resources.TokenExchange);
276+
}
277+
278+
// Generic method used to verify Unified Endpoint Full Path
279+
public void VerifyUnifiedEndpointFullPath(Region region, string publicKeyId, string url, string apiService, string resource)
280+
{
281+
// Configuration
282+
payConfig.Region = region;
283+
payConfig.PublicKeyId = publicKeyId;
284+
apiUrlBuilder = new ApiUrlBuilder(payConfig);
285+
286+
// Building URL
287+
Uri expectedURL = new Uri(url);
288+
Uri actualURL = apiUrlBuilder.BuildFullApiPath(apiService, resource);
289+
290+
// Assertion
291+
Assert.AreEqual(expectedURL, actualURL);
292+
}
138293
}
139294
}

Amazon.Pay.API.SDK/Amazon.Pay.API.SDK.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
5-
<Version>2.4.7</Version>
6-
<AssemblyVersion>2.4.7.0</AssemblyVersion>
5+
<Version>2.4.8</Version>
6+
<AssemblyVersion>2.4.8.0</AssemblyVersion>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<Authors>Amazon Pay</Authors>
99
<Company>Amazon</Company>

Amazon.Pay.API.SDK/ApiUrlBuilder.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@ public Uri GetApiEndPointBaseUrl()
4848
{
4949
string regionDomain = config.Region.ToDomain();
5050
string environment = config.Environment.ToString().ToLower();
51+
string serviceURL;
5152

52-
string serviceURL = $"https://pay-api.amazon.{regionDomain}/{environment}/";
53-
53+
if (config.PublicKeyId.ToUpper().StartsWith("LIVE") || config.PublicKeyId.ToUpper().StartsWith("SANDBOX"))
54+
{
55+
serviceURL = $"https://pay-api.amazon.{regionDomain}/";
56+
}
57+
else
58+
{
59+
serviceURL = $"https://pay-api.amazon.{regionDomain}/{environment}/";
60+
}
5461
return new Uri(serviceURL);
5562
}
5663
}

Amazon.Pay.API.SDK/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Amazon.Pay.API
44
{
55
public class Constants
66
{
7-
public const string SdkVersion = "2.4.7.0";
7+
public const string SdkVersion = "2.4.8.0";
88
public const string SdkName = "amazon-pay-api-sdk-dotnet";
99
public const int ApiVersion = 2;
1010

Amazon.Pay.API.SDK/Types/ApiConfiguration.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public ApiConfiguration(Region region, Environment environment, string publicKey
2525
PrivateKey = privateKey;
2626
}
2727

28+
/// <summary>
29+
/// Initializes a new instance of the ApiConfiguration class without Enviroment.
30+
/// Use this initialization for having environment specific publicKeyId (i.e PublicKeyId starts with prefix LIVE or SANDBOX)
31+
/// </summary>
32+
/// <param name="region">The payment region the Amazon Pay merchant account is registered in.</param>
33+
/// <param name="publicKeyId">The identifier for the registered key pair.</param>
34+
/// <param name="privateKey">The private key in form of a file path, or directly as a string.</param>
35+
public ApiConfiguration(Region region, string publicKeyId, string privateKey)
36+
{
37+
Region = region;
38+
PublicKeyId = publicKeyId;
39+
PrivateKey = privateKey;
40+
}
41+
2842
/// <summary>
2943
/// The payment region the Amazon Pay merchant account is registered in.
3044
/// </summary>

Amazon.Pay.API.SDK/WebStore/Types/PaymentDetails.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,11 @@ internal void OnSerialized(StreamingContext content)
9191
/// </summary>
9292
[JsonProperty(PropertyName = "softDescriptor")]
9393
public string SoftDescriptor { get; set; }
94+
95+
/// <summary>
96+
/// Estimate Order Total. TODO : Replace this with the version from the guide, once available
97+
/// </summary>
98+
[JsonProperty(PropertyName = "estimateOrderTotal")]
99+
public Price EstimateOrderTotal { get; set; }
94100
}
95101
}

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version 2.4.8 - May 2021
2+
* 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
3+
* Added "EstimateOrderTotal" to PaymentDetails
4+
15
### Version 2.4.7 - May 2021
26
* Added "recurringMetadata" to UpdateChargePermissionRequest for ChargePermission API
37
* Added "HasPrimeMembershipType" to BuyerResponse for Buyer API

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ This SDK is compatible with .NET Standard 2.0 (including .NET Core 2.0), as well
2626

2727
## SDK Installation
2828

29-
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).
29+
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).
3030

3131
NuGet install from Package Manager:
3232
```
33-
Install-Package Amazon.Pay.API.SDK -Version 2.4.7
33+
Install-Package Amazon.Pay.API.SDK -Version 2.4.8
3434
```
3535

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

4343
Visual Studio Package Manager Console
4444
```
45-
Install-Package Amazon.Pay.API.SDK -Version 2.4.7 -Source %USERPROFILE%\Downloads
45+
Install-Package Amazon.Pay.API.SDK -Version 2.4.8 -Source %USERPROFILE%\Downloads
4646
```
4747

4848
.NET Core CLI
4949
```
50-
dotnet add package Amazon.Pay.API.SDK -v 2.4.7 -s %USERPROFILE%\Downloads\
50+
dotnet add package Amazon.Pay.API.SDK -v 2.4.8 -s %USERPROFILE%\Downloads\
5151
```
5252

5353

@@ -120,6 +120,31 @@ public class Sample
120120
}
121121
}
122122
```
123+
124+
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.
125+
```csharp
126+
using Amazon.Pay.API.Types;
127+
using Amazon.Pay.API.WebStore;
128+
129+
public class Sample
130+
{
131+
public WebStoreClient InitiateClient()
132+
{
133+
// set up config
134+
var payConfiguration = new ApiConfiguration
135+
(
136+
region: Region.Europe,
137+
publicKeyId: "MY_PUBLIC_KEY_ID", // LIVE-XXXXX or SANDBOX-XXXXX
138+
privateKey: "PATH_OR_CONTENT_OF_MY_PRIVATE_KEY"
139+
);
140+
141+
// init API client
142+
var client = new WebStoreClient(payConfiguration);
143+
144+
return client;
145+
}
146+
}
147+
```
123148
### Generate Button Signature
124149

125150
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).

0 commit comments

Comments
 (0)