コード例 #1
0
        public bool WriteAccessToken(string clientId, string prefix, AccessTokenBag accessTokenInfo)
        {
            var token = _db.VendAccessTokens.FirstOrDefault(x => x.Prefix.Equals(prefix));

            if (token != null)
            {
                //update
                token.ExpiresIn        = accessTokenInfo.ExpiresIn;
                token.Expires          = accessTokenInfo.Expires;
                token.AccessToken      = accessTokenInfo.AccessToken;
                token.RefreshToken     = accessTokenInfo.RefreshToken;
                token.TokenType        = accessTokenInfo.TokenType;
                _db.Entry(token).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                //new
                token = new VendAccessToken()
                {
                    Prefix       = prefix,
                    ExpiresIn    = accessTokenInfo.ExpiresIn,
                    Expires      = accessTokenInfo.Expires,
                    AccessToken  = accessTokenInfo.AccessToken,
                    RefreshToken = accessTokenInfo.RefreshToken,
                    TokenType    = accessTokenInfo.TokenType
                };
                _db.VendAccessTokens.Add(token);
            }

            return(_db.SaveChanges() == 1);
        }
コード例 #2
0
        public static bool DeleteById(string id, StallEntities db)
        {
            var p = FindById(id, db);

            if (p == null)
            {
                return(false);
            }
            db.Products.Remove(p);
            return(db.SaveChanges() > 0);
        }
コード例 #3
0
        public static bool SetInventoryById(string id, int count, StallEntities db)
        {
            var p = FindById(id, db);

            if (p == null)
            {
                return(false);
            }
            p.Stock = count;
            return(db.SaveChanges() > 0);
        }
コード例 #4
0
        public bool Save(StallEntities db)
        {
            var result = UserManager.Update(_greenspotUser);

            if (!result.Succeeded)
            {
                return(false);
            }

            db.Set <User>().AddOrUpdate(this);
            db.SaveChanges();
            return(true);
        }
コード例 #5
0
        public static OperationResult <Stall> CraeteStall(string userId, string name, string prefix, StallEntities db)
        {
            var result = new OperationResult <Stall>(false);

            if (string.IsNullOrEmpty(userId))
            {
                result.Message = string.Format("UserId不能为空", prefix);
            }
            else if (string.IsNullOrEmpty(name))
            {
                result.Message = string.Format("铺名不能为空", prefix);
            }
            //else if (string.IsNullOrEmpty(prefix))
            //{
            //    result.Message = "Vend前缀不能为空";
            //}
            else if (Stall.FindByName(name, db) != null)
            {
                result.Message = string.Format("铺名 {0} 已占用", name);
            }
            //else if (Stall.FindByVendPrefix(prefix, db) != null)
            //{
            //    result.Message = string.Format("{0}.vendhq.com 已注册", prefix);
            //}
            else
            {
                var stall = new Stall()
                {
                    UserId = userId, StallName = name, Prefix = prefix
                };
                result.Data = db.Stalls.Add(stall);
                try
                {
                    db.SaveChanges();
                }
                catch
                {
                    result.Message = string.Format("无法保存店铺 {0}", name);
                }
            }

            if (result.Data != null)
            {
                result.Succeeded = true;
            }

            return(result);
        }
コード例 #6
0
        public static int?GetDistance(string depCountry, string depCity, string depSuburb,
                                      string destCountry, string destCity, string destSuburb)
        {
            //get from db
            using (var db = new StallEntities())
            {
                var id       = BuilId(depCountry, depCity, depSuburb, destCountry, destCity, destSuburb);
                var dbResult = GetDistance(id, db);
                if (dbResult != null)
                {
                    return(dbResult);
                }

                //call google map api
                var glResult = GoogleMapHelper.GetSuburbDistanceFromGoogleMapApi(depCountry, depCity, depSuburb, destCountry, destCity, destSuburb,
                                                                                 GreenspotConfiguration.AccessAccounts["google.map"].Secret);
                if (glResult == null)
                {
                    StallApplication.SysError($"[GOOGLE DISTANCE]failed to get distance {depCountry},{depCity},{depSuburb} to {destCountry},{destCity},{destSuburb}");
                    return(null);
                }
                else
                {
                    //save to db
                    var distance = new SuburbDistance()
                    {
                        ID = id,
                        DepartureCountryCode = depCountry,
                        DepartureCity        = depCity,
                        DepartureSuburb      = depSuburb,

                        DestinationCountryCode = destCountry,
                        DestinationCity        = destCity,
                        DestinationSuburb      = destSuburb,

                        Meters = glResult.Value
                    };

                    db.SuburbDistances.Add(distance);
                    db.SaveChanges();

                    return(glResult.Value);
                }
            }
        }
コード例 #7
0
 public bool Save()
 {
     using (var db = new StallEntities())
     {
         try
         {
             db.Set <Stall>().AddOrUpdate(this);
             //db.Set<StallContact>().AddOrUpdate(Contacts.ToArray());
             db.Set <Product>().AddOrUpdate(Products.ToArray());
             //db.Set<VendWebhook>().AddOrUpdate(Webhooks.ToArray());
             db.SaveChanges();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }
コード例 #8
0
        public static Payment CreatePayment(StallEntities db, decimal amount, string orderIds = null)
        {
            //set total price
            var payment = new Payment()
            {
                Amount     = amount,
                CreateTime = DateTime.Now,
                OrderIds   = orderIds,
                HasPaid    = false
            };

            db.Payments.Add(payment);
            if (db.SaveChanges() > 0)
            {
                return(payment);
            }
            else
            {
                return(null);
            }
        }
コード例 #9
0
        public async Task Notify(StallEntities db, IList <string> openids)
        {
            //vend
            if (!HasSendToVend)
            {
                HasSendToVend = await SendToVend();
            }

            if (!HasSendToWechat)
            {
                //wechat
                HasSendToWechat = await SendToWechat(openids);
            }

            if (IsPrintOrder && !HasSendToPrinter)
            {
                //print
                HasSendToPrinter = await SendToPrinter();
            }

            //update
            db.SaveChanges();
        }
コード例 #10
0
 public OperationResult <bool> Delete(StallEntities db)
 {
     db.Products.Remove(this);
     return(new OperationResult <bool>(db.SaveChanges() > 0));
 }
コード例 #11
0
 public OperationResult <bool> Save(StallEntities db)
 {
     db.Set <Product>().AddOrUpdate(this);
     return(new OperationResult <bool>(db.SaveChanges() > 0));
 }
コード例 #12
0
        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);
        }
コード例 #13
0
 public bool Save(StallEntities db)
 {
     db.Payments.AddOrUpdate(this);
     return(db.SaveChanges() > 0);
 }
コード例 #14
0
 public bool Save(StallEntities db)
 {
     db.DeliveryAddresses.Add(this);
     return(db.SaveChanges() >= 0);
 }
コード例 #15
0
        /// <summary>
        /// verify px payment match transaction
        /// if pay success set transaction as paid
        /// </summary>
        /// <param name="resultId"></param>
        /// <param name="isSuccess"></param>
        /// <returns></returns>
        public static bool VerifyPxPayPayment(string resultId, bool isSuccess, out int outPaymentId)
        {
            outPaymentId = 0;
            try
            {
                //check response
                StallApplication.BizInfoFormat("[ACCOUNTANT-PXPAY]start to get pxpay payment result={0}", resultId);
                ResponseOutput output = _pxPay.ProcessResponse(resultId);
                if (output == null)
                {
                    StallApplication.BizError("[ACCOUNTANT-PXPAY]can not get pxpay payment result - resposne is null");
                    return(false);
                }
                if (!(isSuccess ? "1" : "0").Equals(output.Success))
                {
                    StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]payment result not match except {0} - actual {1}", output.Success, isSuccess);
                    StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]{0}", output);
                    return(false);
                }

                //set payment
                int     paymentId = int.Parse(output.TxnId.Split('-')[0]);
                decimal amount    = decimal.Parse(output.AmountSettlement);
                using (StallEntities db = new StallEntities())
                {
                    Payment payment = db.Payments.FirstOrDefault(o => o.Id == paymentId);
                    if (payment == null)
                    {
                        StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]payment {0} can not mactch payment {1}", resultId, outPaymentId);
                        StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]{0}", output);
                        return(false);
                    }

                    payment.ResponseTime = DateTime.Now;
                    outPaymentId         = paymentId;
                    bool result = true;
                    if (!isSuccess)
                    {
                        //pay fail
                        StallApplication.BizErrorFormat("[ACCOUNT-PAPAY]pay failed payment id={0}", payment.Id);
                    }
                    else
                    {
                        //pay success
                        if (payment.Amount != amount)
                        {
                            StallApplication.BizErrorFormat("[ACCOUNT-PAPAY]pxpay amount {0} <> transaction amount {1}", amount, payment.Amount);
                            result = false;
                        }
                        else
                        {
                            payment.HasPaid = true;
                        }
                    }
                    payment.PxPayResponse = output.ToString();
                    db.SaveChanges();
                    return(result);
                }
            }
            catch (Exception ex)
            {
                StallApplication.SysError("can not get pxpay payment result", ex);
                return(false);
            }
        }