Skip to content
Permalink
 
 
Cannot retrieve contributors at this time
189 lines (176 sloc) 10.4 KB
//
// FetchData.swift
// SynchronyFinancial WatchKit Extension
//
// Created by Rahul Kantesaria on 3/14/19.
// Copyright © 2019 Alan Maynard. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
class FetchData {
static func attemptLogin(completion: @escaping (Bool, Error?) -> Void) {
Alamofire.request(Defaults.LOGIN_URL, method: .post, parameters: Defaults.headerForLogin, encoding: JSONEncoding.default).responseJSON { payload in
switch payload.result {
case .success(let value):
let dict = JSON(value).dictionaryValue
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else {
completion(false, NSError())
return
}
if let token = dict["access_token"]?.string {
// now we should save our token somewhere safe (UserDefaults)
UserDefaults.standard.set(token, forKey: "access_token")
completion(true, nil)
}
case .failure(let error):
NSLog("Error: %s", error.localizedDescription)
completion(false, error)
}
}
}
static func getAccountInfo(completion: @escaping ([Account], Error?) -> Void) {
var accountList: [Account] = []
Alamofire.request(Defaults.MULTI_ACCT_URL, method: .post, parameters: Defaults.headerForMulti, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
switch payload.result {
case .success(let value):
let json = JSON(value)
let dict = json.dictionaryValue
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
//print(json)
if let accounts = dict["account_number_list"]?.arrayValue {
accounts.forEach {
if let accountAlias = $0.dictionaryValue["account_alias"]?.string,
let last4 = $0["last4_acct_number"].string,
let creditLimitString = $0.dictionaryValue["credit_limit"]?.string,
let creditLimit = Double(creditLimitString),
let payDueDateString = $0.dictionaryValue["next_payment_due_date"]?.string,
let paymentDueDate = Defaults.careCreditDateFormatter.date(from: payDueDateString),
let curBalString = $0.dictionaryValue["current_balance"]?.string,
let currentBalance = Double(curBalString),
let availCreditString = $0.dictionaryValue["available_credit"]?.string,
let availableCredit = Double(availCreditString),
let minPayDueString = $0.dictionaryValue["minimum_payment_due"]?.string,
let minPaymentDue = Double(minPayDueString),
let statementBalString = $0.dictionaryValue["statement_bal"]?.string,
let statementBalance = Double(statementBalString),
let accountName = $0.dictionaryValue["cc_program_name"]?.string {
let inAccount = Account(accountAlias, creditLimit, last4, paymentDueDate, currentBalance, availableCredit, minPaymentDue, statementBalance, accountName)
accountList.append(inAccount)
}
}
}
completion(accountList, nil)
case .failure(let error):
NSLog("Error: %s", error.localizedDescription)
completion([], error)
}
}
}
static func fetchTransactionsFor(accountAlias: String, completion: @escaping ([Transaction], Error?) -> Void) {
// get the transaction header and set value for account alias to the selected account
var header = Defaults.headerForTransaction
header["account_alias"] = accountAlias
var transactions: [Transaction] = []
Alamofire.request(Defaults.TRANS_HISTORY_URL, method: .post, parameters: header, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
switch payload.result {
case .success(let value):
let dict = JSON(value).dictionaryValue
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
//let's first parse the pending transactions
dict["pending_transaction_list"]?.arrayValue.forEach {
if let desc = $0["description"].string,
let date = Defaults.careCreditDateFormatter.date(from: $0["transaction_date"].stringValue),
let amountString = $0["transaction_amount"].string,
let amount = Double(amountString),
let confirmationNum = $0["payment_confirmation_number"].string,
let paymentIdString = $0["payment_id"].string,
let paymentId = Int(paymentIdString),
let modifiable = $0["is_payment_modifiable"].string {
let type: TransactionType = $0["payment_amount_type"].stringValue == "" ? .purchase : .reimbursement
let isModifiable: Bool = modifiable == "Y" ? true : false
transactions.append(Transaction(type: type, amount: amount, merchantID: desc, date: date, confirmationNum: confirmationNum, paymentId: paymentId, isPending: true, isModifiable: isModifiable))
}
}
// let's parse just processed transactions now
dict["processed_transaction_list"]?.arrayValue.forEach {
if let desc = $0["description"].string,
let date = Defaults.careCreditDateFormatter.date(from: $0["transaction_date"].stringValue),
let amountString = $0["transaction_amount"].string,
let amount = Double(amountString) {
let type: TransactionType = $0["payment_amount_type"].stringValue == "" ? .purchase : .reimbursement
transactions.append(Transaction(type: type, amount: amount, merchantID: desc, date: date, isPending: false))
}
}
completion(transactions, nil)
case .failure(let error):
NSLog("Error: %s", error.localizedDescription)
completion([], error)
}
}
}
static func getBankInfo(completion: @escaping ([BankAcct], Error?) -> Void) {
var bankIds: [BankAcct] = []
Alamofire.request(Defaults.FETCH_BANKS_URL, method: .post, parameters: Defaults.headerForMulti, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
switch payload.result {
case .success(let value):
let dict = JSON(value).dictionaryValue
//print(dict)
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else {return }
//"fetch_bank_account_details"
dict["fetch_bank_account_details"]?.arrayValue.forEach {
if let bankId = $0["bank_account_id"].string,
let bankName = $0["bank_name"].string,
let last4Acct = $0["last4_bank_acct_number"].string,
let accttype: AccountType = $0["bank_account_type"].stringValue ==
"C" ? .checkings : .savings {
bankIds.append(BankAcct(bankAcctId: bankId, acctType: accttype, bankName: bankName, last4Acct: last4Acct))
}
}
completion(bankIds, nil)
case .failure(let error):
NSLog("Error: %s", error.localizedDescription)
completion([], error)
}
}
}
static func cancelPayment(accountAlias: String, confirmationNum: String, paymentId: Int, completion: @escaping (String, Error?) -> Void) {
var paymentHeader = Defaults.headerForCancelPmt
paymentHeader["account_alias"] = accountAlias
paymentHeader["payment_confirmation_number"] = confirmationNum
paymentHeader["payment_id"] = paymentId
Alamofire.request(Defaults.CANCEL_PAYMENT_URL, method: .post, parameters: paymentHeader, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
switch payload.result {
case .success(let value):
let dict = JSON(value).dictionaryValue
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
if let paymentCancelNum = dict["cancellation_confirmation_number"]?.stringValue {
completion(paymentCancelNum, nil)
}
case .failure(let error):
NSLog("Error: \(error.localizedDescription)")
}
}
}
static func submitPayment(for alias: String, type: PaymentType, amount: Double, bankID: String, completion: @escaping (String, String, Error?) -> Void) {
var paymentHeader = Defaults.headerForPmt
paymentHeader["account_alias"] = alias
paymentHeader["bank_account_id"] = bankID
paymentHeader["payment_amount_type"] = type.rawValue
paymentHeader["payment_amount"] = amount
paymentHeader["scheduled_payment_post_date"] = Defaults.careCreditDateFormatter.string(from: Date())
Alamofire.request(Defaults.MAKE_PAYMENT_URL, method: .post, parameters: paymentHeader, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
switch payload.result {
case .success(let value):
let dict = JSON(value).dictionaryValue
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
if let paymentConfirmationNum = dict["payment_confirmation_number"]?.stringValue,
let paymentID = dict["payment_id"]?.stringValue {
completion(paymentConfirmationNum, paymentID, nil)
}
case .failure(let error):
NSLog("Error: \(error.localizedDescription)")
}
}
}
}
You can’t perform that action at this time.