互动
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.

447 lines
13 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pay_service
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. core2 "github.com/chanxuehong/wechat/mch/core"
  6. "github.com/chanxuehong/wechat/mch/pay"
  7. "github.com/pkg/errors"
  8. "go.uber.org/zap"
  9. "hudongzhuanjia/logger"
  10. "hudongzhuanjia/models"
  11. "hudongzhuanjia/utils"
  12. "io"
  13. "math"
  14. "strconv"
  15. "time"
  16. )
  17. func init() {
  18. go loopUnifiedOrder()
  19. }
  20. var orderDelayQueue = make(chan *orderDelayQueueParam, math.MaxInt8)
  21. type orderDelayQueueParam struct {
  22. Order *models.UserOrder
  23. First bool `json:"first"`
  24. Expires int64 `json:"expires"`
  25. Delay int `json:"delay"`
  26. }
  27. func PutOrderDelayQueue(order *models.UserOrder, expires int64, delay int) {
  28. if expires == 0 {
  29. expires = time.Now().Add(12 * time.Hour).Unix() // 2 个小时
  30. }
  31. if delay == 0 {
  32. delay = 1000
  33. }
  34. orderDelayQueue <- &orderDelayQueueParam{
  35. First: true,
  36. Expires: expires,
  37. Delay: delay,
  38. Order: order,
  39. }
  40. }
  41. func loopUnifiedOrder() {
  42. orders, err := models.GetUserOrdersByStatus(core2.FormatTime(time.Now().Add(12*time.Hour)), 0, 3)
  43. if err != nil {
  44. panic(err)
  45. }
  46. for _, order := range orders {
  47. expire, err := core2.ParseTime(order.TimeExpire)
  48. if err != nil {
  49. expire = time.Now().Add(12 * time.Hour)
  50. }
  51. PutOrderDelayQueue(order, expire.Add(12*time.Hour).Unix(), 0)
  52. }
  53. defer func() {
  54. if err := recover(); err != nil {
  55. logger.Error("订单轮询查询", zap.Any("panic 恢复错误", err))
  56. }
  57. // 重启
  58. time.Sleep(5 * time.Second)
  59. loopUnifiedOrder()
  60. }()
  61. for {
  62. select {
  63. case param, ok := <-orderDelayQueue:
  64. if !ok {
  65. panic("通道异常关闭")
  66. }
  67. if param.Expires <= time.Now().Unix() {
  68. if param.Order.Status == 0 {
  69. order := new(models.UserOrder)
  70. _, err = order.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 7)
  71. Close(param.Order.OutTradeNo) // 超时关闭订单
  72. go HandleFailed(param)
  73. }
  74. continue // 超时
  75. }
  76. // 首次进入不延迟
  77. if !param.First {
  78. time.Sleep(time.Duration(param.Delay) * time.Millisecond)
  79. }
  80. param.First = false
  81. if param.Order.Status == 0 {
  82. res, err := OrderQuery(param.Order.OutTradeNo)
  83. // 出现错误
  84. if err != nil {
  85. logger.Error("查询订单出现错误", zap.String("错误原因", err.Error()),
  86. zap.String("交易订单号", param.Order.OutTradeNo))
  87. orderDelayQueue <- param // 重新进入队列
  88. continue
  89. }
  90. if res.Query.TradeState == CODE_TRADE_SUCCESS {
  91. param.Order = res.Order
  92. go HandleSuccess(param)
  93. } else if res.Query.TradeState == CODE_TRADE_REFUND {
  94. param.Order.Status = 3
  95. orderDelayQueue <- param
  96. continue
  97. } else if res.Query.TradeState == CODE_TRADE_NOTPAY || res.Query.TradeState == CODE_TRADE_USERPAYING {
  98. orderDelayQueue <- param
  99. continue
  100. }
  101. } else if param.Order.Status == 3 {
  102. _, err = QueryRefund(param.Order.OutTradeNo)
  103. if err != nil {
  104. logger.Error("退款订单查询错误", zap.String("错误原因", err.Error()),
  105. zap.String("交易订单号", param.Order.OutTradeNo))
  106. continue
  107. }
  108. } else {
  109. continue
  110. }
  111. }
  112. }
  113. }
  114. const CallbackOrderUrl = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackOrder"
  115. func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) (map[string]interface{}, error) {
  116. client, err := Client()
  117. if err != nil {
  118. return nil, err
  119. }
  120. now := time.Now()
  121. timeStart := core2.FormatTime(now)
  122. timeExpire := core2.FormatTime(now.Add(12 * time.Hour))
  123. outTradeNo := utils.RandomStr(32)
  124. nonceStr := utils.RandomStr(32)
  125. resp, err := pay.UnifiedOrder2(client, &pay.UnifiedOrderRequest{
  126. Body: body,
  127. OutTradeNo: outTradeNo,
  128. TotalFee: fee,
  129. NotifyURL: CallbackOrderUrl,
  130. TradeType: "JSAPI",
  131. DeviceInfo: "WEB",
  132. NonceStr: nonceStr,
  133. SignType: core2.SignType_MD5,
  134. TimeStart: now,
  135. OpenId: openid,
  136. })
  137. if err != nil {
  138. return nil, err
  139. }
  140. // 记录这次订单
  141. userOrder := new(models.UserOrder)
  142. userOrder.DeviceInfo = "WEB"
  143. userOrder.Body = body
  144. userOrder.UserId = userId
  145. userOrder.ActivityId = activityId
  146. userOrder.FeeType = "CNY"
  147. userOrder.GoodType = goodType
  148. userOrder.OpenId = openid
  149. userOrder.OutTradeNo = outTradeNo
  150. userOrder.TimeStart = timeStart
  151. userOrder.TimeExpire = timeExpire
  152. userOrder.TotalFee = fee
  153. userOrder.TradeType = "JSAPI"
  154. userOrder.PrepayId = resp.PrepayId
  155. userOrder.Status = 0
  156. userOrder.IsDelete = false
  157. userOrder.CreatedAt = time.Now()
  158. userOrder.UpdatedAt = time.Now()
  159. if _, err = models.Add(userOrder); err != nil {
  160. return nil, err
  161. }
  162. timestamp := strconv.FormatInt(time.Now().Unix(), 10)
  163. //获取H5支付需要的paySign
  164. pac := "prepay_id=" + resp.PrepayId
  165. paySign := core2.JsapiSign(client.AppId(), timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey)
  166. go PutOrderDelayQueue(userOrder, 0, 0)
  167. return map[string]interface{}{
  168. "appid": Appid,
  169. "timestamp": timestamp,
  170. "nonce_str": nonceStr,
  171. "package": pac,
  172. "sign_type": core2.SignType_MD5,
  173. "pay_sign": paySign,
  174. "out_trade_no": outTradeNo,
  175. "user_order_id": userOrder.Id,
  176. }, nil
  177. }
  178. func ReOrder(outTradeNo string) (map[string]interface{}, error) {
  179. userOrder := new(models.UserOrder)
  180. exist, err := userOrder.GetByOutTradeNo(outTradeNo)
  181. if err != nil {
  182. return nil, err
  183. }
  184. if !exist {
  185. return nil, errors.New("订单不存在")
  186. }
  187. timestamp := strconv.FormatInt(time.Now().Unix(), 10)
  188. nonceStr := utils.RandomStr(32)
  189. //获取H5支付需要的paySign
  190. pac := "prepay_id=" + userOrder.PrepayId
  191. paySign := core2.JsapiSign(Appid, timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey)
  192. go PutOrderDelayQueue(userOrder, 0, 0)
  193. return map[string]interface{}{
  194. "appid": Appid,
  195. "timestamp": timestamp,
  196. "nonce_str": nonceStr,
  197. "package": pac,
  198. "sign_type": core2.SignType_MD5,
  199. "pay_sign": paySign,
  200. "out_trade_no": outTradeNo,
  201. "user_order_id": userOrder.Id,
  202. }, nil
  203. }
  204. // Notify
  205. type NotifyRequest struct {
  206. ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"`
  207. ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
  208. ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"`
  209. ErrCode string `xml:"err_code,omitempty" json:"err_code,omitempty"`
  210. ErrCodeDes string `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"`
  211. Appid string `xml:"appid,omitempty" json:"appid,omitempty"`
  212. MchId string `xml:"mch_id,omitempty" json:"mch_id,omitempty"`
  213. DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
  214. NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
  215. Sign string `xml:"sign,omitempty" json:"sign,omitempty"`
  216. SignType string `xml:"sign_type,omitempty" json:"sign_type,omitempty"`
  217. Openid string `xml:"openid,omitempty" json:"openid,omitempty"`
  218. IsSubscribe string `xml:"is_subscribe,omitempty" json:"is_subscribe,omitempty"`
  219. TradeType string `xml:"trade_type,omitempty" json:"trade_type,omitempty"`
  220. BankType string `xml:"bank_type,omitempty" json:"bank_type,omitempty"`
  221. TotalFee int `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
  222. SettlementTotalFee int `xml:"settlement_total_fee,omitempty" json:"settlement_total_fee,omitempty"`
  223. FeeType string `xml:"fee_type,omitempty" json:"fee_type,omitempty"`
  224. CashFee int `xml:"cash_fee,omitempty" json:"cash_fee,omitempty"`
  225. CashFeeType string `xml:"cash_fee_type,omitempty" json:"cash_fee_type,omitempty"`
  226. CouponFee int `xml:"coupon_fee,omitempty" json:"coupon_fee,omitempty"`
  227. CouponCount int `xml:"coupon_count,omitempty" json:"coupon_count,omitempty"`
  228. CouponType0 string `xml:"coupon_type_0,omitempty" json:"coupon_type_0,omitempty"`
  229. CouponType1 string `xml:"coupon_type_1,omitempty" json:"coupon_type_1,omitempty"`
  230. CouponId0 string `xml:"coupon_id_0,omitempty" json:"coupon_id_0,omitempty"`
  231. CouponId1 string `xml:"coupon_id_1,omitempty" json:"coupon_id_1,omitempty"`
  232. CouponFee0 int `xml:"coupon_fee_0,omitempty" json:"coupon_fee_0,omitempty"`
  233. CouponFee1 int `xml:"coupon_fee_1,omitempty" json:"coupon_fee_1,omitempty"`
  234. TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"`
  235. OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
  236. Attach string `xml:"attach,omitempty" json:"attach,omitempty"`
  237. TimeEnd string `xml:"time_end,omitempty" json:"time_end,omitempty"`
  238. }
  239. func NotifyOrder(w io.Reader) (*models.UserOrder, error) {
  240. res := new(NotifyRequest)
  241. if err := xml.NewDecoder(w).Decode(res); err != nil {
  242. return nil, fmt.Errorf("xml.NewDecoder.Decode:%w", err)
  243. }
  244. if res.ReturnCode != CODE_SUCCESS {
  245. return nil, fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg)
  246. }
  247. if res.ResultCode == CODE_SUCCESS && res.TradeType == CODE_TRADE_SUCCESS {
  248. userOrder := new(models.UserOrder)
  249. userOrder.TimeEnd = res.TimeEnd
  250. userOrder.TransactionId = res.TransactionId
  251. userOrder.Status = 1
  252. if _, err := userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1); err != nil {
  253. return nil, err
  254. }
  255. return userOrder, nil
  256. } else {
  257. return nil, fmt.Errorf("trade error, err_code: %v and err_code_des: %v", res.ErrCode, res.ErrCodeDes)
  258. }
  259. }
  260. type OrderQueryResult struct {
  261. Order *models.UserOrder
  262. Query *pay.OrderQueryResponse
  263. }
  264. func OrderQuery(outTradeNo string) (*OrderQueryResult, error) {
  265. client, err := Client()
  266. if err != nil {
  267. return nil, err
  268. }
  269. // 请求订单查询,成功后得到结果
  270. userOrder := new(models.UserOrder)
  271. exist, err := userOrder.GetByOutTradeNo(outTradeNo)
  272. if err != nil {
  273. return nil, err
  274. }
  275. if !exist {
  276. return nil, errors.New("订单不存在")
  277. }
  278. res, err := pay.OrderQuery2(client, &pay.OrderQueryRequest{
  279. OutTradeNo: outTradeNo,
  280. NonceStr: utils.RandomStr(32),
  281. SignType: core2.SignType_MD5,
  282. })
  283. if err != nil {
  284. userOrder.ErrMsg = err.Error()
  285. userOrder.UpdateErrByOutTradeNo(outTradeNo)
  286. return nil, err
  287. }
  288. userOrder.TransactionId = res.TransactionId
  289. userOrder.TimeEnd = core2.FormatTime(res.TimeEnd)
  290. switch res.TradeState {
  291. case CODE_TRADE_SUCCESS:
  292. userOrder.Status = 1
  293. case CODE_TRADE_REVOKED:
  294. userOrder.Status = 2
  295. case CODE_TRADE_REFUND:
  296. userOrder.Status = 3
  297. case CODE_TRADE_PAYERROR:
  298. userOrder.Status = 5
  299. case CODE_TRADE_CLOSED:
  300. userOrder.Status = 6
  301. case CODE_TRADE_NOTPAY: // 超过期限,就关闭
  302. userOrder.Status = 0
  303. }
  304. if _, err = userOrder.UpdateStatusByOutTradeNo(outTradeNo, userOrder.Status); err != nil {
  305. return nil, err
  306. }
  307. return &OrderQueryResult{
  308. Order: userOrder,
  309. Query: res,
  310. }, nil
  311. }
  312. func Close(outTradeNo string) error {
  313. client, err := Client()
  314. if err != nil {
  315. return err
  316. }
  317. err = pay.CloseOrder2(client, &pay.CloseOrderRequest{
  318. OutTradeNo: outTradeNo,
  319. NonceStr: utils.RandomStr(32),
  320. SignType: core2.SignType_MD5,
  321. })
  322. // 请求关闭订单,成功后得到结果
  323. if err != nil {
  324. return err
  325. }
  326. return nil
  327. }
  328. //const CALLBACK_REFUND_URL = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackRefund"
  329. func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) {
  330. userOrder := new(models.UserOrder)
  331. exist, err := userOrder.GetByOutTradeNo(outTradeNo)
  332. if err != nil {
  333. return nil, err
  334. }
  335. if !exist {
  336. return nil, errors.New("订单不存在")
  337. }
  338. client, err := Client()
  339. outRefundNo := utils.RandomStr(64)
  340. nonceStr := utils.RandomStr(32)
  341. res, err := pay.Refund2(client, &pay.RefundRequest{
  342. TransactionId: "",
  343. OutTradeNo: outTradeNo,
  344. OutRefundNo: outRefundNo,
  345. TotalFee: userOrder.TotalFee,
  346. RefundFee: userOrder.TotalFee,
  347. NonceStr: nonceStr,
  348. SignType: core2.SignType_MD5,
  349. RefundFeeType: "CNY",
  350. RefundDesc: reason,
  351. })
  352. //
  353. if err != nil {
  354. userOrder.ErrMsg = err.Error()
  355. userOrder.UpdateErrByOutTradeNo(outTradeNo)
  356. return nil, err
  357. }
  358. userOrder.Status = 3
  359. _, err = userOrder.UpdateStatusByOutTradeNo(outTradeNo, userOrder.Status)
  360. if err != nil {
  361. return nil, err
  362. }
  363. PutOrderDelayQueue(userOrder, 0, 0) // 退款查询
  364. return res, nil
  365. }
  366. type QueryRefundResult struct {
  367. Order *models.UserOrder
  368. Query *pay.RefundQueryResponse
  369. }
  370. func QueryRefund(outTradeNo string) (*QueryRefundResult, error) {
  371. userOrder := new(models.UserOrder)
  372. exist, err := userOrder.GetByOutTradeNo(outTradeNo)
  373. if err != nil {
  374. return nil, err
  375. }
  376. if !exist {
  377. return nil, errors.New("不存在改笔退款")
  378. }
  379. client, err := Client()
  380. res, err := pay.RefundQuery2(client, &pay.RefundQueryRequest{
  381. OutTradeNo: outTradeNo,
  382. NonceStr: utils.RandomStr(32),
  383. SignType: core2.SignType_MD5,
  384. })
  385. //请求申请退款
  386. if err != nil {
  387. userOrder.ErrMsg = err.Error()
  388. userOrder.UpdateErrByOutTradeNo(outTradeNo)
  389. return nil, err
  390. }
  391. userOrder.RefundAccount = res.RefundList[0].RefundAccount
  392. userOrder.RefundRecvAccount = res.RefundList[0].RefundRecvAccout
  393. userOrder.SuccessTime = res.RefundList[0].RefundSuccessTime
  394. userOrder.Status = 4
  395. _, err = userOrder.UpdateRefundByOutTradeNo(outTradeNo)
  396. if err != nil {
  397. return nil, err
  398. }
  399. return &QueryRefundResult{
  400. Order: userOrder,
  401. Query: res,
  402. }, nil
  403. }