Issue #856
Use PKPaymentRequest
and PKPaymentAuthorizationViewController
@MainActor
final class WalletViewModel: NSObject, ObservableObject {
var canMakePayments: Bool {
PKPaymentAuthorizationViewController.canMakePayments()
}
func showApplePay(amount: Amount, from window: UIWindow) {
let request = PKPaymentRequest()
request.supportedNetworks = [PKPaymentNetwork.amex, .discover, .masterCard, .visa]
request.countryCode = "US"
request.currencyCode = "USD"
request.merchantIdentifier = "merchant.\(Bundle.main.bundleIdentifier!)"
request.merchantCapabilities = .capability3DS
let item = PKPaymentSummaryItem(label: "Add Cash", amount: amount.toNsDecimal)
request.paymentSummaryItems = [item]
guard
let vc = PKPaymentAuthorizationViewController(paymentRequest: request)
else { return }
vc.delegate = self
window.rootViewController?.present(vc, animated: true, completion: nil)
}
}
extension WalletViewModel: PKPaymentAuthorizationViewControllerDelegate {
func paymentAuthorizationViewControllerDidFinish(
_ controller: PKPaymentAuthorizationViewController
) {
controller.dismiss(animated: true, completion: nil)
}
func paymentAuthorizationViewController(
_ controller: PKPaymentAuthorizationViewController,
didAuthorizePayment payment: PKPayment
) async -> PKPaymentAuthorizationResult {
PKPaymentAuthorizationResult(status: .success, errors: nil)
}
}