verifyFundsAndCreateTransfer

A closure to verify sender funds and create a transfer

Overview

    +-------------+           +----------+           +------------+
    |  Customer   |----(1)--->| Customer |----(2)--->| ReadyRemit |
    | Application |<---(7)----| API (4)  |<---(3)----|    API     |
    +-------------+           +----------+           +------------+
           |                      | |
           |                      | |                +------------+
           |                      |  ---------(5)--->| ReadyRemit |
           |                       <----------(6)----|    API     |
           |                                         +------------+
           |
           |                  +------------+
            -----------(8)--->| ReadyRemit |
                              |    SDK     |
                              +------------+
  1. Customer application makes a network request to the Customer API to initiate the creation of a transfer
  2. Customer API makes a network request to the ReadyRemit API to read quote details
  3. ReadyRemit API returns the quote details or an error
  4. Customer API makes performs additional prerequisites, such as verification of funds
  5. Customer API makes a network request to the ReadyRemit API to create a transfer
  6. ReadyRemit API returns the transfer details or an error
  7. Customer API returns the transfer details or an error
  8. Customer application returns the transfer details or throws an error

Definition

((TransferRequest) async throws(ReadyRemitError) -> TransferDetails)?

Parameters

TypeDescription
TransferRequestInformation that is required to include when creating the transfer

Error

TypeDescription
ReadyRemitErrorAn error value communicating the process of verifying funds and creating a transfer was not successful

Response

TypeDescription
TransferDetailsThe transfer ID for the successfully created transfer

Sample Implementation

struct CreateTransferResponse: TransferDetails {
    var transferId: String
}

private func verifyFundsAndCreateTransfer(
    transferRequest: ReadyRemit.TransferRequest
) async throws(ReadyRemitError) -> TransferDetails {
    guard let url = URL(string: "https://example.com/v1/verifyFundsAndCreateTransfer") else {
        throw URLError(.badURL)
    }
    var urlRequest = URLRequest(url: url)
    urlRequest.httpMethod = "POST"
    urlRequest.httpBody = try JSONEncoder().encode(transferRequest)
    let (data, urlResponse) = try await URLSession.shared.data(for: urlRequest)
    guard let httpURLResponse = urlResponse as? HTTPURLResponse else {
        throw URLError(.badServerResponse)
    }
    switch httpURLResponse.statusCode {
    case 200...299:
        return try JSONDecoder().decode(CreateTransferResponse.self, from: data)
    default:
        throw ReadyRemitError(code: .none, message: "Insufficient Funds")
    }
}