Beispiel #1
0
        public static void ProcessCCBillPayments()
        {
            DateTime dtLastCCBillCheck = DBSettings.Get("Payments_LastCCBillCheck", DateTime.Now.AddHours(-12));

            double ccbillTimeOffset = Convert.ToDouble(ConfigurationManager.AppSettings["CCBillTimeOffset"]);
            string ccbillUrl = String.Format(ConfigurationManager.AppSettings["CCBillDataLinkUrl"],
                dtLastCCBillCheck.AddHours(ccbillTimeOffset).ToString("yyyyMMddHHmmss"),
                DateTime.Now.AddHours(ccbillTimeOffset).ToString("yyyyMMddHHmmss"));
            WebClient wc = new WebClient();
            string ccbillResponse = wc.DownloadString(ccbillUrl);

            Global.Logger.LogError("CCBill Response - " + ccbillResponse);
            string[] lines = Regex.Split(ccbillResponse, @"\n");

            foreach (string line in lines)
            {
                if (line.Trim().Length == 0) continue;
                string[] cells = Regex.Split(line, ",");

                try
                {
                    if (cells[0].Trim('"') == "NEW")
                    {
                        /* -- subscription creation handled by posback handler; datalink commented
                        string subscriptionId = cells[3].Trim('"');
                        string username = cells[7].Trim('"');
                        int initialPeriod = Convert.ToInt32(cells[18].Trim('"'));

                        User user = Classes.User.Load(username);
                        if (user.BillingDetails == null)
                            user.BillingDetails = new BillingDetails();
                        user.BillingDetails.SubscriptionId = subscriptionId;

                        BillingPlan billingPlan = null;
                        foreach (BillingPlan plan in BillingPlan.Fetch())
                        {
                            if (billingPlan == null) billingPlan = plan;
                            if (plan.Cycle == initialPeriod)
                            {
                                billingPlan = plan;
                                break;
                            }
                        }

                        int subscriptionID = Subscription.Create(user.Username, billingPlan.ID);
                        Subscription newSubscription = Subscription.Fetch(subscriptionID);
                        newSubscription.Activate(DateTime.Now, billingPlan);
                        */

                        //Global.Logger.LogError("Subscription activated - " + line);
                    }
                    else if (cells[0].Trim('"') == "REBILL")
                    {
                        string subscriptionId = cells[3].Trim('"'); //ccbill subscription id

                        //BasicSearch userSearch = new BasicSearch();
                        //userSearch.Paid = true;
                        //userSearch.Active = true;
                        //UserSearchResults results = userSearch.GetResults();
                        //if (results != null && results.Usernames != null)
                        //{
                        //    foreach (string username in results.Usernames)
                        //    {
                        //        User user = Classes.User.Load(username);
                        //        if (user.BillingDetails != null
                        //            && user.BillingDetails.SubscriptionId == subscriptionId)
                        //        {
                        //            Subscription subscription = Subscription.FetchActiveSubscription(username);
                        //            if (subscription == null) break;
                        //            BillingPlan plan = BillingPlan.FetchBySubscriptionID(subscription.ID);
                        //            subscription.Renew(plan);
                        //            break;
                        //        }
                        //    }
                        //}

                        Subscription subscription = Subscription.Fetch(subscriptionId/*this is a ccbill subscription id*/);

                        if (subscription != null)
                        {
                            BillingPlan plan = BillingPlan.FetchBySubscriptionID(subscription.ID);
                            subscription.Renew(plan);
                            Global.Logger.LogError("Subscription rebilled - " + line);

                            #region Send an email

                            try
                            {
                                var user = Classes.User.Load(subscription.Username);

                                MiscTemplates.SubscriptionCharge sendChargeNotificationEmail = new MiscTemplates.SubscriptionCharge();
                                Email.Send(Config.Urls.Home, user.Email,
                                                                sendChargeNotificationEmail.GetFormattedSubject(Config.Misc.SiteTitle),
                                                                sendChargeNotificationEmail.GetFormattedBody(Config.Urls.Home, user.Username), false);
                            }
                            catch (NotFoundException)
                            { 
                            }

                            #endregion
                        }
                        else
                        {
                            Global.Logger.LogError("Subscription rebilled but no subscription with such id was found in the database " + line);
                        }
                        
                    }
                    else if (cells[0].Trim('"') == "EXPIRE" || cells[0].Trim('"') == "CHARGEBACK" ||
                             cells[0].Trim('"') == "VOID")
                    {
                        string subscriptionId = cells[3].Trim('"'); //ccbill subscription id

                        //BasicSearch userSearch = new BasicSearch();
                        //userSearch.Paid = true;
                        //userSearch.Active = true;
                        //UserSearchResults results = userSearch.GetResults();
                        //if (results != null && results.Usernames != null)
                        //{
                        //    foreach (string username in results.Usernames)
                        //    {
                        //        User user = Classes.User.Load(username);
                        //        if (user.BillingDetails != null
                        //            && user.BillingDetails.SubscriptionId == subscriptionId)
                        //        {
                        //            Subscription subscription = Subscription.FetchActiveSubscription(username);
                        //            if (subscription == null) break;
                        //            Subscription.Cancel(subscription.ID);
                        //            break;
                        //        }
                        //    }
                        //}

                        Subscription subscription = Subscription.Fetch(subscriptionId);

                        if (subscription != null)
                        {
                            Subscription.Cancel(subscription.ID);
                        }

                        Global.Logger.LogError("Subscription cancelled - " + line);
                    }
                }
                catch (Exception err)
                {
                    Global.Logger.LogError("Payments_LastCCBillCheck", "Error while handling payment line: " + err);
                }
            }

            DBSettings.Set("Payments_LastCCBillCheck", DateTime.Now);
        }
Beispiel #2
0
        public static void CheckPaymentStatus(string username)
        {
            User user = null;
            try
            {
                user = Classes.User.Load(username);
            }
            catch (NotFoundException)
            {
                return;
            }


            Subscription subscription = Subscription.FetchActiveSubscription(username);
            if (subscription == null || subscription.RenewDate > DateTime.Now || subscription.PaymentProcessor == "CCBill")
                return;

            if (subscription.CancellationRequested || user.Deleted)
            {
                Subscription.Cancel(subscription.ID);
                user.Paid = false;
                user.Update(true);
                return;
            }

            BillingPlan plan = BillingPlan.FetchBySubscriptionID(subscription.ID);
            plan.ApplyDiscounts(user);

            TransactionDetails transactionDetails = null;

            if (subscription.PaymentProcessor == "PayflowPro" ||
                subscription.PaymentProcessor == "Authorize.NET")
            {
                transactionDetails = TransactionDetails.FromBillingDetails(
                    user.BillingDetails);
                transactionDetails.Amount = Convert.ToDecimal(plan.Amount);
            }

            IPaymentGateway gateway;
            switch (subscription.PaymentProcessor)
            {
                case "PayflowPro":
                    gateway = new PayflowPro();
                    break;
                case "Authorize.NET":
                    gateway = new AuthorizeNet();
                    break;
                case "Check":
                    gateway = new Check();
                    break;
                default:
                    Global.Logger.LogError("CheckPaymentStatus", "Selected payment processor was not recognized!");
                    return;
            }


            eGatewayResponse gatewayResponse;

            if (subscription.PaymentProcessor == "Check")
            {
                gatewayResponse = eGatewayResponse.Declined;
            }
            else
            {
                gatewayResponse = gateway.SubmitTransaction(username, transactionDetails,
                                                    "Subscription fee (" +
                                                    plan.Amount.ToString("c") + ", " +
                                                    subscription.RenewDate.ToShortDateString() +
                                                    ")");
            }

            if (gatewayResponse == eGatewayResponse.Approved)
            {
                subscription.Renew(plan);

                // send an email for every new charge
                #region Send an email

                MiscTemplates.SubscriptionCharge sendChargeNotificationEmail = new MiscTemplates.SubscriptionCharge(user.LanguageId);
                Email.Send(Config.Urls.Home, user.Email,
                                                sendChargeNotificationEmail.GetFormattedSubject(Config.Misc.SiteTitle),
                                                sendChargeNotificationEmail.GetFormattedBody(Config.Urls.Home, user.Username), false);

                #endregion
                
            }
            else
            {
                if (DateTime.Now.Subtract(subscription.RenewDate) > TimeSpan.FromDays(3))
                {
                    Subscription.Cancel(subscription.ID);
                    user.Paid = false;
                    user.Update(true);
                }
            }
        }