Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change Conforming PaymentMethods to Codable #1478

Merged
merged 13 commits into from
Jan 25, 2024
4 changes: 4 additions & 0 deletions Adyen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
81825CC22AC59C6400F91912 /* XCTestCase+RootViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81825CC02AC59C6400F91912 /* XCTestCase+RootViewController.swift */; };
81825CC42AC59C6C00F91912 /* XCTestCase+Wait.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81825CC32AC59C6C00F91912 /* XCTestCase+Wait.swift */; };
81825CC52AC59C6C00F91912 /* XCTestCase+Wait.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81825CC32AC59C6C00F91912 /* XCTestCase+Wait.swift */; };
81881BCE2B1A0A510020E3F2 /* AnyPaymentMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81881BCD2B1A0A510020E3F2 /* AnyPaymentMethod.swift */; };
81896E852A4DB5F300C532CA /* SearchViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81896E842A4DB5F300C532CA /* SearchViewControllerTests.swift */; };
8191838E2A53062F008EB61A /* FormAddressItem+Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8191838D2A53062F008EB61A /* FormAddressItem+Configuration.swift */; };
819CC3342B14C53200D2EEE9 /* PaymentMethods+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819CC3332B14C53200D2EEE9 /* PaymentMethods+Equatable.swift */; };
Expand Down Expand Up @@ -1367,6 +1368,7 @@
81825CBA2AC59C4000F91912 /* UIViewController+Search.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Search.swift"; sourceTree = "<group>"; };
81825CC02AC59C6400F91912 /* XCTestCase+RootViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "XCTestCase+RootViewController.swift"; sourceTree = "<group>"; };
81825CC32AC59C6C00F91912 /* XCTestCase+Wait.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "XCTestCase+Wait.swift"; sourceTree = "<group>"; };
81881BCD2B1A0A510020E3F2 /* AnyPaymentMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyPaymentMethod.swift; sourceTree = "<group>"; };
81896E842A4DB5F300C532CA /* SearchViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchViewControllerTests.swift; sourceTree = "<group>"; };
8191838D2A53062F008EB61A /* FormAddressItem+Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FormAddressItem+Configuration.swift"; sourceTree = "<group>"; };
819CC3332B14C53200D2EEE9 /* PaymentMethods+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PaymentMethods+Equatable.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3735,6 +3737,7 @@
E746E67327B429DF0076BB71 /* Abstract */ = {
isa = PBXGroup;
children = (
81881BCD2B1A0A510020E3F2 /* AnyPaymentMethod.swift */,
F9D5751723799BB0009C18B5 /* AnyPaymentMethodDecoder.swift */,
F9D5753023828EBB009C18B5 /* AnyCardPaymentMethod.swift */,
E9B36C9C223FDE4F00EAA368 /* PaymentMethods.swift */,
Expand Down Expand Up @@ -6458,6 +6461,7 @@
E278EB4322AA5EC800497FD5 /* IssuerListPaymentMethod.swift in Sources */,
F90FB7BC2446E8C8005BFE0E /* BrowserInfo.swift in Sources */,
A0414C1F278C27CF00DF3FE9 /* ACHDirectDebitPaymentMethod.swift in Sources */,
81881BCE2B1A0A510020E3F2 /* AnyPaymentMethod.swift in Sources */,
E787C9D1246E948F00B401E0 /* CoreFonts.swift in Sources */,
F9589D032601EE7800E4113F /* FormItemInjector.swift in Sources */,
5A15D5A1264BE1E500A8E3C7 /* PrefilledShopperInformation.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Adyen/Core/Core Protocols/PaymentMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// A payment method that is available to use.
public protocol PaymentMethod: Decodable {
public protocol PaymentMethod: Codable {

/// A string identifying the type of payment method, such as `"card"`, `"ideal"`, `"applepay"`.
var type: PaymentMethodType { get }
Expand Down
4 changes: 2 additions & 2 deletions Adyen/Core/Models/ShopperInteraction.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// Copyright (c) 2021 Adyen N.V.
// Copyright (c) 2023 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//

import Foundation

/// A type of shopper interaction during a payment.
public enum ShopperInteraction: String, Decodable {
public enum ShopperInteraction: String, Codable {

/// Indicates the shopper is present during the payment.
case shopperPresent = "Ecommerce"
Expand Down
112 changes: 112 additions & 0 deletions Adyen/Core/Payment Methods/Abstract/AnyPaymentMethod.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Copyright (c) 2023 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//

import Foundation

internal enum AnyPaymentMethod: Codable {
case storedInstant(StoredInstantPaymentMethod)
case storedCard(StoredCardPaymentMethod)
case storedPayPal(StoredPayPalPaymentMethod)
case storedBCMC(StoredBCMCPaymentMethod)
case storedBlik(StoredBLIKPaymentMethod)
case storedAchDirectDebit(StoredACHDirectDebitPaymentMethod)
case storedCashAppPay(StoredCashAppPayPaymentMethod)

case instant(PaymentMethod)
case card(AnyCardPaymentMethod)
case issuerList(IssuerListPaymentMethod)
case sepaDirectDebit(SEPADirectDebitPaymentMethod)
case bacsDirectDebit(BACSDirectDebitPaymentMethod)
case achDirectDebit(ACHDirectDebitPaymentMethod)
case applePay(ApplePayPaymentMethod)
case qiwiWallet(QiwiWalletPaymentMethod)
case weChatPay(WeChatPayPaymentMethod)
case mbWay(MBWayPaymentMethod)
case blik(BLIKPaymentMethod)
case giftcard(GiftCardPaymentMethod)
case mealVoucher(MealVoucherPaymentMethod)
case doku(DokuPaymentMethod)
case sevenEleven(SevenElevenPaymentMethod)
case econtextStores(EContextPaymentMethod)
case econtextATM(EContextPaymentMethod)
case econtextOnline(EContextPaymentMethod)
case boleto(BoletoPaymentMethod)
case affirm(AffirmPaymentMethod)
case atome(AtomePaymentMethod)
case onlineBanking(OnlineBankingPaymentMethod)
case upi(UPIPaymentMethod)
case cashAppPay(CashAppPayPaymentMethod)

case none

internal var value: PaymentMethod? {
switch self {
case let .storedCard(paymentMethod): return paymentMethod
case let .storedPayPal(paymentMethod): return paymentMethod
case let .storedBCMC(paymentMethod): return paymentMethod
case let .instant(paymentMethod): return paymentMethod
case let .storedInstant(paymentMethod): return paymentMethod
case let .storedAchDirectDebit(paymentMethod): return paymentMethod
case let .storedCashAppPay(paymentMethod): return paymentMethod
case let .card(paymentMethod): return paymentMethod
case let .issuerList(paymentMethod): return paymentMethod
case let .sepaDirectDebit(paymentMethod): return paymentMethod
case let .bacsDirectDebit(paymentMethod): return paymentMethod
case let .achDirectDebit(paymentMethod): return paymentMethod
case let .applePay(paymentMethod): return paymentMethod
case let .qiwiWallet(paymentMethod): return paymentMethod
case let .weChatPay(paymentMethod): return paymentMethod
case let .mbWay(paymentMethod): return paymentMethod
case let .blik(paymentMethod): return paymentMethod
case let .storedBlik(paymentMethod): return paymentMethod
case let .doku(paymentMethod): return paymentMethod
case let .giftcard(paymentMethod): return paymentMethod
case let .mealVoucher(paymentMethod): return paymentMethod
case let .sevenEleven(paymentMethod): return paymentMethod
case let .econtextStores(paymentMethod): return paymentMethod
case let .econtextATM(paymentMethod): return paymentMethod
case let .econtextOnline(paymentMethod): return paymentMethod
case let .boleto(paymentMethod): return paymentMethod
case let .affirm(paymentMethod): return paymentMethod
case let .atome(paymentMethod): return paymentMethod
case let .onlineBanking(paymentMethod): return paymentMethod
case let .upi(paymentMethod): return paymentMethod
case let .cashAppPay(paymentMethod): return paymentMethod
case .none: return nil
}
}

// MARK: - Decoding

internal init(from decoder: Decoder) throws {
self = AnyPaymentMethodDecoder.decode(from: decoder)
}

internal func encode(to encoder: Encoder) throws {
try value?.encode(to: encoder)
}

internal enum CodingKeys: String, CodingKey {
case type
case details
case brand
case issuers
}
}

extension AnyPaymentMethod {

init(_ paymentMethod: PaymentMethod) {
self = AnyPaymentMethodDecoder.anyPaymentMethod(from: paymentMethod)
}
}

extension PaymentMethod {

var toAnyPaymentMethod: AnyPaymentMethod {
.init(self)
}
}
Loading