From 5fa8b940724a6b06af10ead5062fc363916a4ce8 Mon Sep 17 00:00:00 2001 From: Wei Zang Date: Thu, 14 Nov 2024 20:15:27 +0800 Subject: [PATCH 1/2] feat(appstore): add verify function to appstore server api --- appstore/validator.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/appstore/validator.go b/appstore/validator.go index b5dbe74..1faa23b 100644 --- a/appstore/validator.go +++ b/appstore/validator.go @@ -12,6 +12,7 @@ import ( "net/http" "time" + "github.com/awa/go-iap/appstore/api" "github.com/golang-jwt/jwt/v4" ) @@ -39,7 +40,7 @@ type Client struct { httpCli *http.Client } -// list of errore +// list of errors var ( ErrAppStoreServer = errors.New("AppStore server error") @@ -214,3 +215,29 @@ func (c *Client) ParseNotificationV2WithClaim(tokenStr string, result jwt.Claims }) return err } + +// IAPAPIClient is an interface to call validation API in App Store Server API +type IAPAPIClient interface { + Verify(ctx context.Context, transactionId string) (interface{}, error) +} + +type APIClient struct { + productionCli *api.StoreClient + sandboxCli *api.StoreClient +} + +func NewAPIClient(config api.StoreConfig) *APIClient { + prodConf := config + prodConf.Sandbox = false + sandboxConf := config + sandboxConf.Sandbox = true + return &APIClient{productionCli: api.NewStoreClient(&prodConf), sandboxCli: api.NewStoreClient(&sandboxConf)} +} + +func (c *APIClient) Verify(ctx context.Context, transactionId string) (interface{}, error) { + result, err := c.productionCli.GetTransactionInfo(ctx, transactionId) + if err != nil && errors.Is(err, api.TransactionIdNotFoundError) { + result, err = c.sandboxCli.GetTransactionInfo(ctx, transactionId) + } + return result, err +} From 186ba2be090ae7a28248e580d94da8f562b76510 Mon Sep 17 00:00:00 2001 From: Wei Zang Date: Thu, 14 Nov 2024 20:23:37 +0800 Subject: [PATCH 2/2] fix(appstore): fix import cycle --- appstore/api/validator.go | 32 ++++++++++++++++++++++++++++++++ appstore/validator.go | 27 --------------------------- 2 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 appstore/api/validator.go diff --git a/appstore/api/validator.go b/appstore/api/validator.go new file mode 100644 index 0000000..4d1e6ae --- /dev/null +++ b/appstore/api/validator.go @@ -0,0 +1,32 @@ +package api + +import ( + "context" + "errors" +) + +// IAPAPIClient is an interface to call validation API in App Store Server API +type IAPAPIClient interface { + Verify(ctx context.Context, transactionId string) (interface{}, error) +} + +type APIClient struct { + productionCli *StoreClient + sandboxCli *StoreClient +} + +func NewAPIClient(config StoreConfig) *APIClient { + prodConf := config + prodConf.Sandbox = false + sandboxConf := config + sandboxConf.Sandbox = true + return &APIClient{productionCli: NewStoreClient(&prodConf), sandboxCli: NewStoreClient(&sandboxConf)} +} + +func (c *APIClient) Verify(ctx context.Context, transactionId string) (interface{}, error) { + result, err := c.productionCli.GetTransactionInfo(ctx, transactionId) + if err != nil && errors.Is(err, TransactionIdNotFoundError) { + result, err = c.sandboxCli.GetTransactionInfo(ctx, transactionId) + } + return result, err +} diff --git a/appstore/validator.go b/appstore/validator.go index 1faa23b..fc34afd 100644 --- a/appstore/validator.go +++ b/appstore/validator.go @@ -12,7 +12,6 @@ import ( "net/http" "time" - "github.com/awa/go-iap/appstore/api" "github.com/golang-jwt/jwt/v4" ) @@ -215,29 +214,3 @@ func (c *Client) ParseNotificationV2WithClaim(tokenStr string, result jwt.Claims }) return err } - -// IAPAPIClient is an interface to call validation API in App Store Server API -type IAPAPIClient interface { - Verify(ctx context.Context, transactionId string) (interface{}, error) -} - -type APIClient struct { - productionCli *api.StoreClient - sandboxCli *api.StoreClient -} - -func NewAPIClient(config api.StoreConfig) *APIClient { - prodConf := config - prodConf.Sandbox = false - sandboxConf := config - sandboxConf.Sandbox = true - return &APIClient{productionCli: api.NewStoreClient(&prodConf), sandboxCli: api.NewStoreClient(&sandboxConf)} -} - -func (c *APIClient) Verify(ctx context.Context, transactionId string) (interface{}, error) { - result, err := c.productionCli.GetTransactionInfo(ctx, transactionId) - if err != nil && errors.Is(err, api.TransactionIdNotFoundError) { - result, err = c.sandboxCli.GetTransactionInfo(ctx, transactionId) - } - return result, err -}