Skip to content

vapor-china/wechat

Repository files navigation

Build Status Swift Swift Xcode Xcode MIT

Wechat 是一个基于vapor4的微信SDK。支持 macOS, Ubuntu。

English 📔

安装

Swift Package Manager

要使用苹果的 Swift Package Manager 集成,将以下内容作为依赖添加到你的 Package.swift

.package(url: "https://github.com/vapor-china/wechat.git", from: "2.0.0")

这里是一个 PackageDescription 实例:

// swift-tools-version:5.2
import PackageDescription

let package = Package(
    name: "MyPackage",
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]),
    ],
    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc"),
        .package(url: "https://github.com/vapor/fluent.git", from: "4.0.0-rc"),
        .package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0-rc"),
        .package(url: "https://github.com/vapor-china/wechat.git", from: "2.0.0")
    ],
    targets: [
        .target(name: "App", dependencies: [
            .product(name: "Fluent", package: "fluent"),
            .product(name: "FluentMySQLDriver", package: "fluent-mysql-driver"),
            .product(name: "Vapor", package: "vapor"),
            .product(name: "Wechat", package: "wechat")
        ]),
        .target(name: "Run", dependencies: ["App"]),
        .testTarget(name: "AppTests", dependencies: [
            .target(name: "App"),
            .product(name: "XCTVapor", package: "vapor"),
        ])
    ]
)

使用

注入wechat

        let mch = WechatConfiguare.MCH(mchId: "you mchId", secret: "your mch secret") // 如果不需要使用微信支付,这个可以设为nil
        let wechatConfig = try WechatConfiguare(appId: "your appid", appSecret: "your app secret", mch: mch)
        app.wechat.use(wechatConfig)
        

微信授权

获取 access token

        try req.wechat.fetchUserAccessToken(code)

获取用户信息

        try req.wechat.getUserInfo(access: atk, openId: openid)

通过code直接获取用户信息示例

        try req.wechat.fetchUserAccessToken(code).flatMapThrowing({ (tokenModel) in
            if let errmsg = tokenModel.isErrMsg {
                throw RespError(err: .normal, msg: errmsg)
            }
            guard let atk = tokenModel.accessToken else { throw RespError(err: .normal, msg: "access token is not exists") }
            guard let openid = tokenModel.openId else { throw RespError(err: .normal, msg: "openid is not exists") }
            return try req.wechat.getUserInfo(access: atk, openId: openid).encodeResponse(for: req)
        }).flatMap { $0 }

刷新 access token

        try req.wechat.refresh(access: token)

验证access token是否有效

        req.wechat.valid(access: token, openId: openId)

支付相关

支付预下单

        let param = WxPayUnifiedOrderPramas(outTradeNo: "macos\(Int(Date().timeIntervalSince1970))", body: "vapor test", totalFee: 1, spbillCreateIp: "127.0.0.1", notifyUrl: "http://notify.objcoding.com/notify", tradeType: .app)
         
        return try req.wechat.unified(order: param)

查询支付结果

    	let param = WxPayOrderQueryPramas(outTradeNo: "your out trade no")
        try req.wechat.query(order: param)

关闭订单

    	let param = WxPayCloseOrderParams(outTradeNo: "your out trade no")
    	try req.wechat.close(order: param)

退款

    	let param = WxPayRefundOrderParams(outTradeNo: "out trade no", outRefundNo: " out refund no", totalFee: 1, refundFee: 1, refundFeeType: "", refundDesc: "", refundAccount: "", notifyUrl: "http://notify.objcoding.com/notify")
        try req.wechat.refund(order: param)

支付回调解析处理

router请写post请求

     		let res = try req.wechat.payCallback()
   				 ······
    		if res.isTransactionSuccess {
     			 return WxPayCallbackReturn.OK.encodeResponse(for: req)
   		  } else {
      		 return WxPayCallbackReturn.NotOK(errMsg: "msg").encodeResponse(for: req)
     		}   

License

Wechat is released under an MIT license. See License.md for more information.