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

标签 swift applepay

我能够成功地从 Apple Pay 检索所有数据,包括加密的支付数据、名字、姓氏和账单地址。但是,我似乎无法弄清楚如何从 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"] = "null@sample.com"
        parameters["billTo_phoneNumber"] = "5555555555"

最佳答案

你可以试试

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

关于swift - 如何使用 Swift 从 Apple Pay 获取电子邮件和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908539/

相关文章:

xcode - 更新到 Xcode 7.3/Swift 2.2 后计时器停止工作

ios - 如果钱包中没有卡,Apple Pay 按钮的行为更可取?

ios - Swift 应用程序中的 Stripe iOS 库导致无法识别的选择器错误

Swift Block 值错误

swift - 什么时候应该使用断言和前提条件,什么时候可以使用保护语句,强制展开和错误处理?

objective-c - 在 Swift 中访问 object-c NSDictionary 值很慢

ios - 仅当水平滚动至少是垂直滚动的 2 倍时,才开始在 UITableView 中水平滚动 UIScrollView

ios - 苹果支付 : is there a difference in payment tokens created on the web vs in-app?

ios - 我们可以获得测试卡来在 BrainTree SDK 中测试 ApplePay 吗?

ios - Apple Pay 的服务器端支付 token 解密