public void Post([FromBody] Model.BillPlz.Bill bill)
        {
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(bill))
            {
                string name  = descriptor.Name;
                object value = descriptor.GetValue(bill);
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, name + ": " + value);
            }

            try
            {
                if (ConfigurationManager.AppSettings.Get("Debug") != "0")
                {
                    // Debug mode, push the notification straight away

                    var debugJobId      = Utils.DecodeUniqueId(bill.reference_1);
                    var debugJobDetails = jobDetailsDao.GetByJobId(debugJobId);
                    if (debugJobDetails == null)
                    {
                        DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, string.Format("Debug - Payment callback does not found job details. Job id: {0}, PaymentId: {1}. {2}", debugJobId, bill.id, bill));
                        return;
                    }

                    var extraDataPartner       = Helper.PushNotification.ConstructExtraData(Helper.PushNotification.ECategories.NewOpenJob, debugJobId);
                    var partnerListIdentifiers = userDao.GetUserIdentifiersByRoleId(((int)Constants.Configuration.Role.CompanyAdmin).ToString());
                    if (int.Parse(debugJobDetails.jobTypeId) == (int)Constants.Configuration.DeliveryJobType.Standard)
                    {
                        Utility.UtilNotification.BroadCastMessage(
                            partnerListIdentifiers.ToArray(),
                            extraDataPartner,
                            NotificationMsg.NewOpenJob_Title,
                            NotificationMsg.NewOpenJob_Desc + string.Format("From: {0}\nTo: {1}\nAmount:{2}",
                                                                            debugJobDetails.addressFrom[0].address3,
                                                                            debugJobDetails.addressTo[0].address3,
                                                                            debugJobDetails.amountPartner)
                            );
                    }
                    else if (int.Parse(debugJobDetails.jobTypeId) == (int)Constants.Configuration.DeliveryJobType.Disposal)
                    {
                        Utility.UtilNotification.BroadCastMessage(
                            partnerListIdentifiers.ToArray(),
                            extraDataPartner,
                            NotificationMsg.NewOpenJob_Title,
                            NotificationMsg.NewOpenJob_Desc + string.Format("Dispose items from: {0}\nAmount:{1}",
                                                                            debugJobDetails.addressFrom[0].address3,
                                                                            debugJobDetails.amountPartner)
                            );
                    }

                    // do not proceed for debug mode
                    return;
                }
            }
            catch (Exception e)
            {
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Critical, e.Message);
                return;
            }



            var jobId      = Utils.DecodeUniqueId(bill.reference_1);
            var jobDetails = jobDetailsDao.GetByJobId(jobId);

            if (jobDetails == null)
            {
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Critical, string.Format("Payment callback does not found job details. Job id: {0}, PaymentId: {1}. {2}", jobId, bill.id, bill));
                return;
            }

            // cross check the job id match with exisiting payment id
            var dbBill = paymentsDao.Get(bill.id);

            if (dbBill == null ||
                dbBill.reference_1 != jobId)
            {
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Critical, string.Format("Payment callback does not found matched job id. Job id: {0}, PaymentId: {1}. {2}", jobId, bill.id, bill));
                return;
            }

            // cross check with billplz server status is correct
            var request = WebRequest.Create(string.Format("https://www.billplz.com/api/v3/bills/{0}", bill.id)) as HttpWebRequest;

            using (var responseApi = request.GetResponse() as HttpWebResponse)
            {
                using (var reader = new StreamReader(responseApi.GetResponseStream()))
                {
                    String responseContent = reader.ReadToEnd();
                    Bill   jsonObj         = JsonConvert.DeserializeObject <Bill>(responseContent);

                    // add to database
                    var id = paymentsDao.AddOrUpdate(bill.reference_1, jsonObj);
                    if (id == null || id == "0")
                    {
                        DBLogger.GetInstance().Log(DBLogger.ESeverity.Critical, string.Format("Payment callback unable to update database. Job id: {0}, PaymentId: {1}. {2}, {3}", jobId, bill.id, bill.due_at));
                        return;
                    }

                    // update the job details on the amount paid
                    var totalPaidAmount = jobDetailsDao.UpdatePaidAmount(jobId, jsonObj.paid_amount);
                    if (totalPaidAmount < jobDetails.amount)
                    {
                        DBLogger.GetInstance().Log(DBLogger.ESeverity.Warning, string.Format("Payment callback paid amount not same as total amount. Job id: {0}, PaymentId: {1}. {2}", jobId, bill.id, bill));
                        return;
                    }

                    // update the job order status
                    if (false == jobDeliveryDao.UpdateJobStatus(jobId, ((int)Constants.Configuration.JobStatus.PaymentVerifying).ToString()))
                    {
                        DBLogger.GetInstance().Log(DBLogger.ESeverity.Critical, string.Format("Payment callback unable to update job order status. Job id: {0}, PaymentId: {1}. {2}", jobId, bill.id, bill));
                        return;
                    }

                    // send notification to partners
                    var extraDataPartner       = Helper.PushNotification.ConstructExtraData(Helper.PushNotification.ECategories.NewOpenJob, jobId);
                    var partnerListIdentifiers = userDao.GetUserIdentifiersByRoleId(((int)Constants.Configuration.Role.CompanyAdmin).ToString());
                    if (int.Parse(jobDetails.jobTypeId) == (int)Constants.Configuration.DeliveryJobType.Standard)
                    {
                        Utility.UtilNotification.BroadCastMessage(
                            partnerListIdentifiers.ToArray(),
                            extraDataPartner,
                            NotificationMsg.NewOpenJob_Title,
                            NotificationMsg.NewOpenJob_Desc + string.Format("From: {0}\nTo: {1}\nAmount:{2}",
                                                                            jobDetails.addressFrom[0].address3,
                                                                            jobDetails.addressTo[0].address3,
                                                                            jobDetails.amountPartner)
                            );
                    }
                    else if (int.Parse(jobDetails.jobTypeId) == (int)Constants.Configuration.DeliveryJobType.Disposal)
                    {
                        Utility.UtilNotification.BroadCastMessage(
                            partnerListIdentifiers.ToArray(),
                            extraDataPartner,
                            NotificationMsg.NewOpenJob_Title,
                            NotificationMsg.NewOpenJob_Desc + string.Format("Dispose items from: {0}\nAmount:{1}",
                                                                            jobDetails.addressFrom[0].address3,
                                                                            jobDetails.amountPartner)
                            );
                    }

                    // temporary send email to care about the payment
                    var userObj   = userDao.GetUserById(jobDetails.ownerUserId);
                    var fleetType = fleetTypeDao.Get(jobDetails.fleetTypeId);
                    var jobType   = jobTypeDao.GetById(jobDetails.jobTypeId);
                    UtilEmail.SendOrderReceived(jobDetails.jobId, userObj, jobDetails, fleetType.name, jobType.name);
                }
            }
        }
Beispiel #2
0
        public string AddOrUpdate(string jobId, Model.BillPlz.Bill bill)
        {
            MySqlCommand    mySqlCmd = null;
            MySqlDataReader reader   = null;

            try
            {
                DateTime dueDate = new DateTime();
                try
                {
                    DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, bill.due_at);
                    dueDate = DateTime.ParseExact(bill.due_at, "yyyy-MM-dd", null);
                }
                catch (Exception e)
                {
                    // do nothing
                    DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, e.Message);
                    DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, e.StackTrace);
                }


                // add to user table
                string query = string.Format("INSERT INTO {0} (payment_id, collection_id, paid, state, amount, paid_amount, due_at, email, mobile, name, url, job_id, paid_at) " +
                                             "VALUES (@payment_id, @collection_id, @paid, @state, @amount, @paid_amount, @due_at, @email, @mobile, @name, @url, @job_id, @paid_at);",
                                             TABLE_PAYMENTS);

                /*
                 * string query = string.Format("INSERT INTO {0} (payment_id, collection_id, paid, state, amount, paid_amount, due_at, email, mobile, name, url, job_id, paid_at) " +
                 *  "VALUES (@payment_id, @collection_id, @paid, @state, @amount, @paid_amount, @due_at, @email, @mobile, @name, @url, @job_id, @paid_at) " +
                 *  "ON DUPLICATE KEY UPDATE id= LAST_INSERT_ID(id), paid=@paid, state=@state, paid_amount=@paid_amount, paid_at=@paid_at;",
                 *  TABLE_PAYMENTS); */

                mySqlCmd = new MySqlCommand(query);
                mySqlCmd.Parameters.AddWithValue("@payment_id", bill.id);
                mySqlCmd.Parameters.AddWithValue("@collection_id", bill.collection_id);
                mySqlCmd.Parameters.AddWithValue("@paid", bill.paid);
                mySqlCmd.Parameters.AddWithValue("@state", bill.state);
                mySqlCmd.Parameters.AddWithValue("@amount", bill.amount);
                mySqlCmd.Parameters.AddWithValue("@paid_amount", bill.paid_amount);
                mySqlCmd.Parameters.AddWithValue("@due_at", dueDate.ToString("yyyy-MM-dd HH:mm:ss"));
                mySqlCmd.Parameters.AddWithValue("@email", bill.email);
                mySqlCmd.Parameters.AddWithValue("@mobile", bill.mobile == null? "":bill.mobile);
                mySqlCmd.Parameters.AddWithValue("@name", bill.name);
                mySqlCmd.Parameters.AddWithValue("@url", bill.url);
                mySqlCmd.Parameters.AddWithValue("@job_id", jobId);
                mySqlCmd.Parameters.AddWithValue("@paid_at", bill.paid_at);

                PerformSqlNonQuery(mySqlCmd);
                return(mySqlCmd.LastInsertedId.ToString());
            }
            catch (Exception e)
            {
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, e.Message);
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Info, e.StackTrace);
            }
            finally
            {
                CleanUp(reader, mySqlCmd);
            }

            return(null);
        }