Created
January 9, 2018 05:33
-
-
Save shiningabdul/44bde0dc0adf73c66b9caf39234c4948 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UIViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate { | |
@objc private func applePayButtonTapped(sender: UIButton) { | |
// Cards that should be accepted | |
let paymentNetworks:[PKPaymentNetwork] = [.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 tshirt = PKPaymentSummaryItem(label: "T-shirt", amount: NSDecimalNumber(decimal:1.00), type: .final) | |
let shipping = PKPaymentSummaryItem(label: "Shipping", 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 = [tshirt, shipping, 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment