微信红包 插件加载证书

This commit is contained in:
李子铭 2024-09-02 13:59:24 +08:00
parent 673859b923
commit cb3c0def58
3 changed files with 47 additions and 13 deletions

View File

@ -33,13 +33,40 @@ func wechatRedPack() {
if err != nil {
log.Fatalln(err)
}
wechatRedPackQuery()
}
func wechatRedPackOrder() {
req := &proto.OrderRequest{
Config: getWechatRedPackConf(),
Order: &proto.OrderRequest_Order{
OrderNo: "240403164049635931",
Account: "oO3vO5AxRWgTjmMD38FTvnB5Rq6o",
Amount: 0.01,
Quantity: 1,
Extra: []byte(`{"app_id":"wxbfcf9fd93f390c3a", "out_detail_no":"123456789"}`),
},
Product: &proto.OrderRequest_Product{
ProductNo: "",
Extra: []byte(`{"batch_name":"13100720242", "batch_remark":"stock_creator_mchid"}`),
},
}
res, err := instance.Order(context.Background(), wechatRedPackConf.Tag, req)
if err != nil {
log.Fatalln(err)
}
log.Printf("Order res:%+v", res)
}
func wechatRedPackQuery() {
//{status:ING order_no:"240403164049635931" trade_no:"131000508038701499912952024090256259261117" message:"成功"}
queryRequest := &proto.QueryRequest{
Config: getWechatRedPackConf(),
Order: &proto.QueryRequest_Order{
OrderNo: "",
TradeNo: "202408211517304392270563",
TradeNo: "131000508038701499912952024090256259261117",
Account: "",
Extra: []byte(`{"out_detail_no":"20240821151730439227056399842027"}`),
Extra: []byte(`{"out_detail_no":"123456789"}`),
},
}
resQuery, err := instance.Query(context.Background(), wechatRedPackConf.Tag, queryRequest)

View File

@ -28,6 +28,8 @@ func TestOrder(t *testing.T) {
Order: &proto.OrderRequest_Order{
OrderNo: "240403164049635931",
Account: "oO3vO5AxRWgTjmMD38FTvnB5Rq6o",
Quantity: 1,
Amount: 0.01,
Extra: []byte(`{"app_id":"13100720242", "out_detail_no":"stock_creator_mchid"}`),
},
Product: &proto.OrderRequest_Product{

View File

@ -31,18 +31,23 @@ func newClient(ctx context.Context, c *Server) (*core.Client, error) {
if err != nil {
return nil, fmt.Errorf("MchID[%s] load merchant private key error:%v", c.MchID, err)
}
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(c.MchID, c.MchCertificateSerialNumber, mchPrivateKey, c.MchAPIv3Key),
}
var client *core.Client
// 微信支付平台配置
payCert, err := utils.LoadCertificateWithPath(fmt.Sprintf("%s/%s", filePath, "wechat_cert.pem"))
wechatPayCertificate, err := utils.LoadCertificateWithPath(fmt.Sprintf("%s/%s", filePath, "wechat_cert.pem"))
if err != nil {
return nil, fmt.Errorf("MchID[%s] load pay certificate error:%v", c.MchID, err)
return nil, fmt.Errorf("平台证书有误:%v", err)
}
opts = append(opts, option.WithWechatPayCertificate([]*x509.Certificate{payCert}))
client, err := core.NewClient(ctx, opts...)
client, err = core.NewClient(
ctx,
option.WithMerchantCredential(
c.MchID,
c.MchCertificateSerialNumber,
mchPrivateKey,
),
option.WithWechatPayCertificate([]*x509.Certificate{wechatPayCertificate}),
)
if err != nil {
return nil, fmt.Errorf("MchID[%s] %v", c.MchID, err)
return nil, fmt.Errorf("创建 Client 失败:%v", err)
}
return client, nil
}