Example #1
0
        public async Task <ActionResult> Payment(int id, string stripeToken, string stripeEmail)
        {
            var selectQuery = await _orderService.Query(x => x.ID == id).Include(x => x.Listing).SelectAsync();

            // Check if order exists
            var order = selectQuery.FirstOrDefault();

            if (order == null)
            {
                return(new HttpNotFoundResult());
            }

            var stripeConnectQuery = await _stripeConnectService.Query(x => x.UserID == order.UserProvider).SelectAsync();

            var stripeConnect = stripeConnectQuery.FirstOrDefault();

            if (stripeConnect == null)
            {
                return(new HttpNotFoundResult());
            }

            // Check for customer profile
            var userId = User.Identity.GetUserId();

            // Create a customer profile for future checkouts
            var customerCreate = new StripeCustomerCreateOptions()
            {
                Email  = stripeEmail,
                Source = new StripeSourceOptions()
                {
                    TokenId = stripeToken
                }
            };
            var customerCreateService = new StripeCustomerService(CacheHelper.GetSettingDictionary("StripeApiKey").Value);
            var response = customerCreateService.Create(customerCreate);

            var customer = new StripeCustomerReference()
            {
                UserID           = userId,
                StripeCustomerID = response.Id,
                ObjectState      = Repository.Pattern.Infrastructure.ObjectState.Added
            };

            _stripeCusRefService.Insert(customer);
            await _unitOfWorkAsyncStripe.SaveChangesAsync();

            //https://stripe.com/docs/checkout
            var charge = new StripeChargeCreateOptions();

            // always set these properties
            charge.Amount     = order.PriceInCents;
            charge.Currency   = CacheHelper.Settings.Currency;
            charge.CustomerId = customer.StripeCustomerID;

            // set booking fee
            var bookingFee = (int)Math.Round(CacheHelper.Settings.TransactionFeePercent * order.PriceInCents);

            if (bookingFee < CacheHelper.Settings.TransactionMinimumFee * 100)
            {
                bookingFee = (int)(CacheHelper.Settings.TransactionMinimumFee * 100);
            }

            charge.ApplicationFee = bookingFee;
            charge.Capture        = false;
            charge.Description    = order.Description;
            charge.Destination    = stripeConnect.stripe_user_id;
            var          chargeService = new StripeChargeService(CacheHelper.GetSettingDictionary("StripeApiKey").Value);
            StripeCharge stripeCharge  = chargeService.Create(charge);

            // Update order status
            order.Status        = (int)Enum_OrderStatus.Pending;
            order.PaymentPlugin = StripePlugin.PluginName;
            _orderService.Update(order);

            // Save transaction
            var transaction = new StripeTransaction()
            {
                OrderID        = id,
                ChargeID       = stripeCharge.Id,
                StripeEmail    = stripeEmail,
                CustomerId     = customer.StripeCustomerID,
                Created        = DateTime.Now,
                LastUpdated    = DateTime.Now,
                FailureCode    = stripeCharge.FailureCode,
                FailureMessage = stripeCharge.FailureMessage,
                ObjectState    = Repository.Pattern.Infrastructure.ObjectState.Added
            };

            _transactionService.Insert(transaction);

            await _unitOfWorkAsync.SaveChangesAsync();

            await _unitOfWorkAsyncStripe.SaveChangesAsync();

            ClearCache();

            // Payment succeeded
            if (string.IsNullOrEmpty(stripeCharge.FailureCode))
            {
                // Send message to the user
                var message = new MessageSendModel()
                {
                    UserFrom  = order.UserReceiver,
                    UserTo    = order.UserProvider,
                    Subject   = order.Listing.Title,
                    ListingID = order.ListingID,
                    Body      = HttpContext.ParseAndTranslate(string.Format(
                                                                  "[[[Order Requested - %0 - Total Price %1 %2. <a href=\"%3\">See Details</a>|||{0}|||{1}|||{2}|||{3}]]]",
                                                                  order.Description,
                                                                  order.Price,
                                                                  order.Currency,
                                                                  Url.Action("Orders", "Payment")))
                };

                await MessageHelper.SendMessage(message);

                TempData[TempDataKeys.UserMessage] = "[[[Thanks for your order! You payment will not be charged until the provider accepted your request.]]]";
                return(RedirectToAction("Orders", "Payment"));
            }
            else
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = stripeCharge.FailureMessage;

                return(RedirectToAction("Payment"));
            }
        }
Example #2
0
        public async Task <ActionResult> Payment(int id, string stripeToken, string stripeEmail)
        {
            var selectQuery = await _orderService.Query(x => x.ID == id).Include(x => x.Listing).SelectAsync();

            // Check if order exists
            var order = selectQuery.FirstOrDefault();

            if (order == null)
            {
                return(new HttpNotFoundResult());
            }

            var stripeConnectQuery = await _stripConnectService.Query(x => x.UserID == order.UserProvider).SelectAsync();

            var stripeConnect = stripeConnectQuery.FirstOrDefault();

            if (stripeConnect == null)
            {
                return(new HttpNotFoundResult());
            }

            //https://stripe.com/docs/checkout
            var charge = new StripeChargeCreateOptions();

            // always set these properties
            charge.Amount   = order.PriceInCents;
            charge.Currency = CacheHelper.Settings.Currency;
            charge.Source   = new StripeSourceOptions()
            {
                TokenId = stripeToken
            };

            // set booking fee
            var bookingFee = (int)Math.Round(CacheHelper.Settings.TransactionFeePercent * order.PriceInCents);

            if (bookingFee < CacheHelper.Settings.TransactionMinimumFee * 100)
            {
                bookingFee = (int)(CacheHelper.Settings.TransactionMinimumFee * 100);
            }

            charge.ApplicationFee = bookingFee;
            charge.Capture        = false;
            charge.Description    = order.Description;
            charge.Destination    = stripeConnect.stripe_user_id;
            var          chargeService = new StripeChargeService(CacheHelper.GetSettingDictionary("StripeApiKey").Value);
            StripeCharge stripeCharge  = chargeService.Create(charge);

            // Update order status
            order.Status        = (int)Enum_OrderStatus.Pending;
            order.PaymentPlugin = StripePlugin.PluginName;
            _orderService.Update(order);

            // Save transaction
            var transaction = new StripeTransaction()
            {
                OrderID        = id,
                ChargeID       = stripeCharge.Id,
                StripeEmail    = stripeEmail,
                StripeToken    = stripeToken,
                Created        = DateTime.Now,
                LastUpdated    = DateTime.Now,
                FailureCode    = stripeCharge.FailureCode,
                FailureMessage = stripeCharge.FailureMessage,
                ObjectState    = Repository.Pattern.Infrastructure.ObjectState.Added
            };

            _transactionService.Insert(transaction);

            await _unitOfWorkAsync.SaveChangesAsync();

            await _unitOfWorkAsyncStripe.SaveChangesAsync();

            ClearCache();

            // Payment succeeded
            if (string.IsNullOrEmpty(stripeCharge.FailureCode))
            {
                TempData[TempDataKeys.UserMessage] = "[[[Thanks for your order! You payment will not be charged until the provider accepted your request.]]]";
                return(RedirectToAction("Orders", "Payment"));
            }
            else
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = stripeCharge.FailureMessage;

                return(RedirectToAction("Payment"));
            }
        }