Skip to content

Commit

Permalink
feat(appstore): add verify function to appstore server api
Browse files Browse the repository at this point in the history
  • Loading branch information
richzw committed Nov 14, 2024
1 parent 1c60340 commit 5fa8b94
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion appstore/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"time"

"github.com/awa/go-iap/appstore/api"
"github.com/golang-jwt/jwt/v4"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ type Client struct {
httpCli *http.Client
}

// list of errore
// list of errors
var (
ErrAppStoreServer = errors.New("AppStore server error")

Expand Down Expand Up @@ -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
}

0 comments on commit 5fa8b94

Please sign in to comment.