Skip to content

Commit

Permalink
[V7] Update BTCard (#1443)
Browse files Browse the repository at this point in the history
* Include all properties in public init

* Make all properties internal

* Update BTCard tests

* Update BTVardClient tests

* Update Integration tests

* Update CardHelper newCard method

* Update demo app Amex View Controller

* Update CHANGELOG

* Update V7 readme

* Move property docstrings to the init method

* Add optional on postalCode docstring

* Fix code snippet

* Add the missing commas in the documentation example

* Remove optional for required properties, change boolean to let

* Make return optional for static method card helper

* Add message when user uses optional card helper method

* Fix BTCardTests

* Fix BTCardClient tests

* Fix BTCardClient integration tests

* Remove unnecessary code sample

* Address PR feedback

* Remove cvv note

* Add init with cvv

* Update amex integration test

* Update docstring and empty validation

* Update UTs
  • Loading branch information
richherrera authored Nov 19, 2024
1 parent a815b97 commit 416968f
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 331 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* Update `BTSEPADirectDebitRequest` to make all properties accessible on the initializer only vs via the dot syntax.
* BraintreeLocalPayment
* Update `BTLocalPaymentRequest` to make all properties accessible on the initializer only vs via the dot syntax.
* BraintreeCard
* BraintreeCard
* Update `BTCard` to make all properties accessible on the initializer only vs via the dot syntax.
* Remove `BTCardRequest`, use `BTCard` directly instead
* BraintreeThreeDSecure
* Update `BTThreeDSecureRequest` to make all properties accessible on the initializer only vs via the dot syntax.
Expand Down
11 changes: 6 additions & 5 deletions Demo/Application/Features/AmexViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class AmexViewController: PaymentButtonBaseViewController {
}

private func getRewards(for cardNumber: String) {
let card = BTCard()
card.number = cardNumber
card.expirationMonth = "12"
card.expirationYear = CardHelpers.generateFuture(.year)
card.cvv = "1234"
let card = BTCard(
number: cardNumber,
expirationMonth: "12",
expirationYear: CardHelpers.generateFuture(.year),
cvv: "1234"
)

progressBlock("Tokenizing Card")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class CardTokenizationViewController: PaymentButtonBaseViewController {
@objc func tappedSubmit() {
progressBlock("Tokenizing card details!")

guard let card = CardHelpers.newCard(from: cardFormView) else {
progressBlock("Fill in all the card fields.")
return
}
let cardClient = BTCardClient(apiClient: apiClient)
let card = CardHelpers.newCard(from: cardFormView)

setFieldsEnabled(false)
cardClient.tokenize(card) { nonce, error in
Expand Down
33 changes: 13 additions & 20 deletions Demo/Application/Features/Helpers/CardHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,19 @@ enum Format {

enum CardHelpers {

static func newCard(from cardFormView: BTCardFormView) -> BTCard {
let card = BTCard()

if let cardNumber = cardFormView.cardNumber {
card.number = cardNumber
}

if let expirationYear = cardFormView.expirationYear {
card.expirationYear = expirationYear
}

if let expirationMonth = cardFormView.expirationMonth {
card.expirationMonth = expirationMonth
}

if let cvv = cardFormView.cvv {
card.cvv = cvv
}

return card
static func newCard(from cardFormView: BTCardFormView) -> BTCard? {
let cardNumber = cardFormView.cardNumber ?? ""
let expirationMonth = cardFormView.expirationMonth ?? ""
let expirationYear = cardFormView.expirationYear ?? ""

guard let cvv = cardFormView.cvv else { return nil }

return BTCard(
number: cardNumber,
expirationMonth: expirationMonth,
expirationYear: expirationYear,
cvv: cvv
)
}

static func generateFuture(_ format: Format) -> String {
Expand Down
5 changes: 4 additions & 1 deletion Demo/Application/Features/ThreeDSecureViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class ThreeDSecureViewController: PaymentButtonBaseViewController {
callbackCount = 0
updateCallbackCount()

let card = CardHelpers.newCard(from: cardFormView)
guard let card = CardHelpers.newCard(from: cardFormView) else {
progressBlock("Fill in all the card fields.")
return
}
let cardClient = BTCardClient(apiClient: apiClient)

cardClient.tokenize(card) { tokenizedCard, error in
Expand Down
102 changes: 46 additions & 56 deletions IntegrationTests/BTCardClient_IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ class BTCardClient_IntegrationTests: XCTestCase {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxTokenizationKey)!
let cardClient = BTCardClient(apiClient: apiClient)
let expectation = expectation(description: "Tokenize card")

cardClient.tokenize(invalidCard()) { tokenizedCard, error in
let card = BTCard(
number: "123123",
expirationMonth: "XX",
expirationYear: "XXXX",
cvv: "1234"
)

cardClient.tokenize(card) { tokenizedCard, error in
guard let tokenizedCard else {
XCTFail("Expect a nonce to be returned")
return
Expand All @@ -27,11 +33,13 @@ class BTCardClient_IntegrationTests: XCTestCase {
func testTokenizeCard_whenCardIsInvalidAndValidationIsEnabled_failsWithExpectedValidationError() {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxClientToken)!
let cardClient = BTCardClient(apiClient: apiClient)
let card = BTCard()
card.number = "123"
card.expirationMonth = "12"
card.expirationYear = Helpers.shared.futureYear()
card.shouldValidate = true
let card = BTCard(
number: "123",
expirationMonth: "12",
expirationYear: Helpers.shared.futureYear(),
cvv: "1234",
shouldValidate: true
)

let expectation = expectation(description: "Tokenize card")

Expand All @@ -56,8 +64,15 @@ class BTCardClient_IntegrationTests: XCTestCase {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxTokenizationKey)!
let cardClient = BTCardClient(apiClient: apiClient)
let expectation = expectation(description: "Tokenize card")

cardClient.tokenize(validCard()) { tokenizedCard, error in
let card = BTCard(
number: "123",
expirationMonth: "12",
expirationYear: Helpers.shared.futureYear(),
cvv: "1234",
cardholderName: "Cookie Monster"
)

cardClient.tokenize(card) { tokenizedCard, error in
guard let tokenizedCard else {
XCTFail("Expect a nonce to be returned")
return
Expand Down Expand Up @@ -89,8 +104,13 @@ class BTCardClient_IntegrationTests: XCTestCase {
func testTokenizeCard_whenUsingTokenizationKeyAndCardHasValidationEnabled_failsWithAuthorizationError() {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxTokenizationKey)!
let cardClient = BTCardClient(apiClient: apiClient)
let card = invalidCard()
card.shouldValidate = true
let card = BTCard(
number: "123123",
expirationMonth: "XX",
expirationYear: "XXXX",
cvv: "1234",
shouldValidate: true
)

let expectation = expectation(description: "Tokenize card")
cardClient.tokenize(card) { tokenizedCard, error in
Expand All @@ -114,8 +134,14 @@ class BTCardClient_IntegrationTests: XCTestCase {
func testTokenizeCard_whenUsingClientTokenAndCardHasValidationEnabledAndCardIsValid_tokenizesSuccessfully() {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxClientToken)!
let cardClient = BTCardClient(apiClient: apiClient)
let card = validCard()
card.shouldValidate = true
let card = BTCard(
number: "4111111111111111",
expirationMonth: "12",
expirationYear: Helpers.shared.futureYear(),
cvv: "123",
cardholderName: "Cookie Monster",
shouldValidate: true
)

let expectation = expectation(description: "Tokenize card")

Expand All @@ -137,8 +163,13 @@ class BTCardClient_IntegrationTests: XCTestCase {
func testTokenizeCard_whenUsingVersionThreeClientTokenAndCardHasValidationEnabledAndCardIsValid_tokenizesSuccessfully() {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxClientTokenVersion3)!
let cardClient = BTCardClient(apiClient: apiClient)
let card = validCard()
card.shouldValidate = true
let card = BTCard(
number: "4111111111111111",
expirationMonth: "12",
expirationYear: Helpers.shared.futureYear(),
cvv: "123",
shouldValidate: true
)

let expectation = expectation(description: "Tokenize card")

Expand All @@ -156,45 +187,4 @@ class BTCardClient_IntegrationTests: XCTestCase {

waitForExpectations(timeout: 5)
}

func testTokenizeCard_withCVVOnly_tokenizesSuccessfully() {
let apiClient = BTAPIClient(authorization: BTIntegrationTestsConstants.sandboxClientTokenVersion3)!
let cardClient = BTCardClient(apiClient: apiClient)
let card = BTCard()
card.cvv = "123"

let expectation = expectation(description: "Tokenize card")

cardClient.tokenize(card) { tokenizedCard, error in
guard let tokenizedCard else {
XCTFail("Expect a nonce to be returned")
return
}

XCTAssertTrue(tokenizedCard.nonce.isValidNonce)
XCTAssertNil(error)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}

// MARK: - Private Helper Methods

func invalidCard() -> BTCard {
let card = BTCard()
card.number = "123123"
card.expirationMonth = "XX"
card.expirationYear = "XXXX"
return card
}

func validCard() -> BTCard {
let card = BTCard()
card.number = "4111111111111111"
card.expirationMonth = "12"
card.expirationYear = Helpers.shared.futureYear()
card.cardholderName = "Cookie Monster"
return card
}
}
11 changes: 6 additions & 5 deletions IntegrationTests/BraintreeAmexExpress_IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class BraintreeAmexExpress_IntegrationTests: XCTestCase {
let cardClient = BTCardClient(apiClient: apiClient)
let amexClient = BTAmericanExpressClient(apiClient: apiClient)

let card = BTCard()
card.number = "371260714673002"
card.expirationMonth = "12"
card.expirationYear = Helpers.shared.futureYear()
card.cvv = "1234"
let card = BTCard(
number: "371260714673002",
expirationMonth: "12",
expirationYear: Helpers.shared.futureYear(),
cvv: "1234"
)

do {
let tokenizedCard = try await cardClient.tokenize(card)
Expand Down
Loading

0 comments on commit 416968f

Please sign in to comment.