From e85d2cb36e52d87070cdb4b17a57422bed7e3e6c Mon Sep 17 00:00:00 2001 From: Alan Maynard Date: Wed, 13 Mar 2019 21:29:24 -0400 Subject: [PATCH] SWF-21 initial web service setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - created `Defaults` to hold all of our url’s and other static info - set up initial login web service call --- .../ExtensionDelegate.swift | 48 ++++----- .../MainMenuInterfaceController.swift | 19 +++- .../project.pbxproj | 14 ++- .../SynchronyFinancial/Defaults.swift | 97 +++++++++++++++++++ 4 files changed, 151 insertions(+), 27 deletions(-) create mode 100644 SynchronyFinancial/SynchronyFinancial/Defaults.swift diff --git a/SynchronyFinancial/SynchronyFinancial WatchKit Extension/ExtensionDelegate.swift b/SynchronyFinancial/SynchronyFinancial WatchKit Extension/ExtensionDelegate.swift index eed655b..9d9c78e 100644 --- a/SynchronyFinancial/SynchronyFinancial WatchKit Extension/ExtensionDelegate.swift +++ b/SynchronyFinancial/SynchronyFinancial WatchKit Extension/ExtensionDelegate.swift @@ -27,28 +27,32 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate { // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one. for task in backgroundTasks { // Use a switch statement to check the task type - switch task { - case let backgroundTask as WKApplicationRefreshBackgroundTask: - // Be sure to complete the background task once you’re done. - backgroundTask.setTaskCompletedWithSnapshot(false) - case let snapshotTask as WKSnapshotRefreshBackgroundTask: - // Snapshot tasks have a unique completion call, make sure to set your expiration date - snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil) - case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask: - // Be sure to complete the connectivity task once you’re done. - connectivityTask.setTaskCompletedWithSnapshot(false) - case let urlSessionTask as WKURLSessionRefreshBackgroundTask: - // Be sure to complete the URL session task once you’re done. - urlSessionTask.setTaskCompletedWithSnapshot(false) - case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask: - // Be sure to complete the relevant-shortcut task once you're done. - relevantShortcutTask.setTaskCompletedWithSnapshot(false) - case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask: - // Be sure to complete the intent-did-run task once you're done. - intentDidRunTask.setTaskCompletedWithSnapshot(false) - default: - // make sure to complete unhandled task types - task.setTaskCompletedWithSnapshot(false) + if #available(watchOSApplicationExtension 5.0, *) { + switch task { + case let backgroundTask as WKApplicationRefreshBackgroundTask: + // Be sure to complete the background task once you’re done. + backgroundTask.setTaskCompletedWithSnapshot(false) + case let snapshotTask as WKSnapshotRefreshBackgroundTask: + // Snapshot tasks have a unique completion call, make sure to set your expiration date + snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil) + case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask: + // Be sure to complete the connectivity task once you’re done. + connectivityTask.setTaskCompletedWithSnapshot(false) + case let urlSessionTask as WKURLSessionRefreshBackgroundTask: + // Be sure to complete the URL session task once you’re done. + urlSessionTask.setTaskCompletedWithSnapshot(false) + case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask: + // Be sure to complete the relevant-shortcut task once you're done. + relevantShortcutTask.setTaskCompletedWithSnapshot(false) + case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask: + // Be sure to complete the intent-did-run task once you're done. + intentDidRunTask.setTaskCompletedWithSnapshot(false) + default: + // make sure to complete unhandled task types + task.setTaskCompletedWithSnapshot(false) + } + } else { + // Fallback on earlier versions } } } diff --git a/SynchronyFinancial/SynchronyFinancial WatchKit Extension/MainMenuInterfaceController.swift b/SynchronyFinancial/SynchronyFinancial WatchKit Extension/MainMenuInterfaceController.swift index e9c9906..7d55d77 100644 --- a/SynchronyFinancial/SynchronyFinancial WatchKit Extension/MainMenuInterfaceController.swift +++ b/SynchronyFinancial/SynchronyFinancial WatchKit Extension/MainMenuInterfaceController.swift @@ -8,6 +8,8 @@ import WatchKit import Foundation +import Alamofire +import SwiftyJSON class MainMenuInterfaceController: WKInterfaceController { @IBOutlet weak var accountsButton: WKInterfaceButton! @@ -16,7 +18,22 @@ class MainMenuInterfaceController: WKInterfaceController { override func awake(withContext context: Any?) { super.awake(withContext: context) - // Configure interface objects here. + // We should find a better place for this call, just for testing purposes + Alamofire.request(Defaults.LOGIN_URL, method: .post, parameters: Defaults.headerForLogin, encoding: JSONEncoding.default).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 } + if let token = dict["access_token"]?.string { + debugPrint(token) + // now we should save our token somewhere safe (UserDefaults) + UserDefaults.standard.set(token, forKey: "access_token") + } + case .failure(let error): + NSLog("Error: %s", error.localizedDescription) + } + } } override func willActivate() { diff --git a/SynchronyFinancial/SynchronyFinancial.xcodeproj/project.pbxproj b/SynchronyFinancial/SynchronyFinancial.xcodeproj/project.pbxproj index 9d5f11e..09140a7 100644 --- a/SynchronyFinancial/SynchronyFinancial.xcodeproj/project.pbxproj +++ b/SynchronyFinancial/SynchronyFinancial.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ 67BAC287219E254900713FEF /* SynchronyFinancial WatchKit Extension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 67BAC286219E254900713FEF /* SynchronyFinancial WatchKit Extension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 67BAC28E219E254900713FEF /* ExtensionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67BAC28D219E254900713FEF /* ExtensionDelegate.swift */; }; 67BAC292219E254900713FEF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 67BAC291219E254900713FEF /* Assets.xcassets */; }; + 67E17B86223812C2008871FE /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67E17B85223812C2008871FE /* Defaults.swift */; }; + 67E17B87223812C2008871FE /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67E17B85223812C2008871FE /* Defaults.swift */; }; A023013199652496572E43E0 /* libPods-SynchronyFinancial.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FC287DF4ECF0A5ECBD80C136 /* libPods-SynchronyFinancial.a */; }; BBECE7F685B263BB9794F863 /* libPods-SynchronyFinancial WatchKit App.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4EA10823B2E5A7A4FEF31740 /* libPods-SynchronyFinancial WatchKit App.a */; }; /* End PBXBuildFile section */ @@ -91,6 +93,7 @@ 67BAC28D219E254900713FEF /* ExtensionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionDelegate.swift; sourceTree = ""; }; 67BAC291219E254900713FEF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 67BAC293219E254900713FEF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 67E17B85223812C2008871FE /* Defaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Defaults.swift; sourceTree = ""; }; 9F53D0FFFE126D2BE8D08B80 /* Pods-SynchronyFinancial WatchKit App.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SynchronyFinancial WatchKit App.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SynchronyFinancial WatchKit App/Pods-SynchronyFinancial WatchKit App.debug.xcconfig"; sourceTree = ""; }; B4F433747E1B9A2ED14BA462 /* Pods-SynchronyFinancial.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SynchronyFinancial.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SynchronyFinancial/Pods-SynchronyFinancial.debug.xcconfig"; sourceTree = ""; }; CAA6D46F907ADAABF49FD409 /* libPods-SynchronyFinancial WatchKit Extension.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-SynchronyFinancial WatchKit Extension.a"; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -151,6 +154,7 @@ 67BAC267219E254700713FEF /* SynchronyFinancial */ = { isa = PBXGroup; children = ( + 67E17B85223812C2008871FE /* Defaults.swift */, 6717460B21F8FCC100696468 /* Account.swift */, 6717460921F8FCBE00696468 /* Transaction.swift */, 67BAC268219E254700713FEF /* AppDelegate.swift */, @@ -430,6 +434,7 @@ buildActionMask = 2147483647; files = ( 6717460A21F8FCBF00696468 /* Transaction.swift in Sources */, + 67E17B86223812C2008871FE /* Defaults.swift in Sources */, 67BAC269219E254700713FEF /* AppDelegate.swift in Sources */, 6717460C21F8FCC100696468 /* Account.swift in Sources */, ); @@ -441,6 +446,7 @@ files = ( 67BAC28E219E254900713FEF /* ExtensionDelegate.swift in Sources */, 673F396E21A644570051469E /* MainMenuInterfaceController.swift in Sources */, + 67E17B87223812C2008871FE /* Defaults.swift in Sources */, 6717460E21F8FCDB00696468 /* Transaction.swift in Sources */, 6717460D21F8FCD800696468 /* Account.swift in Sources */, ); @@ -624,7 +630,7 @@ SKIP_INSTALL = YES; SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = 4; - WATCHOS_DEPLOYMENT_TARGET = 5.1; + WATCHOS_DEPLOYMENT_TARGET = 4.2; }; name = Debug; }; @@ -647,7 +653,7 @@ SKIP_INSTALL = YES; SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = 4; - WATCHOS_DEPLOYMENT_TARGET = 5.1; + WATCHOS_DEPLOYMENT_TARGET = 4.2; }; name = Release; }; @@ -667,7 +673,7 @@ SKIP_INSTALL = YES; SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = 4; - WATCHOS_DEPLOYMENT_TARGET = 5.1; + WATCHOS_DEPLOYMENT_TARGET = 4.2; }; name = Debug; }; @@ -687,7 +693,7 @@ SKIP_INSTALL = YES; SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = 4; - WATCHOS_DEPLOYMENT_TARGET = 5.1; + WATCHOS_DEPLOYMENT_TARGET = 4.2; }; name = Release; }; diff --git a/SynchronyFinancial/SynchronyFinancial/Defaults.swift b/SynchronyFinancial/SynchronyFinancial/Defaults.swift new file mode 100644 index 0000000..d464095 --- /dev/null +++ b/SynchronyFinancial/SynchronyFinancial/Defaults.swift @@ -0,0 +1,97 @@ +// +// Defaults.swift +// SynchronyFinancial +// +// Created by Alan Maynard on 3/12/19. +// Copyright © 2019 Alan Maynard. All rights reserved. +// + +import Foundation + +final class Defaults { + + // MARK: - Request URLs + static let LOGIN_URL = "https://syf-paysol-mocks.getsandbox.com/paysol/login/api/rest/v1_0/login" + static let MULTI_ACCT_URL = "https://syf-paysol-mocks.getsandbox.com/paysol/account/api/rest/v1_0/multi_account_summary_details" + static let FETCH_BANKS_URL = "https://syf-paysol-mocks.getsandbox.com/paysol/payment/api/rest/v1_0/fetch_banks" + static let TRANS_HISTORY_URL = "https://syf-paysol-mocks.getsandbox.com/paysol/account/api/rest/v1_0/transaction_history" + static let CANCEL_PAYMENT_URL = "https://syf-paysol-mocks.getsandbox.com/paysol/payment/api/rest/v1_0/cancel_payment" + static let MAKE_PAYMENT_URL = "https://syf-paysol-mocks.getsandbox.com/paysol/payment/api/rest/v1_0/make_payment" + + static let defaultHeader = ["channel": "AW", "device_id": "apple_watch", "trans_id": "apple_watch"] + + static var headerForLogin: [String: Any] = { + return ["client_id": "carecredit", + "client_secret": "", + "grant_type": "password", + "header": defaultHeader, + "iovation": ["device_fingerprint": "apple_watch", + "ip_address": "0.0.0.0", + "mobile_latitude": 0.0, + "mobile_longitude": 0.0], + "password": "Test12test", + "scope": "default", + "username": "carecredit"] + }() + + // this header is used for multi-account and fetch banks resources + static var headerForMulti: [String: Any] = { + return ["header": defaultHeader, + "username": "carecredit"] + }() + + static var headerForTransaction: [String: Any] = { + // get account alias + var account_alias = "" + + // get today's date as ISO860 - YYYYMMDD string + var end_date = "" + + // get desired start date as ISO860 - YYYYMMDD string + var start_date = "" + + return ["account_alias": account_alias, + "begin_sequence": "1", + "end_date": end_date, + "end_sequence": "30", + "header": defaultHeader, + "scope_indicator": "B", + "start_date": start_date, + "username": "carecredit"] + }() + + static var headerForCancelPmt: [String: Any] = { + // get account_alias + var account_alias = "" + + // get payment confirmation number + var pmt_confirmation = "" + + // get payment id + var payment_id = "" + + return ["account_alias": account_alias, + "header": defaultHeader, + "scope_indicator": "B", + "payment_confirmation_number": pmt_confirmation, + "payment_id": payment_id, + "username": "carecredit"] + }() + + + static var headerForPmt: [String: Any] = { + // get account alias + var account_alias = "" + + // get bank account id to pay from + var bank_acct_id = "" + + return ["account_alias": account_alias, + "bank_account_id": bank_acct_id, + "header": defaultHeader, + "payment_amount": 123.45, + "payment_amount_type": "CBL", + "scheduled_payment_post_date": "20190306", + "username": "carecredit"] + }() +}