蛋蛋星球-制度模式
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.

162 line
4.6 KiB

  1. package wechat
  2. import (
  3. "crypto/sha1"
  4. "encoding/base64"
  5. "encoding/json"
  6. "testing"
  7. "github.com/go-pay/crypto/xrsa"
  8. "github.com/go-pay/gopay"
  9. "github.com/go-pay/util"
  10. "github.com/go-pay/xlog"
  11. )
  12. func TestClient_Transfer(t *testing.T) {
  13. // 初始化参数结构体
  14. bm := make(gopay.BodyMap)
  15. bm.Set("nonce_str", util.RandomString(32)).
  16. Set("partner_trade_no", util.RandomString(32)).
  17. Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8").
  18. Set("check_name", "FORCE_CHECK"). // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名
  19. Set("re_user_name", "付明明"). // 收款用户真实姓名。 如果check_name设置为FORCE_CHECK,则必填用户真实姓名
  20. Set("amount", 30). // 企业付款金额,单位为分
  21. Set("desc", "测试转账"). // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
  22. Set("spbill_create_ip", "127.0.0.1")
  23. // 企业向微信用户个人付款(不支持沙箱环境)
  24. // body:参数Body
  25. wxRsp, err := client.Transfer(ctx, bm)
  26. if err != nil {
  27. xlog.Errorf("client.Transfer(%+v),error:%+v", bm, err)
  28. return
  29. }
  30. xlog.Debug("wxRsp:", *wxRsp)
  31. }
  32. func Test_ProfitSharing(t *testing.T) {
  33. type Receiver struct {
  34. Type string `json:"type"`
  35. Account string `json:"account"`
  36. Amount int `json:"amount"`
  37. Description string `json:"description"`
  38. }
  39. // 初始化参数结构体
  40. bm := make(gopay.BodyMap)
  41. bm.Set("nonce_str", util.RandomString(32)).
  42. Set("transaction_id", "4208450740201411110007820472").
  43. Set("out_order_no", "P20150806125346")
  44. var rs []*Receiver
  45. item := &Receiver{
  46. Type: "MERCHANT_ID",
  47. Account: "190001001",
  48. Amount: 100,
  49. Description: "分到商户",
  50. }
  51. rs = append(rs, item)
  52. item2 := &Receiver{
  53. Type: "PERSONAL_OPENID",
  54. Account: "86693952",
  55. Amount: 888,
  56. Description: "分到个人",
  57. }
  58. rs = append(rs, item2)
  59. bs, _ := json.Marshal(rs)
  60. bm.Set("receivers", string(bs))
  61. wxRsp, err := client.ProfitSharing(ctx, bm)
  62. if err != nil {
  63. xlog.Errorf("client.ProfitSharingAddReceiver(%+v),error:%+v", bm, err)
  64. return
  65. }
  66. xlog.Debug("wxRsp:", wxRsp)
  67. }
  68. func Test_ProfitSharingAddReceiver(t *testing.T) {
  69. // 初始化参数结构体
  70. bm := make(gopay.BodyMap)
  71. bm.Set("nonce_str", util.RandomString(32))
  72. receiver := make(gopay.BodyMap)
  73. receiver.Set("type", "MERCHANT_ID").
  74. Set("account", "190001001").
  75. Set("name", "商户全称").
  76. Set("relation_type", "STORE_OWNER")
  77. bm.Set("receiver", receiver.JsonBody())
  78. wxRsp, err := client.ProfitSharingAddReceiver(ctx, bm)
  79. if err != nil {
  80. xlog.Errorf("client.ProfitSharingAddReceiver(%+v),error:%+v", bm, err)
  81. return
  82. }
  83. xlog.Debug("wxRsp:", wxRsp)
  84. }
  85. func Test_ProfitSharingRemoveReceiver(t *testing.T) {
  86. // 初始化参数结构体
  87. bm := make(gopay.BodyMap)
  88. bm.Set("nonce_str", util.RandomString(32))
  89. receiver := make(gopay.BodyMap)
  90. receiver.Set("type", "MERCHANT_ID").
  91. Set("account", "190001001")
  92. bm.Set("receiver", receiver.JsonBody())
  93. wxRsp, err := client.ProfitSharingRemoveReceiver(ctx, bm)
  94. if err != nil {
  95. xlog.Errorf("client.ProfitSharingRemoveReceiver(%+v),error:%+v", bm, err)
  96. return
  97. }
  98. xlog.Debug("wxRsp:", wxRsp)
  99. }
  100. func TestClient_GetRSAPublicKey(t *testing.T) {
  101. bm := make(gopay.BodyMap)
  102. bm.Set("nonce_str", util.RandomString(32)).
  103. Set("sign_type", SignType_MD5)
  104. publicKey, err := client.GetRSAPublicKey(ctx, bm)
  105. if err != nil {
  106. xlog.Error(err)
  107. return
  108. }
  109. xlog.Debugf("publicKey:%#v", publicKey)
  110. }
  111. func TestClient_PayBank(t *testing.T) {
  112. // 初始化参数结构体
  113. bm := make(gopay.BodyMap)
  114. bm.Set("partner_trade_no", mchId).
  115. Set("nonce_str", util.RandomString(32)).
  116. Set("bank_code", "1001"). // 招商银行,https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_4&index=5
  117. Set("amount", 1)
  118. // publicKey 通过 client.GetRSAPublicKey() 获取
  119. // 加密 银行账号,需要转 base64,微信解密使用的是 sha1
  120. encryptBank, err := xrsa.RsaEncryptOAEP(sha1.New(), xrsa.PKCS1, "publicKey content", []byte("621400000000567"), nil)
  121. if err != nil {
  122. xlog.Error(err)
  123. return
  124. }
  125. // 加密 银行收款人,需要转 base64,微信解密使用的是 sha1
  126. encryptName, err := xrsa.RsaEncryptOAEP(sha1.New(), xrsa.PKCS1, "publicKey content", []byte("Jerry"), nil)
  127. if err != nil {
  128. xlog.Error(err)
  129. return
  130. }
  131. bm.Set("enc_bank_no", base64.StdEncoding.EncodeToString(encryptBank)).
  132. Set("enc_true_name", base64.StdEncoding.EncodeToString(encryptName))
  133. // 企业付款到银行卡API
  134. wxRsp, err := client.PayBank(ctx, bm)
  135. if err != nil {
  136. xlog.Errorf("client.EntrustPaying(%+v),error:%+v", bm, err)
  137. return
  138. }
  139. xlog.Debug("wxRsp:", wxRsp)
  140. }