互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
16 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package wx
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "hudongzhuanjia/utils/define"
  6. )
  7. //var (
  8. // WxCertPath = `./cacert/apiclient_cert.pem`
  9. // WxKeyPath = `./cacert/apiclient_key.pem`
  10. // WxCaPath = `./cacert/rootca.pem`
  11. // Appid = "wx7b0bcf476552c5e9"
  12. // Secret = "f6aabdd40ea25272f4442603a7dc8028"
  13. // Mchid = `1394404502`
  14. // ApiKey = `2c82c64ceec6ba89ffc9f593c671a12f`
  15. // mainHost = `https://api.mch.weixin.qq.com`
  16. // backHost = `https://api2.mch.weixin.qq.com`
  17. //)
  18. var (
  19. RedirectUri = "http://api.hudongzhuanjia.com/"
  20. CodeUrl = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect"
  21. AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code "
  22. RefreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"
  23. AuthUrl = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s"
  24. UserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s"
  25. TicketTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
  26. TicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"
  27. Expired = 7200
  28. )
  29. var (
  30. NotifyURL = fmt.Sprintf("%s/wechat/callback", define.HOST) // 微信回调通知
  31. UnifiedOrderURL = fmt.Sprintf("%s/pay/unifiedorder", define.WxHost) // 统一下单url
  32. QueryOrderURL = fmt.Sprintf("%s/pay/orderquery", define.WxHost) // 微信查询订单
  33. CloseOrderURL = fmt.Sprintf("%s/pay/closeorder", define.WxHost) // 微信关闭订单
  34. RefundURL = fmt.Sprintf("%s/secapi/pay/refund", define.WxHost) // 微信退款
  35. RefundQueryURL = fmt.Sprintf("%s/pay/refundquery", define.WxHost) // 微信查询退款
  36. DownloadBillURL = fmt.Sprintf("%s/pay/downloadbill", define.WxHost) // 微信下载账单
  37. DownloadFundFlowURL = fmt.Sprintf("%s/pay/downloadfundflow", define.WxHost)
  38. TransfersURL = fmt.Sprintf("%s/mmpaymkttransfers/promotion/transfers", define.WxHost)
  39. SendRedPackURL = fmt.Sprintf("%s/mmpaymkttransfers/sendredpack", define.WxHost)
  40. QueryRedPackURL = fmt.Sprintf("%s/mmpaymkttransfers/gethbinfo", define.WxHost)
  41. )
  42. type CommonReturn struct {
  43. ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"`
  44. ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
  45. }
  46. type CommonResult struct {
  47. ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"`
  48. ResultMsg string `xml:"result_msg,omitempty" json:"result_msg,omitempty"`
  49. ErrCode string `xml:"err_code,omitempty" json:"err_code,omitempty"`
  50. ErrCodeDes string `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"`
  51. }
  52. type CommonParam struct {
  53. Appid string `xml:"appid,omitempty" json:"appid,omitempty"`
  54. MchId string `xml:"mch_id,omitempty" json:"mch_id,omitempty"`
  55. NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
  56. Sign string `xml:"sign,omitempty" json:"sign,omitempty"`
  57. SignType string `xml:"sign_type,omitempty" json:"sign_type,omitempty"`
  58. }
  59. // ========================================== 基础配置
  60. type UnifiedOrderParam struct {
  61. CommonParam
  62. XMLName xml.Name `xml:"xml" json:"xml_name,omitempty"`
  63. DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
  64. Body cdata `xml:"body,omitempty" json:"body,omitempty"`
  65. Detail cdata `xml:"detail,omitempty" json:"detail,omitempty"`
  66. Attach string `xml:"attach,omitempty" json:"attach,omitempty"`
  67. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  68. FeeType string `xml:"fee_type,omitempty" json:"fee_type,omitempty"`
  69. TotalFee string `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
  70. SpbillCreateIp string `xml:"spbill_create_ip,omitempty" json:"spbill_create_ip,omitempty"`
  71. TimeStart string `xml:"time_start,omitempty" json:"time_start,omitempty"`
  72. TimeExpire string `xml:"time_expire,omitempty" json:"time_expire,omitempty"`
  73. GoodsTag string `xml:"goods_tag,omitempty" json:"goods_tag,omitempty"`
  74. NotifyUrl string `xml:"notify_url,omitempty" json:"notify_url,omitempty"`
  75. TradeType string `xml:"trade_type,omitempty" json:"trade_type,omitempty"`
  76. ProductId string `xml:"product_id,omitempty" json:"product_id,omitempty"`
  77. LimitPay string `xml:"limit_pay,omitempty" json:"limit_pay,omitempty"`
  78. Openid string `xml:"openid,omitempty" json:"openid,omitempty"`
  79. Receipt string `xml:"receipt,omitempty" json:"receipt,omitempty"`
  80. SceneInfo *UnifiedOrderSceneInfo `xml:"scene_info,omitempty" json:"scene_info,omitempty"`
  81. }
  82. type UnifiedOrderSceneInfo struct {
  83. Id string `xml:"id,omitempty" json:"id,omitempty"`
  84. Name string `xml:"name,omitempty" json:"name,omitempty"`
  85. AreaCode string `xml:"area_code,omitempty" json:"area_code,omitempty"`
  86. Address string `xml:"address,omitempty" json:"address,omitempty"`
  87. }
  88. type UnifiedOrderResult struct {
  89. CommonReturn
  90. CommonResult
  91. CommonParam
  92. DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
  93. NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
  94. TradeType string `xml:"trade_type,omitempty" json:"trade_type,omitempty"`
  95. PrepayId string `xml:"prepay_id,omitempty" json:"prepay_id,omitempty"`
  96. CodeUrl string `xml:"code_url,omitempty" json:"code_url,omitempty"`
  97. }
  98. // =================================================支付
  99. type QueryOrderParam struct {
  100. CommonParam
  101. XMLName xml.Name `xml:"xml" json:"xml_name,omitempty"`
  102. TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"`
  103. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  104. }
  105. type QueryOrderResult struct {
  106. CommonReturn
  107. CommonResult
  108. CommonParam
  109. DeviceInfo string `xml:"device_info" json:"device_info,omitempty"`
  110. Openid string `xml:"openid" json:"openid,omitempty"`
  111. IsSubscribe string `xml:"is_subscribe" json:"is_subscribe,omitempty"`
  112. TradeType string `xml:"trade_type" json:"trade_type,omitempty"`
  113. TradeState string `xml:"trade_state" json:"trade_state,omitempty"`
  114. BankType string `xml:"bank_type" json:"bank_type,omitempty"`
  115. TotalFee string `xml:"total_fee" json:"total_fee,omitempty"`
  116. SettlementTotalFee string `xml:"settlement_total_fee" json:"settlement_total_fee,omitempty"`
  117. FeeType string `xml:"fee_type" json:"fee_type,omitempty"`
  118. CashFee string `xml:"cash_fee" json:"cash_fee,omitempty"`
  119. CashFeeType string `xml:"cash_fee_type" json:"cash_fee_type,omitempty"`
  120. CouponFee string `xml:"coupon_fee" json:"coupon_fee,omitempty"`
  121. CouponCount string `xml:"coupon_count" json:"coupon_count,omitempty"`
  122. CouponTypeN string `xml:"coupon_type_$n" json:"coupon_type_$n,omitempty"`
  123. CouponIdN string `xml:"coupon_id_$n" json:"coupon_id_$n,omitempty"`
  124. CouponFeeN string `xml:"coupon_fee_$n" json:"coupon_fee_$n,omitempty"`
  125. TransactionId string `xml:"transaction_id" json:"transaction_id,omitempty"`
  126. OutTradeNo string `xml:"out_trade_no" json:"out_trade_no,omitempty"`
  127. Attach string `xml:"attach" json:"attach,omitempty"`
  128. TimeEnd string `xml:"time_end" json:"time_end,omitempty"`
  129. TradeStateDesc string `xml:"trade_state_desc" json:"trade_state_desc,omitempty"`
  130. }
  131. // ============================================================查询订单
  132. type CloseOrderParam struct {
  133. CommonParam
  134. XMLName xml.Name `xml:"xml" json:"xml_name,omitempty"`
  135. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  136. }
  137. type CloseOrderResult struct {
  138. CommonReturn
  139. CommonResult
  140. CommonParam
  141. }
  142. // ======================================== 关闭订单
  143. type RefundParam struct {
  144. CommonParam
  145. XMLName xml.Name `xml:"xml" json:"xml_name,omitempty"`
  146. TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"` // 与订单号二选一
  147. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  148. OutRefundNo string `xml:"out_refund_no,omitempty" json:"out_refund_no,omitempty"`
  149. TotalFee string `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
  150. RefundFee string `xml:"refund_fee,omitempty" json:"refund_fee,omitempty"`
  151. RefundFeeType string `xml:"refund_fee_type,omitempty" json:"refund_fee_type,omitempty"`
  152. RefundDesc string `xml:"refund_desc,omitempty" json:"refund_desc,omitempty"`
  153. RefundAccount string `xml:"refund_account,omitempty" json:"refund_account,omitempty"`
  154. NotifyUrl string `xml:"notify_url,omitempty" json:"notify_url,omitempty"`
  155. }
  156. type RefundResult struct {
  157. CommonReturn
  158. CommonResult
  159. CommonParam
  160. TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id"`
  161. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  162. OutRefundNo string `xml:"out_refund_no,omitempty" json:"out_refund_no,omitempty"`
  163. RefundId string `xml:"refund_id,omitempty" json:"refund_id,omitempty"`
  164. RefundFee int `xml:"refund_fee,omitempty" json:"refund_fee,omitempty"`
  165. SettlementRefundFee int `xml:"settlement_refund_fee,omitempty" json:"settlement_refund_fee,omitempty"`
  166. TotalFee int `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
  167. SettlementTotalFee int `xml:"settlement_total_fee,omitempty" json:"settlement_total_fee,omitempty"`
  168. FeeType string `xml:"fee_type,omitempty" json:"fee_type,omitempty"`
  169. CashFee int `xml:"cash_fee,omitempty" json:"cash_fee,omitempty"`
  170. CashFeeType string `xml:"cash_fee_type,omitempty" json:"cash_fee_type,omitempty"`
  171. CashRefundFee string `xml:"cash_refund_fee,omitempty" json:"cash_refund_fee,omitempty"`
  172. CouponTypeN string `xml:"coupon_type_$n,omitempty" json:"coupon_type_$n,omitempty"`
  173. CouponRefundFee string `xml:"coupon_refund_fee,omitempty" json:"coupon_refund_fee,omitempty"`
  174. CouponRefundFeeN string `xml:"coupon_refund_fee_$n,omitempty" json:"coupon_refund_fee_$n,omitempty"`
  175. CouponRefundCount string `xml:"coupon_refund_count,omitempty" json:"coupon_refund_count,omitempty"`
  176. CouponRefundIdN string `xml:"coupon_refund_id_$n,omitempty" json:"coupon_refund_id_$n,omitempty"`
  177. }
  178. // ===================================================== 退款申请
  179. type RefundQueryParam struct {
  180. CommonParam
  181. XMLName xml.Name `xml:"xml" json:"xml_name,omitempty"`
  182. TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"`
  183. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  184. OutRefundNo string `xml:"out_refund_no,omitempty" json:"out_refund_no,omitempty"`
  185. RefundId string `xml:"refund_id,omitempty" json:"refund_id,omitempty"`
  186. Offset int `xml:"offset,omitempty" json:"offset,omitempty"`
  187. }
  188. type RefundQueryResult struct {
  189. CommonReturn
  190. CommonResult
  191. CommonParam
  192. TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"`
  193. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  194. TotalRefundCount int `xml:"total_refund_count,omitempty" json:"total_refund_count,omitempty"`
  195. TotalFee int `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
  196. FeeType string `xml:"fee_type,omitempty" json:"fee_type,omitempty"`
  197. CashFee int `xml:"cash_fee,omitempty" json:"cash_fee,omitempty"`
  198. CashFeeType string `xml:"cash_fee_type,omitempty" json:"cash_fee_type,omitempty"`
  199. SettlementTotalFee int `xml:"settlement_total_fee,omitempty" json:"settlement_total_fee,omitempty"`
  200. RefundCount int `xml:"refund_count,omitempty" json:"refund_count,omitempty"`
  201. OutRefundNoN string `xml:"out_refund_no_$n,omitempty" json:"out_refund_no_$n,omitempty"`
  202. RefundIdN string `xml:"refund_id_$n,omitempty" json:"refund_id_$n,omitempty"`
  203. RefundChannelN string `xml:"refund_channel_$n,omitempty" json:"refund_channel_$n,omitempty"`
  204. RefundFeeN int `xml:"refund_fee_$n,omitempty" json:"refund_fee_$n,omitempty"`
  205. CouponRefundFeeN int `xml:"coupon_refund_fee_$n,omitempty" json:"coupon_refund_fee_$n,omitempty"`
  206. CouponRefundCountN int `xml:"coupon_refund_count_$n,omitempty" json:"coupon_refund_count_$n,omitempty"`
  207. CouponRefundIdNM string `xml:"coupon_refund_id_$n_$m,omitempty" json:"coupon_refund_id_$n_$m,omitempty"`
  208. CouponTypeNM string `xml:"coupon_type_$n_$m,omitempty" json:"coupon_type_$n_$m,omitempty"`
  209. CouponRefundFeeNM int `xml:"coupon_refund_fee_$n_$m,omitempty" json:"coupon_refund_fee_$n_$m,omitempty"`
  210. RefundStatusN string `xml:"refund_status_$n,omitempty" json:"refund_status_$n,omitempty"`
  211. RefundAccountN string `xml:"refund_account_$n" json:"refund_account_$n,omitempty"`
  212. RefundRecvAccoutN string `xml:"refund_recv_accout_$n,omitempty" json:"refund_recv_accout_$n,omitempty"` // 少了n
  213. RefundSuccessTimeN string `xml:"refund_success_time_$n,omitempty" json:"refund_success_time_$n,omitempty"`
  214. }
  215. // =================================================== 退款查询
  216. type RedPackParam struct {
  217. CommonParam
  218. XMLName xml.Name `xml:"xml" json:"xml_name,omitempty"`
  219. Appid string `xml:"wxappid,omitempty" json:"wxappid,omitempty"` // wxappid
  220. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  221. SendName string `xml:"send_name,omitempty" json:"send_name,omitempty"`
  222. ReOpenid string `xml:"re_openid,omitempty" json:"re_openid,omitempty"`
  223. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  224. TotalNum int `xml:"total_num,omitempty" json:"total_num,omitempty"`
  225. Wishing string `xml:"wishing,omitempty" json:"wishing,omitempty"`
  226. ClientIp string `xml:"client_ip,omitempty" json:"client_ip,omitempty"`
  227. ActName string `xml:"act_name,omitempty" json:"act_name,omitempty"`
  228. Remark string `xml:"remark,omitempty" json:"remark,omitempty"`
  229. SendId string `xml:"send_id,omitempty" json:"send_id,omitempty"`
  230. RiskInfo string `xml:"risk_info,omitempty" json:"risk_info,omitempty"`
  231. }
  232. type RedPackResult struct {
  233. CommonReturn
  234. CommonResult
  235. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  236. MchId string `xml:"mch_id,omitempty" json:"mch_id,omitempty"`
  237. Wxappid string `xml:"wxappid,omitempty" json:"wxappid,omitempty"` // wxappid
  238. ReOpenid string `xml:"re_openid,omitempty" json:"re_openid,omitempty"`
  239. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  240. SendListid string `xml:"send_listid,omitempty" json:"send_listid,omitempty"`
  241. }
  242. type QueryRedPackResult struct {
  243. CommonReturn
  244. CommonResult
  245. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  246. Mchid string `xml:"mchid,omitempty" json:"mchid,omitempty"`
  247. DetailId string `xml:"detail_id,omitempty" json:"detail_id,omitempty"`
  248. Status string `xml:"status,omitempty" json:"status,omitempty"`
  249. SendType string `xml:"send_type,omitempty" json:"send_type,omitempty"`
  250. HbType string `xml:"hb_type,omitempty" json:"hb_type,omitempty"`
  251. TotalNum int `xml:"total_num,omitempty" json:"total_num,omitempty"`
  252. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  253. Reason string `xml:"reason,omitempty" json:"reason,omitempty"`
  254. SendTime string `xml:"send_time,omitempty" json:"send_time,omitempty"`
  255. RefundTime string `xml:"refund_time,omitempty" json:"refund_time,omitempty"`
  256. RefundAmount int `xml:"refund_amount,omitempty" json:"refund_amount,omitempty"`
  257. Wishing string `xml:"wishing,omitempty" json:"wishing,omitempty"`
  258. Remark string `xml:"remark,omitempty" json:"remark,omitempty"`
  259. ActName string `xml:"act_name,omitempty" json:"act_name,omitempty"`
  260. }