예제 #1
0
        public async Task <IActionResult> RedeemVoucher([FromQuery] String voucherCode,
                                                        [FromQuery] String applicationVersion,
                                                        CancellationToken cancellationToken)
        {
            if (ClaimsHelper.IsPasswordToken(this.User) == false)
            {
                return(this.Forbid());
            }

            // Do the software version check
            try
            {
                VersionCheckRequest versionCheckRequest = VersionCheckRequest.Create(applicationVersion);
                await this.Mediator.Send(versionCheckRequest, cancellationToken);
            }
            catch (VersionIncompatibleException vex)
            {
                Logger.LogError(vex);
                return(this.StatusCode(505));
            }

            Guid estateId   = Guid.Parse(ClaimsHelper.GetUserClaim(this.User, "estateId").Value);
            Guid contractId = Guid.Parse(ClaimsHelper.GetUserClaim(this.User, "contractId").Value);

            // Now do the GET
            RedeemVoucherRequest request = RedeemVoucherRequest.Create(estateId, contractId, voucherCode);

            RedeemVoucherResponse response = await this.Mediator.Send(request, cancellationToken);

            return(this.Ok(this.ModelFactory.ConvertFrom(response)));
        }
예제 #2
0
        public void RedeemVoucherRequest_CanBeCreated_IsCreated()
        {
            RedeemVoucherRequest request = RedeemVoucherRequest.Create(TestData.EstateId, TestData.ContractId, TestData.VoucherCode);

            request.EstateId.ShouldBe(TestData.EstateId);
            request.ContractId.ShouldBe(TestData.ContractId);
            request.VoucherCode.ShouldBe(TestData.VoucherCode);
        }
        public void RedeemVoucherRequest_CanBeCreated_IsCreated()
        {
            RedeemVoucherRequest redeemVoucherRequest = RedeemVoucherRequest.Create(TestData.EstateId, TestData.VoucherCode, TestData.RedeemedDateTime);

            redeemVoucherRequest.ShouldNotBeNull();
            redeemVoucherRequest.VoucherCode.ShouldBe(TestData.VoucherCode);
            redeemVoucherRequest.RedeemedDateTime.ShouldBe(TestData.RedeemedDateTime);
            redeemVoucherRequest.EstateId.ShouldBe(TestData.EstateId);
        }
        public async Task VoucherRequestHandler_RedeemVoucherRequest_Handle_RequestIsHandled()
        {
            Mock <IVoucherManagementACLApplicationService> voucherManagementACLApplicationService = new Mock <IVoucherManagementACLApplicationService>();
            VoucherRequestHandler requestHandler = new VoucherRequestHandler(voucherManagementACLApplicationService.Object);

            RedeemVoucherRequest request = RedeemVoucherRequest.Create(TestData.EstateId, TestData.ContractId, TestData.VoucherCode);

            Should.NotThrow(async() =>
            {
                await requestHandler.Handle(request, CancellationToken.None);
            });
        }
예제 #5
0
        public async Task <IActionResult> RedeemVoucher(RedeemVoucherRequest redeemVoucherRequest,
                                                        CancellationToken cancellationToken)
        {
            // Reject password tokens
            if (ClaimsHelper.IsPasswordToken(this.User))
            {
                return(this.Forbid());
            }

            DateTime redeemedDateTime = redeemVoucherRequest.RedeemedDateTime.HasValue ? redeemVoucherRequest.RedeemedDateTime.Value : DateTime.Now;

            BusinessLogic.Requests.RedeemVoucherRequest request = BusinessLogic.Requests.RedeemVoucherRequest.Create(redeemVoucherRequest.EstateId, redeemVoucherRequest.VoucherCode, redeemedDateTime);

            RedeemVoucherResponse response = await this.Mediator.Send(request, cancellationToken);

            return(this.Ok(this.ModelFactory.ConvertFrom(response)));
        }
        public async Task VoucherManagementRequestHandler_RedeemVoucherRequest_IsHandled()
        {
            Mock <IVoucherDomainService> voucherDomainService = new Mock <IVoucherDomainService>();

            voucherDomainService.Setup(v => v.RedeemVoucher(It.IsAny <Guid>(), It.IsAny <String>(), It.IsAny <DateTime>(),
                                                            It.IsAny <CancellationToken>())).ReturnsAsync(TestData.RedeemVoucherResponse);

            VoucherManagementRequestHandler handler = new VoucherManagementRequestHandler(voucherDomainService.Object);


            RedeemVoucherRequest command = TestData.RedeemVoucherRequest;

            Should.NotThrow(async() =>
            {
                await handler.Handle(command, CancellationToken.None);
            });
        }
        public async Task WhenIRedeemTheVoucherForTransactionIdTheVoucherBalanceWillBe(Guid transactionId, Decimal balance)
        {
            (IssueVoucherRequest request, IssueVoucherResponse response)voucher = this.TestingContext.GetVoucherByTransactionId(transactionId);

            RedeemVoucherRequest redeemVoucherRequest = new RedeemVoucherRequest
            {
                EstateId         = voucher.request.EstateId,
                RedeemedDateTime = DateTime.Now,
                VoucherCode      = voucher.response.VoucherCode
            };
            // Do the redeem
            await Retry.For(async() =>
            {
                RedeemVoucherResponse response = await this.TestingContext.DockerHelper.VoucherManagementClient
                                                 .RedeemVoucher(this.TestingContext.AccessToken, redeemVoucherRequest, CancellationToken.None)
                                                 .ConfigureAwait(false);
                response.RemainingBalance.ShouldBe(balance);
            });
        }
        /// <summary>
        /// Redeems the voucher.
        /// </summary>
        /// <param name="accessToken">The access token.</param>
        /// <param name="redeemVoucherRequest">The redeem voucher request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <RedeemVoucherResponse> RedeemVoucher(String accessToken,
                                                                RedeemVoucherRequest redeemVoucherRequest,
                                                                CancellationToken cancellationToken)
        {
            RedeemVoucherResponse response = null;

            String requestUri = $"{this.BaseAddress}/api/vouchers";

            try
            {
                String requestSerialised = JsonConvert.SerializeObject(redeemVoucherRequest);

                StringContent httpContent = new StringContent(requestSerialised, Encoding.UTF8, "application/json");

                // Add the access token to the client headers
                this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                // Make the Http Call here
                HttpResponseMessage httpResponse = await this.HttpClient.PutAsync(requestUri, httpContent, cancellationToken);

                // Process the response
                String content = await this.HandleResponse(httpResponse, cancellationToken);

                // call was successful so now deserialise the body to the response object
                response = JsonConvert.DeserializeObject <RedeemVoucherResponse>(content);
            }
            catch (Exception ex)
            {
                // An exception has occurred, add some additional information to the message
                Exception exception = new Exception("Error redeeming voucher.", ex);

                throw exception;
            }

            return(response);
        }
예제 #9
0
        public async Task <RedeemVoucherResponse> RedeemVoucher(Guid estateId,
                                                                Guid contractId,
                                                                String voucherCode,
                                                                CancellationToken cancellationToken)
        {
            // Get a client token to call the Voucher Management
            String clientId     = ConfigurationReader.GetValue("AppSettings", "ClientId");
            String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");

            TokenResponse accessToken = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);

            RedeemVoucherResponse response = null;

            try
            {
                RedeemVoucherRequest redeemVoucherRequest = new RedeemVoucherRequest
                {
                    EstateId    = estateId,
                    VoucherCode = voucherCode
                };

                VoucherManagement.DataTransferObjects.RedeemVoucherResponse redeemVoucherResponse = await this.VoucherManagementClient.RedeemVoucher(accessToken.AccessToken, redeemVoucherRequest, cancellationToken);

                response = new RedeemVoucherResponse
                {
                    ResponseCode    = "0000", // Success
                    ResponseMessage = "SUCCESS",
                    ContractId      = contractId,
                    EstateId        = estateId,
                    ExpiryDate      = redeemVoucherResponse.ExpiryDate,
                    Balance         = redeemVoucherResponse.RemainingBalance,
                    VoucherCode     = redeemVoucherResponse.VoucherCode
                };
            }
            catch (Exception ex)
            {
                if (ex.InnerException is InvalidOperationException)
                {
                    // This means there is an error in the request
                    response = new RedeemVoucherResponse
                    {
                        ResponseCode    = "0001", // Request Message error
                        ResponseMessage = ex.InnerException.Message,
                    };
                }
                else if (ex.InnerException is HttpRequestException)
                {
                    // Request Send Exception
                    response = new RedeemVoucherResponse
                    {
                        ResponseCode    = "0002", // Request Message error
                        ResponseMessage = "Error Sending Request Message",
                    };
                }
                else
                {
                    response = new RedeemVoucherResponse
                    {
                        ResponseCode    = "0003", // General error
                        ResponseMessage = "General Error",
                    };
                }
            }

            return(response);
        }
예제 #10
0
 /// <summary>
 /// Handles a request
 /// </summary>
 /// <param name="request">The request</param>
 /// <param name="cancellationToken">Cancellation token</param>
 /// <returns>
 /// Response from the request
 /// </returns>
 public async Task <RedeemVoucherResponse> Handle(RedeemVoucherRequest request,
                                                  CancellationToken cancellationToken)
 {
     return(await this.ApplicationService.RedeemVoucher(request.EstateId, request.ContractId, request.VoucherCode, cancellationToken));
 }
예제 #11
0
 /// <summary>
 /// Handles a request
 /// </summary>
 /// <param name="request">The request</param>
 /// <param name="cancellationToken">Cancellation token</param>
 /// <returns>
 /// Response from the request
 /// </returns>
 public async Task <RedeemVoucherResponse> Handle(RedeemVoucherRequest request,
                                                  CancellationToken cancellationToken)
 {
     return(await this.VoucherDomainService.RedeemVoucher(request.EstateId, request.VoucherCode, request.RedeemedDateTime, cancellationToken));
 }