voltrue2 / in-app-purchase

A Node.js module for in-App-Purchase for iOS, Android, Amazon and Windows.
http://iap.gracenode.org
Other
1.05k stars 289 forks source link

validate missing documentation #303

Open anshul-kai opened 4 years ago

anshul-kai commented 4 years ago

@voltrue2 I think we should add some documentation for the validate method. I'm also happy to submit a PR for this. I'm receiving the following errors sometimes and it is unclear without investigating the code whether this means the request failed or if the receipt is invalid. In my particular case, both of these failures were for valid receipts.

{ Error: read ECONNRESET
    at TLSWrap.onread (net.js:602:25)
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read',
  validatedData: { sandbox: false, status: 1, message: 'Unknown' } }
Error: failed to validate purchase
    at handleResponse (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/apple.js:399:8)
    at done (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/apple.js:247:9)
    at _finish (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/async.js:129:9)
    at series (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/async.js:105:16)
    at _onSeries (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/async.js:124:5)
    at trySandbox (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/apple.js:189:20)
    at series (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/async.js:114:5)
    at _onSeries (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/async.js:124:5)
    at /home/app/bnirvana-server/node_modules/in-app-purchase/lib/apple.js:183:13
    at Request._callback (/home/app/bnirvana-server/node_modules/in-app-purchase/lib/apple.js:410:16)
    at Request.self.callback (/home/app/bnirvana-server/node_modules/in-app-purchase/node_modules/request/request.js:185:22)
    at Request.emit (events.js:180:13)
    at Request.emit (domain.js:422:20)
    at Request.<anonymous> (/home/app/bnirvana-server/node_modules/in-app-purchase/node_modules/request/request.js:1161:10)
    at Request.emit (events.js:180:13)
    at Request.emit (domain.js:422:20)
    at IncomingMessage.<anonymous> (/home/app/bnirvana-server/node_modules/in-app-purchase/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:272:13)
    at IncomingMessage.emit (events.js:185:15)
    at IncomingMessage.emit (domain.js:422:20)
    at endReadableNT (_stream_readable.js:1106:12)
    at process._tickCallback (internal/process/next_tick.js:178:19)

Is it safe to assume that if validate returns an error, the validation should be retried?

triplef commented 4 years ago

An ECONNRESET error happens when the TCP connection was abruptly closed by the server, e.g. because it was overloaded. I’ve recently seen these errors happen with Apple’s servers as well. Catching them and adding a one-time retry fixed these in almost all cases for me, and this seems to be a common practice in other Node modules as well. Maybe it’s something the validate() method could do internally.

anshul-kai commented 4 years ago

That's definitely a possible solution, although, this issue is to suggest better documentation for the validate method. It is unclear whether all errors are failures in the execution of the validate method or if invalid receipts sometimes throw an error too. As can be seen above, there were two separate valid receipts that ended up with two different errors.