aporat / store-receipt-validator

PHP receipt validator for Apple iTunes, Google Play and Amazon App Store
Apache License 2.0
633 stars 153 forks source link

How to determine valid receipts in google play? #143

Closed Gameonn closed 2 years ago

Gameonn commented 3 years ago

In iTunes, we have methods like isValid() and getReceipt(), do we have similar methods in google play as well??

What are the methods to determine whether it is a test/sandbox purchase or real purchase??

Stafox commented 3 years ago

If you got a response from google api with status 200 - purchase is valid.

What kind of service provide to customer depends on your logic.

To detect sandbox purchase use method getPurchaseType().

More info here: https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptions

Gameonn commented 3 years ago

Presently I am using getPurchaseType() method but in real purchase I get null value and for sandbox I get 0 value. Rest response remains same for sandbox and production.

Is this the expected behaviour?

Sample Response:

PurchaseResponse {#211 ▼
  #response: Google_Service_AndroidPublisher_ProductPurchase {#262 ▼
    +acknowledgementState: 1
    +consumptionState: 1
    +developerPayload: "{"developerPayload":"","is_free_trial":false,"has_introductory_price_trial":false,"is_updated":false,"accountId":""}"
    +kind: "androidpublisher#productPurchase"
    +obfuscatedExternalAccountId: null
    +obfuscatedExternalProfileId: null
    +orderId: "GPA.3305-3625-0851-58138"
    +productId: null
    +purchaseState: 1
    +purchaseTimeMillis: "1607046594799"
    +purchaseToken: null
    +purchaseType: null
    +quantity: null
    +regionCode: "MX"
    #internal_gapi_mappings: []
    #modelData: []
    #processed: []
  }
  #developerPayload: array:5 [▼
    "developerPayload" => ""
    "is_free_trial" => false
    "has_introductory_price_trial" => false
    "is_updated" => false
    "accountId" => ""
  ]
}

Are there any other methods for the above response that I can execute in order to check sandbox purchase?

Stafox commented 2 years ago

Presently I am using getPurchaseType() method but in real purchase I get null value and for sandbox I get 0 value. Rest response remains same for sandbox and production.

Is this the expected behaviour?

Yes, it is also described in documentation. purchaseType=0 is about sandbox. _The type of purchase of the subscription. This field is only set if this purchase was not made using the standard in-app billing flow. Possible values are:

  1. Test (i.e. purchased from a license testing account)
  2. Promo (i.e. purchased using a promo code)_

Are there any other methods for the above response that I can execute in order to check sandbox purchase? no.