Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带迅捷条纹_Ios_Swift_Swiftui_Stripe Payments - Fatal编程技术网

Ios 带迅捷条纹

Ios 带迅捷条纹,ios,swift,swiftui,stripe-payments,Ios,Swift,Swiftui,Stripe Payments,我正在尝试使用STPApplePayContextDelegate在我的SwiftUI应用程序中实现Stripe 我已经创建了一个类,它符合这个委托,但是没有运气。我得到了这个错误://表达式类型不明确,没有更多的上下文在这一行让applePayContext=stppapplepaycontext(paymentRequest:paymentRequest,delegate:self) 我做错了什么 struct PaymentButtonController : UIViewControll

我正在尝试使用
STPApplePayContextDelegate
在我的SwiftUI应用程序中实现Stripe

我已经创建了一个类,它符合这个委托,但是没有运气。我得到了这个错误:
//表达式类型不明确,没有更多的上下文
在这一行
让applePayContext=stppapplepaycontext(paymentRequest:paymentRequest,delegate:self)

我做错了什么

struct PaymentButtonController : UIViewControllerRepresentable {
    
    class Coordinator : NSObject, STPApplePayContextDelegate {
        var vc : UIViewController?
        
        @objc func buttonPressed() {
            let merchantIdentifier = "merchant.com.your_app_name"
            let paymentRequest = StripeAPI.paymentRequest(withMerchantIdentifier: merchantIdentifier, country: "US", currency: "USD")

            // Configure the line items on the payment request
            paymentRequest.paymentSummaryItems = [
                // The final line should represent your company;
                // it'll be prepended with the word "Pay" (i.e. "Pay iHats, Inc $50")
                PKPaymentSummaryItem(label: "iHats, Inc", amount: 50.00),
            ]
            
            // Initialize an STPApplePayContext instance
                if let applePayContext = STPApplePayContext(paymentRequest: paymentRequest, delegate: self) {
                    // Present Apple Pay payment sheet
                    if let vc = vc {
                        applePayContext.presentApplePay(on: vc)
                    }
                    
                } else {
                    // There is a problem with your Apple Pay configuration
                }
        }
        
        func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
            let clientSecret = "..."
            print("ENDLICH")
            // Retrieve the PaymentIntent client secret from your backend (see Server-side step above)
            // Call the completion block with the client secret or an error
            completion(clientSecret, nil);
        }
        
        func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
            print("ENDLICH")
            switch status {
            case .success:
                // Payment succeeded, show a receipt view
                break
            case .error:
                // Payment failed, show the error
                break
            case .userCancellation:
                // User cancelled the payment
                break
            @unknown default:
                fatalError()
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }
    
    func makeUIViewController(context: Context) -> UIViewController {
        let button = PKPaymentButton(paymentButtonType: .plain, paymentButtonStyle: .automatic)
        button.addTarget(context.coordinator, action: #selector(context.coordinator.buttonPressed), for: .touchUpInside)
        let vc = UIViewController()
        context.coordinator.vc = vc
        vc.view.addSubview(button)
        return vc
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        
    }
}

您正在调用的函数(
presentApplePay
)需要一个
UIViewController
作为其输入,但您正在传递
self
,它是您的
ApplePayContext
,上面定义为
NSObject、ObservableObject、STApplePayContextDelegate

您将要面临的挑战是获得传递给它的
UIViewController
上下文,因为在纯SwiftUI中您将不会有任何对
UIViewController
的引用

您有几种可能的解决方案:

  • 在SceneDelegate中,将UIHostingController的引用向下传递到视图,然后将其用作
    presentApplePay
  • 使用
    UIViewControllerRepresentable
    获取一个UIViewController,您可以将其作为参数()
  • 使用类似SwiftUI内省的库将底层UIViewController获取到当前SwiftUI视图()
  • 更新: 作为对代码请求的响应,这里有一些东西可以开始。注意,并不是所有的东西都连接好了——您需要连接buttonPressed方法,向按钮添加布局约束,等等,但是它提供了一种方法来确定如何获取对UIViewController的引用

    struct PaymentButtonController : UIViewControllerRepresentable {
        
        class Coordinator : NSObject {
            var vc : UIViewController?
            
            @objc func buttonPressed() {
                print("Button with VC: \(vc)")
            }
        }
        
        func makeCoordinator() -> Coordinator {
            return Coordinator()
        }
        
        func makeUIViewController(context: Context) -> UIViewController {
            let button = PKPaymentButton()
            button.addTarget(context.coordinator, action: #selector(context.coordinator.buttonPressed), for: .touchUpInside)
            let vc = UIViewController()
            context.coordinator.vc = vc
            vc.view.addSubview(button)
            return vc
        }
        
        func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
            
        }
    }
    
    通过在SwiftUI代码中使用以下命令,将其嵌入到视图中:

    PaymentButtonController()
    

    您正在调用的函数(
    presentApplePay
    )需要一个
    UIViewController
    作为其输入,但您正在传递
    self
    ,它是您的
    ApplePayContext
    ,上面定义为
    NSObject、ObservableObject、STApplePayContextDelegate

    您将要面临的挑战是获得传递给它的
    UIViewController
    上下文,因为在纯SwiftUI中您将不会有任何对
    UIViewController
    的引用

    您有几种可能的解决方案:

  • 在SceneDelegate中,将UIHostingController的引用向下传递到视图,然后将其用作
    presentApplePay
  • 使用
    UIViewControllerRepresentable
    获取一个UIViewController,您可以将其作为参数()
  • 使用类似SwiftUI内省的库将底层UIViewController获取到当前SwiftUI视图()
  • 更新: 作为对代码请求的响应,这里有一些东西可以开始。注意,并不是所有的东西都连接好了——您需要连接buttonPressed方法,向按钮添加布局约束,等等,但是它提供了一种方法来确定如何获取对UIViewController的引用

    struct PaymentButtonController : UIViewControllerRepresentable {
        
        class Coordinator : NSObject {
            var vc : UIViewController?
            
            @objc func buttonPressed() {
                print("Button with VC: \(vc)")
            }
        }
        
        func makeCoordinator() -> Coordinator {
            return Coordinator()
        }
        
        func makeUIViewController(context: Context) -> UIViewController {
            let button = PKPaymentButton()
            button.addTarget(context.coordinator, action: #selector(context.coordinator.buttonPressed), for: .touchUpInside)
            let vc = UIViewController()
            context.coordinator.vc = vc
            vc.view.addSubview(button)
            return vc
        }
        
        func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
            
        }
    }
    
    通过在SwiftUI代码中使用以下命令,将其嵌入到视图中:

    PaymentButtonController()
    

    我选择了做这个选择,尽管我觉得我快结束了,但我仍然没有完全弄清楚。我确实创建了一个符合
    UIViewControllerRepresentable
    的类,我试图将其作为参数传递,但我得到:
    表达式的类型在没有更多上下文的情况下是不明确的。我更新了我的答案@jn_pdxI可以看到您所做的,但是您需要将UIViewControllerRepresentable嵌入到视图层次结构中,而不仅仅是在ObserveObject中创建它。很难给你一个例子,因为你没有显示任何实际的视图代码(即如何开始支付),但你可以用这样的答案作为灵感:这可能是最简单的,是的。添加了示例代码谢谢,我喜欢SwiftUI,但没有人有关于如何使用其库的文档,他们都认为你在使用故事板,非常令人沮丧!我选择了做这个选择,尽管我觉得我快结束了,但我仍然没有完全弄清楚。我确实创建了一个符合
    UIViewControllerRepresentable
    的类,我试图将其作为参数传递,但我得到:
    表达式的类型在没有更多上下文的情况下是不明确的。我更新了我的答案@jn_pdxI可以看到您所做的,但是您需要将UIViewControllerRepresentable嵌入到视图层次结构中,而不仅仅是在ObserveObject中创建它。很难给你一个例子,因为你没有显示任何实际的视图代码(即如何开始支付),但你可以用这样的答案作为灵感:这可能是最简单的,是的。添加了示例代码谢谢,我喜欢SwiftUI,但没有人有关于如何使用其库的文档,他们都认为你在使用故事板,非常令人沮丧!