附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

128 lines
4.1 KiB

  1. package svc
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/pay"
  10. "errors"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "github.com/iGoogle-ink/gopay/alipay"
  14. "github.com/tidwall/gjson"
  15. )
  16. // 支付宝回调处理
  17. func AlipayCallback(c *gin.Context) (string, error) {
  18. data, ok := c.Get("callback")
  19. if data == nil || !ok {
  20. return "", e.NewErrCode(e.ERR_INVALID_ARGS)
  21. }
  22. args := data.(*md.AliPayCallback)
  23. _, ok = db.DBs[args.MasterID]
  24. if !ok {
  25. return "", logx.Warn("Alipay Failed : master_id not found")
  26. }
  27. c.Set("mid", args.MasterID)
  28. // 回调交易状态失败
  29. if args.TradeStatus != "TRADE_SUCCESS" {
  30. return "", logx.Warn("Alipay Failed : trade status failed")
  31. }
  32. return args.OutTradeNo, nil
  33. }
  34. func PrepareAlipayCode(c *gin.Context, p *md.AliPayPayParams) (interface{}, error) {
  35. req, err := CommAlipayConfig(c, p)
  36. if err != nil {
  37. return "", err
  38. }
  39. var param interface{}
  40. switch req["platform"] {
  41. case md.PLATFORM_ALIPAY_APPLET:
  42. param, err = pay.AlipayApplet(req)
  43. case md.PLATFORM_WAP:
  44. param, err = pay.AlipayWap(req)
  45. case md.PLATFORM_ANDROID, md.PLATFORM_IOS:
  46. param, err = pay.AlipayApp(req)
  47. default:
  48. return "", e.NewErrCode(e.ERR_PLATFORM)
  49. }
  50. if err != nil {
  51. fmt.Println("支付宝错误日志")
  52. fmt.Println(param)
  53. fmt.Println(err)
  54. return "", e.NewErrCode(e.ERR_ALIPAY_ORDER_ERR)
  55. }
  56. return utils.AnyToString(param), nil
  57. }
  58. func CommAlipayConfig(c *gin.Context, p *md.AliPayPayParams) (map[string]string, error) {
  59. //获取支付配置
  60. req := map[string]string{
  61. "pay_ali_use_type": SysCfgGet(c, "pay_ali_use_type"),
  62. "private_key": SysCfgGet(c, "pay_ali_private_key"),
  63. "app_id": SysCfgGet(c, "pay_ali_app_id"),
  64. "rsa": SysCfgGet(c, "pay_ali_key_len_type"),
  65. "pkcs": SysCfgGet(c, "pay_ali_key_format_type"),
  66. }
  67. payAliNewPrivateKey := SysCfgGet(c, "pay_ali_new_private_key")
  68. payAliNewAppId := SysCfgGet(c, "pay_ali_new_app_id")
  69. payAppCertSn := SysCfgGet(c, "pay_app_cert_sn")
  70. payAlipayrsaPublicKey := SysCfgGet(c, "pay_alipayrsa_public_key")
  71. if p.AgentId > 0 {
  72. user, _ := db.UserThirdPartyFindByID(MasterDb(c), p.AgentId)
  73. if user.AlipayPayInfo == "" {
  74. return nil, errors.New("支付失败")
  75. }
  76. req["pay_ali_use_type"] = gjson.Get(user.AlipayPayInfo, "pay_ali_use_type").String()
  77. req["private_key"] = gjson.Get(user.AlipayPayInfo, "pay_ali_private_key").String()
  78. req["app_id"] = gjson.Get(user.AlipayPayInfo, "pay_ali_app_id").String()
  79. req["rsa"] = gjson.Get(user.AlipayPayInfo, "pay_ali_key_len_type").String()
  80. req["pkcs"] = gjson.Get(user.AlipayPayInfo, "pay_ali_key_format_type").String()
  81. payAliNewPrivateKey = gjson.Get(user.AlipayPayInfo, "pay_ali_new_private_key").String()
  82. payAliNewAppId = gjson.Get(user.AlipayPayInfo, "pay_ali_new_app_id").String()
  83. payAppCertSn = gjson.Get(user.AlipayPayInfo, "pay_app_cert_sn").String()
  84. payAlipayrsaPublicKey = gjson.Get(user.AlipayPayInfo, "pay_alipayrsa_public_key").String()
  85. }
  86. if req["pay_ali_use_type"] == "1" {
  87. req["private_key"] = payAliNewPrivateKey
  88. req["app_id"] = payAliNewAppId
  89. appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + payAppCertSn)
  90. if err != nil {
  91. fmt.Println(err)
  92. return nil, err
  93. }
  94. if appCertSN == "" {
  95. fmt.Println(err)
  96. return nil, err
  97. }
  98. req["pay_app_cert_sn"] = appCertSN
  99. aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + payAlipayrsaPublicKey)
  100. if err != nil {
  101. fmt.Println(err)
  102. return nil, err
  103. }
  104. if aliPayPublicCertSN == "" {
  105. fmt.Println(err)
  106. return nil, err
  107. }
  108. req["pay_alipayrsa_public_key"] = aliPayPublicCertSN
  109. }
  110. if req["private_key"] == "" || req["app_id"] == "" {
  111. return req, e.NewErr(400, "请在后台正确配置支付宝")
  112. }
  113. req["ord_id"] = p.OrdId
  114. req["amount"] = p.Amount
  115. req["subject"] = p.Subject
  116. req["order_type"] = p.OrderType
  117. req["notify_url"] = fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), p.OrderType, md.ALIPAY)
  118. req["platform"] = c.GetHeader("Platform")
  119. req["page_url"] = c.Query("page_url")
  120. utils.FilePutContents(c.GetString("mid")+"alipay", utils.SerializeStr(req))
  121. return req, nil
  122. }