修改支付
This commit is contained in:
parent
e6fa2ea5f8
commit
71037c6c67
|
|
@ -1,7 +1,10 @@
|
|||
using HuanMeng.MiaoYu.Code.Cache.Special;
|
||||
using HuanMeng.MiaoYu.Code.DataAccess;
|
||||
using HuanMeng.MiaoYu.Code.Payment;
|
||||
using HuanMeng.MiaoYu.Model.Dto.Order;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
|
@ -55,7 +58,7 @@ namespace HuanMeng.MiaoYu.Code.Order
|
|||
{
|
||||
price = (decimal)0.01;
|
||||
}
|
||||
(var orderId, var order) = await payment.CreateOrder(product.Id,product.ProductName, price, product, ip);
|
||||
(var orderId, var order) = await payment.CreateOrder(product.Id, product.ProductName, price, product, ip);
|
||||
var t = product.ToIntentOrder(paymentMethod, orderId);
|
||||
t.UserId = _UserId;
|
||||
Dao.daoDbMiaoYu.context.Add(t);
|
||||
|
|
@ -76,4 +79,144 @@ namespace HuanMeng.MiaoYu.Code.Order
|
|||
return new BaseResponse<IntentOrderDto>(ResonseCode.Success, "", intentOrderDto);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单扩展类
|
||||
/// </summary>
|
||||
public static class OrderExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 带锁版本
|
||||
/// </summary>
|
||||
/// <param name="product"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="pay"></param>
|
||||
/// <param name="orderId"></param>
|
||||
/// <param name="price"></param>
|
||||
/// <param name="intentDate"></param>
|
||||
/// <param name="dao"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> OrderRewardsAsync(this T_Products product, T_User user, string pay, string orderId, decimal price, DateTime intentDate, DAO dao)
|
||||
{
|
||||
var userId = user.Id;
|
||||
var chargeMoneyCount = dao.daoDbMiaoYu.context.T_Order.Count(it => it.UserId == userId && it.ProductId == product.ProductId);
|
||||
var productReward = await dao.daoDbMiaoYu.context.T_Products_Reward.Where(it => it.ProductId == product.ProductId).ToListAsync();
|
||||
|
||||
if (productReward != null && productReward.Count > 0)
|
||||
{
|
||||
using (IDbContextTransaction transaction = dao.daoDbMiaoYu.context.Database.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
//List<T_User_Currency> user_Currencies = new List<T_User_Currency>();
|
||||
foreach (var reward in productReward)
|
||||
{
|
||||
var money = reward.Money;
|
||||
if (product.IsFirstCharge && chargeMoneyCount == 0)
|
||||
{
|
||||
money = reward.FirstChargeMoney ?? reward.Money;
|
||||
}
|
||||
var currency = (UserCurrencyType)reward.CurrencyType;
|
||||
var userCurrency = new T_User_Currency();
|
||||
user.ConsumeMoneyNoWork(currency, money, dao, userCurrency, orderId);
|
||||
}
|
||||
T_Order order = new T_Order()
|
||||
{
|
||||
OrderId = orderId,
|
||||
CreatedAt = DateTime.Now,
|
||||
OrderDate = intentDate,
|
||||
PaymentDate = DateTime.Now,
|
||||
PaymentDay = DateOnly.FromDateTime(DateTime.Now),
|
||||
PaymentMethod = pay,
|
||||
ProductId = product.ProductId,
|
||||
Status = (int)OrderState.已完成,
|
||||
TenantId = product.TenantId,
|
||||
TotalPrice = price,
|
||||
UpdatedAt = DateTime.Now,
|
||||
UserId = userId,
|
||||
};
|
||||
T_OrderItems t_OrderItems = new T_OrderItems()
|
||||
{
|
||||
OrderId = orderId,
|
||||
PaymentInfo = "",
|
||||
Product = product.Id,
|
||||
ProductId = product.ProductId,
|
||||
RewardInfo = JsonConvert.SerializeObject(productReward),
|
||||
TenantId = product.TenantId,
|
||||
};
|
||||
dao.daoDbMiaoYu.context.T_OrderItems.Add(t_OrderItems);
|
||||
dao.daoDbMiaoYu.context.T_Order.Add(order);
|
||||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 不带锁,出现异常需要自己处理
|
||||
/// </summary>
|
||||
/// <param name="product"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="pay"></param>
|
||||
/// <param name="orderId"></param>
|
||||
/// <param name="price"></param>
|
||||
/// <param name="intentDate"></param>
|
||||
/// <param name="dao"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task OrderRewardsNoWorkAsync(this T_Products product, T_User user, string pay, string orderId, decimal price, DateTime intentDate, DAO dao)
|
||||
{
|
||||
var userId = user.Id;
|
||||
var chargeMoneyCount = dao.daoDbMiaoYu.context.T_Order.Count(it => it.UserId == userId && it.ProductId == product.ProductId);
|
||||
var productReward = await dao.daoDbMiaoYu.context.T_Products_Reward.Where(it => it.ProductId == product.ProductId).ToListAsync();
|
||||
|
||||
if (productReward != null && productReward.Count > 0)
|
||||
{
|
||||
foreach (var reward in productReward)
|
||||
{
|
||||
var money = reward.Money;
|
||||
if (product.IsFirstCharge && chargeMoneyCount == 0)
|
||||
{
|
||||
money = reward.FirstChargeMoney ?? reward.Money;
|
||||
}
|
||||
var currency = (UserCurrencyType)reward.CurrencyType;
|
||||
var userCurrency = new T_User_Currency();
|
||||
user.ConsumeMoneyNoWork(currency, money, dao, userCurrency, orderId);
|
||||
}
|
||||
T_Order order = new T_Order()
|
||||
{
|
||||
OrderId = orderId,
|
||||
CreatedAt = DateTime.Now,
|
||||
OrderDate = intentDate,
|
||||
PaymentDate = DateTime.Now,
|
||||
PaymentDay = DateOnly.FromDateTime(DateTime.Now),
|
||||
PaymentMethod = pay,
|
||||
ProductId = product.ProductId,
|
||||
Status = (int)OrderState.已完成,
|
||||
TenantId = product.TenantId,
|
||||
TotalPrice = price,
|
||||
UpdatedAt = DateTime.Now,
|
||||
UserId = userId,
|
||||
};
|
||||
T_OrderItems t_OrderItems = new T_OrderItems()
|
||||
{
|
||||
OrderId = orderId,
|
||||
PaymentInfo = "",
|
||||
Product = product.Id,
|
||||
ProductId = product.ProductId,
|
||||
RewardInfo = JsonConvert.SerializeObject(productReward),
|
||||
TenantId = product.TenantId,
|
||||
};
|
||||
dao.daoDbMiaoYu.context.T_OrderItems.Add(t_OrderItems);
|
||||
dao.daoDbMiaoYu.context.T_Order.Add(order);
|
||||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using HuanMeng.DotNetCore.Utility;
|
|||
using HuanMeng.MiaoYu.Code.AppExtend;
|
||||
using HuanMeng.MiaoYu.Code.Base;
|
||||
using HuanMeng.MiaoYu.Code.DataAccess;
|
||||
using HuanMeng.MiaoYu.Code.Order;
|
||||
using HuanMeng.MiaoYu.Code.Payment;
|
||||
using HuanMeng.MiaoYu.Code.Users;
|
||||
using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
|
|
@ -108,71 +109,23 @@ namespace HuanMeng.MiaoYu.WebPayApi.Controllers
|
|||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
return $"error;用户不存在";
|
||||
}
|
||||
var chargeMoneyCount = dao.daoDbMiaoYu.context.T_Order.Count(it => it.UserId == intentOrder.UserId && it.ProductId == it.ProductId);
|
||||
var productReward = await dao.daoDbMiaoYu.context.T_Products_Reward.Where(it => it.ProductId == intentOrder.ProductId).ToListAsync();
|
||||
|
||||
if (productReward != null && productReward.Count > 0)
|
||||
using (IDbContextTransaction transaction = dao.daoDbMiaoYu.context.Database.BeginTransaction())
|
||||
{
|
||||
using (IDbContextTransaction transaction = dao.daoDbMiaoYu.context.Database.BeginTransaction())
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
//List<T_User_Currency> user_Currencies = new List<T_User_Currency>();
|
||||
foreach (var reward in productReward)
|
||||
{
|
||||
var money = reward.Money;
|
||||
if (product.IsFirstCharge && chargeMoneyCount == 0)
|
||||
{
|
||||
money = reward.FirstChargeMoney ?? reward.Money;
|
||||
}
|
||||
var currency = (UserCurrencyType)reward.CurrencyType;
|
||||
var userCurrency = new T_User_Currency();
|
||||
user.ConsumeMoneyNoWork(currency, money, dao, userCurrency, orderId);
|
||||
}
|
||||
T_Order order = new T_Order()
|
||||
{
|
||||
OrderId = orderId,
|
||||
CreatedAt = DateTime.Now,
|
||||
OrderDate = intentOrder.IntentDate,
|
||||
PaymentDate = DateTime.Now,
|
||||
PaymentDay = DateOnly.FromDateTime(DateTime.Now),
|
||||
PaymentMethod = pay,
|
||||
ProductId = intentOrder.ProductId,
|
||||
Status = (int)OrderState.已完成,
|
||||
TenantId = intentOrder.TenantId,
|
||||
TotalPrice = intentOrder.Price,
|
||||
UpdatedAt = DateTime.Now,
|
||||
UserId = intentOrder.UserId,
|
||||
};
|
||||
T_OrderItems t_OrderItems = new T_OrderItems()
|
||||
{
|
||||
OrderId = orderId,
|
||||
PaymentInfo = "",
|
||||
Product = product.Id,
|
||||
ProductId = intentOrder.ProductId,
|
||||
RewardInfo = JsonConvert.SerializeObject(productReward),
|
||||
TenantId = intentOrder.TenantId,
|
||||
};
|
||||
dao.daoDbMiaoYu.context.T_OrderItems.Add(t_OrderItems);
|
||||
dao.daoDbMiaoYu.context.T_Order.Add(order);
|
||||
intentOrder.Status = (int)OrderState.已完成;
|
||||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
intentOrder.Status = (int)OrderState.发货失败;
|
||||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
throw;
|
||||
}
|
||||
await product.OrderRewardsNoWorkAsync(user, pay, orderId, intentOrder.Price, intentOrder.IntentDate, dao);
|
||||
intentOrder.Status = (int)OrderState.已完成;
|
||||
dao.daoDbMiaoYu.context.SaveChanges();
|
||||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
intentOrder.Status = (int)OrderState.发货失败;
|
||||
dao.daoDbMiaoYu.context.SaveChanges();
|
||||
await dao.daoDbMiaoYu.context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//redis.ListRightPush("myQueue", "value3");
|
||||
//await redis.PublishAsync("order_rewards_queue", orderData);
|
||||
redis.KeyDelete(baseKey);
|
||||
context.Request.EnableBuffering(); // Enable buffering to allow the body to be read multiple times
|
||||
using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, leaveOpen: true))
|
||||
{
|
||||
|
|
@ -180,7 +133,7 @@ namespace HuanMeng.MiaoYu.WebPayApi.Controllers
|
|||
logger.LogInformation($"请求支付回调接口,请求路径: {context.Request.Path}, 请求Body: {bodyContent}");
|
||||
context.Request.Body.Position = 0;
|
||||
}
|
||||
redis.KeyDelete(baseKey);
|
||||
|
||||
return $"success";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user