Skip to content

Commit

Permalink
fix: load 로 통일, widget 객체 nullable로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
mqzkim committed Jun 26, 2023
1 parent 216e2ef commit 264fb4a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ public final class PaymentWidgetViewController: ViewController {
button.setTitle("결제하기", for: .normal)
button.addTarget(self, action: #selector(requestPayment), for: .touchUpInside)

let paymentMethodWidget = widget.renderPaymentMethods(amount: Constant.defaultAmount)
let agreementWidget = widget.renderAgreement()

stackView.addArrangedSubview(amountInputField)
stackView.addArrangedSubview(orderIdInputField)
stackView.addArrangedSubview(orderNameInputField)
stackView.addArrangedSubview(widget.paymentMethodWidget)
stackView.addArrangedSubview(paymentMethodWidget)
stackView.addArrangedSubview(빈화면)
stackView.addArrangedSubview(widget.agreementWidget)
stackView.addArrangedSubview(agreementWidget)

amountInputField.title = "amount (원)"
amountInputField.text = "\(Constant.defaultAmount)"
Expand All @@ -77,17 +80,15 @@ public final class PaymentWidgetViewController: ViewController {
amountInputField.textField.keyboardType = .numberPad

widget.delegate = self
widget.paymentMethodWidget.widgetUIDelegate = self
widget.agreementWidget.agreementUIDelegate = self
widget.paymentMethodWidget.widgetStatusDelegate = self
widget.agreementWidget.widgetStatusDelegate = self
widget.paymentMethodWidget?.widgetUIDelegate = self
widget.agreementWidget?.agreementUIDelegate = self
widget.paymentMethodWidget?.widgetStatusDelegate = self
widget.agreementWidget?.widgetStatusDelegate = self

NSLayoutConstraint.activate([
빈화면.heightAnchor.constraint(equalToConstant: 200)
])
빈화면.backgroundColor = .lightGray
widget.renderPaymentMethods(amount: Constant.defaultAmount)
widget.renderAgreement()
}

@objc func requestPayment() {
Expand Down Expand Up @@ -174,16 +175,8 @@ extension PaymentWidgetViewController: TossPaymentsAgreementUIDelegate {
}

extension PaymentWidgetViewController: TossPaymentsWidgetStatusDelegate {
public func didReceivedLoading(_ name: String) {
Logger.debug("didReceivedLoading \(name)")
}

public func didReceivedLoaded(_ name: String) {
Logger.debug("didReceivedLoaded \(name)")
}

public func didReceivedFailed(_ name: String) {
Logger.debug("didReceivedFailed \(name)")
public func didReceivedLoad(_ name: String) {
Logger.debug("didReceivedLoad \(name)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ public extension TossPaymentsAgreementUIDelegate {
}

public protocol TossPaymentsWidgetStatusDelegate: AnyObject {
func didReceivedLoading(_ name: String)
func didReceivedLoaded(_ name: String)
func didReceivedFailed(_ name: String)
func didReceivedLoad(_ name: String)
}

public extension TossPaymentsWidgetStatusDelegate {
func didReceivedLoading(_ name: String) {}
func didReceivedLoaded(_ name: String) {}
func didReceivedFailed(_ name: String) {}
func didReceivedLoad(_ name: String) {}
}
16 changes: 4 additions & 12 deletions Sources/TossPayments/UIKit/Message/MessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,10 @@ final class MessageHandler {
case "widgetStatus":
guard let widgetStatus = WidgetStatusMessage(jsonObject: jsonObject).response else { return }
switch (widgetStatus.widget, widgetStatus.status) {
case ("paymentMethods", "loading"):
paymentMethod?.widgetStatusDelegate?.didReceivedLoading(widgetStatus.widget)
case ("paymentMethods", "loaded"):
paymentMethod?.widgetStatusDelegate?.didReceivedLoaded(widgetStatus.widget)
case ("paymentMethods", "failed"):
paymentMethod?.widgetStatusDelegate?.didReceivedFailed(widgetStatus.widget)
case ("agreement", "loading"):
agreement?.widgetStatusDelegate?.didReceivedLoading(widgetStatus.widget)
case ("agreement", "loaded"):
agreement?.widgetStatusDelegate?.didReceivedLoaded(widgetStatus.widget)
case ("agreement", "failed"):
agreement?.widgetStatusDelegate?.didReceivedFailed(widgetStatus.widget)
case ("paymentMethods", "load"):
paymentMethod?.widgetStatusDelegate?.didReceivedLoad(widgetStatus.widget)
case ("agreement", "load"):
agreement?.widgetStatusDelegate?.didReceivedLoad(widgetStatus.widget)
default: break
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class RequestHTMLMessageHandler: NSObject, WKScriptMessageHandler {
viewController.modalPresentationStyle = .fullScreen
viewController.success = { javascript in
viewController.dismiss(animated: true) {
self.widget?.paymentMethodWidget.evaluateJavaScript(javascript)
self.widget?.paymentMethodWidget?.evaluateJavaScript(javascript)
}
}
UIApplication.shared.keyWindow?.visibleViewController?.present(viewController, animated: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class RequestPaymentsMessageHandler: NSObject, WKScriptMessageHandler {
}
viewController.success = { javascript in
viewController.dismiss(animated: true) {
self.widget?.paymentMethodWidget.evaluateJavaScript(javascript)
self.widget?.paymentMethodWidget?.evaluateJavaScript(javascript)
}
}
UIApplication.shared.keyWindow?.visibleViewController?.present(viewController, animated: true)
Expand Down
21 changes: 13 additions & 8 deletions Sources/TossPayments/UIKit/Widget/PaymentWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class PaymentWidget: NSObject, HandleURLResult {
private var amount: Double = 0 {
didSet {
guard amount != oldValue else { return }
paymentMethodWidget.evaluateJavaScript("""
paymentMethodWidget?.evaluateJavaScript("""
updateAmount(\(amount))
""")
}
Expand All @@ -37,8 +37,8 @@ public final class PaymentWidget: NSObject, HandleURLResult {
return url
}

public let paymentMethodWidget: PaymentMethodWidget
public let agreementWidget: AgreementWidget
public var paymentMethodWidget: PaymentMethodWidget?
public var agreementWidget: AgreementWidget?
public init(
clientKey: String,
customerKey: String,
Expand All @@ -47,12 +47,12 @@ public final class PaymentWidget: NSObject, HandleURLResult {
self.clientKey = clientKey
self.customerKey = customerKey
self.options = options
self.paymentMethodWidget = PaymentMethodWidget()
self.agreementWidget = AgreementWidget()

super.init()
}

public func renderPaymentMethods(amount: Double) {
public func renderPaymentMethods(amount: Double) -> PaymentMethodWidget {
let paymentMethodWidget = PaymentMethodWidget()
self.amount = amount
paymentMethodWidget.configuration.userContentController.addUserScript(paymentMethodScript)
paymentMethodWidget.configuration.userContentController.add(
Expand All @@ -72,9 +72,12 @@ public final class PaymentWidget: NSObject, HandleURLResult {
name: ScriptName.message.rawValue
)
paymentMethodWidget.loadHTMLString(htmlString, baseURL: baseURL)
self.paymentMethodWidget = paymentMethodWidget
return paymentMethodWidget
}

public func renderAgreement() {
public func renderAgreement() -> AgreementWidget {
let agreementWidget = AgreementWidget()
agreementWidget.configuration.userContentController.addUserScript(agreementScript)
agreementWidget.configuration.userContentController.add(
UpdateHeightMessageHandler(),
Expand All @@ -85,6 +88,8 @@ public final class PaymentWidget: NSObject, HandleURLResult {
name: ScriptName.message.rawValue
)
agreementWidget.loadHTMLString(htmlString, baseURL: baseURL)
self.agreementWidget = agreementWidget
return agreementWidget
}

public func updateAmount(_ amount: Double) {
Expand Down Expand Up @@ -160,7 +165,7 @@ public final class PaymentWidget: NSObject, HandleURLResult {
widget.requestPaymentForNativeSDK(\(jsonString));
"""
guard let encodedScript = javascriptString.urlEncoded.data(using: .utf8)?.base64EncodedString() else { return }
paymentMethodWidget.evaluateJavaScript(
paymentMethodWidget?.evaluateJavaScript(
"""
var script = decodeURIComponent(window.atob('\(encodedScript)'));
eval(script);
Expand Down

0 comments on commit 264fb4a

Please sign in to comment.