Skip to content

Commit

Permalink
feat : example 본인인증 통신사 선택 추가
Browse files Browse the repository at this point in the history
feat : example phone 결제시 digital toggle 버튼 추가
  • Loading branch information
kjh5833 committed Jul 7, 2021
1 parent 3b1da40 commit 1a3d442
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
1 change: 1 addition & 0 deletions Example/iamport-ios/Model/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ class Order: ObservableObject, Then {
var name = PubData()
var appScheme = PubData()
var merchantUid = PubData()
var digital = PubData()
}
1 change: 1 addition & 0 deletions Example/iamport-ios/Model/PubData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import Then

class PubData: ObservableObject, Then {
var value: String = ""
var flag: Bool = false
}
27 changes: 16 additions & 11 deletions Example/iamport-ios/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
}
}
}

Expand All @@ -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)
}
Expand All @@ -68,7 +72,7 @@ struct ContentView: View {
buttonCertification()
}
}.tabItem {
Image(systemName: "cpu")
Image(systemName: "gift")
Text("본인인증")
.font(.title)
.fontWeight(.heavy)
Expand All @@ -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("닫기"))])
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
27 changes: 21 additions & 6 deletions Example/iamport-ios/View/PickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import Foundation
import SwiftUI
import iamport_ios

struct PickerView: View {
@EnvironmentObject var viewModel: ViewModel
Expand All @@ -13,27 +14,41 @@ 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)")
}
}
}

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)

Expand All @@ -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)
Expand Down
34 changes: 27 additions & 7 deletions Example/iamport-ios/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,6 +20,8 @@ public enum ItemType: Int, CaseIterable {
return "PG"
case .PayMethod:
return "결제수단"
case .Carrier:
return "(선택)통신사"
}
}
}
Expand All @@ -29,14 +31,18 @@ public class ViewModel: ObservableObject, Then {
var userCodeList = Utils.getUserCodeList() // 유저코드 리스트(예제용)
var pgList = PG.allCases // PG 리스트
var payMethodList: Array<PayMethod> = [] // PayMethod 리스트
var carrierList: Array<String> = ["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?

Expand All @@ -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 = [
Expand All @@ -72,7 +79,7 @@ public class ViewModel: ObservableObject, Then {

certInfos = [
("주문번호", cert.merchantUid),
("(선택)통신사", cert.carrier),
// ("(선택)통신사", cert.carrier),
("(선택)이름", cert.name),
("(선택)휴대폰번호", cert.phone),
("(선택)최소나이", cert.minAge),
Expand All @@ -88,13 +95,16 @@ 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,
amount: order.price.value).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
}
}
Expand All @@ -111,6 +121,12 @@ public class ViewModel: ObservableObject, Then {

iamPortResponse = response
showResult = true
clearButton()
}

func clearButton() {
isPayment = false
isCert = false
}

// 아임포트 본인인증 데이터 생성
Expand All @@ -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
}
Expand Down Expand Up @@ -161,6 +176,11 @@ public class ViewModel: ObservableObject, Then {
return payMethodList.map {
($0.rawValue, $0.name)
}

case .Carrier:
return carrierList.map {
($0, "")
}
}
}

Expand Down

0 comments on commit 1a3d442

Please sign in to comment.