Esempio n. 1
0
        public async Task <IViewComponentResult> InvokeAsync(bool isEditable)
        {
            var cart = await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), ShoppingCartType.ShoppingCart, (await _storeContext.GetCurrentStoreAsync()).Id);

            var model = await _shoppingCartModelFactory.PrepareOrderTotalsModelAsync(cart, isEditable);

            return(View(model));
        }
Esempio n. 2
0
        public async Task CanPrepareOrderTotalsModel()
        {
            var model = await _shoppingCartModelFactory.PrepareOrderTotalsModelAsync(new List <ShoppingCartItem> {
                _shoppingCartItem
            }, true);

            model.SubTotal.Should().Be("$1,200.00");
            model.OrderTotal.Should().Be("$1,200.00");

            model.GiftCards.Any().Should().BeFalse();
            model.Shipping.Should().Be("$0.00");
            model.Tax.Should().Be("$0.00");
            model.WillEarnRewardPoints.Should().Be(120);
        }
        private async Task <AuthenticationTokenResponse> FindStatusCallAsync()
        {
            var storeScope = await _storeContext.GetActiveStoreScopeConfigurationAsync();

            var paySynchronyPaymentSettings = await _settingService.LoadSettingAsync <SynchronyPaymentSettings>(storeScope);

            var cart = await _shoppingCartService.GetShoppingCartAsync(
                await _workContext.GetCurrentCustomerAsync(),
                ShoppingCartType.ShoppingCart,
                (await _storeContext.GetCurrentStoreAsync()).Id);

            var orderTotalsModel = await _shoppingCartModelFactory.PrepareOrderTotalsModelAsync(cart, false);

            string authorizationRegionStatusURL = paySynchronyPaymentSettings.Integration
                ? SynchronyPaymentConstants.DemoInquiryEndpoint
                : SynchronyPaymentConstants.LiveInquiryEndpoint;
            var merchantId        = paySynchronyPaymentSettings.MerchantId;
            var merchantPassword  = paySynchronyPaymentSettings.MerchantPassword;
            var Integration       = paySynchronyPaymentSettings.Integration;
            var token             = HttpContext.Session.GetString("token").Replace("\"", "");
            var transactionAmount = Convert.ToDecimal(orderTotalsModel.OrderTotal.Replace("$", ""));
            var model             = new AuthenticationTokenResponse();
            // take reference from below link - Answer 1  by Seema As
            // https://stackoverflow.com/questions/39190018/how-to-get-object-using-httpclient-with-response-ok-in-web-api

            var response = new HttpResponseMessage();

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(authorizationRegionStatusURL);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0");
                ServicePointManager.Expect100Continue = true;

                var requestBody = new
                {
                    merchantNumber = merchantId,
                    password       = merchantPassword,
                    userToken      = token
                };
                string json = JsonSerializer.Serialize(requestBody);

                if (_synchronyPaymentSettings.IsDebugMode)
                {
                    await _logger.InsertLogAsync(Core.Domain.Logging.LogLevel.Debug, $"FindStatusCallAsync request - {requestBody.ToString()}");
                }

                var content = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
                ServicePointManager.SecurityProtocol =
                    SecurityProtocolType.Tls13 |
                    SecurityProtocolType.Tls12 |
                    SecurityProtocolType.Tls11 |
                    SecurityProtocolType.Tls;
                response = await client.PostAsync(authorizationRegionStatusURL, content);
            }

            var authResponseJsonAsString = await response.Content.ReadAsStringAsync();

            if (_synchronyPaymentSettings.IsDebugMode)
            {
                await _logger.InsertLogAsync(Core.Domain.Logging.LogLevel.Debug, $"FindStatusCallAsync response - {authResponseJsonAsString}");
            }

            if (authResponseJsonAsString.Contains("Unauthorized"))
            {
                await _logger.ErrorAsync($"Payments.Synchrony: Failed authenticating - {authResponseJsonAsString}");

                throw new HttpRequestException("There was an error, please select another payment method.");
            }

            var authResponse = JsonSerializer.Deserialize <AuthenticationTokenResponse>(
                authResponseJsonAsString
                );

            var customer = await _workContext.GetCurrentCustomerAsync();

            var billingAddress = await _addressService.GetAddressByIdAsync(customer.BillingAddressId.Value);

            model.Integration   = Integration;
            model.MerchantId    = merchantId;
            model.clientToken   = token;
            model.transactionId = authResponse.transactionId;
            model.responseCode  = authResponse.responseCode;
            model.responseDesc  = authResponse.responseDesc;
            model.ZipCode       = billingAddress.ZipPostalCode;
            model.State         = (await _stateProvinceService.GetStateProvinceByAddressAsync(
                                       billingAddress
                                       )).Abbreviation;
            model.FirstName         = billingAddress.FirstName;
            model.City              = billingAddress.City;
            model.Address1          = billingAddress.Address1;
            model.LastName          = billingAddress.LastName;
            model.StatusCode        = authResponse.StatusCode;
            model.AccountNumber     = authResponse.AccountNumber;
            model.StatusMessage     = authResponse.StatusMessage;
            model.TransactionAmount = transactionAmount.ToString();

            model.ClientTransactionID    = authResponse.ClientTransactionID;
            model.TransactionDate        = authResponse.TransactionDate;
            model.TransactionDescription = authResponse.TransactionDescription;
            model.AuthCode   = authResponse.AuthCode;
            model.PromoCode  = authResponse.PromoCode;
            model.PostbackId = authResponse.PostbackId;

            return(model);
        }