Wechat
是一个基于vapor4的微信SDK。支持 macOS, Ubuntu。
要使用苹果的 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"),
])
]
)
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)
try req.wechat.fetchUserAccessToken(code)
try req.wechat.getUserInfo(access: atk, openId: openid)
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 }
try req.wechat.refresh(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)
}
Wechat is released under an MIT license. See License.md for more information.