diff --git a/appstore/api/validator.go b/appstore/api/validator.go new file mode 100644 index 0000000..86f8538 --- /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) (*TransactionInfoResponse, 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) (*TransactionInfoResponse, 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 b5dbe74..fc34afd 100644 --- a/appstore/validator.go +++ b/appstore/validator.go @@ -39,7 +39,7 @@ type Client struct { httpCli *http.Client } -// list of errore +// list of errors var ( ErrAppStoreServer = errors.New("AppStore server error")