예제 #1
0
        public ActionResult Pay(StripeCheckoutViewModel stripeData, string stripeToken, string next, string back)
        {
            var checkoutData = GetCheckoutData(stripeData);

            if (!String.IsNullOrWhiteSpace(back))
            {
                return(RedirectToAction(checkoutData.Amount >= 0 ? "SendMoney" : "Ship"));
            }
            var subTotal       = 0.0;
            var total          = checkoutData.Amount;
            var isProductOrder = checkoutData.CheckoutItems.Any();

            if (isProductOrder)
            {
                var taxes = checkoutData.Taxes == null ? 0 : checkoutData.Taxes.Amount;
                subTotal = checkoutData.CheckoutItems.Sum(i => i.Price * i.Quantity + i.LinePriceAdjustment);
                total    = subTotal + taxes + checkoutData.ShippingOption.Price;
            }
            // Call Stripe to charge card
            var stripeCharge = _stripeService.Charge(stripeToken, total);

            if (stripeCharge.Error != null)
            {
                Logger.Error(stripeCharge.Error.Type + ": " + stripeCharge.Error.Message);
                _workflowManager.TriggerEvent("OrderError", null,
                                              () => new Dictionary <string, object> {
                    { "CheckoutError", stripeCharge.Error }
                });
                if (stripeCharge.Error.Type == "card_error")
                {
                    return(Pay(stripeCharge.Error.Message));
                }
                throw new InvalidOperationException(stripeCharge.Error.Type + ": " + stripeCharge.Error.Message);
            }

            var userId      = -1;
            var currentUser = _wca.GetContext().CurrentUser;

            if (currentUser != null)
            {
                userId = currentUser.Id;
            }

            var order = _orderService.CreateOrder(
                stripeCharge,
                checkoutData.CheckoutItems,
                subTotal,
                total,
                checkoutData.Taxes,
                checkoutData.ShippingOption,
                checkoutData.ShippingAddress,
                checkoutData.BillingAddress,
                checkoutData.Email,
                checkoutData.Phone,
                checkoutData.SpecialInstructions,
                OrderPart.Pending,
                null,
                _stripeService.IsInTestMode(),
                userId,
                total,
                checkoutData.PurchaseOrder);

            TempData["OrderId"] = order.Id;
            _workflowManager.TriggerEvent(
                isProductOrder ? "NewOrder" : "NewPayment",
                order,
                () => new Dictionary <string, object> {
                { "Content", order },
                { "Order", order }
            });
            order.LogActivity(OrderPart.Event, T("Order created.").Text);
            // Clear checkout info from temp data
            TempData.Remove(NwazetStripeCheckout);

            return(RedirectToAction("Confirmation", "OrderSsl"));
        }