Skip to content

Commit

Permalink
Merge pull request #2 from AssemblyPayments/spi-2.1
Browse files Browse the repository at this point in the history
SPI 2.1 Samples
  • Loading branch information
mgouline authored Jul 3, 2018
2 parents 29b85ad + 0350a29 commit b105f4d
Show file tree
Hide file tree
Showing 163 changed files with 26,591 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ xcuserdata/
*.ipa
*.dSYM.zip
*.dSYM

834 changes: 834 additions & 0 deletions kebabpos/KebabPos.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions kebabpos/KebabPos.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
21 changes: 21 additions & 0 deletions kebabpos/KebabPos/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// AppDelegate.swift
// KebabPos
//
// Created by Amir Kamali on 29/5/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
KebabApp.current.initialize()
return true
}

}
782 changes: 782 additions & 0 deletions kebabpos/KebabPos/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions kebabpos/KebabPos/Logics/Enums/AppEvents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AppEvents.swift
// KebabPos
//
// Created by Amir Kamali on 28/5/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation

enum AppEvent: String {
case connectionStatusChanged
case pairingFlowChanged
case transactionFlowStateChanged
}
74 changes: 74 additions & 0 deletions kebabpos/KebabPos/Logics/Extensions/SPIExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// SPIExtensions.swift
// KebabPos
//
// Created by Amir Kamali on 6/6/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation
import SPIClient_iOS

extension SPIMessageSuccessState {
var name: String {
switch self {
case .failed:
return "failed"
case .success:
return "success"
case .unknown:
return "unknown"
}
}
}

extension SPIStatus {
var name: String {
switch self {
case .unpaired:
return "Unpaired"
case .pairedConnecting:
return "Connecting"
case .pairedConnected:
return "Connected"
}
}
}

extension SPIFlow {
var name: String {
switch self {
case .idle:
return "Idle"
case .pairing:
return "Pairing"
case .transaction:
return "Transaction"
}
}
}

extension SPITransactionType {
var name: String {
switch self {
case .getLastTransaction:
return "Get Last Transaction"
case .purchase:
return "Purchase"
case .refund:
return "Refund"
case .settle:
return "Settle"
case .cashoutOnly:
return "Cashout Only"
case .MOTO:
return "MOTO"
case .settleEnquiry:
return "Settle Enquiry"
case .preAuth:
return "Pre Auth"
case .accountVerify:
return "Account Verify"
}
}
}
65 changes: 65 additions & 0 deletions kebabpos/KebabPos/Logics/Extensions/UIViewController+Alert.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// UIViewController+Alert.swift
// KebabPos
//
// Created by Amir Kamali on 29/5/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation
import UIKit

var remainingAlerts:[UIAlertController] = []

extension UIViewController {

func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
showAlert(alertController: alertController)
}

func showAlert(alertController: UIAlertController) {
func displayNext(){
if remainingAlerts.count > 0 {
let nextAlert = remainingAlerts.remove(at: 0)
self.showAlert(alertController: nextAlert)
}
}

func display() {
DispatchQueue.main.async {
self.present(alertController, animated: true) {
displayNext()
}
}
}

if (presentedViewController == nil) {
display()
} else if let existingAlert = presentedViewController as? UIAlertController {
if (!existingAlert.isBeingDismissed && !existingAlert.isBeingPresented){
existingAlert.dismiss(animated: false, completion: {
display()
})
} else {
addToRemaining(alert: alertController)
}
return
}
}

// In case multiple alerts needs to be shown in a sequence, we keep them in an array to display later
private func addToRemaining(alert:UIAlertController){
if let existingAlert = presentedViewController as? UIAlertController , existingAlert.message == alert.message {
return
}
else if remainingAlerts.isEmpty{
remainingAlerts.append(alert)
} else if let lastAlert = remainingAlerts.last, lastAlert.message != alert.message {
//if not duplicated messages
remainingAlerts.append(alert)
}
}

}
37 changes: 37 additions & 0 deletions kebabpos/KebabPos/Logics/Extensions/toString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Date+String.swift
// KebabPos
//
// Created by Amir Kamali on 6/6/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation

extension Date {

func toString(includeTime: Bool = false) -> String {
var format = ""
if (includeTime) {
format = "dd/MM/yyyy hh:mm a"
} else {
format = "dd/MM/yyyy"
}
return toString(format: format)
}

func toString(format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}

}

extension Bool {

func toString() -> String {
return self ? "true" : "false"
}

}
61 changes: 61 additions & 0 deletions kebabpos/KebabPos/Logics/KebabApp+SPIConnectionDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// SPIConnectionDelegate.swift
// KebabPos
//
// Created by Amir Kamali on 28/5/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation
import SPIClient_iOS

extension KebabApp: SPIDelegate {

/// Called when we received a Status Update i.e. Unpaired/PairedConnecting/PairedConnected
func spi(_ spi: SPIClient, statusChanged state: SPIState) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: AppEvent.connectionStatusChanged.rawValue), object: state)

SPILogMsg("statusChanged \(state)")
}

// Called during the pairing process to let us know how it's going.
// We just update our screen with the information, and provide relevant Actions to the user.
func spi(_ spi: SPIClient, pairingFlowStateChanged state: SPIState) {
SPILogMsg("pairingFlowStateChanged \(state)")

NotificationCenter.default.post(name: NSNotification.Name(rawValue: AppEvent.pairingFlowChanged.rawValue), object: state)
}

func spi(_ spi: SPIClient!, secretsChanged secrets: SPISecrets?, state: SPIState!) {
SPILogMsg("secrets \(state)")
if let secrets = secrets {
SPILogMsg("\n\n")
SPILogMsg("# ----------- I GOT NEW SECRETS -----------")
SPILogMsg("# ---------- PERSIST THEM SAFELY ----------")
SPILogMsg("# \(secrets.encKey):\(secrets.hmacKey)")
SPILogMsg("# -----------------------------------------")

// In prod store them in the key chain
settings.encriptionKey = secrets.encKey!
settings.hmacKey = secrets.hmacKey!
} else {
SPILogMsg("\n\n")
SPILogMsg("# ------ THE SECRETS HAVE BEEN VOIDED -----")
SPILogMsg("# ---------- CONSIDER ME UNPAIRED ---------")
SPILogMsg("# -----------------------------------------")

settings.encriptionKey = ""
settings.hmacKey = ""
}
}

// Called during a transaction to let us know how it's going.
// We just update our screen with the information, and provide relevant Actions to the user.
func spi(_ spi: SPIClient, transactionFlowStateChanged state: SPIState) {
SPILogMsg("transactionFlowStateChanged \(state)")

// Let's show the user what options he has at this stage.
NotificationCenter.default.post(name: NSNotification.Name(rawValue: AppEvent.transactionFlowStateChanged.rawValue), object: state)
}

}
43 changes: 43 additions & 0 deletions kebabpos/KebabPos/Logics/KebabApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// KebabContext.swift
// KebabPos
//
// Created by Amir Kamali on 28/5/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation
import SPIClient_iOS

class KebabApp: NSObject {

private static var _instance: KebabApp = KebabApp()

var settings = SettingsProvider()
var client = SPIClient()

static var current: KebabApp {
return _instance
}

func initialize() {
guard let eftposAddress = settings.eftposAddress else { return }
guard let eftposId = settings.posId else { return }

if let encriptionKey = settings.encriptionKey, let hmacKey = settings.hmacKey {
SPILogMsg("LOADED KEYS FROM USERDEFAULTS")
SPILogMsg("KEYS \(encriptionKey):\(hmacKey)")
client.setSecretEncKey(encriptionKey, hmacKey: hmacKey)
}
client.eftposAddress = eftposAddress
client.posId = eftposId
client.config.signatureFlowOnEftpos = settings.customerSignatureromEFTPos ?? false
client.config.promptForCustomerCopyOnEftpos = settings.customerReceiptFromEFTPos ?? false
client.delegate = self
}

func start() {
client.start()
}

}
22 changes: 22 additions & 0 deletions kebabpos/KebabPos/Logics/Protocols/NotificationListener.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// NotificationListener.swift
// KebabPos
//
// Created by Amir Kamali on 29/5/18.
// Copyright © 2018 Assembly Payments. All rights reserved.
//

import Foundation

@objc
protocol NotificationListener {
@objc func onNotificationArrived(notification: NSNotification)
}

extension NotificationListener {
func registerForEvents(appEvents: [AppEvent]) {
for event in appEvents {
NotificationCenter.default.addObserver(self, selector: #selector(onNotificationArrived), name: NSNotification.Name(rawValue: event.rawValue), object: nil)
}
}
}
Loading

0 comments on commit b105f4d

Please sign in to comment.