Skip to content
Permalink
Newer
Older
100644 174 lines (160 sloc) 9.3 KB
1
//
2
// FetchData.swift
3
// SynchronyFinancial WatchKit Extension
4
//
5
// Created by Rahul Kantesaria on 3/14/19.
6
// Copyright © 2019 Alan Maynard. All rights reserved.
7
//
8
9
import Foundation
10
import Alamofire
11
import SwiftyJSON
12
13
class FetchData {
14
static func attemptLogin(completion: @escaping (Bool, Error?) -> Void) {
15
Alamofire.request(Defaults.LOGIN_URL, method: .post, parameters: Defaults.headerForLogin, encoding: JSONEncoding.default).responseJSON { payload in
16
switch payload.result {
17
case .success(let value):
18
let dict = JSON(value).dictionaryValue
19
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else {
20
completion(false, NSError())
21
return
22
}
23
24
if let token = dict["access_token"]?.string {
25
// now we should save our token somewhere safe (UserDefaults)
26
UserDefaults.standard.set(token, forKey: "access_token")
27
completion(true, nil)
28
}
29
case .failure(let error):
30
NSLog("Error: %s", error.localizedDescription)
31
completion(false, error)
32
}
33
}
34
}
35
36
static func getAccountInfo(completion: @escaping ([Account], Error?) -> Void) {
37
var accountList: [Account] = []
38
Alamofire.request(Defaults.MULTI_ACCT_URL, method: .post, parameters: Defaults.headerForMulti, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
39
switch payload.result {
40
case .success(let value):
41
let json = JSON(value)
42
let dict = json.dictionaryValue
43
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
45
46
let formatter = DateFormatter()
47
formatter.locale = Locale.current
48
formatter.dateFormat = "yyyyMMdd"
49
if let accounts = dict["account_number_list"]?.arrayValue {
50
accounts.forEach {
51
if let accountAlias = $0.dictionaryValue["account_alias"]?.string,
52
let creditLimitString = $0.dictionaryValue["credit_limit"]?.string,
53
let creditLimit = Double(creditLimitString),
54
let payDueDateString = $0.dictionaryValue["next_payment_due_date"]?.string,
55
let paymentDueDate = formatter.date(from: payDueDateString),
56
let curBalString = $0.dictionaryValue["current_balance"]?.string,
57
let currentBalance = Double(curBalString),
58
let availCreditString = $0.dictionaryValue["available_credit"]?.string,
59
let availableCredit = Double(availCreditString),
60
let minPayDueString = $0.dictionaryValue["minimum_payment_due"]?.string,
61
let minPaymentDue = Double(minPayDueString),
62
let statementBalString = $0.dictionaryValue["statement_bal"]?.string,
63
let statementBalance = Double(statementBalString),
64
let accountName = $0.dictionaryValue["cc_program_name"]?.string {
65
let inAccount = Account(accountAlias: accountAlias, creditLimit: creditLimit, paymentDueDate: paymentDueDate, curBalance: currentBalance, availCredit: availableCredit, minPayDue: minPaymentDue, statementBal: statementBalance, accountName: accountName)
66
accountList.append(inAccount)
67
}
68
}
70
completion(accountList, nil)
71
case .failure(let error):
72
NSLog("Error: %s", error.localizedDescription)
73
completion([], error)
77
78
static func fetchTransactionsFor(accountAlias: String, completion: @escaping ([Transaction], Error?) -> Void) {
79
// get the transaction header and set value for account alias to the selected account
80
var header = Defaults.headerForTransaction
81
header["account_alias"] = accountAlias
82
var transactions: [Transaction] = []
83
let formatter = DateFormatter()
84
formatter.locale = Locale.current
85
formatter.dateFormat = "yyyyMMdd"
86
87
Alamofire.request(Defaults.TRANS_HISTORY_URL, method: .post, parameters: header, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
88
switch payload.result {
89
case .success(let value):
90
let dict = JSON(value).dictionaryValue
91
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
92
93
//let's first parse the pending transactions
94
dict["pending_transaction_list"]?.arrayValue.forEach {
95
if let desc = $0["description"].string,
96
let date = formatter.date(from: $0["transaction_date"].stringValue),
97
let amountString = $0["transaction_amount"].string,
98
let amount = Double(amountString),
99
let confirmationNum = $0["payment_confirmation_number"].string,
100
let paymentIdString = $0["payment_id"].string,
101
let paymentId = Int(paymentIdString),
102
let modifiable = $0["is_payment_modifiable"].string {
103
let type: TransactionType = $0["payment_amount_type"].stringValue == "" ? .purchase : .reimbursement
104
let isModifiable: Bool = modifiable == "Y" ? true : false
105
106
transactions.append(Transaction(type: type, amount: amount, merchantID: desc, date: date, confirmationNum: confirmationNum, paymentId: paymentId, isPending: true, isModifiable: isModifiable))
107
}
108
}
109
110
// let's parse just processed transactions now
111
dict["processed_transaction_list"]?.arrayValue.forEach {
112
if let desc = $0["description"].string,
113
let date = formatter.date(from: $0["transaction_date"].stringValue),
114
let amountString = $0["transaction_amount"].string,
115
let amount = Double(amountString) {
116
let type: TransactionType = $0["payment_amount_type"].stringValue == "" ? .purchase : .reimbursement
117
transactions.append(Transaction(type: type, amount: amount, merchantID: desc, date: date, isPending: false))
118
}
119
}
120
completion(transactions, nil)
121
case .failure(let error):
122
NSLog("Error: %s", error.localizedDescription)
123
completion([], error)
124
}
125
}
126
}
127
128
static func getBankInfo(completion: @escaping ([BankAcct], Error?) -> Void) {
129
var bankIds: [BankAcct] = []
130
Alamofire.request(Defaults.FETCH_BANKS_URL, method: .post, parameters: Defaults.headerForMulti, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
131
switch payload.result {
132
case .success(let value):
133
let dict = JSON(value).dictionaryValue
134
//print(dict)
135
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else {return }
136
//"fetch_bank_account_details"
137
dict["fetch_bank_account_details"]?.arrayValue.forEach {
138
if let bankId = $0["bank_account_id"].string,
139
let accttype: AccountType = $0["bank_account_type"].stringValue ==
140
"C" ? .checkings : .savings {
141
//print(bankId)
142
//print(accttype)
143
bankIds.append(BankAcct(bankAcctId: bankId, acctType: accttype))
144
}
145
}
146
completion(bankIds, nil)
147
case .failure(let error):
148
NSLog("Error: %s", error.localizedDescription)
149
completion([], error)
150
}
151
}
152
}
153
154
static func cancelPayment(accountAlias: String, confirmationNum: String, paymentId: Int, completion: @escaping (String, Error?) -> Void){
155
156
var paymentHeader = Defaults.headerForCancelPmt
157
paymentHeader["account_alias"] = accountAlias
158
paymentHeader["payment_confirmation_number"] = confirmationNum
159
paymentHeader["payment_id"] = paymentId
160
161
Alamofire.request(Defaults.CANCEL_PAYMENT_URL, method: .post, parameters: paymentHeader, encoding: JSONEncoding.default, headers: Defaults.authHeader).responseJSON { payload in
162
switch payload.result {
163
case .success(let value):
164
let dict = JSON(value).dictionaryValue
165
guard dict["status"]?.dictionaryValue["response_code"]?.string == "0" else { return }
166
if let paymentCancelNum = dict["cancellation_confirmation_number"]?.stringValue {
167
completion(paymentCancelNum, nil)
168
}
169
case .failure(let error):
170
NSLog("Error: \(error.localizedDescription)")
171
}
172
}
173
}
You can’t perform that action at this time.