C# 跨平台的支付类库ICanPay

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 跨平台的支付类库ICanPay相关的知识,希望对你有一定的参考价值。

随着微软的开源,越来越多的项目支持跨平台,但是各种支付平台提供的类库,又老又不支持跨平台,吐槽下,尤其是微信,还有好多坑,于是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上提问。
项目地址:ICanPay

以上是关于C# 跨平台的支付类库ICanPay的主要内容,如果未能解决你的问题,请参考以下文章

.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

Java C# 加密解密类库

C#开源类库推荐:拼多多开放平台SDK,开源免费,支持.NET Core!

C#写PDF文件类库PDF File Writer介绍

C# .Net Framework

C# Telnet 类库代码,谨献给还在寻找C# Telnet类库的兄弟