Skip to content

Commit

Permalink
Merge branch 'feature/SP-70-Ändring-av-format-för-timeZoneOffset' int…
Browse files Browse the repository at this point in the history
…o 'feature/native-payments'

SP-70 Ändring av format för timeZoneOffset

See merge request swedbank-pay/swedbank-pay-sdk-ios!5
  • Loading branch information
mbalsiger committed Sep 6, 2024
2 parents 2558b96 + 8107429 commit ea726e2
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ extension TimeZone {
localTimeZoneFormatter.dateFormat = "Z"
return localTimeZoneFormatter.string(from: Date())
}

func minutesFromGMT() -> String {
let minutes = (secondsFromGMT() / 60)
return String(minutes)
}
}
19 changes: 19 additions & 0 deletions SwedbankPaySDK/Classes/Api/Models/IntegrationTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

struct IntegrationTask: Codable, Hashable {
let rel: IntegrationTaskRel?
let href: String?
Expand Down Expand Up @@ -60,3 +62,20 @@ struct ExpectationModel: Codable, Hashable {
let type: String?
let value: String?
}

extension Array where Element == ExpectationModel {
var httpBody: Data? {
return self.filter({ $0.type == "string" })
.compactMap({
guard let name = $0.name else {
return nil
}

let value = $0.value?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""

return name + "=" + value
})
.joined(separator: "&")
.data(using: .utf8)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct OperationOutputModel: Codable, Hashable {
let method: String?
let next: Bool?
let tasks: [IntegrationTask]?
let expects: [ExpectationModel]?
}

extension OperationOutputModel {
Expand Down
1 change: 1 addition & 0 deletions SwedbankPaySDK/Classes/Api/Models/ProblemDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public extension SwedbankPaySDK {
public let title: String?
public let status: Int32?
public let detail: String?
public let originalDetail: String?

let operation: OperationOutputModel?
}
Expand Down
2 changes: 0 additions & 2 deletions SwedbankPaySDK/Classes/Api/SwedbankPayAPIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ struct SwedbankPayAPIConstants {
static var requestTimeoutInterval = 10.0
static var sessionTimeoutInterval = 20.0
static var creditCardTimoutInterval = 30.0

static var notificationUrl = "https://fake.payex.com/notification"
}

private enum HTTPHeaderField: String {
Expand Down
74 changes: 39 additions & 35 deletions SwedbankPaySDK/Classes/Api/SwedbankPayAPIEnpointRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,38 @@
import Foundation
import UIKit

struct Endpoint {
let router: EnpointRouter?
let href: String?
let method: String?
}

enum EnpointRouter {
case expandMethod(instrument: SwedbankPaySDK.PaymentAttemptInstrument)
case startPaymentAttempt(instrument: SwedbankPaySDK.PaymentAttemptInstrument, culture: String?)
case createAuthentication(methodCompletionIndicator: String, notificationUrl: String)
case completeAuthentication(cRes: String)
case getPayment
case preparePayment
case acknowledgeFailedAttempt
case abortPayment
}

protocol EndpointRouterProtocol {
var body: [String: Any?]? { get }
var requestTimeoutInterval: TimeInterval { get }
var sessionTimeoutInterval: TimeInterval { get }
}

struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
let model: OperationOutputModel
let culture: String?
let instrument: SwedbankPaySDK.PaymentAttemptInstrument?
let methodCompletionIndicator: String?
let cRes: String?

let endpoint: Endpoint
let sessionStartTimestamp: Date

var body: [String: Any?]? {
switch model.rel {
case .expandMethod:
return ["instrumentName": instrument?.identifier]
case .startPaymentAttempt:
switch endpoint.router {
case .expandMethod(instrument: let instrument):
return ["instrumentName": instrument.identifier]
case .startPaymentAttempt(let instrument, let culture):
switch instrument {
case .swish(let msisdn):
return ["culture": culture,
Expand All @@ -58,14 +70,6 @@ struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
"screenWidth": String(Int32(UIScreen.main.nativeBounds.width)),
"screenColorDepth": String(24)]
]
case .none:
return ["culture": culture,
"client": ["userAgent": SwedbankPaySDK.VersionReporter.userAgent,
"ipAddress": NetworkStatusProvider.getAddress(for: .wifi) ?? NetworkStatusProvider.getAddress(for: .cellular) ?? "",
"screenHeight": String(Int32(UIScreen.main.nativeBounds.height)),
"screenWidth": String(Int32(UIScreen.main.nativeBounds.width)),
"screenColorDepth": String(24)]
]
}
case .preparePayment:
return ["integration": "HostedView",
Expand All @@ -76,28 +80,28 @@ struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
"screenWidth": String(Int32(UIScreen.main.nativeBounds.width)),
"screenColorDepth": String(24)],
"browser": ["acceptHeader": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"languageHeader": Locale.current.identifier,
"timeZoneOffset": TimeZone.current.offsetFromGMT(),
"languageHeader": Locale.current.identifier.replacingOccurrences(of: "_", with: "-"),
"timeZoneOffset": TimeZone.current.minutesFromGMT(),
"javascriptEnabled": true],
"service": ["name": "SwedbankPaySDK-iOS",
"version": SwedbankPaySDK.VersionReporter.currentVersion]
]
case .createAuthentication:
return ["methodCompletionIndicator": methodCompletionIndicator ?? "N",
"notificationUrl": SwedbankPayAPIConstants.notificationUrl,
case .createAuthentication(let methodCompletionIndicator, let notificationUrl):
return ["methodCompletionIndicator": methodCompletionIndicator,
"notificationUrl": notificationUrl,
"requestWindowSize": "FULLSCREEN",
"client": ["userAgent": SwedbankPaySDK.VersionReporter.userAgent,
"ipAddress": NetworkStatusProvider.getAddress(for: .wifi) ?? NetworkStatusProvider.getAddress(for: .cellular) ?? "",
"screenHeight": String(Int32(UIScreen.main.nativeBounds.height)),
"screenWidth": String(Int32(UIScreen.main.nativeBounds.width)),
"screenColorDepth": String(24)],
"browser": ["acceptHeader": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"languageHeader": Locale.current.identifier,
"timeZoneOffset": TimeZone.current.offsetFromGMT(),
"languageHeader": Locale.current.identifier.replacingOccurrences(of: "_", with: "-"),
"timeZoneOffset": TimeZone.current.minutesFromGMT(),
"javascriptEnabled": true]
]
case .completeAuthentication:
return ["cRes": cRes ?? "",
case .completeAuthentication(let cRes):
return ["cRes": cRes,
"client": ["userAgent": SwedbankPaySDK.VersionReporter.userAgent,
"ipAddress": NetworkStatusProvider.getAddress(for: .wifi) ?? NetworkStatusProvider.getAddress(for: .cellular) ?? ""],
]
Expand All @@ -107,8 +111,8 @@ struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
}

var requestTimeoutInterval: TimeInterval {
switch model.rel {
case .startPaymentAttempt:
switch endpoint.router {
case .startPaymentAttempt(let instrument, _):
switch instrument {
case .creditCard:
return SwedbankPayAPIConstants.creditCardTimoutInterval
Expand All @@ -124,8 +128,8 @@ struct SwedbankPayAPIEnpointRouter: EndpointRouterProtocol {
}

var sessionTimeoutInterval: TimeInterval {
switch model.rel {
case .startPaymentAttempt:
switch endpoint.router {
case .startPaymentAttempt(let instrument, _):
switch instrument {
case .creditCard:
return SwedbankPayAPIConstants.creditCardTimoutInterval
Expand Down Expand Up @@ -174,7 +178,7 @@ extension SwedbankPayAPIEnpointRouter {
}

private func requestWithDataResponse(requestStartTimestamp: Date, handler: @escaping (Result<Data, Error>) -> Void) {
guard let href = model.href,
guard let href = endpoint.href,
var components = URLComponents(string: href) else {
handler(.failure(SwedbankPayAPIError.invalidUrl))
return
Expand All @@ -190,7 +194,7 @@ extension SwedbankPayAPIEnpointRouter {
}

var request = URLRequest(url: url)
request.httpMethod = model.method
request.httpMethod = endpoint.method
request.allHTTPHeaderFields = SwedbankPayAPIConstants.commonHeaders
request.timeoutInterval = requestTimeoutInterval

Expand All @@ -213,8 +217,8 @@ extension SwedbankPayAPIEnpointRouter {
}

BeaconService.shared.log(type: .httpRequest(duration: Int32((Date().timeIntervalSince(requestStartTimestamp) * 1000.0).rounded()),
requestUrl: model.href ?? "",
method: model.method ?? "",
requestUrl: endpoint.href ?? "",
method: endpoint.method ?? "",
responseStatusCode: responseStatusCode,
values: values))

Expand Down
Loading

0 comments on commit ea726e2

Please sign in to comment.