/// <summary> /// Get the shipping cost. /// </summary> /// <param name="shippingMethod">The shipping method.</param> /// <param name="shipment">The shipment.</param> /// <param name="currency">The currency.</param> /// <returns>The shipping cost.</returns> public static Money GetShippingCost(Mediachase.Commerce.Orders.Dto.ShippingMethodDto.ShippingMethodRow shippingMethod, Shipment shipment, Currency currency) { Money result = new Money(0, currency); var type = Type.GetType(shippingMethod.ShippingOptionRow.ClassName); if (type == null) { throw new TypeInitializationException(shippingMethod.ShippingOptionRow.ClassName, null); } var message = string.Empty; var provider = (IShippingGateway)Activator.CreateInstance(type, new MarketStorage().GetCurrentMarket()); var shipmentRate = provider.GetRate(shippingMethod.ShippingMethodId, shipment, ref message); if (shipmentRate != null) { var shipmentRateMoney = shipmentRate.Money; if (!shipmentRateMoney.Currency.Equals(currency)) { //The shipment and the shipping method base price are in different currency, need to convert first result = CurrencyFormatter.ConvertCurrency(shipmentRateMoney, currency); } else { result = shipmentRateMoney; } } return(result); }
public ActionResult UpdatePayment(CheckoutPage currentPage, CheckoutViewModel viewModel, IPaymentMethod paymentOption) { if (!paymentOption.ValidateData()) { return(View(viewModel)); } if (paymentOption is GiftCardPaymentOption) { var giftCard = _giftCardService.GetGiftCard(((GiftCardPaymentOption)paymentOption).SelectedGiftCardId); var paymentTotal = CurrencyFormatter.ConvertCurrency(new Money(viewModel.OrderSummary.PaymentTotal, CartWithValidationIssues.Cart.Currency), Currency.USD); if (paymentTotal > giftCard.RemainBalance) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Not enought money in Gift Card")); } } viewModel.Payment = paymentOption; _checkoutService.CreateAndAddPaymentToCart(CartWithValidationIssues.Cart, viewModel); _orderRepository.Save(CartWithValidationIssues.Cart); var model = CreateCheckoutViewModel(currentPage); model.OrderSummary = _orderSummaryViewModelFactory.CreateOrderSummaryViewModel(CartWithValidationIssues.Cart); if (Request.IsAuthenticated) { model.BillingAddressType = 1; } else { model.BillingAddressType = 0; } return(PartialView("_AddPayment", model)); }
public static Money ConvertCurrency(Money money, Currency currency) { var retVal = !CurrencyFormatter.CanBeConverted(money.Currency, currency) ? money : CurrencyFormatter.ConvertCurrency(money, currency); return(retVal); }
/// <summary> /// Converts given sum to the sum in USD. Need to do this since Authorize.NET accepts payments only in USD. /// </summary> /// <param name="sum"></param> /// <param name="currentCurrency"></param> /// <returns></returns> private static decimal GetSumInUSD(decimal sum, string currentCurrency) { if (String.IsNullOrEmpty(currentCurrency)) { return(sum); } decimal usdSum = 0; string toCurrency = "usd"; usdSum = CurrencyFormatter.ConvertCurrency(sum, currentCurrency, ref toCurrency); if (currentCurrency == toCurrency) { throw new PaymentException(PaymentException.ErrorType.ConfigurationError, "", String.Format("Couldn't convert {0} to USD. Please check that the rate for conversion is set.", currentCurrency)); } return(usdSum); }
public static bool PurchaseByGiftCard(IPayment payment, Currency currency) { var giftCard = BusinessManager.Load(GiftCardMetaClass, new PrimaryKeyId(new Guid(payment.Properties["GiftCardId"].ToString()))); var priceInUSD = decimal.Round(CurrencyFormatter.ConvertCurrency(new Money(payment.Amount, currency), Currency.USD)); if (priceInUSD > (decimal)giftCard[RemainBalanceField]) { return(false); } else { giftCard[RemainBalanceField] = (decimal)giftCard[RemainBalanceField] - priceInUSD; if ((decimal)giftCard[RemainBalanceField] <= 0) { giftCard[IsActiveField] = false; } BusinessManager.Update(giftCard); return(true); } }
private ShippingMethodDto.ShippingMethodRow CreateShippingMethod(ShippingMethodDto dto, ShippingMethodDto.ShippingOptionRow shippingOption, string languageId, int sortOrder, string name, string description, Money costInUsd, Currency currency) { Money shippingCost = CurrencyFormatter.ConvertCurrency(costInUsd, currency); if (shippingCost.Currency != currency) { throw new InvalidOperationException("Cannot convert to currency " + currency + " Missing conversion data."); } return(dto.ShippingMethod.AddShippingMethodRow( Guid.NewGuid(), shippingOption, languageId, true, name, "", shippingCost.Amount, shippingCost.Currency, description, false, sortOrder, DateTime.Now, DateTime.Now)); }
/// <summary> /// Processes the shipments. /// </summary> private void ProcessShipments() { ShippingMethodDto methods = ShippingManager.GetShippingMethods(/*Thread.CurrentThread.CurrentUICulture.Name*/ String.Empty); OrderGroup order = OrderGroup; // request rates, make sure we request rates not bound to selected delivery method foreach (OrderForm form in order.OrderForms) { foreach (Shipment shipment in form.Shipments) { bool processThisShipment = true; string discountName = "@ShipmentSkipRateCalc"; // If you find the shipment discount which represents if (shipment.Discounts.Cast <ShipmentDiscount>().Any(x => x.ShipmentId == shipment.ShipmentId && x.DiscountName.Equals(discountName))) { processThisShipment = false; } if (!processThisShipment) { continue; } ShippingMethodDto.ShippingMethodRow row = methods.ShippingMethod.FindByShippingMethodId(shipment.ShippingMethodId); // If shipping method is not found, set it to 0 and continue if (row == null) { Logger.Info(String.Format("Total shipment is 0 so skip shipment calculations.")); shipment.ShipmentTotal = 0; continue; } // Check if package contains shippable items, if it does not use the default shipping method instead of the one specified Logger.Debug(String.Format("Getting the type \"{0}\".", row.ShippingOptionRow.ClassName)); Type type = Type.GetType(row.ShippingOptionRow.ClassName); if (type == null) { throw new TypeInitializationException(row.ShippingOptionRow.ClassName, null); } Logger.Debug(String.Format("Creating instance of \"{0}\".", type.Name)); IShippingGateway provider = null; var orderMarket = ServiceLocator.Current.GetInstance <IMarketService>().GetMarket(order.MarketId); if (orderMarket != null) { provider = (IShippingGateway)Activator.CreateInstance(type, orderMarket); } else { provider = (IShippingGateway)Activator.CreateInstance(type); } Logger.Debug(String.Format("Calculating the rates.")); string message = String.Empty; ShippingRate rate = provider.GetRate(row.ShippingMethodId, shipment, ref message); if (rate != null) { Logger.Debug(String.Format("Rates calculated.")); // check if shipment currency is convertable to Billing currency, and then convert it if (!CurrencyFormatter.CanBeConverted(rate.Money, order.BillingCurrency)) { Logger.Debug(String.Format("Cannot convert selected shipping's currency({0}) to current currency({1}).", rate.Money.Currency.CurrencyCode, order.BillingCurrency)); throw new Exception(String.Format("Cannot convert selected shipping's currency({0}) to current currency({1}).", rate.Money.Currency.CurrencyCode, order.BillingCurrency)); } else { Money convertedRate = CurrencyFormatter.ConvertCurrency(rate.Money, order.BillingCurrency); shipment.ShipmentTotal = convertedRate.Amount; } } else { Warnings[String.Concat("NoShipmentRateFound-", shipment.ShippingMethodName)] = String.Concat("No rates have been found for ", shipment.ShippingMethodName); Logger.Debug(String.Format("No rates have been found.")); } } } }