微信相关接口
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.

111 lines
2.8 KiB

  1. package wechat
  2. import (
  3. "context"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "errors"
  7. "io/ioutil"
  8. "log"
  9. "git.tetele.net/tgo/helper"
  10. "github.com/wechatpay-apiv3/wechatpay-go/core"
  11. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  12. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  13. )
  14. /**
  15. * 查询支付情况
  16. * wx_appid 公众号 appid
  17. * wx_mch_id 商户号
  18. * wx_mch_serial_no 支付密钥
  19. * wx_mch_apiclient_key 商户私钥
  20. * wx_pay_cert 平台证书
  21. */
  22. func GetPayResult(dbname, order_sn, wx_appid, wx_mch_id, wx_mch_serial_no, wx_mch_apiclient_key, wx_pay_cert string) (bool, map[string]interface{}, error) {
  23. var (
  24. ctx context.Context = context.Background()
  25. client *core.Client
  26. )
  27. if wx_appid == "" {
  28. return false, nil, errors.New("没有appid")
  29. }
  30. if wx_mch_id == "" {
  31. return false, nil, errors.New("没有商户号")
  32. }
  33. privateKey, err := utils.LoadPrivateKey(wx_mch_apiclient_key)
  34. if err != nil {
  35. log.Println("商户私钥有误:", err)
  36. return false, nil, errors.New("商户私钥不正确")
  37. }
  38. wechatPayCertificate, err := utils.LoadCertificate(wx_pay_cert)
  39. if err != nil {
  40. log.Println("平台证书有误:", err)
  41. return false, nil, errors.New("平台证书不正确")
  42. }
  43. client, err = CreatePayClient(ctx, wechatPayCertificate, privateKey, wx_mch_id, wx_mch_serial_no)
  44. if err != nil {
  45. log.Println("create pay client error:", err)
  46. return false, nil, errors.New("构造失败")
  47. }
  48. url := "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + order_sn + "?mchid=" + wx_mch_id
  49. ret, err := client.Get(ctx, url)
  50. body, err := ioutil.ReadAll(ret.Response.Body)
  51. var resp map[string]interface{}
  52. err = json.Unmarshal(body, &resp)
  53. if err != nil {
  54. log.Println("json unmarshal error:", err)
  55. return false, nil, err
  56. } else {
  57. if _, exists := resp["trade_state"]; exists {
  58. if helper.ToStr(resp["trade_state"]) == "SUCCESS" {
  59. return true, resp, nil
  60. }
  61. }
  62. }
  63. return false, nil, nil
  64. }
  65. /**
  66. * 构造支付请求
  67. * privateKey 商户私钥
  68. * wechatPayCertificate 平台证书
  69. * mchID 商户号
  70. * mchSerialNo 支付密钥
  71. */
  72. func CreatePayClient(ctx context.Context, wechatPayCertificate *x509.Certificate, privateKey *rsa.PrivateKey, mchID, mchSerialNo string) (*core.Client, error) {
  73. var client *core.Client
  74. var err error
  75. if wechatPayCertificate != nil {
  76. client, err = core.NewClient(
  77. ctx, option.WithMerchantCredential(mchID, mchSerialNo, privateKey),
  78. option.WithWechatPayCertificate([]*x509.Certificate{wechatPayCertificate}),
  79. )
  80. if err != nil {
  81. log.Println("创建 Client 失败:", err)
  82. return nil, err
  83. }
  84. } else {
  85. client, err = core.NewClient(
  86. ctx, option.WithMerchantCredential(mchID, mchSerialNo, privateKey), option.WithoutValidator(),
  87. )
  88. if err != nil {
  89. log.Println("创建 Client 失败:", err)
  90. return nil, err
  91. }
  92. }
  93. return client, nil
  94. }