pay_v3.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package wechat
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "encoding/base64"
  6. "encoding/json"
  7. "strings"
  8. "git.chuangxin1.com/cx/myth/util"
  9. )
  10. // DecryptAES256GCM 使用 AEAD_AES_256_GCM 算法进行解密
  11. func DecryptAES256GCM(aesKey, associatedData, nonce, ciphertext string) (dataBytes []byte, err error) {
  12. var (
  13. decodedCiphertext []byte
  14. c cipher.Block
  15. gcm cipher.AEAD
  16. )
  17. decodedCiphertext, err = base64.StdEncoding.DecodeString(ciphertext)
  18. if err != nil {
  19. return
  20. }
  21. c, err = aes.NewCipher([]byte(aesKey))
  22. if err != nil {
  23. return
  24. }
  25. gcm, err = cipher.NewGCM(c)
  26. if err != nil {
  27. return
  28. }
  29. dataBytes, err = gcm.Open(nil, []byte(nonce), decodedCiphertext, []byte(associatedData))
  30. return
  31. }
  32. // Unmarshal 解析结构
  33. func (r *PayResultNotifyV3) Unmarshal(body []byte) (err error) {
  34. err = json.Unmarshal(body, &r)
  35. return
  36. }
  37. // IsEntranceStateChange 是否微信支付分停车 状态变更通知
  38. func (r *PayResultNotifyV3) IsEntranceStateChange() bool {
  39. return strings.Compare(r.EventType, "VEHICLE.ENTRANCE_STATE_CHANGE") == 0
  40. }
  41. // IsTransactionSuccess 是否支付到账通知
  42. func (r *PayResultNotifyV3) IsTransactionSuccess() bool {
  43. return strings.Compare(r.EventType, "TRANSACTION.SUCCESS") == 0
  44. }
  45. // IsTradeSuccess 是否支付成功
  46. func (r *PayOrderInfoV3) IsTradeSuccess() bool {
  47. return strings.Compare(r.TradeState, "SUCCESS") == 0
  48. }
  49. // Unmarshal 解析结构
  50. func (r *PayScoreParkingNotifyV3Result) Unmarshal(body []byte) (err error) {
  51. err = json.Unmarshal(body, &r)
  52. return
  53. }
  54. // Unmarshal 解析结构
  55. func (r *PayScoreParkingNotifyV3Result) String() (s string) {
  56. bs, _ := json.Marshal(r)
  57. s = util.BytesToString(bs)
  58. return
  59. }
  60. // Unmarshal 解析结构
  61. func (r *PayRefundInfoV3) Unmarshal(body []byte) (err error) {
  62. err = json.Unmarshal(body, &r)
  63. return
  64. }
  65. // Unmarshal 解析结构
  66. func (r *PayOrderInfoV3) Unmarshal(body []byte) (err error) {
  67. err = json.Unmarshal(body, &r)
  68. return
  69. }