Skip to content

Commit

Permalink
add refund, close order
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectatorNan committed Apr 5, 2020
1 parent 77f69e9 commit 1e5a186
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
Binary file not shown.
24 changes: 24 additions & 0 deletions Sources/WechatPay/wxpayCloseOrderParams.swift
Original file line number Diff line number Diff line change
@@ -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?
}
4 changes: 4 additions & 0 deletions Sources/WechatPay/wxpayConst.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
60 changes: 60 additions & 0 deletions Sources/WechatPay/wxpayRefundOrderParams.swift
Original file line number Diff line number Diff line change
@@ -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?
}
37 changes: 37 additions & 0 deletions Sources/WechatPay/wxpayRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,41 @@ extension WxpayClient {
})
}

public func closeOrder(_ params: WxpayCloseOrderParams, req: Request) throws -> EventLoopFuture<WxpayCloseOrderResp> {

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<WxpayRefundOrderResp> {

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
})
}
}

0 comments on commit 1e5a186

Please sign in to comment.