awa / go-iap

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

Apple receipt field app_item_id type mismatch #44

Closed kokteyldev closed 6 years ago

kokteyldev commented 7 years ago

As stated here Apple Receipt Fields "app_item_id" field should be a string but in the library it is mapped to int64. This causes an error during json decoding.

EDIT: Apparently Apple is giving out different responses some has app_item_id as string and some has it as a number. In this case a custom type that implements Unmarshaler interface would be better suited for this. Something like the following:

    Receipt struct {
        ReceiptType                string        `json:"receipt_type"`
        AdamID                     int64         `json:"adam_id"`
        AppItemID                  NumericString `json:"app_item_id"`
        BundleID                   string        `json:"bundle_id"`
        ApplicationVersion         string        `json:"application_version"`
        DownloadID                 int64         `json:"download_id"`
        OriginalApplicationVersion string        `json:"original_application_version"`
        InApp                      []InApp       `json:"in_app"`
        RequestDate
        OriginalPurchaseDate
    }

        func (n NumericString) UnmarshalJSON(data []byte) error {
            n = NumericString(string(data))
            return nil
        }
jun06t commented 6 years ago

"app_item_id" field is string

I guess your receipt is iOS 6 style. This library supports the receipt style for iOS7 or above. However this library doesn't enforce our receipt definition on you, because I implemented the response as interface{}

https://github.com/dogenzaka/go-iap/blob/8266e878ac53f89fbd6528323a34ecc0f783a149/appstore/validator.go#L109-L126

So you can use your original receipt definition to solve it.

wasedaigo commented 6 years ago

I got an error in production. my phone is iOS11. For now, I turned off the validation It seemed to work fine with sandbox and beta... I am confused

Failed ValidateIosReceipt: json: cannot unmarshal string into Go struct field Receipt.app_item_id of type int64
wasedaigo commented 6 years ago

@jun06t Great fix! But would you mind explaining the background of this issue a bit? Did Apple change anything recently?