Skip to content

Forwarding apple pay presentation completion flag to the Stripe SDK client #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Stripe/_stpobjc_STPApplePayContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,17 @@ public class _stpobjc_APContext: NSObject {
/// @deprecated A presenting UIViewController is no longer needed. Use presentApplePay(completion:) instead.
/// - Parameters:
/// - viewController: The UIViewController instance to present the Apple Pay sheet on
/// - completion: Called after the Apple Pay sheet is presented
/// - completion: A block that is called after the sheet is presented.
/// This block is passed the following parameters: A Boolean value that indicates
/// whether the payment sheet was successfully presented.
/// true if the payment sheet was presented successfully; otherwise, false.
@objc(presentApplePayOnViewController:completion:)
@available(
*, deprecated, message: "Use `presentApplePay(completion:)` instead.",
renamed: "presentApplePay(completion:)"
)
public func presentApplePay(
on viewController: UIViewController, completion: STPVoidBlock? = nil
on viewController: UIViewController, completion: STPBoolBlock? = nil
) {
_applePayContext.presentApplePay(on: viewController, completion: completion)
}
Expand All @@ -235,17 +238,20 @@ public class _stpobjc_APContext: NSObject {
macCatalystApplicationExtension, unavailable,
message: "Use `presentApplePay(from:completion:)` in App Extensions."
)
public func presentApplePay(completion: STPVoidBlock? = nil) {
public func presentApplePay(completion: STPBoolBlock? = nil) {
_applePayContext.presentApplePay(completion: completion)
}

/// Presents the Apple Pay sheet from the specified window, starting the payment process.
/// @note This method should only be called once; create a new instance of STPApplePayContext every time you present Apple Pay.
/// - Parameters:
/// - window: The UIWindow to host the Apple Pay sheet
/// - completion: Called after the Apple Pay sheet is presented
/// - window: The UIWindow to host the Apple Pay sheet
/// - completion: A block that is called after the sheet is presented.
/// This block is passed the following parameters: A Boolean value that indicates
/// whether the payment sheet was successfully presented.
/// true if the payment sheet was presented successfully; otherwise, false.
@objc(presentApplePayFromWindow:withCompletion:)
public func presentApplePay(from window: UIWindow?, completion: STPVoidBlock? = nil) {
public func presentApplePay(from window: UIWindow?, completion: STPBoolBlock? = nil) {
_applePayContext.presentApplePay(from: window, completion: completion)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
/// Presents the Apple Pay sheet from the key window, starting the payment process.
/// @note This method should only be called once; create a new instance of STPApplePayContext every time you present Apple Pay.
/// - Parameters:
/// - completion: Called after the Apple Pay sheet is presented
/// - completion: A block that is called after the sheet is presented.
/// This block is passed the following parameters: A Boolean value that indicates
/// whether the payment sheet was successfully presented.
/// true if the payment sheet was presented successfully; otherwise, false.
@available(
iOSApplicationExtension, unavailable,
message: "Use `presentApplePay(from:completion:)` in App Extensions."
Expand All @@ -142,17 +145,20 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
macCatalystApplicationExtension, unavailable,
message: "Use `presentApplePay(from:completion:)` in App Extensions."
)
public func presentApplePay(completion: STPVoidBlock? = nil) {
public func presentApplePay(completion: STPBoolBlock? = nil) {
let window = UIApplication.shared.windows.first { $0.isKeyWindow }
self.presentApplePay(from: window, completion: completion)
}

/// Presents the Apple Pay sheet from the specified window, starting the payment process.
/// @note This method should only be called once; create a new instance of STPApplePayContext every time you present Apple Pay.
/// - Parameters:
/// - window: The UIWindow to host the Apple Pay sheet
/// - completion: Called after the Apple Pay sheet is presented
public func presentApplePay(from window: UIWindow?, completion: STPVoidBlock? = nil) {
/// - window: The UIWindow to host the Apple Pay sheet
/// - completion: A block that is called after the sheet is presented.
/// This block is passed the following parameters: A Boolean value that indicates
/// whether the payment sheet was successfully presented.
/// true if the payment sheet was presented successfully; otherwise, false.
public func presentApplePay(from window: UIWindow?, completion: STPBoolBlock? = nil) {
presentationWindow = window
guard !didPresentApplePay, let applePayController = self.authorizationController else {
assert(
Expand All @@ -172,9 +178,13 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
applePayController, UnsafeRawPointer(&kApplePayContextObjcBridgeAssociatedObjectKey), applePayContextObjCBridge,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

applePayController.present { (presented) in
applePayController.present { [weak self] isBeingPresented in
DispatchQueue.main.async {
completion?()
completion?(isBeingPresented)
if !isBeingPresented {
self?.didPresentApplePay = false
self?._end()
}
}
}
}
Expand All @@ -183,15 +193,18 @@ public class STPApplePayContext: NSObject, PKPaymentAuthorizationControllerDeleg
/// @note This method should only be called once; create a new instance of STPApplePayContext every time you present Apple Pay.
/// @deprecated A presenting UIViewController is no longer needed. Use presentApplePay(completion:) instead.
/// - Parameters:
/// - viewController: The UIViewController instance to present the Apple Pay sheet on
/// - completion: Called after the Apple Pay sheet is presented
/// - viewController: The UIViewController instance to present the Apple Pay sheet on
/// - completion: A block that is called after the sheet is presented.
/// This block is passed the following parameters: A Boolean value that indicates
/// whether the payment sheet was successfully presented.
/// true if the payment sheet was presented successfully; otherwise, false.
@objc(presentApplePayOnViewController:completion:)
@available(
*, deprecated, message: "Use `presentApplePay(completion:)` instead.",
renamed: "presentApplePay(completion:)"
)
public func presentApplePay(
on viewController: UIViewController, completion: STPVoidBlock? = nil
on viewController: UIViewController, completion: STPBoolBlock? = nil
) {
let window = viewController.viewIfLoaded?.window
presentApplePay(from: window, completion: completion)
Expand Down
3 changes: 3 additions & 0 deletions StripeApplePay/StripeApplePay/Source/Blocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import Foundation
/// An empty block, called with no arguments, returning nothing.
public typealias STPVoidBlock = () -> Void

/// An block, called with boolean argument, returning nothing.
public typealias STPBoolBlock = (Bool) -> Void

/// A block to be run with the client secret of a PaymentIntent or SetupIntent.
/// - Parameters:
/// - clientSecret: The client secret of the PaymentIntent or SetupIntent. See https://stripe.com/docs/api/payment_intents/object#payment_intent_object-client_secret
Expand Down