2022-08-15 04:51:07 +00:00
|
|
|
package globalcare
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/logger"
|
|
|
|
"github.com/Selly-Modules/natsio"
|
2022-08-18 10:58:22 +00:00
|
|
|
"github.com/Selly-Modules/natsio/model"
|
2022-08-28 07:18:46 +00:00
|
|
|
"github.com/Selly-Modules/natsio/subject"
|
2022-08-15 04:51:07 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"github.com/thoas/go-funk"
|
|
|
|
|
2022-08-18 10:58:22 +00:00
|
|
|
"github.com/Selly-Modules/3pl/util/base64"
|
|
|
|
"github.com/Selly-Modules/3pl/util/pjson"
|
2022-08-15 04:51:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type Client struct {
|
|
|
|
privateKey *rsa.PrivateKey
|
|
|
|
publicKey *rsa.PublicKey
|
|
|
|
env ENV
|
|
|
|
natsClient natsio.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient generate Client
|
|
|
|
// using privateKey to decrypt data from Global Care
|
|
|
|
// using publicKey to encrypt data before send to Global Care
|
|
|
|
func NewClient(env ENV, privateKey, publicKey string) (*Client, error) {
|
|
|
|
validENVs := []ENV{EnvProd, EnvDev, EnvStaging}
|
|
|
|
if !funk.Contains(validENVs, env) {
|
|
|
|
return nil, fmt.Errorf("globalcare.NewClient - invalid_env: %s", env)
|
|
|
|
}
|
|
|
|
pubKey, err := generatePublicKeyFromBytes([]byte(publicKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("globalcare.NewClient - invalid_public_key: %v", err)
|
|
|
|
}
|
|
|
|
privKey, err := generatePrivateKeyFromBytes([]byte(privateKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("globalcare.NewClient - invalid_private_key: %v", err)
|
|
|
|
}
|
|
|
|
return &Client{
|
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrder ...
|
|
|
|
func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) {
|
|
|
|
url := c.getBaseURL() + apiPathCreateOrder
|
|
|
|
data := createOrderData{
|
|
|
|
ProductCode: productCodeDefault,
|
|
|
|
ProviderID: providerIDDefault,
|
|
|
|
ProductID: productIDDefault,
|
|
|
|
PartnerID: p.PartnerOrderCode,
|
|
|
|
VehicleInfo: p.VehicleInfo,
|
|
|
|
InsuredInfo: p.InsuredInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
body := CommonRequestBody{
|
|
|
|
Signature: "", // TODO:implement
|
|
|
|
Data: base64.Encode(pjson.ToBytes(data)),
|
|
|
|
}
|
2022-08-18 10:58:22 +00:00
|
|
|
natsPayload := model.CommunicationRequestHttp{
|
2022-08-15 04:51:07 +00:00
|
|
|
ResponseImmediately: true,
|
2022-08-18 10:58:22 +00:00
|
|
|
Payload: model.HttpRequest{
|
2022-08-15 04:51:07 +00:00
|
|
|
URL: url,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Data: pjson.ToJSONString(body),
|
|
|
|
},
|
|
|
|
}
|
2022-08-28 07:18:46 +00:00
|
|
|
msg, err := c.requestNats(subject.Communication.RequestHTTP, natsPayload)
|
2022-08-15 04:51:07 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("globalcare.Client.CreateOrder", logger.LogData{
|
|
|
|
"err": err.Error(),
|
|
|
|
"payload": natsPayload,
|
|
|
|
})
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var (
|
2022-08-18 10:58:22 +00:00
|
|
|
r model.CommunicationHttpResponse
|
2022-08-15 04:51:07 +00:00
|
|
|
res CommonResponse
|
|
|
|
)
|
|
|
|
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-15 10:28:35 +00:00
|
|
|
err = r.ParseResponseData(&res)
|
2022-08-15 04:51:07 +00:00
|
|
|
return &res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrder ...
|
|
|
|
func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) {
|
|
|
|
url := c.getBaseURL() + fmt.Sprintf(apiPathGetOrder, orderCode)
|
2022-08-18 10:58:22 +00:00
|
|
|
natsPayload := model.CommunicationRequestHttp{
|
2022-08-15 04:51:07 +00:00
|
|
|
ResponseImmediately: true,
|
2022-08-18 10:58:22 +00:00
|
|
|
Payload: model.HttpRequest{
|
2022-08-15 04:51:07 +00:00
|
|
|
URL: url,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
},
|
|
|
|
}
|
2022-08-28 07:18:46 +00:00
|
|
|
msg, err := c.requestNats(subject.Communication.RequestHTTP, natsPayload)
|
2022-08-15 04:51:07 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("globalcare.Client.GetOrder", logger.LogData{
|
|
|
|
"err": err.Error(),
|
|
|
|
"payload": natsPayload,
|
|
|
|
})
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var (
|
2022-08-18 10:58:22 +00:00
|
|
|
r model.CommunicationHttpResponse
|
2022-08-15 04:51:07 +00:00
|
|
|
res CommonResponse
|
|
|
|
)
|
|
|
|
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-15 10:28:35 +00:00
|
|
|
err = r.ParseResponseData(&res)
|
2022-08-15 04:51:07 +00:00
|
|
|
return &res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) requestNats(subject string, data interface{}) (*nats.Msg, error) {
|
|
|
|
b := toBytes(data)
|
|
|
|
return c.natsClient.Request(subject, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getBaseURL() string {
|
|
|
|
return baseURLENVMapping[c.env]
|
|
|
|
}
|