service/library/payclients/wxpaycli/client_test.go

130 lines
2.7 KiB
Go
Raw Normal View History

2024-02-05 01:15:29 +08:00
package wxpaycli
import (
2024-02-26 19:28:03 +08:00
"bytes"
2024-02-05 01:15:29 +08:00
"context"
2024-02-26 19:28:03 +08:00
"encoding/json"
2024-02-05 01:15:29 +08:00
"fmt"
"os"
2024-02-05 21:55:44 +08:00
"service/app/mix/conf"
2024-02-20 23:05:24 +08:00
"service/bizcommon/util"
2024-02-05 01:15:29 +08:00
"service/library/configcenter"
"service/library/idgenerator"
"testing"
)
func TestMain(m *testing.M) {
2024-02-05 21:55:44 +08:00
cfg := new(conf.ConfigSt)
2024-02-20 23:05:24 +08:00
err := configcenter.LoadConfig("/Users/erwin/wishpalv2/service/etc/mix/mix-local.yaml", cfg)
2024-02-05 21:55:44 +08:00
if err != nil {
fmt.Printf("%v\n", err)
2024-02-05 01:15:29 +08:00
}
2024-02-05 21:55:44 +08:00
2024-05-03 02:26:48 +08:00
err = InitMulti(cfg.Wxpay)
2024-02-05 01:15:29 +08:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
m.Run()
}
func TestWxpayClient_NativePay(t *testing.T) {
cli := GetDefaultWxpayClient()
resp, err := cli.NativePay(context.Background(), &NativePayParam{
Description: "测试哈哈",
OutTradeNo: idgenerator.GenOrderId(),
TotalAmount: 1,
TimeOutSeconds: 3600 * 24,
})
if err != nil {
fmt.Println(err.Error())
return
}
2024-02-05 21:16:49 +08:00
t.Log(fmt.Sprintf("%#v", resp))
2024-02-05 01:15:29 +08:00
return
}
2024-02-20 23:05:24 +08:00
func TestWxpayClient_GetOpenIdByAuthCode(t *testing.T) {
cli := GetDefaultWxpayClient()
openid, _ := cli.GetOpenIdByAuthCode(context.Background(), "091dpIGa1VNiWG0RnnIa1wgqrW1dpIG6")
t.Log(openid)
return
}
func TestWxpayClient_JsapiPay(t *testing.T) {
cli := GetDefaultWxpayClient()
// 先获取openId
openid, err := cli.GetOpenIdByAuthCode(context.Background(), "091hyXFa1SDJXG0lBBGa1qSnw52hyXFZ")
if err != nil {
t.Log(err.Error())
return
}
// jsapi pay
p := &JsapiPayParam{
Description: "测试哈哈",
OutTradeNo: idgenerator.GenOrderId(),
TotalAmount: 1,
TimeOutSeconds: 3600 * 24,
OpenId: openid,
}
r, err := cli.JsapiPay(context.Background(), p)
if err != nil {
t.Log(err.Error())
return
}
t.Log(util.ToJson(r))
}
2024-02-26 19:28:03 +08:00
type Track struct {
XmlRequest string `json:"xmlRequest"`
}
func (t *Track) JSON() ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
// encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
func AT() {
message := Track{}
message.XmlRequest = "<car><mirror>XML</mirror></car>"
fmt.Println("Before Marshal", message)
messageJSON, _ := message.JSON()
fmt.Println("After marshal", string(messageJSON))
}
func TestWxpayClient_H5Pay(t *testing.T) {
cli := GetDefaultWxpayClient()
resp, err := cli.H5Pay(context.Background(), &H5PayParam{
Description: "hello",
OutTradeNo: idgenerator.GenOrderId(),
TotalAmount: 1,
TimeOutSeconds: 3600 * 24,
Cip: "223.104.41.0",
})
if err != nil {
t.Log(err.Error())
return
}
t.Log(resp)
}
2024-03-13 20:35:18 +08:00
func TestWxpayClient_RefundOne(t *testing.T) {
cli := GetDefaultWxpayClient()
resp, err := cli.RefundOne(context.Background(), &RefundOneParam{
2024-03-13 20:51:29 +08:00
OutTradeNo: "1767893922425614336",
RefundAmount: 4900,
2024-03-21 19:54:20 +08:00
RefundReason: "用户退款",
2024-03-13 20:35:18 +08:00
})
if err != nil {
t.Log(err.Error())
return
}
t.Log(util.ToJson(resp))
}