pscbonline-ios/PSCBOnline/Sources/Models/Response.swift

101 lines
2.8 KiB
Swift
Raw Permalink Normal View History

2024-07-08 15:20:00 +03:00
//
// Response.swift
// PSCB-OOS-iOS
//
// Created by Antonov Ilia on 23.10.2020.
//
import Foundation
// MARK: - PSCB OOS Response status
public enum ResponseStatus: String, Decodable {
case success = "STATUS_SUCCESS"
case failure = "STATUS_FAILURE"
}
// MARK: - Acquiring data
public struct AcquiringResponseData: Decodable {
public let paReq: String?
public let order: String?
public let termUrl: String?
public let url: String?
public let md: String?
private enum CodingKeys: String, CodingKey {
case order, termUrl = "TermUrl", url = "URL", md = "MD", paReq = "PaReq"
}
}
// MARK: - OOS Error
/// An error structure for OOS errors returned in flat JSON
/// `errorCode` and `errorDescription`
public struct ResponseError: Decodable {
public let code: String
public let description: String?
private enum CodingKeys: String, CodingKey {
case code = "errorCode"
case description = "errorDescription"
}
}
// MARK: - OOS Response
/// JSON Response from OOS API
public struct Response: Decodable {
public let status: ResponseStatus
public let requestId: String
public let error: ResponseError?
public let payment: ResponsePayment?
public let acquiringData: AcquiringResponseData?
public let description: String?
private enum CodingKeys: String, CodingKey {
case status, requestId, payment, acquiringData, description
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.status = try container.decode(ResponseStatus.self, forKey: .status)
self.requestId = try container.decode(String.self, forKey: .requestId)
self.description = try? container.decode(String.self, forKey: .description)
// Decode flat and nested data into nested structs
self.payment = try? container.decode(ResponsePayment.self, forKey: .payment)
self.acquiringData = try? AcquiringResponseData(from: decoder)
self.error = try? ResponseError(from: decoder)
}
}
// MARK: - Payment status
/// Payment stats
/// see: https://docs.pscb.ru/oos/index.html#obshaya-informaciya-spravochniki-statusy-platezhej
public enum PaymentState: String, Decodable {
case sent = "sent"
case new = "new"
case end = "end"
case refunded = "ref"
case hold = "hold"
case expired = "exp"
case canceled = "canceled"
case error = "error"
case rejected = "rej"
case undefined = "undef"
}
// MARK: - Payment response struct
public struct ResponsePayment: Decodable {
public let orderId: String
public let showOrderId: String
public let paymentId: String
public let amount: Decimal
public let state: PaymentState
public let marketPlace: UInt16
public let stateDate: Date
}