附近小店
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.
 
 
 

162 lines
4.2 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/shopspring/decimal"
  12. "github.com/tidwall/gjson"
  13. "math"
  14. "math/rand"
  15. "time"
  16. )
  17. func BalanceCommunityTeam(c *gin.Context) (interface{}, error) {
  18. ord, err := CheckCommunityTeam(c)
  19. if err != nil || ord == nil {
  20. return nil, err
  21. }
  22. err = BalancePay(c, ord.Amount, utils.Int64ToStr(ord.Oid), md.CommunityTeam)
  23. if err != nil {
  24. return nil, err
  25. }
  26. // 回调
  27. CommonCallbackCommunityTeam(c, utils.AnyToString(ord.Oid), md.BALANCE_PAY)
  28. return nil, nil
  29. }
  30. func AlipayCommunityTeam(c *gin.Context) (interface{}, error) {
  31. ord, err := CheckCommunityTeam(c)
  32. if err != nil {
  33. return nil, err
  34. }
  35. payParams := &md.AliPayPayParams{
  36. Subject: "小店下单",
  37. Amount: ord.Amount,
  38. OrdId: utils.AnyToString(ord.Oid),
  39. OrderType: md.CommunityTeam,
  40. Uid: utils.IntToStr(ord.Uid),
  41. AgentId: ord.ParentUid,
  42. }
  43. r, err := PrepareAlipayCode(c, payParams)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return r, nil
  48. }
  49. func WxPayCommunityTeam(c *gin.Context) (interface{}, error) {
  50. var r interface{}
  51. var err error
  52. ord, err := CheckCommunityTeam(c)
  53. if err != nil {
  54. return nil, err
  55. }
  56. params := map[string]string{
  57. "subject": "小店下单",
  58. "amount": wxMoneyMulHundred(ord.Amount),
  59. "order_type": md.CommunityTeam,
  60. "ord_id": utils.AnyToString(ord.Oid),
  61. "pay_wx_mch_id": SysCfgGet(c, "pay_wx_mch_id"),
  62. "pay_wx_api_key": SysCfgGet(c, "pay_wx_api_key"),
  63. "uid": utils.IntToStr(ord.Uid),
  64. }
  65. if ord.ParentUid > 0 {
  66. user, _ := db.UserThirdPartyFindByID(MasterDb(c), ord.ParentUid)
  67. if user.WechatPayInfo == "" {
  68. return nil, errors.New("支付失败")
  69. }
  70. params["pay_wx_mch_id"] = gjson.Get(user.WechatPayInfo, "pay_wx_mch_id").String()
  71. params["pay_wx_api_key"] = gjson.Get(user.WechatPayInfo, "pay_wx_api_key").String()
  72. }
  73. params["notify_url"] = fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params["order_type"], md.WX_PAY)
  74. r, err = CommPayData(c, params)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return r, nil
  79. }
  80. func AlipayCallbackCommunityTeam(c *gin.Context) {
  81. orderId, err := AlipayCallback(c)
  82. if err != nil || orderId == "" {
  83. return
  84. }
  85. CommonCallbackCommunityTeam(c, orderId, md.ALIPAY)
  86. }
  87. func WxPayCallbackCommunityTeam(c *gin.Context) {
  88. orderId, err := wxPayCallback(c)
  89. if err != nil || orderId == "" {
  90. return
  91. }
  92. CommonCallbackCommunityTeam(c, orderId, md.WX_PAY)
  93. }
  94. // 微信金额乘100
  95. func wxMoneyMulHundred(m string) string {
  96. amount, _ := decimal.NewFromString(m)
  97. newM := amount.Mul(decimal.NewFromInt(100))
  98. return newM.String()
  99. }
  100. func CommonCallbackCommunityTeam(c *gin.Context, orderId string, payMethod string) {
  101. ord := db.GetOrderEg(db.DBs[c.GetString("mid")], orderId)
  102. if ord == nil {
  103. return
  104. }
  105. // 判断是否失效
  106. if ord.State != 0 {
  107. return
  108. }
  109. ord.State = 1
  110. ord.UpdateAt = time.Now()
  111. ord.Code = Code()
  112. ord.PayAt = time.Now()
  113. ord.PayMethod = md.PayMethodIDs[payMethod]
  114. MasterDb(c).Where("id=?", ord.Id).Cols("state,update_at,code,pay_at,pay_method").Update(ord)
  115. engine := MasterDb(c)
  116. storeId, ord1, goodsInfo, err := CommGetPrinterContent(c, utils.Int64ToStr(ord.Oid))
  117. if err != nil {
  118. e.OutErr(c, 400, err)
  119. return
  120. }
  121. //打印操作
  122. go ReplaceDoing(c, engine, storeId, ord1, goodsInfo)
  123. return
  124. }
  125. func Code() string {
  126. rand.Seed(time.Now().UnixNano())
  127. var digits = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
  128. b := make([]rune, 6)
  129. for i := range b {
  130. if fl := float64(rand.Intn(10)); fl > math.Log10(float64(i+1)) {
  131. b[i] = digits[rand.Intn(len(digits))]
  132. } else {
  133. b[i] = digits[rand.Intn(10)]
  134. }
  135. }
  136. fmt.Println(string(b))
  137. return string(b)
  138. }
  139. func CheckCommunityTeam(c *gin.Context) (*model.CommunityTeamOrder, error) {
  140. var args struct {
  141. MainOrdId string `json:"main_ord_id"`
  142. }
  143. if err := c.ShouldBindJSON(&args); err != nil || args.MainOrdId == "" {
  144. return nil, e.NewErrCode(e.ERR_INVALID_ARGS)
  145. }
  146. // 查询订单
  147. ord := db.GetOrderEg(db.DBs[c.GetString("mid")], args.MainOrdId)
  148. if ord == nil {
  149. return nil, e.NewErr(403000, "不存在该订单")
  150. }
  151. // 判断是否失效
  152. if ord.State != 0 {
  153. return nil, e.NewErr(403000, "订单已处理")
  154. }
  155. return ord, nil
  156. }