Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring Apple Pay with Swift

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Wendy Lu
October 29, 2015

Exploring Apple Pay with Swift

Swift Summit 2015, San Francisco

Wendy Lu

October 29, 2015

Transcript

48 slides
  1. Credit Cards ▸ Credit card number: 16 keystrokes ▸ Exp

    date: 5 keystrokes ▸ CVV: 3 keystrokes ▸ Address: 50+ keystrokes
  2. "Apple Pay takes longer than using a credit card if

    you count the 45 minutes of telling people about it afterwards."
  3. let request = PKPaymentRequest() request.supportedNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkDiscover] request.countryCode =

    "US" request.currencyCode = "USD" request.merchantIdentifier = "<#Replace me with your Apple Merchant ID#>" request.merchantCapabilities = .Capability3DS
  4. let subtotalItem = PKPaymentSummaryItem(label: "Subtotal", amount: NSDecimalNumber(string: "100.00")) let taxItem

    = PKPaymentSummaryItem(label: "Tax", amount: NSDecimalNumber(string: "8.74")) let totalItem = PKPaymentSummaryItem(label: "Grass-fed Jeans Inc", amount: NSDecimalNumber(string: "108.74")) let summaryItems = [subtotalItem, taxItem, totalItem] request.paymentSummaryItems = summaryItems
  5. func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didSelectShippingContact contact: PKContact, completion: (PKPaymentAuthorizationStatus, [PKShippingMethod], [PKPaymentSummaryItem])

    -> Void) { let postalCode = contact.postalAddress?.postalCode if (postalCode != nil) { // Get new tax and shipping methods completion(PKPaymentAuthorizationStatus.Success, shippingMethods, paymentSummaryItems) }
  6. Obj-C if ([stripeToken length] > 0 && [braintreeToken length] >

    0) { if (stripeError == nil) { if (braintreeError == nil) { // Handle payment tokens asynchronously [self createPaymentMethodWithStripeToken:stripeToken braintreeToken:braintreeToken success:sucess failure:failure]; } else { //failure } } else { //failure } } else { //failure }
  7. Swift guard stripeToken != nil && stripeToken!.isEmpty == false else

    { throw PaymentError.EmptyStripeToken } guard stripeError != nil else { throw PaymentError.StripeTokenError } guard braintreeToken != nil && braintreeToken!.isEmpty == false else { throw PaymentError.EmptyBraintreeToken } guard braintreeError != nil else { throw PaymentError.EmptyStripeToken } // Handle payment tokens
  8. Obj-C typedef enum { CurrencyTypeUSDollar = 0, CurrencyTypeCanadianDollar, CurrencyTypeEuro, CurrencyTypePound,

    } CurrencyType; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFormatterCurrencyStyle; switch (currency) { case CurrencyTypeUSDollar: formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"]; break; case CurrencyTypeCanadianDollar: formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_CA"]; break; case CurrencyTypeEuro: formatter.locale = [NSLocale localeWithLocaleIdentifier:@"fr_FR"]; break; case CurrencyTypePound: formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"]; break; } NSString *formattedPrice = [formatter stringFromNumber:number];
  9. Swift enum CurrencyType : String { case USDollar = "en_US",

    CanadianDollar = "en_CA", Euro = "fr_FR", Pound = "en_GB" } let formatter = NSNumberFormatter() formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle formatter.locale = NSLocale(localeIdentifier: currencyType.rawValue) let formattedPrice = formatter.stringFromNumber(number)

More Decks by Wendy Lu

Other Decks in Programming