Giter Club home page Giter Club logo

wechat's Introduction

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.

wechat's People

Contributors

spectatornan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.