Skip to content

Instantly share code, notes, and snippets.

@philipshen
Last active September 3, 2019 17:32
Show Gist options
  • Save philipshen/22fa68011be84e30edd944b3fc2b2f44 to your computer and use it in GitHub Desktop.
Save philipshen/22fa68011be84e30edd944b3fc2b2f44 to your computer and use it in GitHub Desktop.
class PaymentAuthorizationController: NSObject {
public typealias AuthorizationHandler = (PaymentAuthorizationController, PKPayment, (PKPaymentAuthorizationResult) -> Void) -> Void
private var onAuthorize: AuthorizationHandler?
func present(paymentRequest: PKPaymentRequest, onAuthorize: @escaping AuthorizationHandler) {
self.onAuthorize = onAuthorize
let controller = PKPaymentAuthorizationController(paymentRequest: paymentRequest)
controller.delegate = self
controller.present()
}
}
extension PaymentAuthorizationController: PKPaymentAuthorizationControllerDelegate {
func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
print("Beep")
guard let onAuthorize = self.onAuthorize else {
return completion(.init(status: .failure, errors: nil))
}
completion(.init(status: .success, errors: nil))
onAuthorize(self, payment, completion)
}
func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
print("Boop")
controller.dismiss()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment