Skip to content

Commit

Permalink
Amazon Pay API SDK (.NET) 2.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Shangamesh T committed Feb 17, 2021
1 parent e3c5d89 commit 89928c3
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Amazon.Pay.API.SDK.Tests/Amazon.Pay.API.SDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.14.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.9" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 53 additions & 1 deletion Amazon.Pay.API.SDK.Tests/SignatureHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Environment = Amazon.Pay.API.Types.Environment;

namespace Amazon.Pay.API.Tests
{
Expand Down Expand Up @@ -110,7 +119,50 @@ public void CanCreateDefaultHeaders()
Assert.True(result.ContainsKey("content-type"));

}


[Test]
public void CanGenerateSignature()
{
// sample, pub/priv key pair for test only
RsaKeyParameters pub1 = new RsaKeyParameters(false,
new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137", 16),
new BigInteger("010001", 16));

RsaKeyParameters prv1 = new RsaPrivateCrtKeyParameters(
new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137", 16),
new BigInteger("010001", 16),
new BigInteger("33a5042a90b27d4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b325", 16),
new BigInteger("e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e86296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda8e6443", 16),
new BigInteger("b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd", 16),
new BigInteger("28fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8dd3ede2448328f385d81b30e8e43b2fffa027861979", 16),
new BigInteger("1a8b38f398fa712049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729", 16),
new BigInteger("27156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24a79f4d", 16));

var message = "POST\n/live/in-store/v1/refund\naccept:application/json\ncontent-type:application/json\naccept;content-type\n95b0d65e9efb9f0b9e8c2f3b7744628c765477";
byte[] bytesToSign = Encoding.UTF8.GetBytes(message);

// Get string version of private key
TextWriter textWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(prv1);
pemWriter.Writer.Flush();
var privateKey = textWriter.ToString();

var helper =
new SignatureHelper(new ApiConfiguration(Region.UnitedStates, Environment.Sandbox, "THISISONLYATEST", privateKey),
new CanonicalBuilder());

var result = helper.GenerateSignature(message);
var sigBytes = Convert.FromBase64String(result);

PssSigner signer = new PssSigner(new RsaEngine(), new Sha256Digest(), 20);
signer.Init(true, pub1);
signer.BlockUpdate(bytesToSign, 0, bytesToSign.Length);
var resultVerify = signer.VerifySignature(sigBytes);

Assert.IsTrue(resultVerify);
}

private Dictionary<string, List<string>> CreateHeaders(Uri uri)
{
Dictionary<string, List<string>> headers = new Dictionary<string, List<string>>();
Expand Down
7 changes: 3 additions & 4 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.3</Version>
<AssemblyVersion>2.4.3.0</AssemblyVersion>
<Version>2.4.4</Version>
<AssemblyVersion>2.4.4.0</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Amazon Pay</Authors>
<Company>Amazon</Company>
Expand All @@ -26,8 +26,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle" Version="[1.8.2,1.8.6)" Condition="'$(TargetFramework)' == 'net45'" />
<PackageReference Include="BouncyCastle.NetCore" Version="[1.8.2,1.8.6)" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.9" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<None Include="logo.png" Pack="true" PackagePath="" />
<None Include="..\LICENSE.txt" Pack="true" PackagePath="" />
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.3.0";
public const string SdkVersion = "2.4.4.0";
public const string SdkName = "amazon-pay-api-sdk-dotnet";
public const int ApiVersion = 2;

Expand Down
3 changes: 1 addition & 2 deletions Amazon.Pay.API.SDK/SignatureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class SignatureHelper : ISignatureHelper
private readonly ApiConfiguration payConfiguration;
private readonly string LineSeparator = "\n";
private readonly int SaltLength = 20;
private readonly byte TrailerField = 1;
private readonly CanonicalBuilder canonicalBuilder;

public SignatureHelper(ApiConfiguration payConfiguration, CanonicalBuilder canonicalBuilder)
Expand Down Expand Up @@ -161,7 +160,7 @@ public string GenerateSignature(string stringToSign)
}

// initiate the signing object
PssSigner pssSigner = new PssSigner(new RsaEngine(), new Sha256Digest(), SaltLength, TrailerField);
PssSigner pssSigner = new PssSigner(new RsaEngine(), new Sha256Digest(), SaltLength);
pssSigner.Init(true, new ParametersWithRandom(parameters, random));
pssSigner.BlockUpdate(bytesToSign, 0, bytesToSign.Length);

Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Version 2.4.4 - February 2021
* Switch from BouncyCastle and BouncyCastle.Core reference to latest release Portable.BouncyCastle (1.8.9), while also switching to use default trailer for PssSigner in signature generation. This resolves previous signature generation issues encountered using non-default trailer for PssSigner on BouncyCastle versions 1.8.6 and higher.

### Version 2.4.3 - January 2021
* Add parameterless constructor for CreateCheckoutSessionRequest to better support Additional Payment Button (APB) workflow
* Add Interface for SignatureHelper to allow mocking in tests
Expand Down
53 changes: 45 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ For integrating the API of other Amazon Pay products, please refer to [Amazon Pa

This SDK is compatible with .NET Standard 2.0 (including .NET Core 2.0), as well as .NET Framework 4.5 or higher. To support usage in strong-named (signed) .NET Framework applications, the SDK defines a different set of dependencies for .NET Framework and .NET Standard. While dependent libraries for .NET Framework are all strong-named, the libaries for .NET Standard may not be strong-named. The SDK assembly itself is strong-named.

Please note that BouncyCastle 1.8.6 has known to cause issues with the signature generation that is required for API calls. The library has therefore been restricted to version >= 1.8.2 && < 1.8.6 until a fix can be provided.

### .NET Standard / .NET Core

* Platform: .NET Standard 2.0
* Newtonsoft.JSON (>= 12.0.2)
* BouncyCastle.NETCore (>= 1.8.2 && < 1.8.6)
* Portable.BouncyCastle 1.8.9

### .NET Framwork

* Platform: .NET Framework 4.5
* Newtonsoft.JSON (>= 12.0.2)
* BouncyCastle (>= 1.8.2 && < 1.8.6)
* Portable.BouncyCastle 1.8.9

## 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.3/Amazon.Pay.API.SDK.2.4.3.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.4/Amazon.Pay.API.SDK.2.4.4.nupkg).

NuGet install from Package Manager:
```
Install-Package Amazon.Pay.API.SDK -Version 2.4.3
Install-Package Amazon.Pay.API.SDK -Version 2.4.4
```

NuGet install from .NET CLI:
Expand All @@ -44,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.3 -Source %USERPROFILE%\Downloads
Install-Package Amazon.Pay.API.SDK -Version 2.4.4 -Source %USERPROFILE%\Downloads
```

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


Expand Down Expand Up @@ -558,6 +556,45 @@ public class Sample : PageModel
}
```

## Alexa Delivery Notifications

### Initiate Client
Use any of the channel-specific client classes if you want to send an Alexa delivery notification. See [Initiate WebStoreClient](#initiate-client) for example.

### Send Alexa Delivery Notification

```csharp
using Amazon.Pay.API;
using Amazon.Pay.API.DeliveryTracker;
using Amazon.Pay.API.Types;

public class Sample
{
public DeliveryTrackerResponse SendDeliveryTrackingInformation()
{
// prepare the request
var request = new DeliveryTrackerRequest
(
objectId: "P00-0000000-0000000", // ChargePermissionID or OrderReferenceID
objectIsChargePermission: true, // true if above is ChargePermissionID, false otherwise
trackingNumber: "1Z999AA10123456784",
carrierCode: "UPS"
);

// send the notification
var result = client.SendDeliveryTrackingInformation(request);

// check if API call was successful
if (!result.Success)
{
// do something, e.g. throw an error
}

return result;
}
}
```

## Other available methods

### Get authorization token for delegated API calls
Expand Down

0 comments on commit 89928c3

Please sign in to comment.