From 1a3d4429bfe858c39d7b8ed79b9ab0da0513777d Mon Sep 17 00:00:00 2001 From: bingbong Date: Wed, 7 Jul 2021 10:46:14 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20example=20=EB=B3=B8=EC=9D=B8?= =?UTF-8?q?=EC=9D=B8=EC=A6=9D=20=ED=86=B5=EC=8B=A0=EC=82=AC=20=EC=84=A0?= =?UTF-8?q?=ED=83=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat : example phone 결제시 digital toggle 버튼 추가 --- Example/iamport-ios/Model/Order.swift | 1 + Example/iamport-ios/Model/PubData.swift | 1 + Example/iamport-ios/View/ContentView.swift | 27 ++++++++++------- Example/iamport-ios/View/PickerView.swift | 27 +++++++++++++---- Example/iamport-ios/ViewModel.swift | 34 +++++++++++++++++----- 5 files changed, 66 insertions(+), 24 deletions(-) diff --git a/Example/iamport-ios/Model/Order.swift b/Example/iamport-ios/Model/Order.swift index 1187278..e0b37b9 100644 --- a/Example/iamport-ios/Model/Order.swift +++ b/Example/iamport-ios/Model/Order.swift @@ -16,4 +16,5 @@ class Order: ObservableObject, Then { var name = PubData() var appScheme = PubData() var merchantUid = PubData() + var digital = PubData() } diff --git a/Example/iamport-ios/Model/PubData.swift b/Example/iamport-ios/Model/PubData.swift index 90675f1..976df19 100644 --- a/Example/iamport-ios/Model/PubData.swift +++ b/Example/iamport-ios/Model/PubData.swift @@ -8,4 +8,5 @@ import Then class PubData: ObservableObject, Then { var value: String = "" + var flag: Bool = false } diff --git a/Example/iamport-ios/View/ContentView.swift b/Example/iamport-ios/View/ContentView.swift index 1ddb816..5b80792 100644 --- a/Example/iamport-ios/View/ContentView.swift +++ b/Example/iamport-ios/View/ContentView.swift @@ -12,8 +12,6 @@ struct ContentView: View { @EnvironmentObject var viewModel: ViewModel - @State var isPayment: Bool = false - @State var isCert: Bool = false @State var buttonTag: Int? = nil @State var paymentView: PaymentView = PaymentView() @@ -37,6 +35,11 @@ struct ContentView: View { ForEach(viewModel.orderInfos, id: \.0) { getNaviOrderInfoView($0.0, $0.1.value) } + if viewModel.isDigital { + Toggle(isOn: $viewModel.order.digital.flag) { + Text("휴대폰소액결제 digital") + }.padding() + } } } @@ -60,6 +63,7 @@ struct ContentView: View { VStack { Form { Section(header: Text("본인인증 정보")) { + getNaviPickerView(itemType: .Carrier) ForEach(viewModel.certInfos, id: \.0) { getNaviCertInfoView($0.0, $0.1.value) } @@ -68,7 +72,7 @@ struct ContentView: View { buttonCertification() } }.tabItem { - Image(systemName: "cpu") + Image(systemName: "gift") Text("본인인증") .font(.title) .fontWeight(.heavy) @@ -82,10 +86,11 @@ struct ContentView: View { .font(.title) .fontWeight(.heavy) } + }.actionSheet(isPresented: $viewModel.showResult) { ActionSheet(title: Text("결제 결과 도착~"), message: Text("\(String(describing: viewModel.iamPortResponse))"), - buttons: [.default(Text("Dismiss"))]) + buttons: [.default(Text("닫기"))]) } } @@ -99,7 +104,7 @@ struct ContentView: View { private func getNaviPickerView(itemType: ItemType) -> some View { NavigationLink(destination: PickerView(itemType: itemType)) { - listItem(itemType.name, viewModel.pgInfos[itemType.rawValue].1.value) + listItem(itemType.name, viewModel.iamportInfos[itemType.rawValue].1.value) } } @@ -136,16 +141,16 @@ struct ContentView: View { private func buttonPayment() -> some View { ZStack { Button(action: { - isPayment = true + viewModel.isPayment = true viewModel.updateMerchantUid() }) { Text("결제하기") .font(.headline) }.onBackgroundDisappear { - isPayment = false + viewModel.clearButton() }.buttonStyle(GradientBackgroundStyle()) - if isPayment { + if viewModel.isPayment { // trick size & opacity paymentView .frame(width: 0, height: 0).opacity(0) @@ -157,16 +162,16 @@ struct ContentView: View { private func buttonCertification() -> some View { ZStack { Button(action: { - isCert = true + viewModel.isCert = true viewModel.updateMerchantUid() }) { Text("본인인증") .font(.headline) }.onBackgroundDisappear { - isCert = false + viewModel.clearButton() }.buttonStyle(GradientBackgroundStyle()) - if isCert { + if viewModel.isCert { // trick size & opacity CertificationView() .frame(width: 0, height: 0).opacity(0) diff --git a/Example/iamport-ios/View/PickerView.swift b/Example/iamport-ios/View/PickerView.swift index a9b0dca..a7ed05f 100644 --- a/Example/iamport-ios/View/PickerView.swift +++ b/Example/iamport-ios/View/PickerView.swift @@ -5,6 +5,7 @@ import Foundation import SwiftUI +import iamport_ios struct PickerView: View { @EnvironmentObject var viewModel: ViewModel @@ -13,8 +14,8 @@ struct PickerView: View { @State private var isPresent = false private func getPicker() -> some View { - Picker(selection: $viewModel.pgInfos[itemType.rawValue].1.value.onUpdate { - updatePayMethodList() + Picker(selection: $viewModel.iamportInfos[itemType.rawValue].1.value.onUpdate { + update() }, label: Text("\(itemType.rawValue)")) { ForEach(viewModel.getItemList(type: itemType), id: \.0) { Text("\($0.0) \($0.1)") @@ -22,18 +23,32 @@ struct PickerView: View { } } + private func update() { + updatePayMethodList() + updateHiddenItem() + } + private func updatePayMethodList() { - print("updated! \(itemType)") + print("updatePayMethodList! \(itemType)") if (itemType == ItemType.PG) { print(viewModel.order.pg.value) viewModel.updatePayMethodList(pg: viewModel.order.pg.value) } } + private func updateHiddenItem() { + print("updateHiddenItem! \(viewModel.order.payMethod.value)") + if (PayMethod.phone == PayMethod.convertPayMethod(viewModel.order.payMethod.value)) { + viewModel.isDigital = true + return + } + viewModel.isDigital = false + } + var body: some View { VStack { - Text("적용할 \(itemType.name) 값 :\(viewModel.pgInfos[itemType.rawValue].1.value)") + Text("적용할 \(itemType.name) 값 :\(viewModel.iamportInfos[itemType.rawValue].1.value)") .padding(.top, 120).frame(height: 30).font(.headline) .multilineTextAlignment(.leading) @@ -49,8 +64,8 @@ struct PickerView: View { .fontWeight(.heavy).multilineTextAlignment(.center).padding(.bottom, 20) }.popover(isPresented: $isPresent) { Text("아래에 입력하세요") - TextField("입력하세요", text: $viewModel.pgInfos[itemType.rawValue].1.value.onUpdate { - updatePayMethodList() + TextField("입력하세요", text: $viewModel.iamportInfos[itemType.rawValue].1.value.onUpdate { + update() }) .font(.title).frame(height: 50).multilineTextAlignment(.center) .padding(10).border(Color.green, width: 1) diff --git a/Example/iamport-ios/ViewModel.swift b/Example/iamport-ios/ViewModel.swift index 88da76c..04600c3 100644 --- a/Example/iamport-ios/ViewModel.swift +++ b/Example/iamport-ios/ViewModel.swift @@ -10,7 +10,7 @@ import iamport_ios public enum ItemType: Int, CaseIterable { - case UserCode, PG, PayMethod + case UserCode, PG, PayMethod, Carrier public var name: String { switch self { @@ -20,6 +20,8 @@ public enum ItemType: Int, CaseIterable { return "PG" case .PayMethod: return "결제수단" + case .Carrier: + return "(선택)통신사" } } } @@ -29,14 +31,18 @@ public class ViewModel: ObservableObject, Then { var userCodeList = Utils.getUserCodeList() // 유저코드 리스트(예제용) var pgList = PG.allCases // PG 리스트 var payMethodList: Array = [] // PayMethod 리스트 + var carrierList: Array = ["SKT", "KTF", "LGT", "MVNO"] // PayMethod 리스트 @Published var order: Order // 옵저빙할 결제 데이터 객체 @Published var cert: Cert // 옵저빙할 본인인증 데이터 객체 - @Published var pgInfos: Array<(ItemType, PubData)> = [] + + @Published var iamportInfos: Array<(ItemType, PubData)> = [] @Published var orderInfos: Array<(String, PubData)> = [] @Published var certInfos: Array<(String, PubData)> = [] - @Published var isPayment = false + @Published var isDigital = false + @Published var isPayment: Bool = false + @Published var isCert: Bool = false @Published var showResult: Bool = false var iamPortResponse: IamPortResponse? @@ -57,10 +63,11 @@ public class ViewModel: ObservableObject, Then { updateMerchantUid() // pub data init - pgInfos = [ + iamportInfos = [ (.UserCode, order.userCode), (.PG, order.pg), - (.PayMethod, order.payMethod) + (.PayMethod, order.payMethod), + (.Carrier, cert.carrier) ] orderInfos = [ @@ -72,7 +79,7 @@ public class ViewModel: ObservableObject, Then { certInfos = [ ("주문번호", cert.merchantUid), - ("(선택)통신사", cert.carrier), +// ("(선택)통신사", cert.carrier), ("(선택)이름", cert.name), ("(선택)휴대폰번호", cert.phone), ("(선택)최소나이", cert.minAge), @@ -88,6 +95,8 @@ public class ViewModel: ObservableObject, Then { return nil } + print("order.digital.flag \(order.digital.flag)") + return IamPortRequest( pg: order.pg.value, merchant_uid: order.merchantUid.value, @@ -95,6 +104,7 @@ public class ViewModel: ObservableObject, Then { $0.pay_method = payMethod $0.name = order.orderName.value $0.buyer_name = order.name.value + $0.digital = order.digital.flag $0.app_scheme = order.appScheme.value } } @@ -111,6 +121,12 @@ public class ViewModel: ObservableObject, Then { iamPortResponse = response showResult = true + clearButton() + } + + func clearButton() { + isPayment = false + isCert = false } // 아임포트 본인인증 데이터 생성 @@ -129,7 +145,6 @@ public class ViewModel: ObservableObject, Then { } func updateMerchantUid() { -// order.merchantUid.value = "muid_ios_\(Int(Date().timeIntervalSince1970))" order.merchantUid.value = UUID().uuidString cert.merchantUid.value = UUID().uuidString } @@ -161,6 +176,11 @@ public class ViewModel: ObservableObject, Then { return payMethodList.map { ($0.rawValue, $0.name) } + + case .Carrier: + return carrierList.map { + ($0, "") + } } }