Example #1
0
        /// <summary>
        /// 微信支付成功返回数据
        /// </summary>
        /// <param name="data"></param>
        private void OnPaySuccess(WxPayData data)
        {
            var attach    = (string)data.GetValue("attach");
            var wxOrderId = (string)data.GetValue("transaction_id");

            if (string.IsNullOrEmpty(attach))
            {
                var em = new ExceptionModel()
                {
                    Content = "微信支付返回:attach为空"
                };
                em.Save();
                throw em;
            }
            if (string.IsNullOrEmpty(wxOrderId))
            {
                var em = new ExceptionModel()
                {
                    Content = "微信支付返回:微信订单号为空"
                };
                em.Save();
                throw em;
            }
            string[] aa                = attach.Split(',');
            string   accountID         = aa[1];
            string   orderID           = aa[2];
            var      mongo             = new MongoDBTool();
            var      accountCollection = mongo.GetMongoCollection <AccountModel>();

            var filter    = Builders <AccountModel> .Filter;
            var filterSum = filter.Eq(x => x.AccountID, new ObjectId(accountID)) & filter.Eq("WeChatOrders.WeChatOrderID", new ObjectId(orderID));
            var account   = accountCollection.Find(filterSum).FirstOrDefault();
            var wcOrder   = account.WeChatOrders.Find(x => x.WeChatOrderID.Equals(new ObjectId(orderID)) && x.WXOrderId == null);

            if (wcOrder == null)
            {
                var em = new ExceptionModel()
                {
                    Content = "微信支付返回:订单不存在或者已经生成"
                };
                em.Save();
                throw em;
            }

            var update = Builders <AccountModel> .Update
                         .Set("WeChatOrders.$.WXOrderId", wxOrderId)
                         .Set(x => x.Balances, account.Balances + wcOrder.Total);

            if (wcOrder.VoicePackets != null)
            {
                var ad           = new AccountData();
                var serviceMoney = ad.GetServiceMoney(account.uniacid, wcOrder.VoicePackets.Amount);
                var balance      = account.Balances + wcOrder.Total;
                balance = balance - (wcOrder.VoicePackets.Amount + serviceMoney);
                if (balance >= 0)
                {
                    update = Builders <AccountModel> .Update.Set("WeChatOrders.$.WXOrderId", wxOrderId);

                    ad.CreateVoicePackets(account, wcOrder.VoicePackets, serviceMoney, balance);
                }
            }
            accountCollection.UpdateOne(filterSum, update);
        }