示例#1
0
        public ResponseData <StripePlan> Purchase([FromBody] PurchaseInformation info)
        {
            try
            {
                var response = new ResponseData <StripePlan>();
                var account  = _accountService.GetAccountById(info.CurrenUserId);
                if (string.IsNullOrEmpty(account.CustomerIdStripe))
                {
                    var stripeCustomer = _stripeService.CreateUser(info, account.Email);
                    account.CustomerIdStripe = stripeCustomer.Id;
                    _accountService.Update(account);
                    var accountAppliance = _accountApplianceService.GetAccountApplianceByAccountIdAndApplianceId(info.CurrenUserId, info.CurrenApplianceId);
                    accountAppliance.SubscriptionId = stripeCustomer.Subscriptions.Data.FirstOrDefault().Id;
                    _accountApplianceService.Update(accountAppliance);
                }
                else
                {
                    var accountAppliance = _accountApplianceService.GetAccountApplianceByAccountIdAndApplianceId(info.CurrenUserId, info.CurrenApplianceId);

                    if (!string.IsNullOrEmpty(accountAppliance.SubscriptionId))
                    {
                        var subscriptionService = _stripeService.RetrieveSubscription(accountAppliance.SubscriptionId);
                        if (subscriptionService != null && subscriptionService.Status == StripeSubscriptionStatuses.Active || subscriptionService.Status == StripeSubscriptionStatuses.Trialing)
                        {
                            var subscription = _stripeService.UpdateSubscription(info, account.CustomerIdStripe, accountAppliance.SubscriptionId);
                        }
                        else
                        {
                            var subscription = _stripeService.CreateSubscription(info, account.CustomerIdStripe);
                            accountAppliance.SubscriptionId = subscription.Id;
                            _accountApplianceService.Update(accountAppliance);
                        }
                    }
                    else
                    {
                        var subscription = _stripeService.CreateSubscription(info, account.CustomerIdStripe);
                        accountAppliance.SubscriptionId = subscription.Id;
                        _accountApplianceService.Update(accountAppliance);
                    }
                }

                var plan = _stripeService.GetPlansByPlanId(info.PlanId);
                if (!string.IsNullOrEmpty(info.Coupon))
                {
                    var coupOn = _stripeService.RetrieveCoupon(info.Coupon);
                    if (coupOn.PercentOff != null && coupOn.PercentOff.HasValue)
                    {
                        plan.Amount = plan.Amount - (plan.Amount * coupOn.PercentOff.Value / 100);
                    }
                    else
                    {
                        plan.Amount = plan.Amount - coupOn.AmountOff.Value;
                    }
                }
                response.Data    = plan;
                response.Message = ResponseMessage.Success;
                response.Status  = ResponseStatus.Success.ToString();
                return(response);
            }
            catch (Exception ex)
            {
                var response = new ResponseData <StripePlan>();
                response.Message = ex.Message;
                response.Status  = ResponseStatus.Error.ToString();
                return(response);
            }
        }