Skip to content

Commit

Permalink
Move card post body init to BTCard parameters method, move instantiat…
Browse files Browse the repository at this point in the history
…ion of parameters
  • Loading branch information
richherrera committed Jan 15, 2025
1 parent 15f3ccd commit 8d8c768
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 117 deletions.
53 changes: 20 additions & 33 deletions Sources/BraintreeCard/BTCard.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation

#if canImport(BraintreeCore)
import BraintreeCore
#endif

/// The card tokenization request represents raw credit or debit card data provided by the customer.
/// Its main purpose is to serve as the input for tokenization.
@objcMembers public class BTCard: NSObject {
Expand Down Expand Up @@ -80,42 +84,25 @@ import Foundation

// MARK: - Internal Methods

func parameters() -> CreditCardPOSTBody.CreditCard {
var cardBody = creditCardParams()

cardBody.billingAddress = billingAddress()
cardBody.options = CreditCardPOSTBody.CreditCard.Options(validate: shouldValidate)
func parameters(apiClient: BTAPIClient) -> CreditCardPOSTBody {
var creditCardBody = CreditCardPOSTBody(card: self)

return cardBody
}

private func creditCardParams() -> CreditCardPOSTBody.CreditCard {
CreditCardPOSTBody.CreditCard(
number: number,
expirationMonth: expirationMonth,
cvv: cvv,
expirationYear: expirationYear,
cardHolderName: cardholderName
)
}

private func billingAddress() -> CreditCardPOSTBody.CreditCard.BillingAddress {
CreditCardPOSTBody.CreditCard.BillingAddress(
firstName: firstName,
lastName: lastName,
company: company,
postalCode: postalCode,
streetAddress: streetAddress,
extendedAddress: extendedAddress,
locality: locality,
region: region,
countryName: countryName,
countryCodeAlpha2: countryCodeAlpha2,
countryCodeAlpha3: countryCodeAlpha3,
countryCodeNumeric: countryCodeNumeric
var meta = CreditCardPOSTBody.Meta(
integration: apiClient.metadata.integration.stringValue,
source: apiClient.metadata.source.stringValue,
sessionId: apiClient.metadata.sessionID
)

creditCardBody.meta = meta

if authenticationInsightRequested {
creditCardBody.authenticationInsight = true
creditCardBody.merchantAccountId = merchantAccountID
}

return creditCardBody
}

func graphQLParameters() -> CreditCardGraphQLBody {
var cardBody = CreditCardGraphQLBody.Variables.Input.CreditCard(
number: number,
Expand Down
24 changes: 1 addition & 23 deletions Sources/BraintreeCard/BTCardClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import BraintreeCore
return
}
} else {
let parameters = self.clientAPIParameters(for: card)
let parameters = card.parameters(apiClient: self.apiClient)

self.apiClient.post("v1/payment_methods/credit_cards", parameters: parameters) {body, _, error in
if let error = error as NSError? {
Expand Down Expand Up @@ -136,28 +136,6 @@ import BraintreeCore
return false
}

private func clientAPIParameters(for card: BTCard) -> CreditCardPOSTBody {

var creditCardBody = CreditCardPOSTBody()

var meta = CreditCardPOSTBody.Meta(
integration: apiClient.metadata.integration.stringValue,
source: apiClient.metadata.source.stringValue,
sessionId: apiClient.metadata.sessionID
)

creditCardBody.meta = meta

if card.authenticationInsightRequested {
creditCardBody.authenticationInsight = true
creditCardBody.merchantAccountId = card.merchantAccountID
}

creditCardBody.creditCard = card.parameters()

return creditCardBody
}

// MARK: - Error Construction Methods

/// Convenience helper method for creating friendlier, more human-readable userInfo dictionaries for 422 HTTP errors
Expand Down
106 changes: 45 additions & 61 deletions Sources/BraintreeCard/CreditCardPOSTBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct CreditCardPOSTBody: Encodable {
var authenticationInsight: Bool?
var merchantAccountId: String?
var meta: Meta?
var creditCard: CreditCard?
let creditCard: CreditCard?

private var usesGraphQL: Bool

Expand All @@ -17,16 +17,16 @@ struct CreditCardPOSTBody: Encodable {
}

init(
card: BTCard,
authenticationInsight: Bool? = nil,
merchantAccountId: String? = nil,
meta: Meta? = nil,
creditCard: CreditCard? = nil,
usesGraphQL: Bool = false
) {
self.creditCard = CreditCard(card: card)
self.authenticationInsight = authenticationInsight
self.merchantAccountId = merchantAccountId
self.meta = meta
self.creditCard = creditCard
self.usesGraphQL = usesGraphQL

}
Expand All @@ -43,31 +43,26 @@ struct CreditCardPOSTBody: Encodable {
}
}

/// POST Body Model
struct CreditCard: Encodable {
var billingAddress: BillingAddress?
var number: String?
var expirationMonth: String?
var cvv: String?
var options: Options?
var expirationYear: String?
var cardHolderName: String?
let billingAddress: BillingAddress?
let number: String?
let expirationMonth: String?
let cvv: String?
let options: Options?
let expirationYear: String?
let cardHolderName: String?

init(
billingAddress: BillingAddress? = nil,
number: String?,
expirationMonth: String?,
cvv: String?,
options: Options? = nil,
expirationYear: String?,
cardHolderName: String?
card: BTCard
) {
self.billingAddress = billingAddress
self.number = number
self.cvv = cvv
self.options = options
self.expirationMonth = expirationMonth
self.expirationYear = expirationYear
self.cardHolderName = cardHolderName
self.billingAddress = BillingAddress(card: card)
self.number = card.number
self.cvv = card.cvv
self.options = Options(validate: card.shouldValidate)
self.expirationMonth = card.expirationMonth
self.expirationYear = card.expirationYear
self.cardHolderName = card.cardholderName
}

enum CodingKeys: String, CodingKey {
Expand All @@ -81,45 +76,34 @@ struct CreditCardPOSTBody: Encodable {
}

struct BillingAddress: Encodable {
var firstName: String?
var lastName: String?
var company: String?
var postalCode: String?
var streetAddress: String?
var extendedAddress: String?
var locality: String?
var region: String?
var countryName: String?
var countryCodeAlpha2: String?
var countryCodeAlpha3: String?
var countryCodeNumeric: String?
let firstName: String?
let lastName: String?
let company: String?
let postalCode: String?
let streetAddress: String?
let extendedAddress: String?
let locality: String?
let region: String?
let countryName: String?
let countryCodeAlpha2: String?
let countryCodeAlpha3: String?
let countryCodeNumeric: String?

init(
firstName: String?,
lastName: String?,
company: String?,
postalCode: String?,
streetAddress: String?,
extendedAddress: String?,
locality: String?,
region: String?,
countryName: String?,
countryCodeAlpha2: String?,
countryCodeAlpha3: String?,
countryCodeNumeric: String?
card: BTCard
) {
self.firstName = firstName
self.lastName = lastName
self.company = company
self.postalCode = postalCode
self.streetAddress = streetAddress
self.extendedAddress = extendedAddress
self.locality = locality
self.region = region
self.countryName = countryName
self.countryCodeAlpha2 = countryCodeAlpha2
self.countryCodeAlpha3 = countryCodeAlpha3
self.countryCodeNumeric = countryCodeNumeric
self.firstName = card.firstName
self.lastName = card.lastName
self.company = card.company
self.postalCode = card.postalCode
self.streetAddress = card.streetAddress
self.extendedAddress = card.extendedAddress
self.locality = card.locality
self.region = card.region
self.countryName = card.countryName
self.countryCodeAlpha2 = card.countryCodeAlpha2
self.countryCodeAlpha3 = card.countryCodeAlpha3
self.countryCodeNumeric = card.countryCodeNumeric
}

enum CodingKeys: String, CodingKey {
Expand All @@ -139,7 +123,7 @@ struct CreditCardPOSTBody: Encodable {
}

struct Options: Encodable {
var validate: Bool
let validate: Bool

init(validate: Bool) {
self.validate = validate
Expand Down

0 comments on commit 8d8c768

Please sign in to comment.