Skip to content

Commit

Permalink
refactor!: rm configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed May 20, 2021
1 parent ee0a76f commit 1731522
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
4 changes: 4 additions & 0 deletions v3/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (c *Client) RequestAccessToken() (*GetAccessTokenResponse, error) {
}

b, err = ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}

dest := GetAccessTokenResponse{}
if err := json.Unmarshal(b, &dest); err != nil {
return nil, err
Expand Down
13 changes: 0 additions & 13 deletions v3/option.go

This file was deleted.

26 changes: 18 additions & 8 deletions v3/payment_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type CreatePaymentCheckoutRequest struct {
Amount uint `json:"amount"`
Currency string `json:"currencyType"`
} `json:"order"`
Customer struct {
UserID string `json:"userId"`
Email string `json:"email,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
} `json:"customer"`
Type PaymentType `json:"type"`
Method []PaymentMethod `json:"method"`
StoreID string `json:"storeId"`
Expand Down Expand Up @@ -83,16 +89,20 @@ func (c *Client) CreatePaymentCheckout(
req.Order.Currency = "MYR"
}
if req.StoreID == "" {
res, err := c.GetStores(ctx)
if err != nil {
return nil, err
}
if c.storeID == "" {
res, err := c.GetStores(ctx)
if err != nil {
return nil, err
}

if len(res.Items) == 0 {
return nil, errors.New("no available store to init payment")
}
if len(res.Items) == 0 {
return nil, errors.New("no available store to init payment")
}

req.StoreID = res.Items[0].ID
req.StoreID = res.Items[0].ID
} else {
req.StoreID = c.storeID
}
}

resp := new(CreatePaymentCheckoutResponse)
Expand Down
49 changes: 32 additions & 17 deletions v3/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ var (
noopTracer = &opentracing.NoopTracer{}
)

type Config struct {
ClientID string
ClientSecret string
PrivateKey []byte
PublicKey []byte
StoreID string
Sandbox bool
TokenSource oauth2.TokenSource
}

// Client :
type Client struct {
mu sync.Mutex
Expand All @@ -47,32 +57,25 @@ type Client struct {
pk *rsa.PrivateKey
pub []byte
oauth2 oauth2.TokenSource
storeID string
}

// NewClient :
func NewClient(
clientID string,
clientSecret string,
privateKey []byte,
publicKey []byte,
sandbox bool,
opts ...Option,
) *Client {
func NewClient(cfg Config) *Client {
var (
c = new(Client)
err error
)
c.clientID = clientID
c.clientSecret = clientSecret
c.oauth2 = c
c.clientID = cfg.ClientID
c.clientSecret = cfg.ClientSecret
c.oauthEndpoint = "https://oauth.revenuemonster.my"
c.openEndpoint = "https://open.revenuemonster.my"
if sandbox {
if cfg.Sandbox {
c.oauthEndpoint = "https://sb-oauth.revenuemonster.my"
c.openEndpoint = "https://sb-open.revenuemonster.my"
}

block, _ := pem.Decode(privateKey)
block, _ := pem.Decode(cfg.PrivateKey)
if block == nil {
panic("invalid format of private key")
}
Expand All @@ -81,13 +84,25 @@ func NewClient(
if err != nil {
panic(err)
}
for _, opt := range opts {
opt(c)
c.pub = cfg.PublicKey
if cfg.TokenSource != nil {
c.oauth2 = cfg.TokenSource
} else {
c.oauth2 = c
}

// if store id is empty, get store id
if cfg.StoreID == "" {
resp, err := c.GetStores(context.Background())
if err != nil {
panic(err)
}
c.storeID = resp.Items[0].ID
}
return c
}

func (c *Client) MaybeStartSpanFromContext(ctx context.Context, operationName string) opentracing.Span {
func (c *Client) maybeStartSpanFromContext(ctx context.Context, operationName string) opentracing.Span {
var span opentracing.Span
if sp := opentracing.SpanFromContext(ctx); sp != nil {
span, _ = opentracing.StartSpanFromContext(ctx, operationName)
Expand All @@ -113,7 +128,7 @@ func (c *Client) do(
err error
)

span := c.MaybeStartSpanFromContext(ctx, operationName)
span := c.maybeStartSpanFromContext(ctx, operationName)
defer span.Finish()

defer func() {
Expand Down
13 changes: 8 additions & 5 deletions v3/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ func TestRMClient(t *testing.T) {
pub, _ := ioutil.ReadFile("../test/server_pub.pem")

client := NewClient(
"1599646279297591629",
"NekiDbnNHbHLWdRmbqtwBCqywfYkVVnE",
pk,
pub,
true,
Config{
ClientID: "1599646279297591629",
ClientSecret: "NekiDbnNHbHLWdRmbqtwBCqywfYkVVnE",
PrivateKey: pk,
PublicKey: pub,
Sandbox: true,
},
)

req := CreatePaymentCheckoutRequest{}
req.Order.ID = uniuri.NewLen(10)
req.Order.Title = "Testing #" + req.Order.ID
req.Order.Amount = 1000
req.Customer.UserID = "1234"
req.StoreID = "2808912573238362402"
req.NotifyURL = "https://www.google.com"
req.RedirectURL = "https://www.google.com"
Expand Down

0 comments on commit 1731522

Please sign in to comment.