截至2019年7月的Braintree SDK开发人员文档提供了以下标准:

let braintreeClient = BTAPIClient(authorization: "<#CLIENT_AUTHORIZATION#>")!
let cardClient = BTCardClient(apiClient: braintreeClient)
let card = BTCard(number: "4111111111111111", expirationMonth: "12", expirationYear: "2018", cvv: nil)
cardClient.tokenizeCard(card) { (tokenizedCard, error) in
    // Communicate the tokenizedCard.nonce to your server, or handle error
}


但是,当初始化需要邮政编码时,该类会接受NSDictionary参数。问题是密钥与Braintree SDK属性不匹配

我用过:

let cardParameters: [String: Any] = [number:"4111111111111111",expirationMonth: "12", expirationYear: "2018", cvv: "111", postalCode: "94107"]
let card = BTCard.init(parameters: cardParameters)

The errors say : "Must provide postal code" or "cvv must be provided"

最佳答案

诀窍是使用内置属性初始化BTCard对象并对其进行匹配,而不是使用NSDictionary。所以这有效:

let braintreeClient = BTAPIClient(authorization: "<#CLIENT_AUTHORIZATION#>")!
let cardClient = BTCardClient(apiClient: braintreeClient)
let card = BTCard.init()
card.number = "4111111111111111"
card.expirationMonth = "12"
card.expirationYear = "2018"
card.cvv = "111"
card.postalCode = "94107"

cardClient.tokenizeCard(card) { (tokenizedCard, error) in
    // Communicate the tokenizedCard.nonce to your server, or handle error
}


这样BTCard对象返回可接受的密钥...这花了我几个小时尝试不同的方法来获得答案。 Braintree和他们的文档没有提供此示例,但这是它对我有效的唯一方法。

(您的每个后端都可以在将此数据发送到Braintree之前对其进行整理,但这在您进行集成时会有所帮助)

10-07 19:41
查看更多