简体   繁体   English

如何使用Swift从Apple Pay获取电子邮件和电话号码

[英]How to get email and phone number from Apple Pay using Swift

I'm able to successfully retrieve all data from Apple Pay including encrypted payment data, first name, last name, and billing address. 我能够从Apple Pay成功检索所有数据,包括加密的付款数据,名字,姓氏和账单地址。 I cannot, however, seems to figure out how to retrieve the phone number and email address from the "contact" section displayed during Apple Pay checkout. 但是,我似乎无法弄清楚如何从Apple Pay结帐时显示的“联系人”部分中检索电话号码电子邮件地址 I need to retrieve these values so I can submit them to my downstream payment provider. 我需要检索这些值,以便可以将它们提交给我的下游付款提供商。 Please help: 请帮忙:

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"] = "null@sample.com"
        parameters["billTo_phoneNumber"] = "5555555555"

you can try 你可以试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM