102 lines
3.9 KiB
Swift
102 lines
3.9 KiB
Swift
//
|
|
// CanPaymentViewModel.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 2023/08/11.
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
import StoreKit
|
|
|
|
final class CanPaymentViewModel: ObservableObject {
|
|
|
|
private let repository = CanRepository()
|
|
private var subscription = Set<AnyCancellable>()
|
|
|
|
@Published var isTermsAgree = false
|
|
@Published var errorMessage = ""
|
|
@Published var isShowPopup = false
|
|
@Published var isLoading = false
|
|
|
|
func chargeCan(canProduct: SKProduct, paymentGateway: PaymentGateway, onSuccess: @escaping (SKProduct, Int) -> Void) {
|
|
isLoading = true
|
|
repository.chargeCan(
|
|
title: canProduct.localizedTitle,
|
|
chargeCan: Int(canProduct.localizedDescription) ?? 0,
|
|
price: canProduct.price.doubleValue,
|
|
locale: canProduct.priceLocale.identifier,
|
|
paymentGateway: paymentGateway
|
|
)
|
|
.sink { result in
|
|
switch result {
|
|
case .finished:
|
|
DEBUG_LOG("finish")
|
|
case .failure(let error):
|
|
ERROR_LOG(error.localizedDescription)
|
|
}
|
|
} receiveValue: { [unowned self] response in
|
|
self.isLoading = false
|
|
let responseData = response.data
|
|
|
|
do {
|
|
let jsonDecoder = JSONDecoder()
|
|
let decoded = try jsonDecoder.decode(ApiResponse<CanChargeResponse>.self, from: responseData)
|
|
|
|
if let data = decoded.data, decoded.success {
|
|
onSuccess(canProduct, data.chargeId)
|
|
} else {
|
|
if let message = decoded.message {
|
|
self.errorMessage = message
|
|
} else {
|
|
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
|
}
|
|
|
|
self.isShowPopup = true
|
|
}
|
|
} catch {
|
|
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
|
self.isShowPopup = true
|
|
}
|
|
}
|
|
.store(in: &subscription)
|
|
}
|
|
|
|
func verifyPayment(receiptString: String, chargeId: Int, onSuccess: @escaping () -> Void) {
|
|
isLoading = true
|
|
repository.verify(receiptString: receiptString, chargeId: chargeId)
|
|
.sink { result in
|
|
switch result {
|
|
case .finished:
|
|
DEBUG_LOG("finish")
|
|
case .failure(let error):
|
|
ERROR_LOG(error.localizedDescription)
|
|
}
|
|
} receiveValue: { [unowned self] response in
|
|
self.isLoading = false
|
|
let responseData = response.data
|
|
|
|
do {
|
|
let jsonDecoder = JSONDecoder()
|
|
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData)
|
|
|
|
if decoded.success {
|
|
onSuccess()
|
|
} else {
|
|
if let message = decoded.message {
|
|
self.errorMessage = message
|
|
} else {
|
|
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
|
}
|
|
|
|
self.isShowPopup = true
|
|
}
|
|
} catch {
|
|
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
|
self.isShowPopup = true
|
|
}
|
|
}
|
|
.store(in: &subscription)
|
|
}
|
|
}
|