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

svc_alipay.go 3.1 KiB

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