Add decline code to userInfo of STPError (#112)
This commit is contained in:
parent
c745623921
commit
87a8b0b386
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue