Example #1
0
        public int InsertSubscriptionbyAdmin(InsertSubscriptionByAdminRequest insertSubscriptionByAdminRequest)
        {
            try
            {
                double planamount = 0, smscreditamount = 0, amount = 0;

                if (insertSubscriptionByAdminRequest.hdnsmscreditamount != null && insertSubscriptionByAdminRequest.hdnsmscreditamount != 0)
                {
                    smscreditamount = Convert.ToDouble(insertSubscriptionByAdminRequest.hdnsmscreditamount) * insertSubscriptionByAdminRequest.smscreditqty;
                }

                amount = Convert.ToDouble(insertSubscriptionByAdminRequest.hdnamount);


                PlanService planService = new PlanService();
                int         planId      = 0;
                int         accountid   = 0;

                if (insertSubscriptionByAdminRequest.PlanId != 0)
                {
                    planId = insertSubscriptionByAdminRequest.PlanId;
                }

                CabicsSubscription.Service.Plan plan = planService.GetPlanDetailByPlanId(planId);

                if (insertSubscriptionByAdminRequest.cabofficeid != 0)
                {
                    accountid = insertSubscriptionByAdminRequest.cabofficeid;
                }

                bool        resultt;
                Transaction transactionid;

                bool chkautorenewel  = false;
                int  noOfInstallment = 0;

                var bit = "off";
                if (insertSubscriptionByAdminRequest.chkautorenewel != null)
                {
                    bit = insertSubscriptionByAdminRequest.chkautorenewel.ToString();
                }

                if (bit == "on")
                {
                    chkautorenewel = true;
                }


                //return RedirectToAction("Show", new { id = transaction.Id });
                SubscriptionService subscriptionService = new SubscriptionService();

                int qty = 1;
                if (insertSubscriptionByAdminRequest.qty != null)
                {
                    qty = Convert.ToInt32(insertSubscriptionByAdminRequest.qty);
                }

                int smscreditqty = 0;
                if (insertSubscriptionByAdminRequest.smscreditqty != 0)
                {
                    smscreditqty = insertSubscriptionByAdminRequest.smscreditqty;
                }

                double hdnsmscreditamount = 0;
                hdnsmscreditamount = Convert.ToInt32(insertSubscriptionByAdminRequest.hdnsmscreditotaltamount);


                int subscriptionId = subscriptionService.PurchaseSubscription(planId, Convert.ToDouble(insertSubscriptionByAdminRequest.hdnamount),
                                                                              insertSubscriptionByAdminRequest.cabofficeid, qty, insertSubscriptionByAdminRequest.chequeNo, smscreditqty, smscreditamount,
                                                                              "adminsubscriptionbycheque", "", chkautorenewel, noOfInstallment);

                if (chkautorenewel == true)
                {
                    //// Insert into execution service

                    WindowsServiceExecution winservice = new WindowsServiceExecution();
                    winservice.WindowsServiceFunction     = "Automatic Charging";
                    winservice.WindowsServiceArgumrnt     = subscriptionId;
                    winservice.WindowsServiceFunctionCode = (int)Constant.WindowsFunction.AutomaticCharging;
                    winservice.WindowsServiceStatus       = (int)Constant.WindowsServiceExecutionStatus.Pending;
                    winservice.IsActive    = true;
                    winservice.CreatedDate = DateTime.UtcNow;

                    WindowsServiceExecutionService windowsServiceExecutionService = new WindowsServiceExecutionService();
                    windowsServiceExecutionService.InsertWindowsServiceExecutionService(winservice);
                }

                return(subscriptionId);
            }
            catch (Exception ex)
            {
                DbErrorLogService.LogError("error", "InsertSubscriptionbyAdmin", "", ex.ToString());

                return(0);
            }
        }
        public void MarkAutoRenewalSubscription(int subscriptionId, WindowsServiceExecution windowsServiceExecution)
        {
            try
            {
                WindowsServiceLogging.WriteEventLog("******************* -- Event: MarkAutoRenewalSubscription( -- ***********************");


                WindowsServiceExecutionService windowsServiceExecutionService = new WindowsServiceExecutionService();
                //List<WindowsServiceExecution> lstSubscription = windowsServiceExecutionService.GetAutoRenewelEntryBySubscriptionID(subscriptionId);
                Subscription subscription = subscriptionService.GetSubscriptionBySubscriptionId(subscriptionId);

                if (subscription == null)
                {
                    return;
                }

                if (subscription.SubscriptionTypeId != (int)Constant.SubscriptionType.Monthly)
                {
                    return;
                }

                int     cabOfficeId        = subscription.AccountId;
                Account account            = accountService.getCabOfficeByCabOfficeId(cabOfficeId);
                int     subscriptionTypeId = subscription.SubscriptionTypeId;
                int     planId             = subscription.PlanId;
                double  amount             = Convert.ToDouble(subscription.TotalPrice);

                DateTime startDate = subscription.StartDate;
                string   endDate   = subscription.EndDate.ToString().Split(' ')[0];
                string   todayDate = DateTime.Now.ToString().Split(' ')[0];

                var    qty                = 1;
                int    smscreditqty       = Convert.ToInt32(subscription.NoOfSmsCreditPurchase) + Convert.ToInt32(subscription.RemainingSmsCreditPurchase);
                double hdnsmscreditamount = Convert.ToInt32(subscription.SMSPrice);
                string transactionId      = subscription.btTransactionId;


                WindowsServiceLogging.WriteEventLog("******************* -- Checking for Date Matched EndDate: " + endDate + " Today Date: " + todayDate + " -- ***********************");
                if (endDate == todayDate)
                {
                    WindowsServiceLogging.WriteEventLog("******************* -- Date Matched -- ***********************");

                    /// Generate Monthly Sale

                    if (account.BtPaymentMethodToken == null || account.BtPaymentMethodToken == "")
                    {
                        return;
                    }

                    #region Payment

                    bool saleResult;
                    Braintree.Environment environment;
                    if (ConfigurationManager.AppSettings["BtEnvironmentTestMode"].ToString() == "1")
                    {
                        environment = Braintree.Environment.SANDBOX;
                    }
                    else
                    {
                        environment = Braintree.Environment.PRODUCTION;
                    }

                    var gateway = new BraintreeGateway
                    {
                        Environment = environment,
                        MerchantId  = ConfigurationManager.AppSettings["BtMerchantId"],
                        PublicKey   = ConfigurationManager.AppSettings["BtPublicKey"],
                        PrivateKey  = ConfigurationManager.AppSettings["BtPrivateKey"]
                    };


                    Result <PaymentMethodNonce> resultN = gateway.PaymentMethodNonce.Create(account.BtPaymentMethodToken);
                    var nonce_Generated = resultN.Target.Nonce;

                    var request = new TransactionRequest
                    {
                        Amount             = Convert.ToDecimal(amount),
                        PaymentMethodNonce = nonce_Generated,
                        Options            = new TransactionOptionsRequest
                        {
                            SubmitForSettlement = true
                        }
                    };

                    Result <Transaction> result = gateway.Transaction.Sale(request);

                    bool        isSuccess   = result.IsSuccess();
                    Transaction transaction = result.Target;

                    #endregion

                    if (isSuccess)
                    {
                        //// Insert Re-Subscription
                        int nSubscriptionId = subscriptionService.PurchaseSubscription(planId, Convert.ToDouble(amount), account.Id, qty, "", smscreditqty, hdnsmscreditamount,
                                                                                       subscription.btTransactionId, subscription.btSubscriptionId, subscription.IsAutoRenewel, subscription.NoOfBillingCycle);


                        RecurringJob.AddOrUpdate(() => MarkAutoRenewalSubscription(nSubscriptionId, null), Cron.Minutely);

                        ////// Mark execution service Done
                        //windowsServiceExecutionService.MarkWindowsServiceStatus(windowsServiceExecution.Id, (int)Constant.WindowsServiceExecutionStatus.Done);

                        //// Insert into execution service
                        WindowsServiceExecution winservice = new WindowsServiceExecution();
                        winservice.WindowsServiceFunction     = "Automatic Charging";
                        winservice.WindowsServiceArgumrnt     = subscriptionId;
                        winservice.WindowsServiceFunctionCode = (int)Constant.WindowsFunction.AutomaticCharging;
                        winservice.WindowsServiceStatus       = (int)Constant.WindowsServiceExecutionStatus.Pending;
                        winservice.IsActive    = true;
                        winservice.CreatedDate = DateTime.Now;
                        windowsServiceExecutionService.InsertWindowsServiceExecutionService(winservice);
                    }
                }
            }
            catch (Exception ex)
            {
                DbErrorLogService.LogError("CabicsSubscriptionModule", "MarkAutoRenewalSubscription", "SubscriptionId: " + subscriptionId, ex.ToString());
            }
        }