Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SwiftUI widget 및 예제 추가 #42

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions Examples/SwiftUI-Example/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
18 changes: 18 additions & 0 deletions Examples/SwiftUI-Example/SwiftUI_ExampleApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// SwiftUI_ExampleApp.swift
// SwiftUI-Example
//
// Created by 김진규 on 2023/08/04.
// Copyright © 2023 TossPayments, Inc. All rights reserved.
//

import SwiftUI

@main
struct SwiftUI_ExampleApp: App {
var body: some Scene {
WindowGroup {
TossPaymentsContentView()
}
}
}
61 changes: 61 additions & 0 deletions Examples/SwiftUI-Example/TossPaymentsContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// TossPaymentsContentView.swift
// SwiftUIExample
//
// Created by 김진규 on 2022/09/27.
//

#if !os(macOS) && canImport(SwiftUI)
import SwiftUI
import TossPayments

private enum Constants {
static let clientKey: String = "test_ck_P24xLea5zVAqkMGkoG7VQAMYNwW6"
static let 테스트결제정보: PaymentInfo = DefaultPaymentInfo(
amount: 1000,
orderId: "9lD0azJWxjBY0KOIumGzH",
orderName: "토스 티셔츠 외 2건",
customerName: "박토스"
)
}

struct TossPaymentsContentView: View {
@State private var showingSuccess: Bool = false
@State private var showingFail: Bool = false


@StateObject
var viewModel = TossPaymentsContentViewModel()

var body: some View {
ScrollView {
VStack(spacing: 0) {
PaymentMethodWidgetView(widget: viewModel.widget, amount: PaymentMethodWidget.Amount(value: 1000))
AgreementWidgetView(widget: viewModel.widget)
}
}
Button("결제하기") {
viewModel.requestPayment(info: DefaultWidgetPaymentInfo(orderId: "123", orderName: "김토스"))
}
.alert(isPresented: $showingSuccess, content: {
Alert(title: Text(verbatim: "Success"), message: Text(verbatim: viewModel.onSuccess?.orderId ?? ""))
})
.alert(isPresented: $showingFail, content: {
Alert(title: Text(verbatim: "Fail"), message: Text(verbatim: viewModel.onFail?.orderId ?? ""))
})
.onReceive(viewModel.$onSuccess.compactMap { $0 }) { success in
showingSuccess = true
}
.onReceive(viewModel.$onFail.compactMap { $0 }) { fail in
showingFail = true
}
}
}

struct TossPaymentsContentView_Previews: PreviewProvider {
static var previews: some View {
TossPaymentsContentView()
}
}

#endif
52 changes: 52 additions & 0 deletions Examples/SwiftUI-Example/TossPaymentsContentViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// TossPaymentsContentViewModel.swift
// SwiftUI-Example
//
// Created by 김진규 on 2023/08/04.
// Copyright © 2023 TossPayments, Inc. All rights reserved.
//

import SwiftUI
import TossPayments

private enum Constants {
static let clientKey: String = "test_ck_P24xLea5zVAqkMGkoG7VQAMYNwW6"
static let 테스트결제정보: PaymentInfo = DefaultPaymentInfo(
amount: 1000,
orderId: "9lD0azJWxjBY0KOIumGzH",
orderName: "토스 티셔츠 외 2건",
customerName: "박토스"
)
}

class TossPaymentsContentViewModel: ObservableObject {
let widget = PaymentWidget(clientKey: Constants.clientKey, customerKey: "Test")


@Published
var isShowing: Bool = false

@Published
var onSuccess: TossPaymentsResult.Success?
@Published
var onFail: TossPaymentsResult.Fail?

init() {
widget.delegate = self
}
func requestPayment(info: WidgetPaymentInfo) {
widget.requestPayment(
info: DefaultWidgetPaymentInfo(orderId: "123", orderName: "김토스")
)
}
}

extension TossPaymentsContentViewModel: TossPaymentsDelegate {
func handleSuccessResult(_ success: TossPaymentsResult.Success) {
onSuccess = success
}

func handleFailResult(_ fail: TossPaymentsResult.Fail) {
onFail = fail
}
}
Loading
Loading