Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GMALKHA committed Dec 5, 2024
1 parent 5e2f5aa commit 88aeb2a
Showing 1 changed file with 1 addition and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,6 @@ class PayPalWebViewModel: ObservableObject {
print("❌ failed to fetch orderID with error: \(error.localizedDescription)")
}
}

func captureOrder(orderID: String, selectedMerchantIntegration: MerchantIntegration) async throws {
do {
DispatchQueue.main.async {
self.state.capturedOrderResponse = .loading
}
let payPalClientMetadataID = payPalDataCollector?.collectDeviceData()
let order = try await DemoMerchantAPI.sharedService.captureOrder(
orderID: orderID,
selectedMerchantIntegration: selectedMerchantIntegration,
payPalClientMetadataID: payPalClientMetadataID
)
DispatchQueue.main.async {
self.state.capturedOrderResponse = .loaded(order)
}
} catch {
DispatchQueue.main.async {
self.state.capturedOrderResponse = .error(message: error.localizedDescription)
}
print("❌ Failed to capture order: \(error.localizedDescription)")
}
}

func authorizeOrder(orderID: String, selectedMerchantIntegration: MerchantIntegration) async throws {
do {
DispatchQueue.main.async {
self.state.authorizedOrderResponse = .loading
}
let payPalClientMetadataID = payPalDataCollector?.collectDeviceData()
let order = try await DemoMerchantAPI.sharedService.authorizeOrder(
orderID: orderID,
selectedMerchantIntegration: selectedMerchantIntegration,
payPalClientMetadataID: payPalClientMetadataID
)
DispatchQueue.main.async {
self.state.authorizedOrderResponse = .loaded(order)
}
} catch {
DispatchQueue.main.async {
self.state.authorizedOrderResponse = .error(message: error.localizedDescription)
}
print("❌ Failed to authorize order: \(error.localizedDescription)")
}
}

func paymentButtonTapped(funding: PayPalWebCheckoutFundingSource) {
Task {
Expand Down Expand Up @@ -134,7 +90,7 @@ class PayPalWebViewModel: ObservableObject {
}
} else {
DispatchQueue.main.async {
self.state.approveResultResponse = .loaded(PayPalPaymentState.ApprovalResult(id: orderID, status: "COMPLETED"))
self.state.approveResultResponse = .loaded(PayPalPaymentState.ApprovalResult(id: orderID, status: "APPROVED"))
self.checkoutResult = result
print("✅ Checkout result: \(String(describing: result))")
}
Expand Down Expand Up @@ -166,9 +122,6 @@ class PayPalWebViewModel: ObservableObject {
func completeTransaction() async throws {
do {
setLoadingState()

try await processIntent(orderID: orderID ?? "")

if let orderID = state.createOrder?.id {
let payPalClientMetadataID = payPalDataCollector?.collectDeviceData()
let order = try await DemoMerchantAPI.sharedService.completeOrder(
Expand All @@ -184,15 +137,6 @@ class PayPalWebViewModel: ObservableObject {
}
}

private func processIntent(orderID: String) async throws {
switch intent {
case .authorize:
try await authorizeOrder(orderID: orderID, selectedMerchantIntegration: DemoSettings.merchantIntegration)
case .capture:
try await captureOrder(orderID: orderID, selectedMerchantIntegration: DemoSettings.merchantIntegration)
}
}

private func setLoadingState() {
DispatchQueue.main.async {
switch self.intent {
Expand Down

0 comments on commit 88aeb2a

Please sign in to comment.