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 +}