'Unable to call function after Apple Pay success SwiftUI
I have Apple Pay implemented in my SwiftUI app and I'd like to call a function after payment status returns a success. However, when I try to call the function, I run into error "Type () cannot conform to View". Relevant code:
PaymentView.swift (SwiftUI view where Apple Pay button is displayed):
@StateObject var applePayModel = ApplePayModel()
//....code....//
var body: some View {
PaymentButton(){
// Apple Pay Model
applePayModel.pay(clientSecret: PaymentConfig.shared.paymentIntentClientSecret)
}
if let paymentStatus = applePayModel.paymentStatus {
switch paymentStatus {
case .success:
postPaymentSetup() // trying to call this function!!
Text("Payment complete!")
case .error:
Text("Payment failed")
case .userCancellation:
Text("Payment canceled")
@unknown default:
Text("Unknown status")
}
}
}
ApplePayModel.swift
import Foundation
import Stripe
import PassKit
class ApplePayModel : NSObject, ObservableObject, STPApplePayContextDelegate {
@Published var paymentStatus: STPPaymentStatus?
@Published var lastPaymentError: Error?
var clientSecret: String?
func pay(clientSecret: String?) {
self.clientSecret = clientSecret
// Configure a payment request
let pr = StripeAPI.paymentRequest(withMerchantIdentifier: "merchant.com.app", country: "US", currency: "USD")
// You'd generally want to configure at least `.postalAddress` here.
// We don't require anything here, as we don't want to enter an address
// in CI.
pr.requiredShippingContactFields = []
pr.requiredBillingContactFields = []
// Configure shipping methods
pr.shippingMethods = []
// Build payment summary items
// (You'll generally want to configure these based on the selected address and shipping method.
pr.paymentSummaryItems = [
PKPaymentSummaryItem(label: "Widget)", amount: NSDecimalNumber(string: "0.00")),
PKPaymentSummaryItem(label: "Merchant", amount: NSDecimalNumber(string: "0.00")),
]
// Present the Apple Pay Context:
let applePayContext = STPApplePayContext(paymentRequest: pr, delegate: self)
applePayContext?.presentApplePay()
}
func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
// Confirm the PaymentIntent
if (self.clientSecret != nil) {
// Call the completion block with the PaymentIntent's client secret.
completion(clientSecret, nil)
} else {
completion(nil, NSError())
}
}
func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
// When the payment is complete, display the status.
self.paymentStatus = status
self.lastPaymentError = error
}
}
ApplePayModel is an observable object so I know there should be a way to check when its paymentStatus has changed. Just haven't figured out a way yet. Any ideas?
Thanks!
EDIT:
Here's the function I'm trying to call, located in PaymentView.swift:
func postPaymentSetup(){
DataService.instance.updateUser(userID: currentUserID)
// send notifications to seller
AuthService.instance.getUserOnesignalID(forUserID: selectedSellerID) { onesignalID in
OneSignal.postNotification(["contents": ["en": "Your payment has succeeded!"], "include_player_ids": ["\(onesignalID ?? "")"]])
}
showToast = true
toastMessage = "Payment Completed ✅"
isShowingSheet = false
isShowingSheet_Reservation = false
showSelectButton = false
presentationMode.wrappedValue.dismiss()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
