How to check IAP Transaction error

Issue #695

Inspect SKPaymentTransaction for error. In Swift, any Error can be safely bridged into NSError there you can check errorDomain and code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private func handleFailure(_ transaction: SKPaymentTransaction) {
guard let error = transaction.error else { return }
let nsError = error as NSError
guard nsError.domain == SKError.errorDomain else { return }

switch nsError.code {
case SKError.clientInvalid.rawValue, SKError.paymentNotAllowed.rawValue:
showAlert(text: "You are not allowed to make payment.")
case SKError.paymentCancelled.rawValue:
showAlert(text: "Payment has been cancelled.")
case SKError.unknown.rawValue, SKError.paymentInvalid.rawValue:
fallthrough
default:
showAlert(text: "Something went wrong making payment.")
}
}

Comments