Skip to content

Commit

Permalink
Merge branch 'feature/SP-60-konfiguration-av-betalmenyn' into 'featur…
Browse files Browse the repository at this point in the history
…e/native-payments'

Extended support for web based SDK controller configuration and instrument mode payments

See merge request swedbank-pay/swedbank-pay-sdk-ios!14
  • Loading branch information
alleus committed Nov 14, 2024
2 parents 86a85a0 + 1b1de4f commit e710889
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 88 deletions.
35 changes: 14 additions & 21 deletions SwedbankPaySDK/Classes/Api/Models/MethodBaseModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ enum MethodBaseModel: Codable, Equatable, Hashable {
case swish(prefills: [SwedbankPaySDK.SwishMethodPrefillModel]?, operations: [OperationOutputModel]?)
case creditCard(prefills: [SwedbankPaySDK.CreditCardMethodPrefillModel]?, operations: [OperationOutputModel]?, cardBrands: [String]?)
case applePay(operations: [OperationOutputModel]?, cardBrands: [String]?)

case unknown(String)
case webBased(paymentMethod: String)

private enum CodingKeys: String, CodingKey {
case instrument, prefills, operations, cardBrands
case paymentMethod, prefills, operations, cardBrands
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let type = try container.decode(String.self, forKey: .instrument)
switch type {
let paymentMethod = try container.decode(String.self, forKey: .paymentMethod)
switch paymentMethod {
case "Swish":
self = .swish(
prefills: try? container.decode([SwedbankPaySDK.SwishMethodPrefillModel]?.self, forKey: CodingKeys.prefills),
Expand All @@ -48,7 +47,7 @@ enum MethodBaseModel: Codable, Equatable, Hashable {
cardBrands: try? container.decode([String]?.self, forKey: CodingKeys.cardBrands)
)
default:
self = .unknown(type)
self = .webBased(paymentMethod: paymentMethod)
}
}

Expand All @@ -65,8 +64,8 @@ enum MethodBaseModel: Codable, Equatable, Hashable {
case .applePay(let operations, let cardBrands):
try container.encode(operations)
try container.encode(cardBrands)
case .unknown(let type):
try container.encode(type)
case .webBased(let paymentMethod):
try container.encode(paymentMethod)
}
}

Expand All @@ -78,8 +77,8 @@ enum MethodBaseModel: Codable, Equatable, Hashable {
return "CreditCard"
case .applePay:
return "ApplePay"
case .unknown:
return "Unknown"
case .webBased(let paymentMethod):
return paymentMethod
}
}

Expand All @@ -91,16 +90,10 @@ enum MethodBaseModel: Codable, Equatable, Hashable {
return opertations
case .applePay(let operations, _):
return operations
case .unknown:
case .webBased:
return nil
}
}

var isUnknown: Bool {
if case .unknown = self { return true }

return false
}
}

extension Sequence where Iterator.Element == MethodBaseModel
Expand All @@ -121,18 +114,18 @@ extension SwedbankPaySDK {

case applePay

case webBased(identifier: String)
case webBased(paymentMethod: String)

var identifier: String {
public var paymentMethod: String {
switch self {
case .swish:
return "Swish"
case .creditCard:
return "CreditCard"
case .applePay:
return "ApplePay"
case .webBased(identifier: let identifier):
return identifier
case .webBased(let paymentMethod):
return paymentMethod
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion SwedbankPaySDK/Classes/Api/Models/PaymentAttemptInstrument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension SwedbankPaySDK {
case applePay(merchantIdentifier: String)
case newCreditCard(enabledPaymentDetailsConsentCheckbox: Bool)

var identifier: String {
var paymentMethod: String {
switch self {
case .swish:
return "Swish"
Expand All @@ -32,5 +32,14 @@ extension SwedbankPaySDK {
return "ApplePay"
}
}

var instrumentModeRequired: Bool {
switch self {
case .newCreditCard:
return true
case .swish, .applePay, .creditCard:
return false
}
}
}
}
21 changes: 21 additions & 0 deletions SwedbankPaySDK/Classes/Api/Models/PaymentSessionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation
struct PaymentSessionModel: Codable, Hashable {
let culture: String?
let methods: [MethodBaseModel]?
let settings: SettingsModel?
let urls: UrlsModel?
let instrumentModePaymentMethod: String?
}
Expand All @@ -38,6 +39,22 @@ extension PaymentSessionModel {

return allOperations
}

var allPaymentMethods: [String] {
return methods?.compactMap({$0.name}) ?? []
}

var restrictedToInstruments: [String]? {
guard let methods = methods, let settings = settings else {
return nil
}

if allPaymentMethods.sorted() == settings.enabledPaymentMethods.sorted() {
return nil
} else {
return allPaymentMethods
}
}
}

struct UrlsModel: Codable, Hashable {
Expand All @@ -47,3 +64,7 @@ struct UrlsModel: Codable, Hashable {
let hostUrls: [URL]?
let termsOfServiceUrl: URL?
}

struct SettingsModel: Codable, Hashable {
let enabledPaymentMethods: [String]
}
36 changes: 21 additions & 15 deletions SwedbankPaySDK/Classes/Api/SwedbankPayAPIEnpointRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ enum EnpointRouter {
case preparePayment
case acknowledgeFailedAttempt
case abortPayment
case applePay(paymentPayload: String)
case customizePayment(instrument: SwedbankPaySDK.PaymentAttemptInstrument?)
case attemptPayload(paymentPayload: String)
case customizePayment(instrument: SwedbankPaySDK.PaymentAttemptInstrument?, paymentMethod: String?, restrictToPaymentMethods: [String]?)
case failPaymentAttempt(problemType: String, errorCode: String?)
}

Expand All @@ -50,7 +50,7 @@ struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
var body: [String: Any?]? {
switch endpoint.router {
case .expandMethod(instrument: let instrument):
return ["instrumentName": instrument.identifier]
return ["paymentMethod": instrument.paymentMethod]
case .startPaymentAttempt(let instrument, let culture):
switch instrument {
case .swish(let msisdn):
Expand Down Expand Up @@ -119,24 +119,30 @@ struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
"client": ["userAgent": SwedbankPaySDK.VersionReporter.userAgent,
"ipAddress": NetworkStatusProvider.getAddress(for: .wifi) ?? NetworkStatusProvider.getAddress(for: .cellular) ?? ""],
]
case .applePay(let paymentPayload):
return ["instrument": "ApplePay",
case .attemptPayload(let paymentPayload):
return ["paymentMethod": "ApplePay",
"paymentPayload": paymentPayload]
case .customizePayment(let instrument):
guard let instrument = instrument else {
return ["paymentMethod": nil]
}
case .customizePayment(let instrument, let paymentMethod, let restrictToPaymentMethods):

switch instrument {
case .newCreditCard(let enabledPaymentDetailsConsentCheckbox):
switch (instrument, paymentMethod, restrictToPaymentMethods) {
case (nil, nil, let restrictToPaymentMethods?):
return ["paymentMethod": nil,
"restrictToPaymentMethods": restrictToPaymentMethods.isEmpty ? nil : restrictToPaymentMethods]
case (.newCreditCard(let enabledPaymentDetailsConsentCheckbox), _, _):
return ["paymentMethod": "CreditCard",
"restrictToPaymentMethods": nil,
"hideStoredPaymentOptions": true,
"showConsentAffirmation" : enabledPaymentDetailsConsentCheckbox,
]
case .swish,
.creditCard,
.applePay:
return ["paymentMethod": instrument.identifier]
case (nil, let paymentMethod?, nil):
return ["paymentMethod": paymentMethod,
"restrictToPaymentMethods": nil]
case (let instrument?, nil, nil):
return ["paymentMethod": instrument.paymentMethod,
"restrictToPaymentMethods": nil]
default:
return ["paymentMethod": nil,
"restrictToPaymentMethods": nil]
}
case .failPaymentAttempt(let problemType, let errorCode):
return ["problemType": problemType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ extension SwedbankPayAuthorization: PKPaymentAuthorizationControllerDelegate {
func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
let paymentPayload = payment.token.paymentData.base64EncodedString()

let router = EnpointRouter.applePay(paymentPayload: paymentPayload)
let router = EnpointRouter.attemptPayload(paymentPayload: paymentPayload)

SwedbankPayAPIEnpointRouter(endpoint: Endpoint(router: router, href: operation?.href, method: operation?.method),
sessionStartTimestamp: Date()).makeRequest { result in
Expand Down
Loading

0 comments on commit e710889

Please sign in to comment.