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

Support for SwiftUI #107

Open
ralfebert opened this issue Mar 31, 2021 · 1 comment
Open

Support for SwiftUI #107

ralfebert opened this issue Mar 31, 2021 · 1 comment

Comments

@ralfebert
Copy link

ralfebert commented Mar 31, 2021

When using the Sumup SDK in a SwiftUI app, the usage is a bit cumbersome, because the API assumes you have a UIViewController handy which you usually don't have in a SwiftUI app. It would be nice if the SDK would provide a SwiftUI wrapper or accessible UIViewController classes that can be wrapped with UIViewControllerRepresentable.

A workaround to make it work today - SumupView can be embedded in the SwiftUI view hierarchy to make the Sumup login/checkout controller appear:

import SumUpSDK
import SwiftUI

class SumupPaymentJob {

    let request: CheckoutRequest
    let completion: (_ paid: Bool) -> Void

    init(api_key: String, request: CheckoutRequest, completion: @escaping (_ paid: Bool) -> Void) {
        SumUpSDK.setup(withAPIKey: api_key)
        self.request = request
        self.completion = completion
    }

}

class SumupViewController: UIViewController {

    let paymentJob: SumupPaymentJob

    init(paymentJob: SumupPaymentJob) {
        self.paymentJob = paymentJob
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) not supported")
    }

    func login() {
        SumUpSDK.presentLogin(from: self, animated: false) { success, error in
            if success {
                self.purchase()
            } else {
                self.paymentJob.completion(false)
            }
        }
    }

    func purchase() {
        SumUpSDK.checkout(with: self.paymentJob.request, from: self) { result, error in
            self.paymentJob.completion(result?.success ?? false)
        }
    }

    var started = false

    override func viewWillAppear(_ animated: Bool) {
        if !self.started {
            self.started = true
            if !SumUpSDK.isLoggedIn {
                self.login()
            } else {
                self.purchase()
            }
        }
    }

}

struct SumupView: UIViewControllerRepresentable {

    let paymentJob: SumupPaymentJob

    func makeUIViewController(context: Context) -> UIViewController {
        SumupViewController(paymentJob: self.paymentJob)
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
@chadmulligan
Copy link

Hi Ralf,
Thanks for the code, much appreciated. I am trying to invoke this from the cordova plugin, but so far I've had no luck (I am a very beginner with Swift).
How would one trigger the SDK with the proper UI wrapper ?
Thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants