Esempio n. 1
0
        }                                                                                                    //HandleOnePayment

        /// <summary>
        ///
        /// </summary>
        /// <param name="loanId"></param>
        /// <param name="loanScheduleId"></param>
        /// <param name="initialAmountDue"></param>
        /// <param name="customerId"></param>
        /// <param name="customerMail"></param>
        /// <param name="fullname"></param>
        /// <param name="reductionFee"></param>
        /// <param name="isNonRegulated"></param>
        /// <returns></returns>
        private AutoPaymentResult TryToMakeAutoPayment(
            int loanId,
            int loanScheduleId,
            decimal initialAmountDue,
            int customerId,
            string customerMail,
            string fullname,
            bool reductionFee,
            bool isNonRegulated
            )
        {
            var     result = new AutoPaymentResult();
            decimal actualAmountCharged = initialAmountDue;
            int     counter             = 0;

            while (counter <= 2)
            {
                PayPointReturnData payPointReturnData;

                string message;

                if (MakeAutoPayment(customerId, loanId, loanScheduleId, actualAmountCharged, out payPointReturnData))
                {
                    if (isNonRegulated && IsNotEnoughMoney(payPointReturnData))
                    {
                        if (!reductionFee)
                        {
                            result.PaymentFailed = true;

                            message = string.Format("payPointReturnData={0} result={1}", payPointReturnData, result);
                            NL_AddLog(LogType.Info, "NonRegulated+IsNotEnoughMoney", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, null, null);

                            return(result);
                        }

                        counter++;

                        if (counter > 2)
                        {
                            message = string.Format("payPointReturnData={0} result={1}", payPointReturnData, result);

                            NL_AddLog(LogType.Info, "More that 2 tryings", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, null, null);

                            return(result);
                        }

                        if (counter == 1)
                        {
                            actualAmountCharged = Math.Round((initialAmountDue * (decimal)0.5), 2);

                            message = string.Format("Trying to charge 50% (Attempt #{0}). Customer:{1} Original amount:{2} Calculated amount:{3}. payPointReturnData={4}", counter + 1, customerId, initialAmountDue, actualAmountCharged, payPointReturnData);

                            Log.Info(message);

                            NL_AddLog(LogType.Info, "Counter==1", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, null, null);
                        }
                        else if (counter == 2)
                        {
                            actualAmountCharged = Math.Round((initialAmountDue * (decimal)0.25), 2);

                            message = string.Format("Trying to charge 25% (Attempt #{0}). Customer:{1} Original amount:{2} Calculated amount:{3}, payPointReturnData={4}", counter + 1, customerId, initialAmountDue, actualAmountCharged, payPointReturnData);

                            Log.Info(message);

                            NL_AddLog(LogType.Info, "Counter==2", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, null, null);
                        }
                    }
                    else if (IsCollectionSuccessful(payPointReturnData))
                    {
                        result.PaymentCollectedSuccessfully = true;
                        result.ActualAmountCharged          = actualAmountCharged;

                        message = string.Format("payPointReturnData={0} result={1}", payPointReturnData, result);
                        NL_AddLog(LogType.Info, "Collection Successful", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, null, null);

                        return(result);
                    }
                    else
                    {
                        result.PaymentFailed = true;
                        message = string.Format("payPointReturnData={0} result={1}", payPointReturnData, result);

                        NL_AddLog(LogType.Info, "Collection Failed", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, message, null);

                        return(result);
                    }                    //if
                }
                else
                {
                    SendExceptionMail(initialAmountDue, customerId, customerMail, fullname);
                    result.IsException = true;

                    message = string.Format("payPointReturnData={0} result={1}", payPointReturnData, result);
                    NL_AddLog(LogType.Info, "Collection failed -send mail", new object[] { customerId, loanId, loanScheduleId, initialAmountDue }, message, null, null);

                    return(result);
                }         //if
            }             //while

            return(result);
        }         //TryToMakeAutoPayment
Esempio n. 2
0
        private void HandleOnePayment(LoanAutoChargeModel loan, NLLoanAutoChargeModel nlLoan = null)
        {
            string   message;
            int      loanScheduleId    = loan.LoanScheduleId;
            int      loanId            = loan.LoanId;
            int      customerId        = loan.CustomerId;
            string   customerMail      = loan.Email;
            string   fullname          = loan.Fullname;
            string   typeOfBusinessStr = loan.TypeOfBusiness;
            DateTime dueDate           = loan.DueDate;         // PlannedDate
            bool     reductionFee      = loan.ReductionFee;
            string   refNum            = loan.RefNum;          // of loan
            bool     lastInstallment   = loan.LastInstallment; // bit

            TypeOfBusiness typeOfBusiness = (TypeOfBusiness)Enum.Parse(typeof(TypeOfBusiness), typeOfBusinessStr);
            bool           isNonRegulated = IsRegulated(typeOfBusiness);

            DateTime now      = DateTime.UtcNow;
            TimeSpan span     = now.Subtract(dueDate);
            int      daysLate = (int)span.TotalDays;

            // amount due.
            decimal amountDue = this.payPointApi.GetAmountToPay(loanScheduleId);

            if (nlLoan == null)
            {
                NL_AddLog(LogType.Info, string.Format("nlloan for oldLoan {0} not found", loanId), this.strategyArgs, null, null, null);
            }
            else
            {
                // new loan amount due.
                decimal nlAmountDue = GetAmountToPay(nlLoan.CustomerId, nlLoan.LoanId, nlLoan.LoanScheduleId);

                message = string.Format("LoanID={0} oldLoanID={1} amountDue= {2} nlAmountDue={3}", nlLoan.LoanId, nlLoan.OldLoanID, amountDue, nlAmountDue);
                Log.Debug(message);
                NL_AddLog(LogType.Info, "AmountDue", this.strategyArgs, message, null, null);
            }

            if (!ShouldCharge(lastInstallment, amountDue))
            {
                message = string.Format("Will not charge loan schedule id {0} (amount {1}): the minimal amount for collection is {2}.", loanScheduleId, amountDue, this.amountToChargeFrom);
                Log.Info(message);
                NL_AddLog(LogType.Info, "Exit 1", this.strategyArgs, message, null, null);
                return;
            }            //if

            decimal initialAmountDue = amountDue;

            if ((!isNonRegulated && daysLate > 3) || !this.dueChargingDays.Contains(daysLate))
            {
                message = string.Format("Will not charge loan schedule id {0} (amount {1}): the charging is not scheduled today", loanScheduleId, amountDue);
                Log.Info(message);
                NL_AddLog(LogType.Info, "Exit 2", this.strategyArgs, message, null, null);
                return;
            }

            // step 2 - charging
            AutoPaymentResult autoPaymentResult = TryToMakeAutoPayment(
                loanId,
                loanScheduleId,
                initialAmountDue,
                customerId,
                customerMail,
                fullname,
                reductionFee,
                isNonRegulated
                );

            if (autoPaymentResult.IsException || autoPaymentResult.PaymentFailed)
            {
                Error = string.Format("Failed collection from customer:{0} amount:{1}, loanID={2}", customerId, initialAmountDue, loanId);
                Log.Warn(Error);
                NL_AddLog(LogType.Info, "Exit 3", this.strategyArgs, Error, null, null);
                return;
            }             // if

            // step 4 - notifications
            if (autoPaymentResult.PaymentCollectedSuccessfully)
            {
                // send mail to customer from template "Mandrill - Repayment confirmation"
                PayEarly payEarly = new PayEarly(customerId, autoPaymentResult.ActualAmountCharged, refNum);
                payEarly.Execute();

                SendLoanStatusMail(customerId, loanId, customerMail, autoPaymentResult.ActualAmountCharged); // Will send mail for paid off loans
            }                                                                                                //if
        }                                                                                                    //HandleOnePayment