Add decline code to userInfo of STPError (#112)

This commit is contained in:
Yuki 2021-03-15 16:55:56 -07:00 committed by GitHub
parent c745623921
commit 87a8b0b386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -48,6 +48,10 @@ public class STPError: NSObject {
/// The error type returned by the Stripe API.
/// - seealso: https://stripe.com/docs/api#errors-type
@objc public static let stripeErrorTypeKey = "com.stripe.lib:StripeErrorTypeKey"
/// If the value of `userInfo[stripeErrorCodeKey]` is `STPError.cardDeclined`,
/// the value for this key contains the decline code.
/// - seealso: https://stripe.com/docs/declines/codes
@objc public static let stripeDeclineCodeKey = "com.stripe.lib:DeclineCodeKey"
/// The card number is not a valid credit card number.
@objc public static let invalidNumber = STPCardErrorCode.invalidNumber.rawValue
@ -186,6 +190,10 @@ public enum STPCardErrorCode: String {
let cardErrorCode = codeMapEntry?["code"]
let localizedMessage = codeMapEntry?["message"]
if let cardErrorCode = cardErrorCode {
if cardErrorCode == STPCardErrorCode.cardDeclined.rawValue,
let decline_code = errorDictionary["decline_code"] {
userInfo[STPError.stripeDeclineCodeKey] = decline_code
}
userInfo[STPError.cardErrorCodeKey] = cardErrorCode
}
if localizedMessage != nil {

View File

@ -123,4 +123,24 @@ class StripeErrorTest: XCTestCase {
XCTAssertEqual(
error.userInfo[STPError.errorMessageKey] as? String, response["error"]!["message"])
}
func testCardDeclinedError() {
let response = [
"error": [
"type": "card_error",
"message": "Your card has insufficient funds.",
"code": "card_declined",
"decline_code": "insufficient_funds",
]
]
guard let error = NSError.stp_error(fromStripeResponse: response) else {
XCTFail()
return
}
XCTAssertEqual(error.domain, STPError.stripeDomain)
XCTAssertEqual(error.code, STPErrorCode.cardError.rawValue)
XCTAssertEqual(error.userInfo[STPError.cardErrorCodeKey] as? String, STPCardErrorCode.cardDeclined.rawValue)
XCTAssertEqual(error.userInfo[NSLocalizedDescriptionKey] as? String, NSError.stp_cardErrorDeclinedUserMessage())
XCTAssertEqual(error.userInfo[STPError.stripeDeclineCodeKey] as? String, "insufficient_funds")
}
}