Skip to content

Commit

Permalink
Add convenience method for getting asset based on token
Browse files Browse the repository at this point in the history
  • Loading branch information
NQuinn27 committed Oct 23, 2024
1 parent 0085a49 commit b081bd3
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ extension PrimerHeadlessUniversalCheckout {
return UIImage(named: "\(cardNetwork.rawValue)-logo-colored", in: Bundle.primerResources, compatibleWith: nil)
}

public static func getCardNetworkAsset(tokenData: Response.Body.Tokenization.PaymentInstrumentData) -> PrimerCardNetworkAsset? {
if let network = tokenData.network {
return Self.getCardNetworkAsset(cardNetworkString: network)
} else if let binNetwork = tokenData.binData?.network {
return Self.getCardNetworkAsset(cardNetworkString: binNetwork)
} else if let firstSix = tokenData.first6Digits {
return Self.getCardNetworkAsset(for: CardNetwork(cardNumber: firstSix))
} else {
return Self.getCardNetworkAsset(for: .unknown)
}
}

public static func getCardNetworkAsset(cardNetworkString: String?) -> PrimerCardNetworkAsset? {
guard let cardNetworkString else { return nil }
return Self.getCardNetworkAsset(for: CardNetwork(cardNetworkStr: cardNetworkString))
}

public static func getCardNetworkAsset(for cardNetwork: CardNetwork) -> PrimerCardNetworkAsset? {

let assetName = "\(cardNetwork.assetName.lowercased())-card-icon-colored"
Expand Down
135 changes: 135 additions & 0 deletions Tests/Primer/Assets/AssetsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final class AssetsManagerTests: XCTestCase {
}

func testCardAssets() throws {

XCTAssertNotNil(AssetsManager.getCardNetworkAsset(for: .cartesBancaires)?.cardImage)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(for: .discover)?.cardImage)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(for: .masterCard)?.cardImage)
Expand All @@ -33,6 +34,140 @@ final class AssetsManagerTests: XCTestCase {
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(for: .maestro)?.cardImage)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(for: .mir)?.cardImage)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(for: .unknown)?.cardImage)

}

func testGetNetworkAssetForTokenData() throws {
XCTAssertEqual(AssetsManager.getCardNetworkAsset(tokenData: .visaNetworkToken)?.cardNetwork, .visa)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(tokenData: .visaNetworkToken)?.cardImage)

XCTAssertEqual(AssetsManager.getCardNetworkAsset(tokenData: .mcNetworkToken)?.cardNetwork, .masterCard)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(tokenData: .mcNetworkToken)?.cardImage)

XCTAssertEqual(AssetsManager.getCardNetworkAsset(tokenData: .cbNetworkToken)?.cardNetwork, .cartesBancaires)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(tokenData: .cbNetworkToken)?.cardImage)

XCTAssertEqual(AssetsManager.getCardNetworkAsset(tokenData: .visaBinNetworkToken)?.cardNetwork, .visa)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(tokenData: .visaBinNetworkToken)?.cardImage)

XCTAssertEqual(AssetsManager.getCardNetworkAsset(tokenData: .visafirstSixToken)?.cardNetwork, .visa)
XCTAssertNotNil(AssetsManager.getCardNetworkAsset(tokenData: .visafirstSixToken)?.cardImage)
}
}

private extension Response.Body.Tokenization.PaymentInstrumentData {
static let visaNetworkToken = Self.init(paypalBillingAgreementId: nil,
first6Digits: nil,
last4Digits: nil,
expirationMonth: nil,
expirationYear: nil,
cardholderName: nil,
network: "VISA",
isNetworkTokenized: nil,
klarnaCustomerToken: nil,
sessionData: nil,
externalPayerInfo: nil,
shippingAddress: nil,
binData: nil,
threeDSecureAuthentication: nil,
gocardlessMandateId: nil,
authorizationToken: nil,
mx: nil,
currencyCode: nil,
productId: nil,
paymentMethodConfigId: nil,
paymentMethodType: "PAYMENT_CARD",
sessionInfo: nil)

static let mcNetworkToken = Self.init(paypalBillingAgreementId: nil,
first6Digits: nil,
last4Digits: nil,
expirationMonth: nil,
expirationYear: nil,
cardholderName: nil,
network: "MASTERCARD",
isNetworkTokenized: nil,
klarnaCustomerToken: nil,
sessionData: nil,
externalPayerInfo: nil,
shippingAddress: nil,
binData: nil,
threeDSecureAuthentication: nil,
gocardlessMandateId: nil,
authorizationToken: nil,
mx: nil,
currencyCode: nil,
productId: nil,
paymentMethodConfigId: nil,
paymentMethodType: "PAYMENT_CARD",
sessionInfo: nil)

static let cbNetworkToken = Self.init(paypalBillingAgreementId: nil,
first6Digits: nil,
last4Digits: nil,
expirationMonth: nil,
expirationYear: nil,
cardholderName: nil,
network: "CARTES_BANCAIRES",
isNetworkTokenized: nil,
klarnaCustomerToken: nil,
sessionData: nil,
externalPayerInfo: nil,
shippingAddress: nil,
binData: nil,
threeDSecureAuthentication: nil,
gocardlessMandateId: nil,
authorizationToken: nil,
mx: nil,
currencyCode: nil,
productId: nil,
paymentMethodConfigId: nil,
paymentMethodType: "PAYMENT_CARD",
sessionInfo: nil)

static let visaBinNetworkToken = Self.init(paypalBillingAgreementId: nil,
first6Digits: nil,
last4Digits: nil,
expirationMonth: nil,
expirationYear: nil,
cardholderName: nil,
network: nil,
isNetworkTokenized: nil,
klarnaCustomerToken: nil,
sessionData: nil,
externalPayerInfo: nil,
shippingAddress: nil,
binData: BinData(network: "VISA"),
threeDSecureAuthentication: nil,
gocardlessMandateId: nil,
authorizationToken: nil,
mx: nil,
currencyCode: nil,
productId: nil,
paymentMethodConfigId: nil,
paymentMethodType: "PAYMENT_CARD",
sessionInfo: nil)

static let visafirstSixToken = Self.init(paypalBillingAgreementId: nil,
first6Digits: "401288",
last4Digits: nil,
expirationMonth: nil,
expirationYear: nil,
cardholderName: nil,
network: nil,
isNetworkTokenized: nil,
klarnaCustomerToken: nil,
sessionData: nil,
externalPayerInfo: nil,
shippingAddress: nil,
binData: nil,
threeDSecureAuthentication: nil,
gocardlessMandateId: nil,
authorizationToken: nil,
mx: nil,
currencyCode: nil,
productId: nil,
paymentMethodConfigId: nil,
paymentMethodType: "PAYMENT_CARD",
sessionInfo: nil)
}

0 comments on commit b081bd3

Please sign in to comment.