stripe / stripe-ios

Stripe iOS SDK
https://stripe.com
MIT License
2.08k stars 970 forks source link

Integrating Card Scan Into SwiftUI #2343

Open jalvini opened 1 year ago

jalvini commented 1 year ago

Hello,

I am trying to integrate StripeCardScan into SwiftUI. I am already using Stripe in a custom form within our app and I would like to add the ability for a user to scan their card directly. I am trying to implement this using a ViewController and ViewControllerRepresntable but am having a hard time. There should probably be some documentation on how to do this. Here is my current code, can someone please point me in the right direction. Thanks.

So just to clarify, I just want to be able to add the ability for a user to capture their card info with the phones camera. If there is an easier way to do this without compromising our custom form then please let me know, thanks.

import UIKit
import StripeCardScan
import SwiftUI

class StripeScanViewController: UIViewController {

override func viewDidLoad() {
    print("Card Scan Did Load")
    super.viewDidLoad()
    let cardScanSheet = CardScanSheet()

    // Present it w/o any adjustments so it uses the default sheet presentation.
    cardScanSheet.present(from: self) { [weak self] result in
        print("HEHEHEHEH")
        switch result {
            case .completed(let scannedCard):
            /*
             * The user scanned a card. The result of the scan are detailed
             * in the `scannedCard` field of the result.
             */
            print("scan success")
        case .canceled:
            /*
            * The scan was canceled by the user.
            */
            print("scan canceled")
        case .failed(let error):
             /*
             * The scan failed. The displayable error is
             * included in the `localizedDescription`.
             */
             print("scan failed: \(error.localizedDescription)")
        }
     }  
   }
}

struct StripeCardScanViewRepresentable: UIViewControllerRepresentable {
    typealias stripeScanViewController = StripeScanViewController

    func makeUIViewController(context: Context) -> StripeScanViewController {
        let vc = StripeScanViewController()
        // Do some configurations here if needed.

        print(vc)
        print("VC")
        return vc
    }

    func updateUIViewController(_ uiViewController: StripeScanViewController, context: Context) {
        // Updates the state of the specified view controller with new information from SwiftUI.
    }
}
yuki-stripe commented 1 year ago

Hello @jalvini,

I'm not very familiar with SwiftUI but you might try adding this extension https://swiftuirecipes.com/blog/getting-key-window-and-top-root-view-controller-in-swiftui to grab the rootViewController from SwiftUI, and then hooking your card scan button up to present from the root view controller.

e.g.

Button("scan card") {
  CardScanSheet().present(from: UIApplication.shared.rootViewController)
}

I can't say whether this is good practice, but might help you get something working.

ankitpanchotiya99138 commented 1 year ago

It's present 2 view here on above first example. SimpleScanViewController not able to access on code. is there any solution on swiftUI?

ankitpanchotiya99138 commented 1 year ago

@jaimepark-stripe can you please help us to find out solution on present card scan sheet on swiftUI.

seanperez29 commented 1 month ago

Any updates on a solution for this in SwiftUI?