0

I have a @EnvironmentObject for my Model where I handle the database changes and view state changes.

Then I have a View where I initialise a @StateObject for my ApplePayModel, this handles actual payment transactions.

Currently I have the transactions working however I want to call methods from my model, specifically a method model.completePaymentTransaction this then handles view changes and updates the database.

How can I call methods from @EnvironmentObject my when changes are made to the @StateObject ?

PS: I have considered putting all methods in model, but I want to separate code.

CheckoutView

struct CheckoutView: View {
  @EnvironmentObject var model: Model
  @StateObject var applePayModel = ApplePayModel()

  var body: some View {
      if applePayModel.clientSecret != nil {
           Button(action: {
               //presentS apple pay window

               applePayModel.pay(payment: model.payment!)

           }, label: { EmptyView() } )
           .buttonStyle(PaymentButtonStyle())
      }
  }
}

ApplePayModel

func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
    // get status error
    self.paymentStatus = status
    self.lastPaymentError = error
    
    switch paymentStatus {
        
    case .success:
        // complete payment in model by calling model.completePayment

    case error:
        print("error")
    case .userCancellation:
        print("user cancelled")
    case .none:
        print("none")

    }
}
RileyDev
  • 2,950
  • 3
  • 26
  • 61

0 Answers0