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
的引用
您有几种可能的解决方案:
presentApplePay
UIViewControllerRepresentable
获取一个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
的引用
您有几种可能的解决方案:
presentApplePay
UIViewControllerRepresentable
获取一个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,但没有人有关于如何使用其库的文档,他们都认为你在使用故事板,非常令人沮丧!