public void TokenizeCard_GivenValidResult_ShouldReturnOk() { //Arrange var cardRequest = new TokenizeCardRequest { Name = "Biplov KC", Number = "111222333444", ExpiryYear = 2021, ExpiryMonth = 12, Cvv = "222", }; _merchantQueryMock .Setup(x => x.GetMerchantIdAsync(It.IsNotNull <string>())) .ReturnsAsync(It.IsAny <Guid>()); var innerCommand = new AddNewCardCommand(cardRequest.Name, cardRequest.Number, cardRequest.ExpiryMonth, cardRequest.ExpiryYear, cardRequest.Cvv, null, It.IsAny <string>()); var command = new IdentifiedCommand <AddNewCardCommand, Result>(innerCommand); _mediatorMock.Setup(x => x.Send(command, It.IsAny <CancellationToken>())).ReturnsAsync(Result.Ok); //Act var result = _sut.TokenizeCard(cardRequest).GetAwaiter().GetResult(); var okResult = result as OkObjectResult; //Assert okResult.ShouldNotBeNull(); okResult.StatusCode.ShouldBe(200); }
public async Task <string> TokenizeCard(TokenizeCardRequest cardRequest, MoneyDriver moneyDriver) { if (String.IsNullOrEmpty(moneyDriver.Token)) { throw new Exception("Money Driver Token is required"); } string resource = this.resources[moneyDriver.Environment].Tokenize; List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string> (Headers.Authorization, moneyDriver.Token) }; var response = await new ApiRequest(resource).MakeRequest(data: cardRequest, method: Verbs.POST, headers: headers); return(response); }
public async Task<IActionResult> TokenizeCard([FromBody] TokenizeCardRequest request) { var merchantIdentity = await _merchantQuery.GetMerchantIdAsync(GetAuthorizationKey()); if (merchantIdentity.Equals(Guid.Empty)) return new UnauthorizedObjectResult(ExternalErrorReason.InvalidSecretKey); var innerCommand = new AddNewCardCommand(request.Name, request.Number, request.ExpiryMonth, request.ExpiryYear, request.Cvv, null, HttpContext.TraceIdentifier); var command = new IdentifiedCommand<AddNewCardCommand, Result>(innerCommand); var result = await _mediator.Send(command); if (result.IsSuccess) { var card = await _cardQuery.GetCardByCardNumberAsync(request.Number); return Ok(card); } return UnprocessableEntity(result.Error); }
public JObject Authorize( decimal?amountInCents, string cardNumber, string expirationMonth, string expirationYear, string cvv, bool?isCardPresent) { var httpClient = new HttpClient(); var apiKeyRequest = new HttpRequestMessage(HttpMethod.Get, "https://api.clover.com/pakms/apikey"); apiKeyRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", CloverPrivateToken); apiKeyRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var apiKeyResult = httpClient.SendAsync(apiKeyRequest).Result; var apiKeyResponse = apiKeyResult.Content.ReadAsStringAsync().Result; if (!apiKeyResult.IsSuccessStatusCode) { Logger.Log("Clover API Request Failure" + $" {(int)apiKeyResult.StatusCode} {HttpMethod.Get} https://api.clover.com/pakms/apikey" + $" Received {apiKeyResponse}"); } apiKeyResult.EnsureSuccessStatusCode(); var apiAccessKeyResult = JObject.Parse(apiKeyResponse); var tokenizeCardRequest = new TokenizeCardRequest { Card = new Card { Number = cardNumber, ExpirationMonth = expirationMonth, ExpirationYear = expirationYear, Cvv = cvv } }; var tokenRequest = new HttpRequestMessage(HttpMethod.Post, "https://token.clover.com/v1/tokens"); tokenRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); tokenRequest.Headers.Add("apikey", apiAccessKeyResult["apiAccessKey"].Value <string>()); tokenRequest.Content = new StringContent(JsonConvert.SerializeObject(tokenizeCardRequest, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json"); var tokenResult = httpClient.SendAsync(tokenRequest).Result; var tokenResponse = tokenResult.Content.ReadAsStringAsync().Result; if (!tokenResult.IsSuccessStatusCode) { Logger.Log($"Clover API Request Failure" + $" {(int)tokenResult.StatusCode} {HttpMethod.Post} https://token.clover.com/v1/tokens" + $" Received {tokenResponse}"); } var tokenResultJson = JObject.Parse(tokenResponse); if (tokenResultJson["error"] != null) { return(tokenResultJson); } tokenResult.EnsureSuccessStatusCode(); var sourceToken = tokenResultJson["id"].Value <string>(); var chargeData = new JObject { { "amount", amountInCents }, { "source", sourceToken }, { "capture", false }, { "currency", "usd" } }; if (isCardPresent.HasValue) { chargeData["ecomind"] = isCardPresent.Value ? "ecom" : "moto"; } var chargeRequest = new HttpRequestMessage(HttpMethod.Post, $"{CLOVER_PROD_ECOMMERCE_API}/v1/charges"); chargeRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", CloverPrivateToken); chargeRequest.Content = new StringContent(chargeData.ToString(), Encoding.UTF8, "application/json"); var chargeResult = httpClient.SendAsync(chargeRequest).Result; var chargeResponse = chargeResult.Content.ReadAsStringAsync().Result; if (!chargeResult.IsSuccessStatusCode) { Logger.Log($"Clover API Request Failure" + $" {(int)chargeResult.StatusCode} {HttpMethod.Post} {CLOVER_PROD_ECOMMERCE_API}/v1/charges" + $" Received {chargeResponse}"); } var chargeResponseJson = JObject.Parse(chargeResponse); if (chargeResponseJson["error"] != null) { return(chargeResponseJson); } chargeResult.EnsureSuccessStatusCode(); return(chargeResponseJson); }