I added Stripe to my app so that I can use Apple Pay. One problem that I am having is that I already have a view controller that shows the user the price. What I want to do is make so that when the Apple Pay pop up shows it shows the price of the price view controller. This is the code that I currently have:
import UIKit
import Stripe
import CoreLocation
class massagePriceView: UIViewController, PKPaymentAuthorizationViewControllerDelegate{
var price : Float?
var category : MassageType?
var length: massageLength?
var dateOfMassage: Date?
var physicalAddress : String = ""
fileprivate var currentLocation : CLLocation?
fileprivate let locationManager = CLLocationManager()
@IBOutlet weak var massageTypePrice: UILabel!
@IBOutlet weak var massageLengthPrice: UILabel!
@IBOutlet weak var massageScheduledTime: UILabel!
@IBOutlet weak var massagePriceTotal: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad starts")
if price != nil { massagePriceTotal.text = String(format: "$" + "%.2f", price!) }
print("price is", massagePriceTotal.text!, price!)
if category != nil {massageTypePrice.text = category!.description() }
print("category is", massageTypePrice.text!, category!.description())
if length != nil { massageLengthPrice.text = length!.description() }
print("length is", massageLengthPrice.text!, length!.description())
if dateOfMassage != nil {
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy hh:mm a"
massageScheduledTime.text = formatter.string(from: dateOfMassage!)
}
addApplePayPaymentButtonToView()
}
private func addApplePayPaymentButtonToView() {
let paymentButton = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .black)
paymentButton.translatesAutoresizingMaskIntoConstraints = false
paymentButton.addTarget(self, action: #selector(applePayButtonTapped(sender:)), for: .touchUpInside)
view.addSubview(paymentButton)
view.addConstraint(NSLayoutConstraint(item: paymentButton, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: paymentButton, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: -25))
}
@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 massage = PKPaymentSummaryItem(label: "Massage", 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 = [massage, tax, total]
let authorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request)
if let viewController = authorizationViewController {
viewController.delegate = self
present(viewController, animated: true, completion: nil)
}
}
}
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
// Let the Operating System know that the payment was accepted successfully
completion(PKPaymentAuthorizationResult(status: .success, errors: nil))
}
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
// Dismiss the Apple Pay UI
dismiss(animated: true, completion: nil)
}
@IBAction func massagePriceViewBackButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}