using BPA.Kitchen.Core;
using BPA.KitChen.GroupMealOrder.Core.CacheOption;
using BPA.KitChen.GroupMealOrder.Core.Common;
using BPA.KitChen.GroupMealOrder.Core.Entity;
using BPA.KitChen.GroupMealOrder.SqlSugar;
using BPA.KitChen.WeChat.WechatServer.Dtos;
using BPA.KitChen.WeChat.WechatServer.Service;
using Essensoft.Paylink.WeChatPay.V2.Response;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace Black_B_Project.Application.WechatServer.Service
{
///
/// 微信接口汇总
///
[ApiDescriptionSettings("WECHAT"), AllowAnonymous]
public class WechatAppService : IDynamicApiController
{
private readonly IWechatPayService _IWechatPay;
private readonly IWechatNotityService _IWechatNotityService;
private readonly IWechatService _WechatService;
private readonly IWeChatPayProfitService _IProfitService;
private readonly IMemoryCache _memoryCache;
public WechatAppService(
IWechatPayService IWechatPay,
IWechatService wechatService,
IWechatNotityService IWechatNotityService,
IWeChatPayProfitService IProfitService,
IMemoryCache memoryCache
)
{
_IWechatPay = IWechatPay;
_WechatService = wechatService;
_IWechatNotityService = IWechatNotityService;
_IProfitService = IProfitService;
_memoryCache = memoryCache;
}
///
/// 用户登录,获取openid
///
///
///
[HttpPost]
public WeChatUserInfo wxlogin(DecryptVM vm)
{
return _WechatService.WxLogin(vm);
}
[HttpPost]
public WeChatUserInfo Decryptuserinfo(DecryptVM vm)
{
if (string.IsNullOrEmpty(vm.session_key))
{
throw Oops.Oh("session_key不能为空!");
}
if (string.IsNullOrEmpty(vm.aesiv))
{
throw Oops.Oh("aesiv不能为空!");
}
if (string.IsNullOrEmpty(vm.encryptedData))
{
throw Oops.Oh("encryptedData不能为空!");
}
WeChatUserInfo userinfo = _WechatService.Decryptuserinfo(vm);
return userinfo;
}
///
/// 获取手机号
///
///
///
[HttpGet("/api/wechat/user/phone")]
public async Task WxPhone(string code)
{
WechatPhoneMsg phonemsg = await _WechatService.WxPhone(code);
if (phonemsg.errcode==0)
{
return phonemsg.phone_info.purePhoneNumber;
}
else
{
throw Oops.Bah(phonemsg.errmsg);
}
}
///
/// 调用jsapi支付,返回支付信息
///
///
[HttpPost]
public Task Wechatpay( string ordernum)
{
return _IWechatPay.PayLinkWechatPay(null, ordernum);
}
///
/// 充值
///
///
///
[HttpPost]
public Task Rechargepay(RechargeInput input)
{
return _IWechatPay.RechargePay(input);
}
///
/// 调用Native支付,返回支付信息url
///
///
[HttpPost]
public string Nativepay(decimal amount, string orderid)
{
return _IWechatPay.NativePay(amount, orderid);
}
/***
* @param transaction_id 微信订单号(优先使用)
* @param out_trade_no 商户订单号
* @param total_fee 订单总金额
* @param refund_fee 退款金额
* @return 退款结果(xml格式)
*/
///
/// 申请退款完整业务流程逻辑
///
/// 商户
///
[HttpPost]
public async Task Wechatrefund(RefundInput input)
{
// BPALog.WriteLog($"Wechatrefund请求参数", param: input);
if (string.IsNullOrEmpty(input.AppId))
{
throw Oops.Oh("AppId不能为空!");
}
var config = ApolloApplication.Configs.Find(x => x.Key == input.AppId);
if (config==null)
{
throw Oops.Oh("AppId不存在!");
}
config.WechatConfig.OrderAddress = "https://bpa.black-pa.com:21527/order";
// MyDataResult res = OrderWeApiHepler.GetOrderInfoByNum(config.WechatConfig.OrderAddress,input.OrderNumberId);
var order = await SqlSugarDb.Db.Queryable()
.FirstAsync(x => x.OrderNumber == input.OrderNumberId);
if (order!=null)
{
int totalFee = Convert.ToInt32(order.TotalAmount * 100);
RefundDto refundDto = await _IWechatPay.Refund(
new() {
out_trade_no = input.OrderNumberId,
refund_fee = totalFee,
total_fee = totalFee,
},config
);//提交退款申请给API,接收返回数据
return new JsonResult(refundDto);
}
else
{
throw Oops.Oh("数据接口查询失败:订单不存在");
}
}
///
/// 对账单请求处理
///
///
///
[NonUnify]
[Route("wechat/pay/downloadbill")]
public static string Downloadbill(BillInput input)
{
var config = ApolloApplication.Configs.Find(x => x.Key == input.appid);
string nonceStr = WechatCommon.GenerateNonceStr();
string appid = config.WechatPayConfig.AppId;
string mchid = config.WechatPayConfig.MchId;
string mchkey = config.WechatPayConfig.APIKey;
string strA = $"appid={appid}&bill_date={input.billdate}&bill_type={input.billtype}&mch_id={mchid}&nonce_str={nonceStr}&key=" + mchkey;
string sign = WechatCommon.MD5(strA).ToUpper();
var json = new
{
input.appid,
input.billdate,
input.billtype,
mchid,
nonceStr,
sign
};
return JSON.Serialize(json);
}
///
/// 微信支付通知
///
///
[NonUnify]
[Route("wechat/paynotity")]
public Task PayNotity()
{
return _IWechatNotityService.PayNotity();
}
///
/// 微信支付退款通知
///
[Route("wechat/refundnotity")]
[NonUnify]
public async Task RefundNotity()
{
return await _IWechatNotityService.RefundNotity();
}
///
/// 微信支付-付款码
///
///
///
[Route("wechat/pay/micro")]
[NonUnify]
public async Task MicropayPay(MicropayInput input)
{
return await _IWechatPay.MicropayPay(input);
}
///
/// 查询
///
///
///
///
[Route("wechat/queryorder")]
public async Task PayOrderQuery(string ordernumid,string appId)
{
var config = ApolloApplication.Configs.Find(x => x.Key == appId);
// MyDataResult info = OrderWeApiHepler.GetOrderInfoByNum(config.WechatConfig.OrderAddress, ordernumid);
var order = await SqlSugarDb.Db.Queryable()
.FirstAsync(x => x.OrderNumber == ordernumid);
if (order==null)
{
throw Oops.Oh("查询失败,没有找到订单");
}
var data = await _IWechatPay.PayOrderQuery(appId, order.OrderNumber, order.TransactionId);
return new JsonResult(data);
}
///
/// 发送预定提醒
///
///
///
[Route("wechat/reserve")]
[HttpPost]
public void SendReserveMessage(ReserveInput input)
{
_WechatService.SendReserveMessage(input);
}
///
/// 分账信息
///
///
///
[Route("wechat/profit/sharing")]
[HttpPost]
public Task ProfitSharingRequest(ProfitSharingInput vm)
{
return _IProfitService.ProfitSharingRequest(vm);
}
///
/// 分账余额信息
///
/// 微信支付订单号
///
[Route("wechat/profit/amountquery")]
public Task WeChatPayProfitSharingOrderAmountQuery(string TransactionId,string appId)
{
return _IProfitService.WeChatPayProfitSharingOrderAmountQuery(TransactionId, appId);
}
}
}