Skip to content

Commit

Permalink
添加对账单下载
Browse files Browse the repository at this point in the history
  • Loading branch information
Varorbc committed Nov 17, 2017
1 parent 49052a9 commit ad94a96
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 29 deletions.
18 changes: 17 additions & 1 deletion sample/ICanPay.Demo/Controllers/BillDownloadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public BillDownloadController(IGateways gateways)

public IActionResult Index()
{
WechatpayBillDownload();
UnionpayBillDownload();

return Ok();
}
Expand Down Expand Up @@ -46,5 +46,21 @@ private void WechatpayBillDownload()
BillDate = "20171002"
});
}

/// <summary>
/// 银联对账单下载
/// </summary>
private void UnionpayBillDownload()
{
var gateway = gateways.Get<Unionpay.UnionpayGateway>();

//特殊处理
gateway.Merchant.AppId = "700000000000001";
gateway.BillDownload(new Unionpay.Auxiliary
{
BillDate = "0119",
TxnTime = "20171117154356"
});
}
}
}
5 changes: 5 additions & 0 deletions src/ICanPay.Core/Interfaces/IAuxiliary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public interface IAuxiliary
/// </summary>
string RefundReason { get; set; }

/// <summary>
/// 账单日期
/// </summary>
string BillDate { get; set; }

/// <summary>
/// 验证
/// </summary>
Expand Down
37 changes: 12 additions & 25 deletions src/ICanPay.Unionpay/Auxiliary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Auxiliary : IAuxiliary
/// <summary>
/// 订单发送时间 格式 年年年年月月日日时时分分秒秒
/// </summary>
[Required]
[Required(ErrorMessage = "请设置订单发送时间")]
public string TxnTime { get; set; }

/// <summary>
Expand Down Expand Up @@ -60,38 +60,25 @@ public double? RefundAmount
[StringLength(1024, ErrorMessage = "商户自定义保留域最大长度为1024位")]
public string ReqReserved { get; set; }

public string RefundNo { get; set; }
public string RefundNo { get => null; set => throw new NotImplementedException(); }

/// <summary>
/// 退款的原因说明
/// </summary>
[StringLength(256, ErrorMessage = "退款的原因说明最大长度为256位")]
public string RefundReason { get; set; }
public string RefundReason { get => null; set => throw new NotImplementedException(); }

/// <summary>
/// 账单类型,商户通过接口或商户经开放平台授权后其所属服务商通过接口可以获取以下
/// 账单类型:trade、signcustomer;trade指商户基于支付宝交易收单的业务账单;
/// signcustomer是指基于商户支付宝余额收入及支出等资金变动的帐务账单;
/// 文件类型
/// </summary>
[StringLength(10, ErrorMessage = "账单类型最大长度为10位")]
[Necessary(GatewayAuxiliaryType.BillDownload, ErrorMessage = "请设置账单类型")]
public string BillType { get; set; }
public string FileType => "00";

/// <summary>
/// 账单时间:日账单格式为yyyy-MM-dd,月账单格式为yyyy-MM。
/// 清算日期 格式 月月日日
/// 为银联和入网机构间的交易结算日期。
/// 一般前一日23点至当天23点为一个清算日。
/// 也就是23点前的交易,当天23点之后开始结算,23点之后的交易,要第二天23点之后才会结算。
/// </summary>
[StringLength(15, ErrorMessage = "账单时间最大长度为15位")]
[Necessary(GatewayAuxiliaryType.BillDownload, ErrorMessage = "请设置账单时间")]
[Necessary(GatewayAuxiliaryType.BillDownload, ErrorMessage = "请设置清算日期")]
[ReName(Name = Constant.SETTLEDATE)]
public string BillDate { get; set; }

public bool Validate(GatewayAuxiliaryType gatewayAuxiliaryType)
{
//if (string.IsNullOrEmpty(OutTradeNo) && string.IsNullOrEmpty(TradeNo))
//{
// throw new ArgumentNullException("商户订单号和支付宝订单号不可同时为空");
//}

return true;
}
public bool Validate(GatewayAuxiliaryType gatewayAuxiliaryType) => true;
}
}
2 changes: 2 additions & 0 deletions src/ICanPay.Unionpay/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public static class Constant
public const string RESPMSG = "respMsg";
public const string ORDERDESC = "orderDesc";
public const string ORIGQRYID = "origQryId";
public const string SETTLEDATE = "settleDate";
public const string FRONTURL = "frontUrl";
}
}
10 changes: 10 additions & 0 deletions src/ICanPay.Unionpay/Notify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,15 @@ public double Amount
/// 原交易应答信息
/// </summary>
public string OrigRespMsg { get; set; }

/// <summary>
/// 文件名
/// </summary>
public string FileName { get; set; }

/// <summary>
/// 批量文件内容
/// </summary>
public string FileContent { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/ICanPay.Unionpay/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public class Order : IOrder
/// <summary>
/// 商户订单号,不应含“-”或“_”
/// </summary>
[Required]
[Required(ErrorMessage = "请设置商户订单号")]
[ReName(Name = Constant.ORDERID)]
[StringLength(40, MinimumLength = 8, ErrorMessage = "商户订单号最小长度为8位,最大长度为40位")]
public string OutTradeNo { get; set; }

/// <summary>
/// 交易金额,单位元
/// </summary>
[Required]
[Required(ErrorMessage = "请设置交易金额")]
[ReName(Name = Constant.TXNAMT)]
public double Amount
{
Expand Down
47 changes: 46 additions & 1 deletion src/ICanPay.Unionpay/UnionpayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ICanPay.Core.Exceptions;
using ICanPay.Core.Utils;
using System;
using System.IO;
using System.Threading.Tasks;

namespace ICanPay.Unionpay
Expand All @@ -12,7 +13,7 @@ namespace ICanPay.Unionpay
public class UnionpayGateway
: GatewayBase,
IFormPayment, IAppPayment, IScanPayment, IBarcodePayment,
IQuery, ICancel, IRefund
IQuery, ICancel, IRefund, IBillDownload
{

#region 私有字段
Expand All @@ -21,6 +22,7 @@ public class UnionpayGateway
private const string APPGATEWAYURL = "https://gateway.test.95516.com/gateway/api/appTransReq.do";
private const string BACKGATEWAYURL = "https://gateway.test.95516.com/gateway/api/backTransReq.do";
private const string QUERYGATEWAYURL = "https://gateway.test.95516.com/gateway/api/queryTrans.do";
private const string FILEGATEWAYURL = "https://filedownload.test.95516.com/";

private readonly Merchant _merchant;

Expand Down Expand Up @@ -213,6 +215,49 @@ public void InitRefund(IAuxiliary auxiliary)

#endregion

#region 对账单下载

public FileStream BuildBillDownload(IAuxiliary auxiliary)
{
InitBillDownload(auxiliary);

Commit();

return CreateZip(Notify.FileContent);
}

public void InitBillDownload(IAuxiliary auxiliary)
{
Merchant.TxnType = "76";
Merchant.TxnSubType = "01";
Merchant.ChannelType = "07";
Merchant.BizType = "000000";

GatewayData.Add(Merchant, StringCase.Camel);
GatewayData.Add(auxiliary, StringCase.Camel);
GatewayData.Remove(Constant.BACKURL);
GatewayData.Remove(Constant.FRONTURL);
GatewayData.Add(Constant.SIGNATURE, BuildSign());
GatewayUrl = FILEGATEWAYURL;
}

/// <summary>
/// 创建Zip文件
/// </summary>
/// <param name="content">内容</param>
/// <returns></returns>
private FileStream CreateZip(string content)
{
byte[] buffer = Util.Inflater(content);
FileStream fileStream = new FileStream($"{DateTime.Now.ToString(TIMEFORMAT)}.zip", FileMode.Create);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Position = 0;

return fileStream;
}

#endregion

private void InitAuxiliaryParameter(IAuxiliary auxiliary)
{
Merchant.TxnSubType = "00";
Expand Down
31 changes: 31 additions & 0 deletions src/ICanPay.Unionpay/Util.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ICanPay.Core.Utils;
using ICanPay.Unionpay.Properties;
using ICSharpCode.SharpZipLib.Zip.Compression;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Pkix;
Expand Down Expand Up @@ -395,5 +396,35 @@ public static string DecryptData(AsymmetricKeyParameter key, string data)
}

#endregion

#region Inflater解压缩

/// <summary>
/// Inflater解压缩
/// </summary>
/// <param name="data">数据</param>
/// <returns></returns>
public static byte[] Inflater(string data)
{
byte[] temp = new byte[1024];
MemoryStream memory = new MemoryStream();
Inflater inf = new Inflater();
inf.SetInput(Convert.FromBase64String(data));
while (!inf.IsFinished)
{
int extracted = inf.Inflate(temp);
if (extracted > 0)
{
memory.Write(temp, 0, extracted);
}
else
{
break;
}
}
return memory.ToArray();
}

#endregion
}
}

0 comments on commit ad94a96

Please sign in to comment.