Skip to content
Permalink
cc16cdfc32
Go to file
 
 
Cannot retrieve contributors at this time
125 lines (109 sloc) 5.7 KB
//
// PaymentDetailInterfaceController.swift
// SynchronyFinancial
//
// Created by Monday on 2019/03/24.
// Copyright © 2019 Alan Maynard. All rights reserved.
//
import WatchKit
import Foundation
class PaymentDetailInterfaceController: WKInterfaceController {
var selectedAccount: Account?
var dictForAcct: [String: Account] = [:]
var dictForPayment: [String: String] = [:]
var paymentButtonArmed: Bool = false
var paymentAmount: Double = 0.0
var dictForBankAcct: [String: [BankAcct]] = [:]
var bankID: String = ""
@IBOutlet weak var activityIndicator: WKInterfaceImage!
@IBOutlet weak var activityIndicatorLabel: WKInterfaceLabel!
@IBOutlet weak var contentGroup: WKInterfaceGroup!
@IBOutlet weak var detailButton: WKInterfaceButton!
@IBOutlet weak var amount: WKInterfaceLabel!
@IBOutlet weak var paymentButton: WKInterfaceButton!
@IBOutlet weak var payFrom: WKInterfaceLabel!
@IBOutlet weak var separate: WKInterfaceSeparator!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
guard let data = context as? [String: Any], let acct = data["acct"] as? Account, let amount = data["payment_amount"] as? Double else {
NSLog("Error getting account object and payment amount")
return
}
self.paymentAmount = amount
self.selectedAccount = acct
paymentButton.setTitle(String(format: "Pay $%.2f", amount))
}
@IBAction func paymentAction() {
if paymentButtonArmed {
guard let alias = selectedAccount?.accountAlias else { return }
let cancel = WKAlertAction(title: "Cancel", style: .cancel, handler: {})
let submit = WKAlertAction(title: "Pay Now", style: .default, handler: {
self.contentGroup.setHidden(true)
self.activityIndicator.configureForActivityIndicator()
self.activityIndicatorLabel.setHidden(false)
self.activityIndicatorLabel.setVerticalAlignment(.center)
let type: PaymentType = self.paymentAmount == self.selectedAccount?.curBalance ? .currentBal : .minimumDue
// process this payment using default bank account (9999) until ability to change account is implemented
if let bankacct = UserDefaults.standard.dictionary(forKey: "default_bank_acct") as? [String: String] {
self.bankID = bankacct["bankAcctId"] ?? "9999"
}
FetchData.submitPayment(for: alias, type: type, amount: self.paymentAmount, bankID: self.bankID) { confirmationNum, paymentID, error in
guard error == nil else {
let dismiss = WKAlertAction(title: "Dismiss", style: .cancel, handler: {})
self.presentAlert(withTitle: "Error", message: "We were unable to process your payment at this time. Please try again later.", preferredStyle: .alert, actions: [dismiss])
return
}
self.dictForPayment.updateValue(confirmationNum, forKey: "payment_confirmation_number")
self.dictForPayment.updateValue(paymentID, forKey: "payment_id")
self.activityIndicator.stopAnimatingAsIndicator()
self.activityIndicatorLabel.setHidden(true)
self.contentGroup.setHidden(false)
self.presentController(withName: "PaymentResult", context: self.dictForPayment)
self.popToRootController()
}
})
presentAlert(withTitle: "Disclaimer", message: "By tapping \"Pay Now\", you are authorizing Synchrony Bank to process a one time payment in the amount of \(String(format: "$%.2f", paymentAmount)).", preferredStyle: .actionSheet, actions: [cancel, submit])
} else {
animate(withDuration: 0.5) {
self.paymentButton.setBackgroundColor(#colorLiteral(red: 0.6092301607, green: 0.9366738796, blue: 0.2432599962, alpha: 1))
self.paymentButtonArmed = true
self.paymentButton.setTitle("Pay Now")
}
}
}
@IBAction func bankAccountsTapped() {
configureInterfaceObjects(true)
activityIndicator.configureForActivityIndicator()
FetchData.getBankInfo { accts, error in guard error == nil else {
NSLog("Error retrieving payment methods for account in paymentDetailInterfaceController.")
return
}
self.dictForBankAcct.updateValue(accts, forKey: "paymentOptions")
self.configureInterfaceObjects(false)
//self.detailButton.setTitle("Hello")
self.activityIndicator.stopAnimatingAsIndicator()
self.pushController(withName: "PaymentOptions", context: self.dictForBankAcct)
}
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if let bankacct = UserDefaults.standard.dictionary(forKey: "default_bank_acct") as? [String: String] {
detailButton.setTitle(bankacct["bankName"])
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
private func configureInterfaceObjects(_ hide: Bool) {
detailButton.setHidden(hide)
amount.setHidden(hide)
paymentButton.setHidden(hide)
payFrom.setHidden(hide)
separate.setHidden(hide)
contentGroup.setHidden(hide)
activityIndicatorLabel.setVerticalAlignment(.center)
activityIndicatorLabel.setHidden(!hide)
}
}
You can’t perform that action at this time.