Skip to content

Commit

Permalink
Make order and request ID generation methods public (#7)
Browse files Browse the repository at this point in the history
* Fix: Simplify error handling in session creation

* Fix: Improve error handling for unknown Juspay errors

* Make `generateOrderID` and `generateUniqueRequestID` public
  • Loading branch information
vamsii777 authored Oct 13, 2024
1 parent 6bcbb55 commit e11f8e2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Sources/JuspayKit/Refund/RefundRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public struct JuspayRefundRoutes: RefundRoutes {
let path = "orders/\(orderId)/refunds"
var body = "unique_request_id=\(refund.uniqueRequestId)&amount=\(refund.amount)"
body = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? body

var _headers = headers
_headers.add(name: "Content-Type", value: "application/x-www-form-urlencoded")
_headers.add(name: "version", value: DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .none))

return try await apiHandler.send(method: .POST, path: path, body: .string(body), headers: _headers)
}
}
16 changes: 4 additions & 12 deletions Sources/JuspayKit/Session/SessionRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,9 @@ public struct JuspaySessionRoutes: SessionRoutes {
/// - Returns: A `SessionResponse` object containing the response from the session creation request.
/// - Throws: An error if the JSON encoding fails, if the network request fails, or if the API returns an error.
public func create(session: Session) async throws -> SessionResponse {
do {
let body = try JSONEncoder().encode(session)
var _headers = headers
_headers.add(name: "Content-Type", value: "application/json")
return try await apiHandler.send(method: .POST, path: "session", body: .data(body), headers: _headers)
} catch let error as JuspayError {
// Handle Juspay-specific errors
throw error
} catch {
// Handle other errors
throw error
}
let body = try JSONEncoder().encode(session)
var _headers = headers
_headers.add(name: "Content-Type", value: "application/json")
return try await apiHandler.send(method: .POST, path: "session", body: .data(body), headers: _headers)
}
}
8 changes: 4 additions & 4 deletions Sources/JuspayKit/Util/JuspayError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public struct JuspayError: Error, Codable, Sendable {
/// A short string indicating the error code reported.
public var errorCode: String?
/// A human-readable message providing more details about the error.
public var errorMessage: String
public var errorMessage: String?
/// The status ID of the error.
public var statusId: Int
public var statusId: Int?
/// Additional information about the error.
public var errorInfo: ErrorInfo
public var errorInfo: ErrorInfo?

public var localizedDescription: String {
return errorMessage
return errorMessage ?? "Unknown error"
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/JuspayKit/Util/Order+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension Order {
///
/// - Parameter length: The length of the order ID to generate. The default value is 20.
/// - Returns: A randomly generated order ID string, or `nil` if the specified length is out of the valid range.
static func generateOrderID(length: Int = 20) -> String? {
public static func generateOrderID(length: Int = 20) -> String? {
guard (2...20).contains(length) else { return nil }

var orderID = "O"
Expand Down
2 changes: 1 addition & 1 deletion Sources/JuspayKit/Util/Refund+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension RefundRequest {
/// Special characters are avoided to ensure compatibility with various gateways and aggregators.
///
/// - Returns: A randomly generated unique request ID string of 20 characters, starting with 'R'.
static func generateUniqueRequestID() -> String {
public static func generateUniqueRequestID() -> String {
let characters = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var uniqueRequestID = "R"
var lastChar: Character?
Expand Down

0 comments on commit e11f8e2

Please sign in to comment.