Exemplo n.º 1
0
        //vend call back
        public async Task <ActionResult> Connect()
        {
            var code        = Request["code"];
            var prefix      = Request["domain_prefix"];
            var userId      = Request["state"];
            var accessToken = await StallApplication.GetAccessTokenAsync(prefix, code);

            if (!string.IsNullOrEmpty(accessToken))
            {
                return(Content(string.Format("Vend {0} binding succeed", prefix)));
            }
            else
            {
                return(View("Error"));
            };
        }
Exemplo n.º 2
0
        //public async Task<OperationResult<bool>> CreateDelivery()
        //{
        //    var result = new OperationResult(false);

        //    //create delivery product
        //    var pdtDelivery = new SDK.Vend.VendProduct()
        //    {
        //        Name = "运费",
        //        BaseName = "运费",
        //        Handle = "gs-delivery",
        //        Sku = "gs-delivery",
        //        Price = 0.0M,
        //        Active = true
        //    };

        //    var pdtResp = await SDK.Vend.VendProduct.AddProduct(Prefix, await StallApplication.GetAccessTokenAsync(Prefix), pdtDelivery);
        //    if (pdtResp == null || pdtResp.Product == null || string.IsNullOrEmpty(pdtResp.Product.Id))
        //    {
        //        result.Message = "无法创建运费";
        //        return result;
        //    }

        //    //set data
        //    result.Succeeded = true;
        //    return result;
        //}

        private async Task <OperationResult <bool> > CreateWebhook()
        {
            var result = new OperationResult(false);
            //load webhooks
            var webhooks = await SDK.Vend.VendWebhook.GetWebhooksAsync(Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

            if (!webhooks.Any(x => SDK.Vend.VendWebhook.VendWebhookTypes.ProductUpdate.Equals(x.Type)))
            {
                //create product update webhook
                var pdtUpdate = new SDK.Vend.VendWebhook()
                {
                    Type   = SDK.Vend.VendWebhook.VendWebhookTypes.ProductUpdate,
                    Url    = GreenspotConfiguration.AppSettings["webhook.product.update"].Value,
                    Active = true
                };
                var pdtResp = await SDK.Vend.VendWebhook.CreateVendWebhookAsync(pdtUpdate, Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

                if (pdtResp == null)
                {
                    result.Message = "无法创建商品更新WEBHOOK";
                    return(result);
                }
            }


            if (!webhooks.Any(x => SDK.Vend.VendWebhook.VendWebhookTypes.InventoryUpdate.Equals(x.Type)))
            {
                //create inventory update webhook
                var stockUpdate = new SDK.Vend.VendWebhook()
                {
                    Type   = SDK.Vend.VendWebhook.VendWebhookTypes.InventoryUpdate,
                    Url    = GreenspotConfiguration.AppSettings["webhook.inventory.update"].Value,
                    Active = true
                };
                var stockResp = await SDK.Vend.VendWebhook.CreateVendWebhookAsync(stockUpdate, Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

                if (stockResp == null)
                {
                    result.Message = "无法创建库存更新WEBHOOK";
                    return(result);
                }
            }

            //set data
            result.Succeeded = true;
            return(result);
        }
Exemplo n.º 3
0
        private async Task <OperationResult <bool> > LoadProduct()
        {
            var result = new OperationResult(false);

            //load stall
            var productResult = await VendProduct.GetProductsAsync(Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

            if (productResult?.Products == null)
            {
                result.Message = "无法获取VEND商品信息";
                return(result);
            }

            //set products
            Products.Clear();

            //var products = productResult?.Products.OrderBy(x => x.VariantParentId).ToList();
            var vProducts = productResult?.Products;

            foreach (var vP in vProducts)
            {
                var p = Product.ConvertFrom(vP, Id);
                if (p.Handle.ToLower().Equals("vend-discount"))
                {
                    DiscountProductId = p.Id;
                    p.Active          = false;
                }
                else if (p.Handle.ToLower().Equals("jdl-delivery"))
                {
                    DeliveryProductId = p.Id;
                    p.Active          = false;
                }
                Products.Add(p);
            }

            //set data
            result.Succeeded = true;
            return(result);
        }
        public static async Task <OperationResult> InitMultiStall(int id, StallEntities db)
        {
            var result    = new OperationResult(false);
            var baseStall = Stall.FindById(id, db);

            if (baseStall == null)
            {
                result.Message = $"Stall {id} is not exist";
                return(result);
            }

            //load base info
            var infoResult = await baseStall.LoadInfo();

            if (!infoResult.Succeeded)
            {
                result.Message = infoResult.Message;
                return(result);
            }

            //load product
            var productResult = await baseStall.LoadProduct();

            if (!productResult.Succeeded)
            {
                result.Message = productResult.Message;
                return(result);
            }

            //create webhook
            var webhookResult = await baseStall.CreateWebhook();

            if (!webhookResult.Succeeded)
            {
                result.Message = webhookResult.Message;
                return(result);
            }

            //load suppliers
            var supplierResult = await SDK.Vend.Supplier.GetSuppliersAsync(baseStall.Prefix, await StallApplication.GetAccessTokenAsync(baseStall.Prefix));

            var suppliers = supplierResult.Suppliers;

            if (suppliers == null || suppliers.Count == 0)
            {
                //
                result.Message = "无法获取VEND Supplier信息";
                return(result);
            }

            //seperate by supplier
            var unStalledProduct = new List <Product>();
            var stalls           = new SortedDictionary <string, Stall>();

            foreach (var p in baseStall.Products)
            {
                //loop product
                if (string.IsNullOrEmpty(p.SupplierName))
                {
                    //no supplier product
                    unStalledProduct.Add(p);
                    continue;
                }

                Stall currStall;


                if (stalls.ContainsKey(p.SupplierName))
                {
                    //exist stall
                    currStall = stalls[p.SupplierName];
                }
                else
                {
                    #region new stall
                    var supplier = suppliers.FirstOrDefault(x => x.Name.Equals(p.SupplierName));

                    //new stall
                    currStall = new Stall()
                    {
                        UserId            = baseStall.UserId,
                        Prefix            = baseStall.Prefix,
                        StallName         = p.SupplierName,
                        RetailerId        = baseStall.RetailerId,
                        RegisterId        = baseStall.RegisterId,
                        RegisterName      = baseStall.RegisterName,
                        OutletId          = baseStall.OutletId,
                        PaymentTypeId     = baseStall.PaymentTypeId,
                        DeliveryProductId = baseStall.DeliveryProductId,
                        DiscountProductId = baseStall.DiscountProductId,
                        ContactName       = $"{supplier.Contact.FirstName} {supplier.Contact.LastName}".Trim(),
                        Mobile            = supplier.Contact.Mobile,
                        Phone             = supplier.Contact.Phone,
                        Address1          = supplier.Contact.PhysicalAddress1,
                        Address2          = supplier.Contact.PhysicalAddress2,
                        City          = supplier.Contact.PhysicalCity,
                        CountryId     = supplier.Contact.PhysicalCountryId,
                        Postcode      = supplier.Contact.PhysicalPostcode,
                        StateOrRegion = supplier.Contact.PhysicalState,
                        Suburb        = supplier.Contact.PhysicalSuburb,
                        TimeZone      = baseStall.TimeZone,
                        Status        = StallStatus.Offline
                    };

                    stalls.Add(p.SupplierName, currStall);
                    #endregion
                }

                currStall.Products.Add(p);
            }

            //save to db
            using (var trans = db.Database.BeginTransaction())
            {
                //save base stall
                try
                {
                    //keep unstalled product
                    baseStall.Products = unStalledProduct;
                    db.Set <Stall>().AddOrUpdate(baseStall);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.Message = $"failed to save stall {baseStall.StallName}:{ex.ToString()}";
                    trans.Rollback();
                    return(result);
                }

                foreach (var s in stalls.Values)
                {
                    try
                    {
                        db.Set <Stall>().AddOrUpdate(s);
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        result.Message = $"failed to save stall {s.StallName}:{ex.ToString()}";
                        trans.Rollback();
                        return(result);
                    }
                }

                trans.Commit();
            }

            result.Succeeded = true;
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Send Order To Vend
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public async Task <bool> SendToVend()
        {
            try
            {
                //var tmpOrder = JsonConvert.DeserializeObject<Order>(JsonString);
                //var tmpStall = Models.Stall.FindById(StallId, db);
                //create vend api object
                var vendSale = new VendRegisterSaleRequest();
                vendSale.InvoiceNumber = Id.ToString();
                vendSale.RegisterId    = Stall.RegisterId;
                vendSale.SaleDate      = CreateTime.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
                vendSale.Status        = OrderStatus.CLOSED;
                vendSale.TotalPrice    = (double)CalcTotalPriceExcludeTax();
                vendSale.TotalTax      = (double)CalcTotalTax();
                //vendSale.TaxName = Items[0].Product.TaxName;
                vendSale.RegisterSaleProducts = new List <VendRegisterSaleRequest.RegisterSaleProduct>();
                vendSale.RegisterSalePayments = new List <VendRegisterSaleRequest.RegisterSalePayment>();
                vendSale.Note = string.Format("#{0}@{1}\n{2}\n{3}\n{4}", Id, PaymentId, Receiver, DeliveryAddress, Note);
                foreach (var item in Items)
                {
                    var salePdt = new VendRegisterSaleRequest.RegisterSaleProduct()
                    {
                        ProductId = item.ProductId,
                        Quantity  = item.Quantity,
                        Price     = (double)item.Price,
                        Tax       = (double)item.Tax,
                        TaxId     = item.TaxId,
                        TaxTotal  = (double)item.Tax
                    };

                    salePdt.Attributes.Add(new VendRegisterSaleRequest.Attribute()
                    {
                        Name  = "line_note",
                        Value = item.LineNote
                    });

                    vendSale.RegisterSaleProducts.Add(salePdt);
                }

                vendSale.RegisterSalePayments.Add(new VendRegisterSaleRequest.RegisterSalePayment()
                {
                    RetailerPaymentTypeId = Stall.PaymentTypeId,
                    PaymentDate           = string.Format("{0:yyyy-MM-dd HH:mm:ss}", Payment.ResponseTime?.ToUniversalTime()),
                    Amount = (double)StallAmount
                });

                //do reqeust
                var response = await VendRegisterSale.CreateVendRegisterSalesAsync(vendSale, Stall.Prefix, await StallApplication.GetAccessTokenAsync(Stall.Prefix));

                //update
                if (!string.IsNullOrEmpty(response.RegisterSale.Id) && OrderStatus.CLOSED.Equals(response.RegisterSale.Status))
                {
                    VendResponse = JsonConvert.SerializeObject(response);
                    VendSaleId   = response?.RegisterSale?.Id;
                    return(true);
                }
                else
                {
                    StallApplication.SysError("[MSG]fail to save to vend");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                StallApplication.SysError("[MSG]fail to save to vend", ex);
                return(false);
            }
        }
Exemplo n.º 6
0
        private async Task <OperationResult <bool> > LoadInfo()
        {
            var result = new OperationResult(false);
            //load stall
            var outletResult = await VendOutlet.GetOutletsAsync(Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

            if (outletResult?.Outlets == null || outletResult.Outlets.Count == 0)
            {
                result.Message = "无法同步VEND商铺信息";
                return(result);
            }

            //set stall info
            var outlet = outletResult.Outlets[0];

            RetailerId = outlet.RetailerId;
            OutletId   = outlet.Id;
            //Email = outlet.Email;
            Address1      = outlet.PhysicalAddress1;
            Address2      = outlet.PhysicalAddress2;
            City          = outlet.PhysicalCity;
            CountryId     = outlet.PhysicalCountryId;
            Postcode      = outlet.PhysicalPostcode;
            StateOrRegion = outlet.PhysicalState;
            Suburb        = outlet.PhysicalSuburb;
            TimeZone      = outlet.TimeZone;

            //load payment types
            var paytypeResult = await VendPaymentType.GetPaymentTypetsAsync(Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

            if (paytypeResult?.PaymentTypes == null || paytypeResult.PaymentTypes.Count == 0)
            {
                result.Message = "无法获取VEND PAYMENT TYPE信息";
                return(result);
            }

            //set payment type
            PaymentTypeId = null;
            foreach (var pt in paytypeResult.PaymentTypes)
            {
                if (pt.Name.ToUpper().Equals("JDL PAY"))
                {
                    PaymentTypeId = pt.Id;
                    break;
                }
            }

            if (string.IsNullOrEmpty(PaymentTypeId))
            {
                result.Message = "无法获取名为JDL PAY的VEND PAYMENT TYPE";
                return(result);
            }

            //load registers
            var registerResult = await VendRegister.GetRegistersAsync(Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

            if (registerResult?.Registers == null || registerResult.Registers.Count == 0)
            {
                result.Message = "无法获取VEND REGISTER信息";
                return(result);
            }

            //set registers
            var reg = registerResult.Registers[0];

            RegisterName = reg.Name;
            RegisterId   = reg.Id;

            result.Succeeded = true;
            return(result);
        }
Exemplo n.º 7
0
        public async Task <OperationResult <bool> > UpdateProductById(string id, StallEntities db)
        {
            var result = new OperationResult(false);

            //load product
            var productResult = await VendProduct.GetProductByIdAsync(id, Prefix, await StallApplication.GetAccessTokenAsync(Prefix));

            if (productResult?.Products == null)
            {
                result.Message = string.Format("无法获取商品[{0}]信息", id);
                return(result);
            }

            //set products
            var vProducts = productResult?.Products;

            foreach (var vP in vProducts)
            {
                if (vP.Id.Equals(id))
                {
                    var p = Product.ConvertFrom(vP, Id);
                    return(p.Save(db));
                }
            }

            result.Message = string.Format("无法获取商品[{0}]信息", id);
            return(result);
        }