分享一个c#的跨平台的支付类库
本帖最后由 jmtgj 于 2017-11-29 08:56 编辑随着微软的开源,越来越多的项目支持跨平台,但是各种支付平台提供的类库,又老又不支持跨平台,吐槽下,尤其是微信,还有好多坑,于是ICanPay诞生了,今天就来讲ICanPay是什么,怎么使用?
ICanPay是一个支持多商户多种支付方式的跨平台网关处理类库,使用ICanPay可以简化订单的创建、查询、退款和接收网关返回的支付通知等操作。
目前支持的支付网关有:支付宝(Alipay)、微信支付(Wechatpay)、银联支付(Unionpay)
下面以支付宝为例,其余两种支付,请参考Wiki
1. 配置商户数据1.在Startup中添加商户数据
services.AddICanPay(a => { var gateways = new Gateways();
var merchant = new Merchant { AppId = "", NotifyUrl = "", ReturnUrl = "", AlipayPublicKey = "", Privatekey = "" };
gateways.Add(new AlipayGateway(merchant));
return gateways; });2.在Startup中使用ICanPay
app.UseICanPay();以上就简单的完成了对ICanPay的配置操作
2.支付ICanPay支持多种支付方式,下面就来讲解如何使用
1.获取支付网关
private readonly IGateways gateways;
public YourController(IGateways gateways) { this.gateways = gateways; }2.创建订单
var order = new Order() { Amount = 0.01, OutTradeNo = "订单号", Subject = "测试", };3.设置支付方式
var gateway = gateways.Get<AlipayGateway>(GatewayTradeType.Web);4.支付
gateway.Payment(order);特殊说明:GatewayTradeType.Barcode支付方式,需要再实现PaymentSucceed和PaymentFailed事件
3.异步通知1.获取支付网关
private readonly IGateways gateways;
public NotifyController(IGateways gateways) { this.gateways = gateways; }2.实现支付通知事件
PaymentNotify notify = new PaymentNotify(gateways); notify.PaymentSucceed += Notify_PaymentSucceed; notify.PaymentFailed += Notify_PaymentFailed; notify.UnknownGateway += Notify_UnknownGateway;
private void Notify_PaymentSucceed(object sender, PaymentSucceedEventArgs e) { // 支付成功时时的处理代码 /* 建议添加以下校验。 * 1、需要验证该通知数据中的OutTradeNo是否为商户系统中创建的订单号, * 2、判断Amount是否确实为该订单的实际金额(即商户订单创建时的金额), */ if (e.GatewayType == typeof(AlipayGateway)) { var notify = (Notify)e.Notify; } }
private void Notify_PaymentFailed(object sender, PaymentFailedEventArgs e) { // 支付失败时的处理代码 }
private void Notify_UnknownGateway(object sender, UnknownGatewayEventArgs e) { // 无法识别支付网关时的处理代码 }3.接收支付通知
await notify.ReceivedAsync();4.辅助接口1.查询
var notify = (Notify)gateway.Query(new Auxiliary { OutTradeNo = "订单号" });2.撤销
var notify = (Notify)gateway.Cancel(new Auxiliary { OutTradeNo = "订单号" });3.关闭
var notify = (Notify)gateway.Close(new Auxiliary { OutTradeNo = "订单号" });4.退款
var notify = (Notify)gateway.Refund(new Auxiliary { OutTradeNo = "订单号", RefundAmount = 123, OutRefundNo = "退款单号" });5.退款查询
var notify = (Notify)gateway.RefundQuery(new Auxiliary { OutTradeNo = "订单号", OutRefundNo = "退款单号" });6.对账单下载
gateway.BillDownload(new Auxiliary { BillType = "trade", BillDate = "2017-10-31" });
以上就简单的讲解了如何使用ICanPay,如果有什么问题,也可以在Github上提问。项目地址:https://github.com/Varorbc/ICanPay
页:
[1]