支付模块
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.

179 lines
7.8 KiB

  1. package pay
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_alipay"
  4. local_wxpay "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_wechat"
  5. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
  6. zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
  7. "fmt"
  8. "github.com/iGoogle-ink/gopay"
  9. "github.com/iGoogle-ink/gopay/wechat"
  10. "github.com/pkg/errors"
  11. )
  12. /**
  13. 自有支付 支付宝
  14. req 传参
  15. ord_id //订单号
  16. amount //总金额
  17. subject //描述
  18. order_type //订单类型
  19. notify_url //自有支付回调地址
  20. platform //平台 由前端header传来的
  21. pay_app_cert_sn //应用公钥证书 值
  22. pay_alipayrsa_public_key //支付宝公钥证书 值
  23. pay_ali_use_type //支付宝选择版本 0旧版 1新版
  24. private_key //支付宝商户私钥 数据库字段 旧版 pay_ali_private_key 新版 pay_ali_new_private_key
  25. app_id //支付宝appId 数据库字段 旧版 pay_ali_app_id 新版 pay_ali_new_app_id
  26. rsa //支付宝密钥长度 旧版使用 数据库字段 pay_ali_key_len_type
  27. pkcs //支付宝密钥格式 旧版使用 数据库字段 pay_ali_key_format_type
  28. page_url //支付宝wap支付成功跳转链接
  29. */
  30. func AlipayCofing(args map[string]string) *md.PayData {
  31. var paySet = &md.PayData{
  32. PayAppCertSn: args["pay_app_cert_sn"],
  33. PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"],
  34. PayAliUseType: args["pay_ali_use_type"],
  35. }
  36. return paySet
  37. }
  38. func AlipayApp(args map[string]string) (string, error) {
  39. paySet := AlipayCofing(args)
  40. if args["private_key"] == "" || args["app_id"] == "" {
  41. return "", errors.New("请在后台正确配置支付宝")
  42. }
  43. param, err := local_alipay.TradeAppPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet)
  44. if err != nil {
  45. fmt.Println("支付宝错误日志")
  46. fmt.Println(param)
  47. fmt.Println(err)
  48. return "", errors.New("支付宝订单创建失败")
  49. }
  50. return zhios_pay_utils.AnyToString(param), nil
  51. }
  52. func AlipayPc(args map[string]string) (string, error) {
  53. paySet := AlipayCofing(args)
  54. if args["private_key"] == "" || args["app_id"] == "" {
  55. return "", errors.New("请在后台正确配置支付宝")
  56. }
  57. param, err := local_alipay.TradeAppPc(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], args["page_url"], paySet)
  58. if err != nil {
  59. fmt.Println("支付宝错误日志")
  60. fmt.Println(param)
  61. fmt.Println(err)
  62. return "", errors.New("支付宝订单创建失败")
  63. }
  64. return zhios_pay_utils.AnyToString(param), nil
  65. }
  66. func AlipayWap(args map[string]string) (string, error) {
  67. paySet := AlipayCofing(args)
  68. if args["private_key"] == "" || args["app_id"] == "" {
  69. return "", errors.New("请在后台正确配置支付宝")
  70. }
  71. param, err := local_alipay.TradeWapPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], args["page_url"], paySet)
  72. if err != nil {
  73. fmt.Println("支付宝错误日志")
  74. fmt.Println(param)
  75. fmt.Println(err)
  76. return "", errors.New("支付宝订单创建失败")
  77. }
  78. return zhios_pay_utils.AnyToString(param), nil
  79. }
  80. func AlipayApplet(args map[string]string, isPrd bool) (string, error) {
  81. var url string
  82. if isPrd == true {
  83. url = md.ZHIOS_PAY_URL_PRD + "/kuDianPay/Pay/aliAppPay"
  84. } else {
  85. url = md.ZHIOS_PAY_URL_DEV + "/kuDianPay/Pay/aliAppPay"
  86. }
  87. config := map[string]string{
  88. "pay_ali_app_id": args["pay_ali_app_id"],
  89. "pay_ali_private_key": args["pay_ali_private_key"],
  90. "pay_ali_public_key": args["pay_ali_public_key"],
  91. }
  92. payParams := map[string]interface{}{
  93. "notify_url": args["notify_url"],
  94. "op_app_id": args["op_app_id"],
  95. "buyer_id": args["buyer_id"],
  96. "total_amount": args["amount"],
  97. "out_trade_no": args["out_trade_no"],
  98. "subject": args["subject"],
  99. "app_auth_token": args["app_auth_token"],
  100. "config": config,
  101. }
  102. bytes, err := zhios_pay_utils.CurlPost(url, zhios_pay_utils.Serialize(payParams), nil)
  103. if err != nil {
  104. return "", err
  105. }
  106. return string(bytes), nil
  107. }
  108. // 微信app支付v2
  109. func WxAppPay(params map[string]string) (map[string]string, error) {
  110. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  111. r, err := local_wxpay.TradeAppPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  112. return r, err
  113. }
  114. // 微信H5支付v2
  115. func WxH5Pay(params map[string]string) (map[string]string, error) {
  116. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  117. r, err := local_wxpay.TradeH5Pay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  118. return r, err
  119. }
  120. func WxPcPay(params map[string]string) (map[string]string, error) {
  121. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  122. r, err := local_wxpay.TradePcPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["product_id"])
  123. return r, err
  124. }
  125. // 微信小程序v2
  126. func WxMiniProgPay(params map[string]string) (map[string]string, error) {
  127. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  128. if params["third_party_wechat_openid"] == "" {
  129. return nil, errors.New("请先授权微信")
  130. }
  131. r, err := local_wxpay.TradeMiniProgPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  132. return r, err
  133. }
  134. // 微信JSAPI支付
  135. func WxAppJSAPIPay(params map[string]string) (map[string]string, error) {
  136. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  137. if params["third_party_wechat_openid"] == "" {
  138. return nil, errors.New("请先授权微信")
  139. }
  140. r, err := local_wxpay.TradeJSAPIPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  141. return r, err
  142. }
  143. func WxAppRefund(params map[string]string) (*wechat.RefundResponse, gopay.BodyMap, error) {
  144. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  145. r, r1, err := local_wxpay.TradeAppRefund(client, params["subject"], params["transaction_id"], params["out_refund_no"], params["total_fee"], params["amount"], params["p12_str"], params["notify_url"])
  146. return r, r1, err
  147. }
  148. func WxAppRefundQuery(params map[string]string) (*wechat.QueryRefundResponse, gopay.BodyMap, error) {
  149. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  150. r, r1, err := local_wxpay.TradeAppRefundQuery(client, params["transaction_id"])
  151. return r, r1, err
  152. }
  153. func WxShowQuery(params map[string]string) (*wechat.QueryOrderResponse, gopay.BodyMap, error) {
  154. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  155. r, r1, err := local_wxpay.TradeShowQuery(client, params["transaction_id"])
  156. return r, r1, err
  157. }
  158. func WxShowQuery1(params map[string]string) (*wechat.QueryOrderResponse, gopay.BodyMap, error) {
  159. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  160. r, r1, err := local_wxpay.TradeShowQuery1(client, params["transaction_id"])
  161. return r, r1, err
  162. }
  163. func TradeShowRefundQuery(params map[string]string) (*wechat.QueryRefundResponse, gopay.BodyMap, error) {
  164. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  165. r, r1, err := local_wxpay.TradeShowRefundQuery(client, params["transaction_id"])
  166. return r, r1, err
  167. }