264 lines
8.5 KiB
C#
264 lines
8.5 KiB
C#
using ARW.Admin.WebApi.Controllers;
|
|
using ARW.Model.Models.Business.Payments;
|
|
using ARW.Service.Business.IBusinessService.Payments;
|
|
using Infrastructure.WeChat.TenPay;
|
|
using Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Senparc.CO2NET.HttpUtility;
|
|
using Senparc.Weixin.TenPayV3.Apis;
|
|
using static Infrastructure.WeChat.TenPay.Pay;
|
|
using ARW.Admin.WebApi.Framework;
|
|
using Senparc.Weixin.Open.WxOpenAPIs;
|
|
using Senparc.Weixin.WxOpen.AdvancedAPIs.WxApp;
|
|
using ARW.Service.Business.IBusinessService.Custom.Customers;
|
|
using ARW.Service.Api.IBusinessService.PayManage;
|
|
using ARW.Service.Api.BusinessService.PaymentManage;
|
|
|
|
namespace ARW.WebApi.Controllers.Api.Wechat.WxPay
|
|
{
|
|
/// <summary>
|
|
/// 微信支付控制器
|
|
/// </summary>
|
|
//[Verify]
|
|
[Route("api/[controller]")]
|
|
public class WxPayController : BaseController
|
|
{
|
|
|
|
private readonly ICustomerService _CustomerService;
|
|
private readonly IPayServiceApi _PayServiceApi;
|
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
private readonly SenparcHttpClient _httpClient;
|
|
|
|
public WxPayController(ICustomerService customerService, SenparcHttpClient httpClient, IPayServiceApi payServiceApi)
|
|
{
|
|
_CustomerService = customerService;
|
|
_httpClient = httpClient;
|
|
_PayServiceApi = payServiceApi;
|
|
}
|
|
|
|
|
|
#region 微信支付
|
|
/// <summary>
|
|
/// 微信支付(统一下单)
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("wxPay")]
|
|
public async Task<IActionResult> WxPay([FromBody] PayDto parm)
|
|
{
|
|
if (parm == null) { throw new CustomException("请求参数错误"); }
|
|
|
|
var user = JwtUtil.GetLoginUser(App.HttpContext);
|
|
parm.UserId = user.UserId;
|
|
|
|
/* 统一下单的业务处理 */
|
|
var payRes = await _PayServiceApi.HandelPrePay(parm);
|
|
return SUCCESS(payRes);
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 微信支付回调
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost("Notify")]
|
|
public async Task<IActionResult> Notify()
|
|
{
|
|
try
|
|
{
|
|
logger.Info("进入微信支付回调!", "2134234234");
|
|
Pay pay = new Pay(_httpClient);
|
|
var res = await pay.PayNotifyUrl();
|
|
|
|
if (res.trade_state == "SUCCESS")
|
|
{
|
|
logger.Info("微信支付回调之业务:", res.trade_state);
|
|
// 业务
|
|
var respones = _PayServiceApi.UpdateAsync(f => new Payment
|
|
{
|
|
PaymentWeixinNumber = res.transaction_id,
|
|
PaymentBeforeMoney = res.amount.total,
|
|
PaymentMoney = res.amount.payer_total,
|
|
PaymentStatus = 2,
|
|
Update_time = DateTime.Now,
|
|
}, f => f.PaymentNumber == res.out_trade_no);
|
|
|
|
}
|
|
|
|
return SUCCESS("66666");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error("微信支付回调错误信息:", e);
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 订单查询
|
|
/// <summary>
|
|
/// 微信支付订单查询
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("orderQuery")]
|
|
public async Task<IActionResult> OrderQuery([FromBody] OrderQueryDto parm)
|
|
{
|
|
Pay pay = new Pay(_httpClient);
|
|
var res = await pay.OrderQuery(parm.outTradeNo, parm.transactionId);
|
|
if (res.VerifySignSuccess == null)
|
|
throw new CustomException("订单查询失败!");
|
|
|
|
return SUCCESS(res);
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 关闭订单
|
|
/// <summary>
|
|
/// 关闭订单接口
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("closeOrder")]
|
|
public async Task<IActionResult> CloseOrder([FromBody] OrderQueryDto parm)
|
|
{
|
|
Pay pay = new Pay(_httpClient);
|
|
var res = await pay.CloseOrder(parm.outTradeNo);
|
|
if (res == null)
|
|
throw new CustomException("订单关闭失败!");
|
|
else
|
|
{
|
|
var respones = _PayServiceApi.UpdateAsync(f => new Payment
|
|
{
|
|
PaymentStatus = 3,
|
|
Update_time = DateTime.Now
|
|
}, f => f.PaymentNumber == parm.outTradeNo);
|
|
}
|
|
|
|
return SUCCESS(res);
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 退款
|
|
/// <summary>
|
|
/// 退款接口
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("refund")]
|
|
public async Task<IActionResult> Refund([FromBody] OrderQueryDto parm)
|
|
{
|
|
Pay pay = new Pay(_httpClient);
|
|
var payment = _PayServiceApi.GetFirstAsync(s => s.PaymentNumber == parm.outTradeNo).Result;
|
|
var transactionId = payment.PaymentWeixinNumber;
|
|
string paymentRefundNumber = "";
|
|
if (!string.IsNullOrEmpty(payment.PaymentRefundNumber))
|
|
{
|
|
paymentRefundNumber = payment.PaymentRefundNumber;
|
|
}
|
|
|
|
var totalFee = payment.PaymentMoney;
|
|
var res = await pay.Refund(transactionId, totalFee, paymentRefundNumber);
|
|
if (res.ResultCode.Success == false)
|
|
throw new CustomException("订单退款失败!");
|
|
else
|
|
{
|
|
var respones = _PayServiceApi.UpdateAsync(f => new Payment
|
|
{
|
|
PaymentRefundNumber = res.out_refund_no,
|
|
}, f => f.PaymentNumber == res.out_trade_no);
|
|
}
|
|
|
|
return SUCCESS(res);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 退款回调
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost("refundNotifyUrl")]
|
|
public async Task<IActionResult> RefundNotifyUrl()
|
|
{
|
|
try
|
|
{
|
|
logger.Info("进入退款回调!", "2134234234");
|
|
Pay pay = new Pay(_httpClient);
|
|
var res = await pay.RefundNotifyUrl();
|
|
|
|
if (res.refund_status == "SUCCESS")
|
|
{
|
|
logger.Info("退款回调之业务:", res.refund_status);
|
|
// 业务
|
|
|
|
var respones = _PayServiceApi.UpdateAsync(f => new Payment
|
|
{
|
|
PaymentRefundNumber = res.out_refund_no,
|
|
PaymentStatus = 4,
|
|
Update_time = DateTime.Now
|
|
}, f => f.PaymentNumber == res.out_trade_no);
|
|
|
|
}
|
|
|
|
return SUCCESS("66666");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error("退款回调错误信息:", e);
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 账单
|
|
/// <summary>
|
|
/// 申请交易账单
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("tradeBill")]
|
|
public async Task<IActionResult> TradeBill(string date)
|
|
{
|
|
var filePath = $"wwwroot/wxpay/{date}-TradeBill.csv";
|
|
Console.WriteLine("FilePath:" + filePath);
|
|
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate))
|
|
{
|
|
BasePayApis basePayApis = new BasePayApis();
|
|
|
|
var result = basePayApis.TradeBillQueryAsync(date, fileStream: fs).GetAwaiter().GetResult();
|
|
|
|
fs.Flush();
|
|
}
|
|
return SUCCESS("已经下载倒指定目录,文件名:" + filePath);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 申请资金账单接口
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("fundflowBill")]
|
|
public async Task<IActionResult> FundflowBill(string date)
|
|
{
|
|
var filePath = $"wwwroot/wxpay/{date}-FundflowBill.csv";
|
|
Console.WriteLine("FilePath:" + filePath);
|
|
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate))
|
|
{
|
|
BasePayApis basePayApis = new BasePayApis();
|
|
|
|
var result = await basePayApis.FundflowBillQueryAsync(date, fs);
|
|
|
|
fs.Flush();
|
|
}
|
|
return SUCCESS("已经下载倒指定目录,文件名:" + filePath);
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|