299 lines
9.6 KiB
C#
299 lines
9.6 KiB
C#
using ARW.Admin.WebApi.Controllers;
|
|
using ARW.Model.Models.Business.Payments;
|
|
using ARW.Service.Business.IBusinessService.Customers;
|
|
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;
|
|
|
|
namespace ARW.WebApi.Controllers.Api.Wechat.WxPay
|
|
{
|
|
/// <summary>
|
|
/// 微信支付控制器
|
|
/// </summary>
|
|
//[Verify]
|
|
[Route("api/[controller]")]
|
|
public class WxPayController : BaseController
|
|
{
|
|
|
|
private readonly ICustomerService _CustomerService;
|
|
private readonly IPaymentService _PaymentService;
|
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
private readonly SenparcHttpClient _httpClient;
|
|
|
|
public WxPayController(ICustomerService customerService, SenparcHttpClient httpClient, IPaymentService paymentService)
|
|
{
|
|
_CustomerService = customerService;
|
|
_httpClient = httpClient;
|
|
_PaymentService = paymentService;
|
|
}
|
|
|
|
#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("请求参数错误"); }
|
|
|
|
Pay pay = new Pay(_httpClient);
|
|
|
|
// 获取金额
|
|
var price = 1;//单位:分
|
|
|
|
|
|
var orderNo = Common.Common.CreateNoQuery();
|
|
var payEntity = await pay.PrePay(parm.ProductGuid, parm.openId, orderNo, parm.type, price);
|
|
|
|
if (payEntity == null)
|
|
{
|
|
throw new CustomException("下单失败!");
|
|
}
|
|
else
|
|
{
|
|
var user = JwtUtil.GetLoginUser(App.HttpContext);
|
|
var userId = 1600478763014033408;
|
|
|
|
Payment payment = new Payment
|
|
{
|
|
PaymentBusinessGuid = parm.ProductGuid,
|
|
PaymentNumber = orderNo,
|
|
PaymentBuytype = parm.type,
|
|
CustomerGuid = userId,
|
|
PaymentStatus = 1
|
|
};
|
|
|
|
var response = _PaymentService.InsertReturnSnowflakeId(payment);
|
|
if (response == 0)
|
|
throw new CustomException("数据加入失败");
|
|
|
|
var res = _PaymentService.GetFirst(s => s.PaymentGuid == response);
|
|
|
|
PayParams pay1 = new PayParams
|
|
{
|
|
jsApiUiPackage = payEntity,
|
|
outTradeNo = orderNo,
|
|
CreateTime = res.Create_time,
|
|
OverTime = res.Create_time.AddMinutes(1)
|
|
};
|
|
|
|
return new JsonResult(pay1);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/// <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 = _PaymentService.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 = _PaymentService.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 = _PaymentService.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 = _PaymentService.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 = _PaymentService.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
|
|
|
|
}
|
|
|
|
}
|