I am using swiftUI to create a coffee shop app to order from. I implemented Apple Pay payment handler object as shown in their tutorial. I am using checkout as a payment process and in the paymentAuthorizationController I send the token to checkout for processing. Everything worked fine and as expected except when the user dismiss the paymentController while the payment is processing. If the payment is successfully processed, the user is charged but the completion is false because io orderView (swiftUI View) I called the startpayment method for the paymentHandler object, and when the user dismiss it while processing, the completion of the startpayment returns false. This results in not being able to communicate to the user the results of the payment. Is there a way to access the paymentAuthorizationController completion it self to know the status of the processed payment and get the returned response from checkout?
here is the orderView
if result.canMakePayments {
ApplePaymentButton(action:{
let orderID = UUID()
orders.orderIds.append(orderID)
orders.orders[orderID] = item
orders.additions[orderID] = orderAdditions
let total = Double(orders.total())
paymentHandler.startPayment(total: total, custmName: "", order: orders) {
(success) in
if success {
showOrderSheet.toggle()
} else if paymentHandler.checkoutFailure {
self.showFailureAlert.toggle() orders.emptyBag()
}
}
}
}
here is the payment handler & paymentAuthorizationController
func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
var status = PKPaymentAuthorizationStatus.success
let errors = [Error]()
checkout.processPayment(body: payment.token.paymentData) { (success, msg, id) in
DispatchQueue.main.async {
self.failureMsg = msg
self.paymentID = id
}
if success {
self.orderObject.sendToDB(custmName: self.custmName, paymentID: self.paymentID)
self.paymentStatus = status
debugPrint("\(#fileID):\(#line): Payment Success \(msg)")
completion(PKPaymentAuthorizationResult(status: status, errors: nil))
} else {
debugPrint(self.orderObject.orders)
status = .failure
self.checkoutFailure = true
completion(PKPaymentAuthorizationResult(status: status, errors: errors))
debugPrint("\(#fileID):\(#line): Payment Failed: \(msg)")
}
}
}