/// <summary>
        /// Querys
        /// </summary>
        /// <param name="url">Base Url </param>
        /// <param name="contentTemplate">Razor Template to render for each item of the output</param>
        public object ListCollection(string entityName, Func <dynamic, object> contentTemplate)
        {
#if DEBUG
            if (entityName == null)
            {
                throw new ArgumentNullException("collectionName");
            }
#endif
            var list = Database.QueryAsJObject(entityName, oDataSort: "DisplayOrder");

            foreach (var item in list)
            {
                var output = contentTemplate(item);
                this.WriteLiteral(output);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Handles Query Request (Get /tables/{tableName}?...)
        /// </summary>
        /// <param name="db"></param>
        /// <param name="arg"></param>
        /// <returns></returns>
        protected dynamic HandleQueryRequest_WithoutCountSupport(NancyBlackDatabase db, dynamic arg)
        {
            var entityName = (string)arg.table_name;

            if (this.Request.Query["$select"] == null)
            {
                var rows = db.Query(entityName,
                                    this.Request.Query["$filter"],
                                    this.Request.Query["$orderby"],
                                    this.Request.Query["$skip"],
                                    this.Request.Query["$top"]);

                return(rows);
            }
            else
            {
                var toInclude = ((string)this.Request.Query["$select"]).Split(',');
                if (toInclude.Length == 1 && toInclude[0] == "$select")
                {
                    return(400);
                }

                if (toInclude.Length == 0)
                {
                    return(400);
                }

                var rows = db.QueryAsJObject(entityName,
                                             (string)this.Request.Query["$filter"],
                                             (string)this.Request.Query["$orderby"],
                                             (string)this.Request.Query["$skip"],
                                             (string)this.Request.Query["$top"]).ToList();

                foreach (JObject item in rows)
                {
                    foreach (var property in item.Properties().ToList())
                    {
                        if (toInclude.Contains(property.Name) == false)
                        {
                            property.Remove();
                        }
                    }
                }

                return(rows);
            }
        }
        public static void HandlePayment(NancyBlackDatabase db, PaymentLog log, DateTime paidWhen)
        {
            // ensure only one thread is processing this so
            lock (BaseModule.GetLockObject(log.SaleOrderIdentifier))
            {
                // find the sale order
                var so = db.Query <SaleOrder>()
                         .Where(row => row.SaleOrderIdentifier == log.SaleOrderIdentifier)
                         .FirstOrDefault();

                bool isPaymentReceived = false;

                JArray exceptions = new JArray();

                if (so == null)
                {
                    exceptions.Add(JObject.FromObject(new
                    {
                        type        = "Wrong SO Number",
                        description = "Wrong SO Number"
                    }));

                    goto EndPayment;
                }

                log.SaleOrderId = so.Id;
                log.PaymentDate = paidWhen;

                // check duplicated payment log (sometime we got double request from PaySbuy)
                if (log.PaymentSource == PaymentMethod.PaySbuy && !log.IsErrorCode)
                {
                    var jsonStr            = ((JObject)log.FormResponse).ToString();
                    var duplicatedRequests = db.QueryAsJObject("PaymentLog", "FormResponse eq '" + jsonStr + "'").ToList();

                    if (duplicatedRequests.Count > 0)
                    {
                        exceptions.Add(JObject.FromObject(new
                        {
                            type        = "Duplicated Request",
                            description = string.Format(
                                "Duplicated with Id: {0}", duplicatedRequests.First().Value <int>("Id"))
                        }));

                        goto EndPayment;
                    }
                }

                // Wrong Payment Status
                if (so.PaymentStatus == PaymentStatus.PaymentReceived)
                {
                    so.IsDuplicatePayment = true;
                    exceptions.Add(JObject.FromObject(new
                    {
                        type        = "Wrong Status",
                        description = string.Format(
                            "Current paymentlog status of SO is: {0}", PaymentStatus.DuplicatePayment)
                    }));
                }

                // Error code received
                if (log.IsErrorCode)
                {
                    so.PaymentStatus = PaymentStatus.WaitingForPayment;
                    exceptions.Add(JObject.FromObject(new
                    {
                        type        = "Error Code",
                        description = "Error Code Received from Payment Processor: " + log.ResponseCode
                    }));

                    goto EndPayment;
                }

                // after this line will never be run until EndPayment when IsErrorCode == true
                if (so.PaymentStatus != PaymentStatus.PaymentReceived && log.Amount != so.TotalAmount)
                {
                    log.IsPaymentSuccess   = true;
                    so.PaymentStatus       = PaymentStatus.Deposit;
                    so.PaymentReceivedDate = DateTime.Now; // Need to use this to manage queue

                    exceptions.Add(JObject.FromObject(new
                    {
                        type        = "Split Payment",
                        description = string.Format(
                            "Expects: {0} amount from SO, payment is {1}", so.TotalAmount, log.Amount)
                    }));

                    var paymentlogs = db.Query <PaymentLog>()
                                      .Where(p => p.SaleOrderIdentifier == so.SaleOrderIdentifier);

                    var splitPaymentLogs = (from sPLog in paymentlogs
                                            where sPLog.IsErrorCode == false
                                            select sPLog).ToList();

                    isPaymentReceived = so.TotalAmount <= splitPaymentLogs.Sum(splog => splog.Amount) + log.Amount;
                }

                if (exceptions.Count == 0 || isPaymentReceived)
                {
                    log.IsPaymentSuccess = true;

                    so.PaymentStatus       = PaymentStatus.PaymentReceived;
                    so.PaymentReceivedDate = DateTime.Now;
                }

EndPayment:

                log.Exception = exceptions;
                db.UpsertRecord <PaymentLog>(log);

                CommerceModule.PaymentOccured(so, db);

                if (log.IsPaymentSuccess)
                {
                    // Set Receipt number
                    var rc = db.UpsertRecord <Receipt>(new Receipt()
                    {
                        SaleOrderId = so.Id, PaymentLogId = log.Id
                    });
                    rc.SetIdentifier();
                    db.UpsertRecord(rc);

                    CommerceModule.PaymentSuccess(so, db);
                }

                db.UpsertRecord <SaleOrder>(so);

                // reset the one time code used
                foreach (var item in so.ItemsDetail)
                {
                    if (item.Url.StartsWith("/promotions/code"))
                    {
                        if (item.Attributes.onetime != null)
                        {
                            var product = db.GetById <Product>(item.Id);
                            product.Url = product.Url.Replace("/promotions/code", "/promotions/code/archive-onetime");
                            db.UpsertRecord(product);
                        }
                    }
                }

                // Automate change status to WaitingForOrder for add item to PO
                if (exceptions.Count == 0 || isPaymentReceived)
                {
                    if (so.Status == SaleOrderStatus.Confirmed)
                    {
                        so.Status = SaleOrderStatus.WaitingForOrder;
                        db.UpsertRecord <SaleOrder>(so);
                    }
                }
            }
        }