Skip to content

Commit

Permalink
adding psd2
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Jun 23, 2020
1 parent 66b393a commit 4d1eba3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 46 deletions.
49 changes: 47 additions & 2 deletions Nexmo.Api.Test.Unit/VerifyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void RequestVerification(bool passCreds, bool kitchenSink)
VerifyRequest request = new VerifyRequest { Number= "447700900000", Brand="Acme Inc"};
if (kitchenSink)
{
expectedRequestContent = $"number=447700900000&country=GB&brand={HttpUtility.UrlEncode("Acme Inc")}&sender_id=ACME&code_length=4&lg=en-us&pin_expiry=240&next_event_wait=60&workflow_id=1&api_key={ApiKey}&api_secret={ApiSecret}&";
expectedRequestContent = $"brand={HttpUtility.UrlEncode("Acme Inc")}&sender_id=ACME&workflow_id=1&number=447700900000&country=GB&code_length=4&lg=en-us&pin_expiry=240&next_event_wait=60&api_key={ApiKey}&api_secret={ApiSecret}&";
request.Country = "GB";
request.SenderId = "ACME";
request.CodeLength = 4;
Expand All @@ -36,7 +36,7 @@ public void RequestVerification(bool passCreds, bool kitchenSink)
}
else
{
expectedRequestContent = $"number=447700900000&brand={HttpUtility.UrlEncode("Acme Inc")}&api_key={ApiKey}&api_secret={ApiSecret}&";
expectedRequestContent = $"brand={HttpUtility.UrlEncode("Acme Inc")}&number=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
}
Setup(expectedUri, expectedResponse, expectedRequestContent);
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
Expand Down Expand Up @@ -226,5 +226,50 @@ public void TestControlVerifyInvalidCredentials()
Assert.Equal("invalid credentials", ex.Response.ErrorText);
}
}

[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void Psd2Verification(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
""request_id"": ""abcdef0123456789abcdef0123456789"",
""status"": ""0""
}";
var expectedUri = $"{ApiUrl}/psd2/json";

string expectedRequestContent;
Psd2Request request = new Psd2Request { Number = "447700900000", Payee = "Acme Inc", Amount = 4.8 };
if (kitchenSink)
{
expectedRequestContent = $"payee={HttpUtility.UrlEncode("Acme Inc")}&amount=4.8&workflow_id=1&number=447700900000&country=GB&code_length=4&lg=en-us&pin_expiry=240&next_event_wait=60&api_key={ApiKey}&api_secret={ApiSecret}&";
request.Country = "GB";
request.CodeLength = 4;
request.Lg = "en-us";
request.PinExpiry = 240;
request.NextEventWait = 60;
request.WorkflowId = Psd2Request.Workflow.SMS_TTS_TTS;
}
else
{
expectedRequestContent = $"payee={HttpUtility.UrlEncode("Acme Inc")}&amount=4.8&number=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
}

Setup(expectedUri, expectedResponse, expectedRequestContent);
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
VerifyResponse response;
if (passCreds)
{
response = client.VerifyClient.VerifyRequestWithPSD2(request, creds);
}
else
{
response = client.VerifyClient.VerifyRequestWithPSD2(request);
}

Assert.Equal("abcdef0123456789abcdef0123456789", response.RequestId);
Assert.Equal("0", response.Status);
}
}
}
13 changes: 12 additions & 1 deletion Nexmo.Api/Verify/IVerifyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public interface IVerifyClient
/// <param name="request"></param>
/// <param name="creds"></param>
/// <returns></returns>
VerifyControlResponse VerifyControl(VerifyControlRequest request, Credentials creds = null);
VerifyControlResponse VerifyControl(VerifyControlRequest request, Credentials creds = null);

/// <summary>
/// Use Verify request to generate and send a PIN to your user to authorize a payment:
/// 1. Create a request to send a verification code to your user.
/// 2. Check the status field in the response to ensure that your request was successful (zero is success).
/// 3. Use the request_id field in the response for the Verify check.
/// </summary>
/// <param name="request"></param>
/// <param name="creds"></param>
/// <returns></returns>
VerifyResponse VerifyRequestWithPSD2(Psd2Request request, Credentials creds = null);
}
}
11 changes: 11 additions & 0 deletions Nexmo.Api/Verify/VerifyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public VerifyControlResponse VerifyControl(VerifyControlRequest request, Credent
return response;
}

public VerifyResponse VerifyRequestWithPSD2(Psd2Request request, Credentials creds)
{
var response = ApiRequest.DoPostRequestUrlContentFromObject<VerifyResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, "/psd2/json"),
request,
creds ?? Credentials
);
ValidateVerifyResponse(response);
return response;
}

public void ValidateVerifyResponse(VerifyResponseBase response)
{
if (response.Status != "0")
Expand Down
45 changes: 2 additions & 43 deletions Nexmo.Api/Verify/VerifyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nexmo.Api.Verify
{
public class VerifyRequest
public class VerifyRequest : VerifyRequestBase
{
public enum Workflow
{
Expand All @@ -14,17 +14,6 @@ public enum Workflow
SMS=6,
TTS=7
}
/// <summary>
/// The mobile or landline phone number to verify. Unless you are setting country explicitly, this number must be in E.164 format.
/// </summary>
[JsonProperty("number")]
public string Number { get; set; }

/// <summary>
/// If you do not provide number in international format or you are not sure if number is correctly formatted, specify the two-character country code in country. Verify will then format the number for you.
/// </summary>
[JsonProperty("country")]
public string Country { get; set; }

/// <summary>
/// An 18-character alphanumeric string you can use to personalize the verification request SMS body, to help users identify your company or application name. For example: "Your Acme Inc PIN is ..."
Expand All @@ -39,43 +28,13 @@ public enum Workflow
[JsonProperty("sender_id")]
public string SenderId { get; set; }

/// <summary>
/// The length of the verification code. Must be 4 or 6
/// </summary>
[JsonProperty("code_length")]
public int CodeLength { get; set; }

/// <summary>
/// By default, the SMS or text-to-speech (TTS) message is generated in the locale that matches the number.
/// For example, the text message or TTS message for a 33* number is sent in French.
/// Use this parameter to explicitly control the language used for the Verify request.
/// A list of languages is available: https://developer.nexmo.com/verify/guides/verify-languages
/// </summary>
[JsonProperty("lg")]
public string Lg { get; set; }

/// <summary>
/// How long the generated verification code is valid for, in seconds.
/// When you specify both pin_expiry and next_event_wait then pin_expiry must be an integer
/// multiple of next_event_wait otherwise pin_expiry is defaulted to equal next_event_wait.
/// See changing the event timings: https://developer.nexmo.com/verify/guides/changing-default-timings.
/// </summary>
[JsonProperty("pin_expiry")]
public int PinExpiry { get; set; }

/// <summary>
/// Specifies the wait time in seconds between attempts to deliver the verification code.
/// Must be between 60 and 900
/// </summary>
[JsonProperty("next_event_wait")]
public int NextEventWait { get; set; }

/// <summary>
/// Selects the predefined sequence of SMS and TTS (Text To Speech) actions to use in order to convey the PIN to your user.
/// For example, an id of 1 identifies the workflow SMS - TTS - TTS.
/// For a list of all workflows and their associated ids, please visit the developer portal.
/// </summary>
[JsonProperty("workflow_id")]
public Workflow? WorkflowId { get; set; }

}
}

0 comments on commit 4d1eba3

Please sign in to comment.