-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add EligibilityRequest file * Add EligibilityResult file * Add EligibilityClient file * Add EligilibilityClient method * Add Result properties * Add request properties * Add EligibilityVariables file * Remove header * Add EligibilityAPI * Sort directory * Add properties and initializers * Add tests files * Fix lints * Fix lint * PR Feedback * Conform Encodable and add variable eligibility variables properties * Add supported currency type enum * Add eligibility intent enum * Add eligibility result properties * Add Eligibility Response object * Sort files Eligibility directory * Update eligibility request with enum types * Add parameters and initializer on Client * Add API logic * Add mock and remove intent enum * Remove final * Add docstrings on Client * Expose intents * Change Decodable instead of Codable for response model * Update Variables struct * Remove currency enum * Add docstring for Request model * Add mock API * Add client tests * Add Result docstrings * Rename curency to currencyCode * Add marks * Fix merge conflicts * Fix lint * Add mising file * Add functionality on demo app * PR feedback * Change initializer * Fix tests * Fix coding key * Add EligibilityIntent docstrings and remove unnecessary intents * Fix UTs * Pass intent as parameter and mimic UI as others views * Change initializer * Remove Recovered References directory * Lowercase supported payment methods * Use CurrentState enum instead of VenmoState * Remove VenmoState file * Update Sources/CorePayments/Eligibility/EligibilityAPI.swift Co-authored-by: Jax DesMarais-Leder <[email protected]> --------- Co-authored-by: Jax DesMarais-Leder <[email protected]>
- Loading branch information
1 parent
2cced79
commit c281aa9
Showing
14 changed files
with
543 additions
and
40 deletions.
There are no files selected for viewing
42 changes: 41 additions & 1 deletion
42
Demo/Demo/SwiftUIComponents/VenmoPayments/VenmoPaymentView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,36 @@ | ||
import CorePayments | ||
import Foundation | ||
|
||
class VenmoPaymentsViewModel: ObservableObject { } | ||
class VenmoPaymentsViewModel: ObservableObject { | ||
|
||
let configManager = CoreConfigManager(domain: "Venmo Payments") | ||
|
||
@Published var state: CurrentState = .idle | ||
|
||
private var eligibilityResult: EligibilityResult? | ||
|
||
var isVenmoEligible: Bool { | ||
eligibilityResult?.isVenmoEligible ?? false | ||
} | ||
|
||
func getEligibility(_ intent: EligibilityIntent) async throws { | ||
DispatchQueue.main.async { | ||
self.state = .loading | ||
} | ||
do { | ||
let config = try await configManager.getCoreConfig() | ||
let eligibilityRequest = EligibilityRequest(currencyCode: "USD", intent: intent) | ||
let eligibilityClient = EligibilityClient(config: config) | ||
eligibilityResult = try? await eligibilityClient.check(eligibilityRequest) | ||
|
||
DispatchQueue.main.async { | ||
self.state = .success | ||
} | ||
} catch { | ||
DispatchQueue.main.async { | ||
self.state = .error(message: error.localizedDescription) | ||
} | ||
print("failed in updating setup token. \(error.localizedDescription)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,77 @@ | ||
import Foundation | ||
|
||
final class EligibilityAPI { | ||
/// API that return merchant's eligibility for different payment methods: Venmo, PayPal, PayPal Credit, Pay Later & credit card | ||
class EligibilityAPI { | ||
|
||
// MARK: - Private Propertires | ||
|
||
private let coreConfig: CoreConfig | ||
private let networkingClient: NetworkingClient | ||
|
||
// MARK: - Initializer | ||
// MARK: - Initializers | ||
|
||
/// Initialize the eligibility API to check for payment methods eligibility | ||
/// - Parameter coreConfig: configuration object | ||
init(coreConfig: CoreConfig) { | ||
self.coreConfig = coreConfig | ||
self.networkingClient = NetworkingClient(coreConfig: coreConfig) | ||
} | ||
|
||
/// Exposed for injecting MockNetworkingClient in tests | ||
init(coreConfig: CoreConfig, networkingClient: NetworkingClient) { | ||
self.coreConfig = coreConfig | ||
self.networkingClient = networkingClient | ||
} | ||
|
||
// MARK: - Internal Methods | ||
|
||
/// Checks merchants eligibility for different payment methods. | ||
/// - Returns: An `EligibilityResponse` containing the result of the eligibility check. | ||
/// - Throws: An `Error` describing the failure. | ||
|
||
func check(_ eligibilityRequest: EligibilityRequest) async throws -> EligibilityResponse { | ||
let variables = EligibilityVariables(eligibilityRequest: eligibilityRequest, clientID: coreConfig.clientID) | ||
let graphQLRequest = GraphQLRequest(query: Self.rawQuery, variables: variables) | ||
let httpResponse = try await networkingClient.fetch(request: graphQLRequest) | ||
|
||
return try HTTPResponseParser().parseGraphQL(httpResponse, as: EligibilityResponse.self) | ||
} | ||
} | ||
|
||
extension EligibilityAPI { | ||
|
||
static let rawQuery = """ | ||
query getEligibility( | ||
$clientId: String!, | ||
$intent: FundingEligibilityIntent!, | ||
$currency: SupportedCountryCurrencies!, | ||
$enableFunding: [SupportedPaymentMethodsType] | ||
){ | ||
fundingEligibility( | ||
clientId: $clientId, | ||
intent: $intent | ||
currency: $currency, | ||
enableFunding: $enableFunding){ | ||
venmo{ | ||
eligible | ||
reasons | ||
} | ||
card{ | ||
eligible | ||
} | ||
paypal{ | ||
eligible | ||
reasons | ||
} | ||
paylater{ | ||
eligible | ||
reasons | ||
} | ||
credit{ | ||
eligible | ||
reasons | ||
} | ||
} | ||
} | ||
""" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
import Foundation | ||
|
||
/// The `EligibilityClient` class provides methods to check eligibility status based on provided requests. | ||
public final class EligibilityClient { | ||
|
||
private let api: EligibilityAPI | ||
private let config: CoreConfig | ||
|
||
// MARK: - Initializers | ||
|
||
/// Initializes a new instance of `EligibilityClient`. | ||
/// - Parameter config: The core configuration needed for the client. | ||
public init(config: CoreConfig) { | ||
self.config = config | ||
self.api = EligibilityAPI(coreConfig: config) | ||
} | ||
|
||
/// Exposed for injecting MockEligibilityAPI in tests | ||
init(config: CoreConfig, api: EligibilityAPI) { | ||
self.config = config | ||
self.api = api | ||
} | ||
|
||
// MARK: - Public Methods | ||
|
||
/// Checks the eligibility based on the provided `EligibilityRequest`. | ||
/// | ||
/// This method calls the `EligibilityAPI` to perform the check and converts the response to `EligibilityResult`. | ||
/// | ||
/// - Parameter eligibilityRequest: The eligibility request containing the necessary data to perform the check. | ||
/// - Throws: An error if the network request or parsing fails. | ||
/// - Returns: An `EligibilityResult` containing the result of the eligibility check. | ||
public func check(_ eligibilityRequest: EligibilityRequest) async throws -> EligibilityResult { | ||
// TODO: - Add logic | ||
.init(isVenmoEligible: false) | ||
try await api.check(eligibilityRequest).asResult | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
/// Enum representing the possible intents for eligibility. | ||
public enum EligibilityIntent: String { | ||
|
||
/// Represents the intent to capture a payment. | ||
case capture = "CAPTURE" | ||
|
||
/// Represents the intent to authorize a payment. | ||
case authorize = "AUTHORIZE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
import Foundation | ||
|
||
/// The `EligibilityRequest` structure includes the necessary parameters to make an eligibility check request. | ||
public struct EligibilityRequest { | ||
|
||
let intent: String | ||
let currency: String | ||
let enableFunding: [String] | ||
// MARK: - Internal Properties | ||
|
||
/// The currency code for the eligibility request. | ||
let currencyCode: String | ||
|
||
/// The intent of the eligibility request. | ||
let intent: EligibilityIntent | ||
|
||
/// An array of supported payment methods for the request. Defaults to `[.VENMO]`. | ||
let enableFunding: [SupportedPaymentMethodsType] | ||
|
||
// MARK: - Initializer | ||
|
||
public init(intent: String, currency: String, enableFunding: [String]) { | ||
/// Creates an instance of a eligibility request | ||
/// - Parameters: | ||
/// - currencyCode: The currency code for the eligibility request. | ||
/// - intent: The intent of the eligibility request. | ||
public init(currencyCode: String, intent: EligibilityIntent) { | ||
self.currencyCode = currencyCode | ||
self.intent = intent | ||
self.currency = currency | ||
self.enableFunding = enableFunding | ||
self.enableFunding = [.venmo] | ||
} | ||
} |
Oops, something went wrong.