0

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)")
            }
        }
    }
malik
  • 1
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and see: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Yrb Dec 06 '21 at 12:52
  • use storekit 2 for that very easy here is the link [link] (https://developer.apple.com/videos/play/wwdc2021/10114/) – Muhammad Ahmad Dec 07 '21 at 09:42
  • Thanks @MuhammadAhmad. I think storekit relate to in-app purchases only. I am using Apple Pay so that the merchant can get his funds directly to his account. Apple Pay uses PassKit frameworks and allows you to process payment and connect to your local bank account. – malik Dec 07 '21 at 13:47

0 Answers0