Skip to content

Commit

Permalink
Fix unwrapping of money in mapper class.
Browse files Browse the repository at this point in the history
Fix error reporting on cancel payment.
  • Loading branch information
plinio-square committed Jan 23, 2025
1 parent adfb9c7 commit b0a60e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions ios/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SquareMobilePaymentsSDK

private let SquareErrorDebugCodeKey = "com.squareup.MobilePaymentsSDK.ErrorDebugCode"
private let SquareErrorDebugMessageKey = "com.squareup.MobilePaymentsSDK.ErrorDebugMessage"
private let SquareErrorPaymentDomain = "MobilePaymentsSDKAPI.PaymentError"
// React Native error keys. These will match Android's so you can implement consistent error handling on RN
private let ReactNativeErrorDebugCodeKey = "debugCode"
private let ReactNativeErrorDebugMessageKey = "debugMessage"
Expand Down Expand Up @@ -36,4 +37,8 @@ extension NSError {
newUserInfo[ReactNativeErrorDetailsKey] = nil
return NSError(domain: domain, code: code, userInfo: newUserInfo)
}

class func initSquareError(customMessage: [String: Any]) -> NSError {
return NSError(domain: SquareErrorPaymentDomain, code: 0, userInfo: [SquareErrorDebugMessageKey: customMessage])
}
}
6 changes: 3 additions & 3 deletions ios/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ class Mappers {
}

class func mapToDictionary(money: MoneyAmount?) -> NSDictionary? {
guard let aMoney = money else {
guard let money else {
return nil
}
return [
"amount": aMoney.amount,
"currencyCode": aMoney.currency.currencyCode
"amount": money.amount,
"currencyCode": money.currency.currencyCode
]
}

Expand Down
7 changes: 6 additions & 1 deletion ios/MobilePaymentsSdkReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ extension MobilePaymentsSdkReactNative: PaymentManagerDelegate {
}

func paymentManager(_ paymentManager: PaymentManager, didCancel payment: Payment) {
startPaymentRejectBlock?("PAYMENT_CANCELED", "The payment has been canceled.", nil);
if let paymentDictionary = Mappers.mapToDictionary(payment: payment) as? [String: Any] {
let error = NSError.initSquareError(customMessage: paymentDictionary)
startPaymentRejectBlock?("PAYMENT_CANCELED", "The payment has been canceled.", error.reactNativeError);
} else {
startPaymentRejectBlock?("PAYMENT_CANCELED", "The payment has been canceled.", nil);
}
paymentHandle = nil
}
}

0 comments on commit b0a60e8

Please sign in to comment.