Skip to content

Commit

Permalink
Add Adyen Blik to Headless
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisNikolic committed Sep 27, 2024
1 parent c73f368 commit 46bd9c9
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ extension MerchantHeadlessCheckoutAvailablePaymentMethodsViewController: UITable
break
#endif
default:
redirectManager = try? PrimerHeadlessUniversalCheckout.NativeUIManager(paymentMethodType: paymentMethodType)
try? redirectManager?.showPaymentMethod(intent: sessionIntent)
do {
redirectManager = try PrimerHeadlessUniversalCheckout.NativeUIManager(paymentMethodType: paymentMethodType)
try redirectManager?.showPaymentMethod(intent: sessionIntent)
} catch {
print("\n\nMERCHANT APP\n\(#function)\nerror: \(error)")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ extension PrimerHeadlessUniversalCheckout {
case PrimerPaymentMethodType.xenditRetailOutlets.rawValue:
self.rawDataTokenizationBuilder = PrimerRawRetailerDataTokenizationBuilder(paymentMethodType: paymentMethodType)

case PrimerPaymentMethodType.adyenBlik.rawValue:
self.rawDataTokenizationBuilder = PrimerRawOTPDataTokenizationBuilder(paymentMethodType: paymentMethodType)

default:
let err = PrimerError.unsupportedPaymentMethod(paymentMethodType: paymentMethodType, userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// PrimerRawOTPDataTokenizationBuilder.swift
// PrimerSDK
//
// Created by Boris on 26.9.24..
//

// swiftlint:disable function_body_length

import Foundation

class PrimerRawOTPDataTokenizationBuilder: PrimerRawDataTokenizationBuilderProtocol {

var rawData: PrimerRawData? {
didSet {
if let rawPhoneNumberInput = self.rawData as? PrimerOTPData {
rawPhoneNumberInput.onDataDidChange = {
_ = self.validateRawData(rawPhoneNumberInput)
}
}

if let rawData = self.rawData {
_ = self.validateRawData(rawData)
}
}
}

weak var rawDataManager: PrimerHeadlessUniversalCheckout.RawDataManager?
var isDataValid: Bool = false
var paymentMethodType: String
var delegate: PrimerHeadlessUniversalCheckoutRawDataManagerDelegate?

var requiredInputElementTypes: [PrimerInputElementType] {
[.otp]
}

required init(paymentMethodType: String) {
self.paymentMethodType = paymentMethodType
}

func configure(withRawDataManager rawDataManager: PrimerHeadlessUniversalCheckout.RawDataManager) {
self.rawDataManager = rawDataManager
}

func makeRequestBodyWithRawData(_ data: PrimerRawData) -> Promise<Request.Body.Tokenization> {
return Promise { seal in

guard let paymentMethod = PrimerPaymentMethod.getPaymentMethod(withType: paymentMethodType), let paymentMethodId = paymentMethod.id else {
let err = PrimerError.unsupportedPaymentMethod(paymentMethodType: paymentMethodType, userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString)
ErrorHandler.handle(error: err)
seal.reject(err)
return
}

guard let rawData = data as? PrimerOTPData else {
let err = PrimerError.invalidValue(key: "rawData",
value: nil,
userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString)
ErrorHandler.handle(error: err)
seal.reject(err)
return
}

let sessionInfo = BlikSessionInfo(blikCode: rawData.otp,
locale: PrimerSettings.current.localeData.localeCode)

let paymentInstrument = OffSessionPaymentInstrument(
paymentMethodConfigId: paymentMethodId,
paymentMethodType: paymentMethodType,
sessionInfo: sessionInfo)

let requestBody = Request.Body.Tokenization(paymentInstrument: paymentInstrument)
seal.fulfill(requestBody)
}
}

func validateRawData(_ data: PrimerRawData) -> Promise<Void> {
return Promise { seal in
DispatchQueue.global(qos: .userInteractive).async {
var errors: [PrimerValidationError] = []

guard let rawData = data as? PrimerOTPData else {
let err = PrimerValidationError.invalidRawData(
userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString)
errors.append(err)
ErrorHandler.handle(error: err)

self.isDataValid = false

DispatchQueue.main.async {
if let rawDataManager = self.rawDataManager {
self.rawDataManager?.delegate?.primerRawDataManager?(rawDataManager,
dataIsValid: self.isDataValid,
errors: errors.count == 0 ? nil : errors)
}

seal.reject(err)
}
return
}

if !rawData.otp.isValidOTP {
errors.append(PrimerValidationError.invalidOTPCode(
message: "OTP is not valid.",
userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString))
}

if !errors.isEmpty {
let err = PrimerError.underlyingErrors(
errors: errors,
userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString)
ErrorHandler.handle(error: err)

self.isDataValid = false

DispatchQueue.main.async {
if let rawDataManager = self.rawDataManager {
self.rawDataManager?.delegate?.primerRawDataManager?(rawDataManager,
dataIsValid: self.isDataValid,
errors: errors.count == 0 ? nil : errors)
}

seal.reject(err)
}
} else {
self.isDataValid = true

DispatchQueue.main.async {
if let rawDataManager = self.rawDataManager {
self.rawDataManager?.delegate?.primerRawDataManager?(rawDataManager,
dataIsValid: self.isDataValid,
errors: errors.count == 0 ? nil : errors)
}

seal.fulfill()
}
}
}
}
}
}
// swiftlint:enable function_body_length
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,4 @@ public enum PrimerInputElementType: Int {
}
}
}
// swiftlint:enable cyclomatic_complexity
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class PrimerHeadlessUniversalCheckout: LogReporter {

private var apiConfigurationModule: PrimerAPIConfigurationModuleProtocol = PrimerAPIConfigurationModule()
private let unsupportedPaymentMethodTypes: [String] = [
PrimerPaymentMethodType.adyenBlik.rawValue,
PrimerPaymentMethodType.adyenDotPay.rawValue,
PrimerPaymentMethodType.goCardless.rawValue,
PrimerPaymentMethodType.googlePay.rawValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ class PrimerPaymentMethod: Codable, LogReporter {
case .stripeAch:
categories.append(PrimerPaymentMethodManagerCategory.nativeUI)

default:
break
case .adyenBlik:
categories.append(PrimerPaymentMethodManagerCategory.rawData)

default: break
}

return categories.isEmpty ? nil : categories
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// PrimerOTPData.swift
// PrimerSDK
//
// Created by Boris on 26.9.24..
//

import Foundation

public class PrimerOTPData: PrimerRawData {

public var otp: String {
didSet {
self.onDataDidChange?()
}
}

private enum CodingKeys: String, CodingKey {
case otp
}

public required init(
otp: String
) {
self.otp = otp
super.init()
}
}

0 comments on commit 46bd9c9

Please sign in to comment.