【问题标题】:Swift- import module working in one file, but not in anotherSwift-导入模块在一个文件中工作,但不在另一个文件中
【发布时间】:2020-06-29 05:29:02
【问题描述】:

为了澄清,Stripe 模块给出的错误是没有名为 Stripe 的模块。这是我尝试过的:

文件 1(在这个文件中工作):

import Foundation
import Stripe
import FirebaseFunctions

let StripeApi = _StripeApi()

class _StripeApi : NSObject, STPCustomerEphemeralKeyProvider {

文件 2(不适用于此文件):

import UIKit
import Stripe
import FirebaseFunctions

class CheckoutVC: UIViewController, CartItemDelegate {

此外,同一文件给出此错误:“STPPaymentConfiguration”类型的值没有成员“createCardSources”

func setupStripeConfig() {
        let config = STPPaymentConfiguration.shared()
        config.createCardSources = true
        config.requiredBillingAddressFields = .none
        config.requiredShippingAddressFields = [.postalAddress]

        let customerContext = STPCustomerContext(keyProvider: StripeApi)
        paymentContext = STPPaymentContext(customerContext: customerContext, configuration: config, theme: .default())

        paymentContext.paymentAmount = StripeCart.total
        paymentContext.delegate = self
        paymentContext.hostViewController = self

    }

如果有人能让我走上正轨,那将不胜感激。谢谢!

【问题讨论】:

标签: ios swift xcode module stripe-payments


【解决方案1】:

你是重构代码,在 iOS 12 及更高版本中工作。

func setupStripeConfig() {
    let config = STPPaymentConfiguration.shared
    config.requiredBillingAddressFields = .none
    config.requiredShippingAddressFields = [.postalAddress]
    
    let customerContext = STPCustomerContext(keyProvider: StripeApi)
    paymentContext = STPPaymentContext(customerContext: customerContext, configuration: config, theme: .defaultTheme)
    
    paymentContext.paymentAmount = StripeCart.total
    paymentContext.delegate = self
    paymentContext.hostViewController = self
}

你可以把这部分config.createCardSources = true拿出来,所有的错误都会得到解决。

【讨论】:

    【解决方案2】:

    创建一个stripeClient 文件:

    import Foundation
    import Stripe
    
    class StripeClient {
        
        static let sharedClient = StripeClient()
        
        var baseURLString : String? = nil
        
        var baseURL: URL {
            
            if let URLString = self.baseURLString, let url = URL(string: URLString){
                return url
            }
            else {
                fatalError()
            }
        }
        
        func createAndConfirmPayment(_ token: STPToken, amount: Int, completion: @escaping(_ error: Error?) -> Void) {
            
            let url = self.baseURL.appendingPathComponent("charge")
            
            let params: [String : Any] = ["stripeToken" : token.tokenId, "amount" : amount, "description" : Constants.defaultDescription, "currency" : Constants.defaultCurrency] 
    
    }
    

    将此添加到您的AppDelegate.Swift

    // MARK: - Strip Init
    
    func StripeInit() {
        
    //        STPPaymentConfiguration.shared().publishableKey = Constants.publishedKey
    
        
        STPAPIClient.shared().publishableKey = Constants.publishedKey
        StripeClient.sharedClient.baseURLString = Constants.baseURLString
    }
    }
       
    

    然后在didFinishLaunchingWithOptions调用它

    我创建了CardInfoViewController,其中包含以下内容:

    import UIKit
    import Stripe
    
    protocol CardInfoViewControllerDelegate {
        
        func doneButtonPressed (_ token: STPToken )
        
        func cancelButtonPressed ()
        
    }
    
    
    
    class CardInfoViewController: UIViewController {
    
        // MARK: - Outlets
    
        @IBOutlet weak var doneButtonOutlet: UIButton!
        
        let paymentCardTextField = STPPaymentCardTextField()
        
        var delegate: CardInfoViewControllerDelegate?
        
        // MARK: - Life Cycle
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1)
    
            
            paymentCardTextField.delegate = self
            // Modifying card textfield
            
            view.addSubview(paymentCardTextField)
            paymentCardTextField.translatesAutoresizingMaskIntoConstraints = false
            
            view.addConstraint(NSLayoutConstraint(item: paymentCardTextField, attribute: .top, relatedBy: .equal, toItem: doneButtonOutlet, attribute: .bottom, multiplier: 1, constant: 30))
            
            view.addConstraint(NSLayoutConstraint(item: paymentCardTextField, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -20))
    
            view.addConstraint(NSLayoutConstraint(item: paymentCardTextField, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 20))
            
        }
        
        // MARK: - Actions
    
        @IBAction func cancelButtonAction(_ sender: Any) {
            delegate?.cancelButtonPressed()
            dismissView()
        }
        
    
        
        @IBAction func doneButtonAction(_ sender: Any) {
            
            processCard()
        }
        
        
        
        // MARK: - Support funcs
        
        private func dismissView() {
            self.dismiss(animated: true, completion: nil)
        }
        
        private func processCard () {
            
            let cardParams = STPCardParams()
            cardParams.number = paymentCardTextField.cardNumber
            cardParams.expMonth = paymentCardTextField.expirationMonth
            cardParams.expYear = paymentCardTextField.expirationYear
            cardParams.cvc = paymentCardTextField.cvc
    //        cardParams.addressZip = paymentCardTextField.
             
            STPAPIClient.shared().createToken(withCard: cardParams) { (token, error) in
                
                if error == nil {
                    print("Finally we have a token which is \(token!)")
                    self.delegate?.doneButtonPressed(token!)
                    
                    self.dismissView()
                }
                else {
                    print("error processing card token \(error!.localizedDescription)")
                }
            }
        }
    }
    
    // MARK: - Payment extension
    
    extension CardInfoViewController: STPPaymentCardTextFieldDelegate {
        
        func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
            
            doneButtonOutlet.isEnabled = textField.isValid
            if doneButtonOutlet.isEnabled == true {
                doneButtonOutlet.backgroundColor = #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1)
            }
            else {
                doneButtonOutlet.backgroundColor = #colorLiteral(red: 0.3333333433, green: 0.3333333433, blue: 0.3333333433, alpha: 1)
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-09
      • 2014-09-20
      • 2021-09-18
      • 1970-01-01
      • 2015-05-01
      • 2023-04-05
      • 2011-01-27
      • 1970-01-01
      相关资源
      最近更新 更多