Esempio n. 1
0
        //Billing
        public ActionResult CreateStripeCustomer(string token)
        {
            var          email           = Session["Email"].ToString();
            var          subscription    = new StripeSubscription();
            var          customerDetails = new StripeCustomer();
            var          userInfo        = new UpdateUserInfo();
            string       msg             = "";
            var          cardStatus      = 0;
            DataTable    userData;
            StripeCharge charge = null;

            try
            {
                if (token != null)
                {
                    var ds = new AccountData().ValidateEmailAndGetUserinfo(email);
                    userData = ds.Tables[0];
                    var custID = Convert.ToString(userData.Rows[0]["stp_custId"]);
                    if (userData.Rows.Count > 0 && !string.IsNullOrWhiteSpace(custID))                                 //update
                    {
                        var customerUpdateOptions = new StripeCustomerUpdateOptions
                        {
                            Email       = email,
                            Description = Session["FirstName"] + " " + Session["LastName"],
                            SourceToken = token
                        };

                        //updated customer
                        customerDetails = StripeHelper.UpdateCustomer(custID, customerUpdateOptions);

                        //add a test charge for $1
                        var makeTestCharge = new StripeChargeCreateOptions
                        {
                            CustomerId  = customerDetails.Id,
                            Amount      = 100,
                            Currency    = "usd",
                            Description = "Card verification charge",
                            SourceTokenOrExistingSourceId = customerDetails.DefaultSourceId
                        };
                        charge = StripeHelper.MakePayment(makeTestCharge);
                        if (charge != null)
                        {
                            var refund = StripeHelper.RefundCharge(charge.Id);
                            cardStatus      = 1;
                            userInfo.CustId = customerDetails.Id;
                            userInfo.CardId = customerDetails.DefaultSourceId;
                            userInfo.UserId = Convert.ToInt32(Session["UserID"]);
                            AccountData.UpdateStripeCardData(userInfo);
                            if ((SessionData.PlanID == 1 && SessionData.TrialEndOn < DateTime.Now) || SessionData.PlanStatus == 0) //When trail was expired || Unsubscribed and adding a card since payments are not succesfull
                            {
                                msg = StripeServices.SubscribePlan(SessionData.StripeCustId, userInfo.CardId);
                            }
                            if (String.IsNullOrWhiteSpace(msg))
                            {
                                SessionData.PlanStatus   = 1; //Default subscription
                                SessionData.StripeCustId = customerDetails.Id;
                                SessionData.StripeCardId = customerDetails.DefaultSourceId;
                            }
                        }
                        else
                        {
                            StripeHelper.DeleteCard(customerDetails.DefaultSourceId, custID);
                            cardStatus = 0;
                        }
                    }
                    customerDetails = StripeHelper.GetStripeCustomer(customerDetails.Id);
                }

                var respo = new { Status = cardStatus, Message = msg, CustomerDetails = customerDetails };
                return(Json(respo, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                LogFile.WriteLog("CreateCustomer - " + ex.Message.ToString());
                return(Json(null, JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> CompleteReturn(long id, decimal returnAmount)
        {
            var returnItem = _context.Returns.
                             Where(x => x.OrderId == id).
                             Where(x => x.ReturnCompleteDate == null).
                             SingleOrDefault();
            var          order          = _context.Orders.Where(x => x.Id == returnItem.OrderId).SingleOrDefault();
            UpdateResult result         = UpdateResult.Error;
            string       failureMessage = null;
            string       successMessage = null;

            int amount = Convert.ToInt32(returnAmount * 100);

            if (!string.IsNullOrWhiteSpace(order.StripeSource) && amount > 0)
            {
                var stripeResult = StripeHelper.RefundCharge(order.PaymentId, amount);

                returnItem.RefundStatus      = stripeResult.Status;
                returnItem.RefundCreatedDate = stripeResult.Created;

                if (stripeResult.Status == MasterStrings.StripeResultSucceeded || stripeResult.Status == MasterStrings.StripeResultPending)
                {
                    returnItem.RefundId = stripeResult.BalanceTransactionId;
                    result = UpdateResult.Success;
                }

                if (stripeResult.Status == MasterStrings.StripeResultFailed || stripeResult.Status == MasterStrings.StripeResultCancelled)
                {
                    returnItem.RefundId      = stripeResult.FailureBalanceTransactionId;
                    returnItem.RefundMessage = stripeResult.FailureReason;

                    failureMessage = "Refund failed: " + stripeResult.FailureReason;

                    result = UpdateResult.Error;
                }
            }
            else if (!string.IsNullOrWhiteSpace(order.PayPalSaleId) && amount > 0)
            {
                var payPalResult = PayPalHelper.RefundPayment(order.PayPalSaleId, amount);

                order.RefundStatus      = payPalResult.state;
                order.RefundCreatedDate = Convert.ToDateTime(payPalResult.create_time);
                order.RefundId          = payPalResult.id;
                order.RefundMessage     = payPalResult.reason_code;

                if (payPalResult.state == MasterStrings.PayPalResultCompleted || payPalResult.state == MasterStrings.PayPalResultPending)
                {
                    result = UpdateResult.Success;
                }

                if (payPalResult.state == MasterStrings.PayPalResultFailed || payPalResult.state == MasterStrings.PayPalResultCancelled)
                {
                    failureMessage = "Refund failed";
                    result         = UpdateResult.Error;
                }
            }
            else if (amount == 0)
            {
                result = UpdateResult.Success;
            }
            else
            {
                failureMessage = "Refund couldn't be completed.  Please complete this via payment handler.";
            }

            if (result == UpdateResult.Success)
            {
                returnItem.ReturnCompleteDate = DateTime.Now;
                await UpdateOrder(order.Id, MasterStrings.OrderReturned);

                successMessage = "Order updated.";
            }

            _context.Update(returnItem);
            _context.SaveChanges();


            var redirectUrl = string.Format("/ManageOrders/ViewOrder?id={0}", id);

            if (string.IsNullOrWhiteSpace(failureMessage))
            {
                redirectUrl += string.Format("&&failureMessage={0}", failureMessage);
            }
            if (string.IsNullOrWhiteSpace(successMessage))
            {
                redirectUrl += string.Format("&&successMessage={0}", successMessage);
            }

            return(ControllerHelper.RedirectToLocal(this, redirectUrl));
        }