2015-04-28 68 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

相關問題