Skip to content

Commit

Permalink
Add events to STPApplePayContext (stripe#3514)
Browse files Browse the repository at this point in the history
## Summary
Adding unexpected state state to STPApplePayContext

## Motivation
Needs to be instrumented

## Testing
None

## Changelog
<!-- Is this a notable change that affects users? If so, add a line to
`CHANGELOG.md` and prefix the line with one of the following:
    - [Added] for new features.
    - [Changed] for changes in existing functionality.
    - [Deprecated] for soon-to-be removed features.
    - [Removed] for now removed features.
    - [Fixed] for any bug fixes.
    - [Security] in case of vulnerabilities.
-->
  • Loading branch information
wooj-stripe authored Apr 12, 2024
1 parent 802d4e0 commit d1058b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public protocol ApplePayContextDelegate: _stpinternal_STPApplePayContextDelegate
/// - seealso: ApplePayExampleViewController for an example
@objc(STPApplePayContext)
public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDelegate {

enum Error: Swift.Error {
case invalidFinalState
}
/// A special string that can be passed in place of a intent client secret to force showing success and return a PaymentState of `success`.
/// - Note: ⚠️ If provided, the SDK performs no action to complete the payment or setup - it doesn't confirm a PaymentIntent or SetupIntent or handle next actions.
/// You should only use this if your integration can't create a PaymentIntent or SetupIntent. It is your responsibility to ensure that you only pass this value if the payment or set up is successful.
Expand Down Expand Up @@ -232,7 +234,7 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
@_spi(STP) public var confirmType: ConfirmType?
// Internal state
private var paymentState: PaymentState = .notStarted
private var error: Error?
private var error: Swift.Error?
/// YES if the flow cancelled or timed out. This toggles which delegate method (didFinish or didAuthorize) calls our didComplete delegate method
private var didCancelOrTimeoutWhilePending = false
private var didPresentApplePay = false
Expand Down Expand Up @@ -450,10 +452,10 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
// MARK: - Helpers
func _completePayment(
with payment: PKPayment,
completion: @escaping (PKPaymentAuthorizationStatus, Error?) -> Void
completion: @escaping (PKPaymentAuthorizationStatus, Swift.Error?) -> Void
) {
// Helper to handle annoying logic around "Do I call completion block or dismiss + call delegate?"
let handleFinalState: ((PaymentState, Error?) -> Void) = { state, error in
let handleFinalState: ((PaymentState, Swift.Error?) -> Void) = { state, error in
switch state {
case .error:
self.paymentState = .error
Expand Down Expand Up @@ -483,7 +485,10 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
}
return
case .pending, .notStarted:
assert(false, "Invalid final state")
let errorAnalytic = ErrorAnalytic(event: .unexpectedApplePayError,
error: Error.invalidFinalState)
STPAnalyticsClient.sharedClient.log(analytic: errorAnalytic)
stpAssertionFailure("Invalid final state")
return
}
}
Expand Down Expand Up @@ -679,7 +684,7 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
}
}

func callDidCompleteDelegate(status: PaymentStatus, error: Error?) {
func callDidCompleteDelegate(status: PaymentStatus, error: Swift.Error?) {
if let delegate = self.delegate {
if let delegate = delegate as? ApplePayContextDelegate {
delegate.applePayContext(self, didCompleteWith: status, error: error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ import Foundation
case unexpectedPaymentSheetFormFactoryError = "unexpected_error.paymentsheet.formfactory"
case unexpectedStripeUICoreAddressSpecProvider = "unexpected_error.stripeuicore.addressspecprovider"
case unexpectedStripeUICoreBSBNumberProvider = "unexpected_error.stripeuicore.bsbnumberprovider"
case unexpectedApplePayError = "unexpected_error.applepay"
case unexpectedPaymentSheetError = "unexpected_error.paymentsheet"
case unexpectedCustomerSheetError = "unexpected_error.customersheet"
case unexpectedPaymentSheetConfirmationError = "unexpected_error.paymentsheet.confirmation"
Expand Down

0 comments on commit d1058b7

Please sign in to comment.