Segue Apple Pay Button

I have an apple pay button and I wanted to know how I would segue to another view controller. The apple pay button is added programmatically. I already tried adding a performSegue and that did not work. This is the code for it once it is tapped:


@objc private func applePayButtonTapped(sender: UIButton) {
        // Cards that should be accepted
        let paymentNetworks:[PKPaymentNetwork] = [.discover, .amex, .masterCard, .visa]
        
        if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {
            let request = PKPaymentRequest()
            
            request.merchantIdentifier = "merchant.com.shiningdevelopers"
            request.countryCode = "CA"
            request.currencyCode = "CAD"
            request.supportedNetworks = paymentNetworks
            request.requiredShippingContactFields = [.name, .postalAddress]
            // This is based on using Stripe
            request.merchantCapabilities = .capability3DS
            
            let drive = PKPaymentSummaryItem(label: "Drive", amount: NSDecimalNumber(decimal: 1.00), type: .final)
            let tax = PKPaymentSummaryItem(label: "Tax", amount: NSDecimalNumber(decimal:1.00), type: .final)
            let total = PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(decimal:3.00), type: .final)
            request.paymentSummaryItems = [drive, tax, total]
            
            let authorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request)
            
            if let viewController = authorizationViewController {
                viewController.delegate = self
                
                present(viewController, animated: true, completion: nil)
            }
        }
    }

Replies

What happens, what error do you get, what message ?


It is not enough to say: it does not work.


In the following :

           let authorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request) 
             
            if let viewController = authorizationViewController { 
                viewController.delegate = self 
                 
                present(viewController, animated: true, completion: nil) 
            }


if let has no meaning here.

And it is not enough to create the instance.

What is the type of destination controller ? PKPaymentAuthorizationViewController ?

You need to instanciate the controller, from its storyboard instance

      let selectionView = storyboard?.instantiateViewController(withIdentifier: "IDPKController") as! PKPaymentAuthorizationViewController

It does not give me an error. When I tap the apple pay button the pop-up shows for the fingerprint then the popup goes away and nothing happens. I put the instance there because I followed the instructions that were provided to e by Stripe. What do you mean that I need to instantiate the controller from its storyboard?

OK, I've looked in detail at the doc, that was not the problem.


Did you check that present is called ?


Try this and tell what you get :


            if let viewController = PKPaymentAuthorizationViewController(paymentRequest: request)   { 
                viewController.delegate = self 
                 print("I will present")
                present(viewController, animated: true, completion: nil) 
            }

I am not trying to go to the apple pay view controller I am trying to go to a view controller that presents a map. I have a segue with an identifier called segueToMap. The problem I am having is that once I press the apple pay button it does what it is supposed to it just doesn't go to the new view controller when it is finished.

That's not very clear. When you answer, please take time to be more precise and complete in your answers, it is always very hard to guess what you mean.


go to the new view controller when it is finished.


Which is this viewController ?

So, what is the viewController you want to present with

present(viewController, animated: true, completion: nil)


I am trying to go to a view controller that presents a map. I have a segue with an identifier called segueToMap

Did you connect the segue to the button in IB ?

Or, where do you call the segue ?


Could you also tell if you see the result of

print("I will present")

The present(viewController, animated: true, completion: nil) is for the apple pay popup. Not the segue to the viewController that I want to go to with the map. The new view controller is called map. The segue is called "segueToMap". Yes, I see the result of the "I will present".

And what about the other questions :


I am trying to go to a view controller that presents a map. I have a segue with an identifier called segueToMap

Did you connect the segue to the button in IB ?

Or, where do you call the segue ?


Note: you should not mix an IBAction and a direct segue defined in IB.


What you could try is to call performSegue at the end of IBAction, just after

present(viewController, animated: true, completion: nil)

No, I did not connect it to the button. I made the segue by control dragging from the view controller button in the ib. The perform segue is still not working even when I use it after the present.

Please, show the complete code where the performSegue is called.

This is my code


@objc private func applePayButtonTapped(sender: UIButton) {
        // Cards that should be accepted
        let paymentNetworks:[PKPaymentNetwork] = [.discover, .amex, .masterCard, .visa]
        
        if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {
            let request = PKPaymentRequest()
            
            request.merchantIdentifier = "merchant.com.shiningdevelopers"
            request.countryCode = "CA"
            request.currencyCode = "CAD"
            request.supportedNetworks = paymentNetworks
            request.requiredShippingContactFields = [.name, .postalAddress]
            // This is based on using Stripe
            request.merchantCapabilities = .capability3DS
            
            let drive = PKPaymentSummaryItem(label: "Drive", amount: NSDecimalNumber(decimal: 1.00), type: .final)
            let tax = PKPaymentSummaryItem(label: "Tax", amount: NSDecimalNumber(decimal:1.00), type: .final)
            let total = PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(decimal:3.00), type: .final)
            request.paymentSummaryItems = [drive, tax, total]
            
            let authorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request)
            
            if let viewController = authorizationViewController {
                viewController.delegate = self
                
                present(viewController, animated: true, completion: nil)
                performSegue(withIdentifier: "segueToMap", sender: self)
            }
        }
    }