diff --git a/.swiftpm/xcode/package.xcworkspace/xcuserdata/xj.xcuserdatad/UserInterfaceState.xcuserstate b/.swiftpm/xcode/package.xcworkspace/xcuserdata/xj.xcuserdatad/UserInterfaceState.xcuserstate index 47eeb3f..9d2ef6f 100644 Binary files a/.swiftpm/xcode/package.xcworkspace/xcuserdata/xj.xcuserdatad/UserInterfaceState.xcuserstate and b/.swiftpm/xcode/package.xcworkspace/xcuserdata/xj.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Sources/WechatPay/wxpayCloseOrderParams.swift b/Sources/WechatPay/wxpayCloseOrderParams.swift new file mode 100644 index 0000000..583f449 --- /dev/null +++ b/Sources/WechatPay/wxpayCloseOrderParams.swift @@ -0,0 +1,24 @@ +// +// File.swift +// +// +// Created by xj on 2020/4/5. +// + +import Vapor + +public typealias WxpayCloseOrderParams = WxPayParam + +public struct WxpayCloseOrderResp: Content { + let return_code: String + let return_msg: String? + + let appid: String + let mch_id: String + let nonce_str: String + let sign: String + let result_code: String + let result_msg: String + let err_code: String? + let err_code_des: String? +} diff --git a/Sources/WechatPay/wxpayConst.swift b/Sources/WechatPay/wxpayConst.swift index 5bf65f9..eb62b7c 100644 --- a/Sources/WechatPay/wxpayConst.swift +++ b/Sources/WechatPay/wxpayConst.swift @@ -12,11 +12,15 @@ public struct WxpayConst { public enum Url { case unifiedorder case orderquery + case closeOrder + case refundOrder var str: String { switch self { case .unifiedorder: return "https://api.mch.weixin.qq.com/pay/unifiedorder" case .orderquery: return "https://api.mch.weixin.qq.com/pay/orderquery" + case .closeOrder: return "https://api.mch.weixin.qq.com/pay/closeorder" + case .refundOrder: return "https://api.mch.weixin.qq.com/secapi/pay/refund" } } } diff --git a/Sources/WechatPay/wxpayRefundOrderParams.swift b/Sources/WechatPay/wxpayRefundOrderParams.swift new file mode 100644 index 0000000..fc4cf7c --- /dev/null +++ b/Sources/WechatPay/wxpayRefundOrderParams.swift @@ -0,0 +1,60 @@ +// +// File.swift +// +// +// Created by xj on 2020/4/5. +// + +import Vapor + +public class WxpayRefundOrderParams: WxParams { + public init(transaction_id: String?, out_trade_no: String?, out_refund_no: String, total_fee: Int, refund_fee: Int, refund_fee_type: String?, refund_desc: String?, refund_account: String?, notify_url: String?) { + self.transaction_id = transaction_id + self.out_trade_no = out_trade_no + self.out_refund_no = out_refund_no + self.total_fee = total_fee + self.refund_fee = refund_fee + self.refund_fee_type = refund_fee_type + self.refund_desc = refund_desc + self.refund_account = refund_account + self.notify_url = notify_url + } + + let transaction_id: String? + let out_trade_no: String? + let out_refund_no: String + let total_fee: Int + let refund_fee: Int + let refund_fee_type: String? + let refund_desc: String? + let refund_account: String? + let notify_url: String? +} + +public struct WxpayRefundOrderResp: Content { + + let return_code: String + let return_msg: String? + + let result_code: String + let err_code: String? + let err_code_des: String? + let appid: String + let mch_id: String + let nonce_str: String + let sign: String + let transaction_id: String + let out_trade_no: String + let out_refund_no: String + let refund_id: String + let refund_fee: Int + let settlement_refund_fee: Int? + let total_fee: Int + let settlement_total_fee: Int? + let fee_type: String? + let cash_fee: Int + let cash_fee_type: String? + let cash_refund_fee: Int? + let coupon_refund_fee: Int? + let coupon_refund_count: Int? +} diff --git a/Sources/WechatPay/wxpayRequest.swift b/Sources/WechatPay/wxpayRequest.swift index 8fb0c60..c020b39 100644 --- a/Sources/WechatPay/wxpayRequest.swift +++ b/Sources/WechatPay/wxpayRequest.swift @@ -53,4 +53,41 @@ extension WxpayClient { }) } + public func closeOrder(_ params: WxpayCloseOrderParams, req: Request) throws -> EventLoopFuture { + + return try postWithParam(url: .closeOrder, params: params, req: req).flatMapThrowing { (resp) -> WxpayCloseOrderResp in + + let result = try resp.content.decode(WxpayCloseOrderResp.self, using: XMLDecoder()) + + if result.sign.isEmpty { + throw WxpayError(reason: "verfy sign is empty") + } + + let signDic = MirrorExt.generateDic(model: resp) + let sign2 = try WxpaySign.sign(dic: signDic, key: self.apiKey, signType: self.signType) + if result.sign != sign2 { + throw WxpayError(reason: "verfy sign failed") + } + return result + } + } + + public func refundOrder(_ params: WxpayRefundOrderParams, req: Request) throws -> EventLoopFuture { + + return try postWithParam(url: .refundOrder, params: params, req: req).flatMapThrowing({ (resp) -> WxpayRefundOrderResp in + + let result = try resp.content.decode(WxpayRefundOrderResp.self, using: XMLDecoder()) + + if result.sign.isEmpty { + throw WxpayError(reason: "verfy sign is empty") + } + + let signDic = MirrorExt.generateDic(model: resp) + let sign2 = try WxpaySign.sign(dic: signDic, key: self.apiKey, signType: self.signType) + if result.sign != sign2 { + throw WxpayError(reason: "verfy sign failed") + } + return result + }) + } }