2015-04-28 64 views
2

我能够成功检索来自Apple Pay的所有数据,包括加密的付款数据,名字,姓氏和帐单地址。然而,我看不出如何从Apple Pay结帐期间显示的“联系人”部分检索电话号码电子邮件地址。我需要检索这些值,以便将它们提交给我的下游支付提供商。请帮助:如何使用Swift从Apple Pay获取电子邮件和电话号码

extension ViewController: PKPaymentAuthorizationViewControllerDelegate { 

    /* USER HAS AUTHORIZED PAYMENT */ 
    func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) { 

     //Declare a dictionary of request parameters 
     var parameters = Dictionary<String, String>() 

     //Get Encrypted Payment Data from AP 
     parameters["encryptedPayment_data"] = payment.token.paymentData.base64EncodedStringWithOptions(nil) 

     let billingAddress: ABRecord = payment.billingAddress 

     parameters["billTo_firstName"] = ABRecordCopyValue(billingAddress, kABPersonFirstNameProperty).takeRetainedValue() as? String 
     parameters["billTo_lastName"] = ABRecordCopyValue(billingAddress, kABPersonLastNameProperty).takeRetainedValue() as? String 

     //Extract billing address from ABRecord format and assign accordingly 
     let addressProperty: ABMultiValueRef = ABRecordCopyValue(billingAddress, kABPersonAddressProperty).takeUnretainedValue() as ABMultiValueRef 

     if let dict: NSDictionary = ABMultiValueCopyValueAtIndex(addressProperty, 0).takeUnretainedValue() as? NSDictionary { 
      parameters["billTo_street1"] = dict[String(kABPersonAddressStreetKey)] as? String 
      parameters["billTo_city"] = dict[String(kABPersonAddressCityKey)] as? String 
      parameters["billTo_state"] = dict[String(kABPersonAddressStateKey)] as? String 
      parameters["billTo_postalCode"] = dict[String(kABPersonAddressZIPKey)] as? String 
      parameters["billTo_country"] = dict[String(kABPersonAddressCountryKey)] as? String //"United States" 
     } 

     //Can't figure out how to obtain these from Apple Pay! 
     parameters["billTo_email"] = "[email protected]" 
     parameters["billTo_phoneNumber"] = "5555555555" 

回答

0

你可以尝试

let emails: ABMultiValueRef = ABRecordCopyValue(billingAddress, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef 
    if ABMultiValueGetCount(emails) > 0 { 
    let email = ABMultiValueCopyValueAtIndex(emails, 0).takeRetainedValue() as String 
    NSLog(email) 
    } 
+0

谢谢你的回复,然而,在码结果的第一行“致命错误:意外发现零而展开的可选值(LLDB)” 。我解释这意味着kABPersonEmailProperty在Apple Pay返回的payment.billingAddress中为零。电子邮件和手机可能还会暴露在哪里? – Jeff

+0

忘了提及我将帐单和送货地址设置为必需,并且它们在AP签出过程中出现:request.requiredBillingAddressFields = PKAddressField.All; request.requiredShippingAddressFields = PKAddressField.All; – Jeff

+0

情节变厚...我可以使用payment.shippingAddress中的代码获取电子邮件,但payment.billingAddress返回nil。我想知道苹果是否故意阻止它回来? – Jeff

相关问题