alipay_client_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package gopay
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. )
  8. type List struct {
  9. BillList []fundBillListInfo `json:"bill_list"`
  10. }
  11. func TestJsonToString(t *testing.T) {
  12. list := new(List)
  13. infos := make([]fundBillListInfo, 0)
  14. infos = append(infos, fundBillListInfo{Amount: "1.0.0", FundChannel: "iguiyu"})
  15. infos = append(infos, fundBillListInfo{Amount: "2.0.2", FundChannel: "Jerry"})
  16. list.BillList = infos
  17. bs, err := json.Marshal(list)
  18. if err != nil {
  19. fmt.Println("err:", err)
  20. return
  21. }
  22. fmt.Println("string:", string(bs))
  23. }
  24. type People struct {
  25. Name string `json:"name"`
  26. Age int `json:"age"`
  27. }
  28. func TestAliPayParams(t *testing.T) {
  29. bodyMap := make(BodyMap)
  30. //people := new(People)
  31. //people.Name = "Jerry"
  32. //people.Age = 18
  33. people := make(map[string]interface{})
  34. people["name"] = "jerry"
  35. people["age"] = 18
  36. bodyMap.Set("people", people)
  37. fmt.Println("result:", bodyMap.Get("people"))
  38. }
  39. func TestVerifyAliPaySign(t *testing.T) {
  40. signData := `{"code":"10000","msg":"Success","buyer_logon_id":"854***@qq.com","buyer_pay_amount":"0.01","buyer_user_id":"2088102363632794","fund_bill_list":[{"amount":"0.01","fund_channel":"PCREDIT"}],"gmt_payment":"2019-08-29 20:14:05","invoice_amount":"0.01","out_trade_no":"GZ201901301040361012","point_amount":"0.00","receipt_amount":"0.01","total_amount":"0.01","trade_no":"2019082922001432790585537960"}`
  41. sign := "bk3SzX0CZRI811IJioS2XKQHcgMixUT8mYyGQj+vcOAQas7GIYi6LpykqqSc3m7+yvqoG0TdX/c2JjYnpw/J53JxtC2IC4vsLuIPIgghVo5qafsfSxEJ22w20RZDatI2dYqFVcj8Jp+4aesQ8zMMNw7cX9NLyk7kw3DecYeyQp+zrZMueZPqLh88Z+54G+e6QuSU++0ouqQVd4PkpPqy6YI+8MdMUX4Ve0jOQxMmYH8BC6n5ZsTH/uEaLEtzYVZdSw/xdSQ7K1SH73aEH8XbRYx6rL7RkKksrdvhezX+ThDjQ+fTWjvNFrGcg3fmqXRy2elvoalu+BQmqlkWWjEJYA=="
  42. aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp8gueNlkbiDidz6FBQEBpqoRgH8h7JtsPtYW0nzAqy1MME4mFnDSMfSKlreUomS3a55gmBopL1eF4/Km/dEnaL5tCY9+24SKn1D4iyls+lvz/ZjvUjVwxoUYBh8kkcxMZSDeDz8//o+9qZTrICVP2a4sBB8T0XmU4gxfw8FsmtoomBH1nLk3AO7wgRN2a3+SRSAmxrhIGDmF1lljSlhY32eJpJ2TZQKaWNW+7yDBU/0Wt3kQVY84vr14yYagnSCiIfqyVFqePayRtmVJDr5qvSXr51tdqs2zKZCu+26X7JAF4BSsaq4gmY5DmDTm4TohCnBduI1+bPGD+igVmtl05wIDAQAB"
  43. pKey := FormatAliPayPublicKey(aliPayPublicKey)
  44. err := verifyAliPaySign(signData, sign, "RSA2", pKey)
  45. if err != nil {
  46. fmt.Println("err:", err)
  47. }
  48. }
  49. func TestSubString(t *testing.T) {
  50. str := `{"alipay_trade_pay_response":{"code":"10000","msg":"Success","buyer_logon_id":"854***@qq.com","buyer_pay_amount":"0.01","buyer_user_id":"2088102363632794","fund_bill_list":[{"amount":"0.01","fund_channel":"PCREDIT"}],"gmt_payment":"2019-08-29 20:22:02","invoice_amount":"0.01","out_trade_no":"GZ201901301040361013","point_amount":"0.00","receipt_amount":"0.01","total_amount":"0.01","trade_no":"2019082922001432790585666965"},"sign":"DSX/wmE0nnuxQrWfJZtq0fNntcx5UYtVV35P2VZpoTC2KlIWr4eGNiXcetbb7AkI/1Tyd0+cNtcGMgB7SYzTB15/wDE0vJ+eT5ucqhNkER1kcuCC0k9OkZzU5w8wCJzOgAy52Wso9KnrwkY86mJWt3dC8DNCCi1rlf1a8bTGIBG/diJaKAgP1lGT3aW8jeGGM98zLabqDUNvck2qkgctGR49kBb0ZYmIzmY0x5goVyKnaCkcC/d1VTIIMz81mJbeqU8UZk6TqEplCC8J+dYEUj04pAO4/lwIg/YZdKj3Pz1136/+uy669Pew88+74J/u/zPsehC44PxcUk9YKmkNyw=="}`
  51. index := strings.Index(str, `":`)
  52. fmt.Println("index:", index)
  53. indexEnd := strings.Index(str, `,"sign"`)
  54. fmt.Println("indexEnd:", indexEnd)
  55. fmt.Println("sub:", str[index+2:indexEnd])
  56. }