awa / go-iap

go-iap verifies the purchase receipt via AppStore, GooglePlayStore, AmazonAppStore and Huawei HMS.
MIT License
900 stars 249 forks source link

How to check for error code when calling GetTransactionInfo #300

Open Bevilacqua opened 1 week ago

Bevilacqua commented 1 week ago

The README explains:

If you're unsure about the environment, follow these steps:

  • Initiate a call to the endpoint using the production URL. If the call is successful, the transaction identifier is associated with the production environment.
  • If you encounter an error code 4040010, indicating a TransactionIdNotFoundError, make a call to the endpoint using the sandbox URL.
  • If this call is successful, the transaction identifier is associated with the sandbox environment. If the call fails with the same error code, the transaction identifier doesn't exist in either environment.

How do you actually check the error code? The function only returns the response (which will be nil when there is an error) and an error. The error has the code embedded in the error string but is there a straight forward way to check what error code so we know when to try again with the sandbox URL?

richzw commented 1 week ago

The errors are custom error, you could find more details from here https://github.com/awa/go-iap/blob/master/appstore/api/error.go. You could fetch the error code through ErrorCode()

@Bevilacqua

richzw commented 1 week ago

Sample codes

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

https://github.com/awa/go-iap/blob/master/appstore/api/validator.go#L26 @Bevilacqua