附近小店
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

147 righe
3.9 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "errors"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/tidwall/gjson"
  12. "time"
  13. )
  14. func BalanceCommunityTeamPay(c *gin.Context) (interface{}, error) {
  15. ord, err := CheckCommunityTeamPay(c)
  16. if err != nil || ord == nil {
  17. return nil, err
  18. }
  19. err = BalancePay(c, ord.Amount, utils.Int64ToStr(ord.Oid), md.CommunityTeam)
  20. if err != nil {
  21. return nil, err
  22. }
  23. // 回调
  24. CommonCallbackCommunityTeamPay(c, utils.AnyToString(ord.Oid), md.BALANCE_PAY)
  25. return nil, nil
  26. }
  27. func AlipayCommunityTeamPay(c *gin.Context) (interface{}, error) {
  28. ord, err := CheckCommunityTeamPay(c)
  29. if err != nil {
  30. return nil, err
  31. }
  32. payParams := &md.AliPayPayParams{
  33. Subject: "收款码收款",
  34. Amount: ord.Amount,
  35. OrdId: utils.AnyToString(ord.Oid),
  36. OrderType: md.CommunityTeamPay,
  37. Uid: utils.IntToStr(ord.Uid),
  38. AgentId: ord.ParentUid,
  39. }
  40. r, err := PrepareAlipayCode(c, payParams)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return r, nil
  45. }
  46. func WxPayCommunityTeamPay(c *gin.Context) (interface{}, error) {
  47. var r interface{}
  48. var err error
  49. ord, err := CheckCommunityTeamPay(c)
  50. if err != nil {
  51. return nil, err
  52. }
  53. params := map[string]string{
  54. "subject": "收款码收款",
  55. "amount": wxMoneyMulHundred(ord.Amount),
  56. "order_type": md.AggregationRecharge,
  57. "ord_id": utils.AnyToString(ord.Oid),
  58. "pay_wx_mch_id": SysCfgGet(c, "pay_wx_mch_id"),
  59. "pay_wx_api_key": SysCfgGet(c, "pay_wx_api_key"),
  60. "uid": utils.IntToStr(ord.Uid),
  61. }
  62. if ord.ParentUid > 0 {
  63. user, _ := db.UserThirdPartyFindByID(MasterDb(c), ord.ParentUid)
  64. if user.WechatPayInfo == "" {
  65. return nil, errors.New("支付失败")
  66. }
  67. params["pay_wx_mch_id"] = gjson.Get(user.WechatPayInfo, "pay_wx_mch_id").String()
  68. params["pay_wx_api_key"] = gjson.Get(user.WechatPayInfo, "pay_wx_api_key").String()
  69. }
  70. params["notify_url"] = fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params["order_type"], md.WX_PAY)
  71. r, err = CommPayData(c, params)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return r, nil
  76. }
  77. func AlipayCallbackCommunityTeamPay(c *gin.Context) {
  78. orderId, err := AlipayCallback(c)
  79. if err != nil || orderId == "" {
  80. return
  81. }
  82. CommonCallbackCommunityTeamPay(c, orderId, md.ALIPAY)
  83. }
  84. func WxPayCallbackCommunityTeamPay(c *gin.Context) {
  85. orderId, err := wxPayCallback(c)
  86. if err != nil || orderId == "" {
  87. return
  88. }
  89. CommonCallbackCommunityTeamPay(c, orderId, md.WX_PAY)
  90. }
  91. func CommonCallbackCommunityTeamPay(c *gin.Context, orderId string, payMethod string) {
  92. sess := MasterDb(c).NewSession()
  93. defer sess.Close()
  94. sess.Begin()
  95. ord := db.GetOrderPay(sess, orderId)
  96. if ord == nil {
  97. sess.Rollback()
  98. return
  99. }
  100. // 判断是否失效
  101. if ord.State != 0 {
  102. return
  103. }
  104. ord.State = 1
  105. ord.UpdateAt = time.Now()
  106. ord.PayAt = time.Now()
  107. ord.PayMethod = md.PayMethodIDs[payMethod]
  108. sess.Where("id=?", ord.Id).Cols("state,update_at,pay_at,pay_method").Update(ord)
  109. money := utils.StrToFloat64(ord.Commission)
  110. if ord.ParentUid > 0 {
  111. money = utils.StrToFloat64(ord.Amount) - utils.StrToFloat64(ord.AgentCommission)
  112. }
  113. if ord.StoreType == 1 {
  114. money = utils.StrToFloat64(ord.Amount) - utils.StrToFloat64(ord.PlatformCommission)
  115. }
  116. bools := MoneyCheck(c, sess, ord.StoreUid, ord.ParentUid, ord.StoreType, 0, 4, money, "收款码收款", ord.Oid)
  117. if bools == false {
  118. sess.Rollback()
  119. return
  120. }
  121. sess.Commit()
  122. return
  123. }
  124. func CheckCommunityTeamPay(c *gin.Context) (*model.CommunityTeamPayOrder, error) {
  125. var args struct {
  126. MainOrdId string `json:"main_ord_id"`
  127. }
  128. if err := c.ShouldBindJSON(&args); err != nil || args.MainOrdId == "" {
  129. return nil, e.NewErrCode(e.ERR_INVALID_ARGS)
  130. }
  131. // 查询订单
  132. ord := db.GetOrderPayEg(db.DBs[c.GetString("mid")], args.MainOrdId)
  133. if ord == nil {
  134. return nil, e.NewErr(403000, "不存在该订单")
  135. }
  136. // 判断是否失效
  137. if ord.State != 0 {
  138. return nil, e.NewErr(403000, "订单已处理")
  139. }
  140. return ord, nil
  141. }