From 0bab6240a95d622df0df44c4aec6d9c97b30dd5f Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 12:57:00 +0500 Subject: [PATCH 01/11] qr readers integrated --- src/ios/MoltinCordovaSquareReader.swift | 53 +++-- src/ios/QRAuthorizationViewController.swift | 245 ++++++++++++++++++++ src/ios/SecondaryButton.swift | 51 ++++ www/MoltinCordovaSquareReader.js | 8 +- 4 files changed, 339 insertions(+), 18 deletions(-) create mode 100755 src/ios/QRAuthorizationViewController.swift create mode 100755 src/ios/SecondaryButton.swift diff --git a/src/ios/MoltinCordovaSquareReader.swift b/src/ios/MoltinCordovaSquareReader.swift index 4429d3a..5ec8234 100644 --- a/src/ios/MoltinCordovaSquareReader.swift +++ b/src/ios/MoltinCordovaSquareReader.swift @@ -2,7 +2,7 @@ import SquareReaderSDK import CoreLocation import AVKit -@objc(MoltinCordovaSquareReader) class MoltinCordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate { +@objc(MoltinCordovaSquareReader) class MoltinCordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate,QRAuthorizationViewControllerDelegate { private lazy var locationManager = CLLocationManager() private var currentCommand: CDVInvokedUrlCommand? @@ -13,6 +13,26 @@ import AVKit self.locationManager.delegate = self + func qrAuthorizationViewController(_ qrAuthorizationViewController: QRAuthorizationViewController, didRecognizeAuthorizationCode code: String) { + self.viewController.dismiss(animated: true, completion: nil) + + SQRDReaderSDK.shared.authorize(withCode: code) { location, error in + if let authError = error { + // Handle the error + print(authError) + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: authError.localizedDescription), callbackId: self.recentCommand!.callbackId) + } + else { + // Proceed to the main application interface. + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: self.recentCommand!.callbackId) + } + } + } + + func qrAuthorizationViewControllerDidCancel(_ qrAuthorizationViewController: QRAuthorizationViewController) { + self.viewController.dismiss(animated: true, completion: nil) + } + func requestLocationPermission(callback: @escaping (Bool) -> ()) { switch CLLocationManager.authorizationStatus() { case .notDetermined: @@ -73,21 +93,11 @@ import AVKit print("Already authorized.") self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: command.callbackId) } else { - guard let commandParams = command.arguments.first as? [String: Any], - let authCode = commandParams["authCode"] as? String else { - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "No parameters"), callbackId: command.callbackId) - return - } - SQRDReaderSDK.shared.authorize(withCode: authCode) { location, error in - if let authError = error { - // Handle the error - print(authError) - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: authError.localizedDescription), callbackId: command.callbackId) - } - else { - // Proceed to the main application interface. - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: command.callbackId) - } + if (QRAuthorizationViewController.canScanQRCodes){ + self.currentCommand = command + let qrAuthorizationViewController = QRAuthorizationViewController() + qrAuthorizationViewController.delegate = self + self.viewController.present(qrAuthorizationViewController, animated: true, completion: nil) } } } @@ -126,6 +136,17 @@ import AVKit readerSettingsController.present(from: self.viewController) } + @objc(deauthorize:) + func deauthorize(command: CDVInvokedUrlCommand) { + SQRDReaderSDK.shared.deauthorize { (error) in + if (error == nil){ + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK, messageAs: "Success"), callbackId: command.callbackId) + } + else{ + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error?.localizedDescription), callbackId: command.callbackId) + } + } + } @objc(checkoutControllerDidCancel:) func checkoutControllerDidCancel( diff --git a/src/ios/QRAuthorizationViewController.swift b/src/ios/QRAuthorizationViewController.swift new file mode 100755 index 0000000..454729b --- /dev/null +++ b/src/ios/QRAuthorizationViewController.swift @@ -0,0 +1,245 @@ +// +// Copyright © 2018 Square, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import UIKit +import AVFoundation +import SquareReaderSDK + +protocol QRAuthorizationViewControllerDelegate: class { + func qrAuthorizationViewController(_ qrAuthorizationViewController: QRAuthorizationViewController, didRecognizeAuthorizationCode code: String) + func qrAuthorizationViewControllerDidCancel(_ qrAuthorizationViewController: QRAuthorizationViewController) +} + +class QRAuthorizationViewController: UIViewController { + public weak var delegate: QRAuthorizationViewControllerDelegate? + + lazy var captureSession = AVCaptureSession() + lazy var previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) + + lazy var labelContainerView = makeContainerView() + lazy var label = makeLabel() + lazy var previewView = makePreviewView() + lazy var cancelButtonContainerView = makeContainerView() + lazy var cancelButton = makeCancelButton() + + // MARK: - State + static var canScanQRCodes: Bool { + return AVCaptureDevice.default(for: .video) != nil + } + + static var isCameraPermissionGranted: Bool { + return AVCaptureDevice.authorizationStatus(for: .video) == .authorized + } + + deinit { + captureSession.stopRunning() + UIDevice.current.endGeneratingDeviceOrientationNotifications() + } + + // MARK: - View lifecycle + override func viewDidLoad() { + super.viewDidLoad() + + if #available(iOS 11.0, *) { + additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 32, bottom: 32, right: 32) + } else { + // Fallback on earlier versions + } + view.backgroundColor = .black + + labelContainerView.addSubview(label) + view.addSubview(previewView) + view.addSubview(labelContainerView) + cancelButtonContainerView.addSubview(cancelButton) + view.addSubview(cancelButtonContainerView) + + // Observe changes to device orientation + UIDevice.current.beginGeneratingDeviceOrientationNotifications() + NotificationCenter.default.addObserver(self, selector: #selector(updateVideoOrientation), name: Notification.Name.UIDeviceOrientationDidChange, object: UIDevice.current) + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + if #available(iOS 11.0, *) { + NSLayoutConstraint.activate([ + previewView.topAnchor.constraint(equalTo: view.topAnchor), + previewView.bottomAnchor.constraint(equalTo: view.bottomAnchor), + previewView.leftAnchor.constraint(equalTo: view.leftAnchor), + previewView.rightAnchor.constraint(equalTo: view.rightAnchor), + + labelContainerView.topAnchor.constraint(equalTo: view.topAnchor), + labelContainerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 80), + labelContainerView.leftAnchor.constraint(equalTo: view.leftAnchor), + labelContainerView.rightAnchor.constraint(equalTo: view.rightAnchor), + + label.bottomAnchor.constraint(equalTo: labelContainerView.bottomAnchor), + label.leftAnchor.constraint(equalTo: labelContainerView.leftAnchor), + label.rightAnchor.constraint(equalTo: labelContainerView.rightAnchor), + label.heightAnchor.constraint(equalToConstant: 80), + + cancelButtonContainerView.topAnchor.constraint(equalTo: cancelButtonContainerView.safeAreaLayoutGuide.bottomAnchor, constant: -96), + cancelButtonContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor), + cancelButtonContainerView.leftAnchor.constraint(equalTo: view.leftAnchor), + cancelButtonContainerView.rightAnchor.constraint(equalTo: view.rightAnchor), + + cancelButton.bottomAnchor.constraint(equalTo: cancelButtonContainerView.safeAreaLayoutGuide.bottomAnchor), + cancelButton.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor), + cancelButton.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor), + ]) + } else { + // Fallback on earlier versions + } + + updatePreviewLayerSize() + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + if QRAuthorizationViewController.isCameraPermissionGranted { + startCaptureSession() + } else { + requestCameraPermission() + } + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + + previewLayer.frame = view.bounds + } + + override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransition(to: size, with: coordinator) + + updateVideoOrientation() + } + + override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { + super.traitCollectionDidChange(previousTraitCollection) + + updatePreviewLayerSize() + } + + func updatePreviewLayerSize() { + // Manually set the frame of the layer + view.setNeedsLayout() + } + + @objc func updateVideoOrientation() { + if let connection = previewLayer.connection, connection.isVideoOrientationSupported { + switch UIDevice.current.orientation { + case .portrait: + connection.videoOrientation = .portrait + case .portraitUpsideDown: + connection.videoOrientation = .portraitUpsideDown + case .landscapeLeft: + connection.videoOrientation = .landscapeRight + case .landscapeRight: + connection.videoOrientation = .landscapeLeft + default: + break + } + } + } + + @objc func cancel() { + captureSession.stopRunning() + delegate?.qrAuthorizationViewControllerDidCancel(self) + } +} + +// MARK: - Camera +extension QRAuthorizationViewController: AVCaptureMetadataOutputObjectsDelegate { + func requestCameraPermission() { + AVCaptureDevice.requestAccess(for: .video) { (granted) in + if granted { + DispatchQueue.main.async { + self.startCaptureSession() + } + } else if let url = URL(string: UIApplicationOpenSettingsURLString) { + UIApplication.shared.open(url, options: [:], completionHandler: nil) + } + } + } + + func startCaptureSession() { + // Add the input to the capture session + let captureDevice = AVCaptureDevice.default(for: .video)! + let input = try! AVCaptureDeviceInput(device: captureDevice) + captureSession.addInput(input) + + // Capture metadata output from the session + let captureMetadataOutput = AVCaptureMetadataOutput() + captureSession.addOutput(captureMetadataOutput) + + // Add ourselves as the delegate, only recognize QR codes + captureMetadataOutput.setMetadataObjectsDelegate(self, queue: .main) + captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr] + + // Add the preview layer and start the capture session + captureSession.startRunning() + + // Update the video orientation to match the current device orientation + updateVideoOrientation() + } + + func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { + guard let object = metadataObjects.first as? AVMetadataMachineReadableCodeObject, object.type == .qr, + let code = object.stringValue else { return } + + // Stop capturing frames + captureSession.stopRunning() + + // Notify delegate + delegate?.qrAuthorizationViewController(self, didRecognizeAuthorizationCode: code) + } +} + +// MARK: - UI +extension QRAuthorizationViewController { + private func makeContainerView() -> UIView { + let containerView = UIView() + containerView.backgroundColor = UIColor.black.withAlphaComponent(0.75) + containerView.translatesAutoresizingMaskIntoConstraints = false + return containerView + } + + private func makeLabel() -> UILabel { + let label = UILabel() + label.text = "Scan a QR code." + label.textColor = .white + label.textAlignment = .center + label.font = UIFont.systemFont(ofSize: 24, weight: .semibold) + label.layer.cornerRadius = 8 + label.layer.masksToBounds = true + label.translatesAutoresizingMaskIntoConstraints = false + return label + } + + private func makePreviewView() -> UIView { + let previewView = UIView() + previewLayer.videoGravity = .resizeAspectFill + previewView.layer.insertSublayer(previewLayer, at: 0) + previewView.translatesAutoresizingMaskIntoConstraints = false + return previewView + } + + private func makeCancelButton() -> UIButton { + return SecondaryButton(title: "Cancel", target: self, selector: #selector(cancel)) + } +} diff --git a/src/ios/SecondaryButton.swift b/src/ios/SecondaryButton.swift new file mode 100755 index 0000000..1011fae --- /dev/null +++ b/src/ios/SecondaryButton.swift @@ -0,0 +1,51 @@ +// +// Copyright © 2018 Square, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import UIKit + +class SecondaryButton: UIButton { + struct Color { + static let title = UIColor.white + static let disabledTitle = #colorLiteral(red: 0.6424661279, green: 0.7832983136, blue: 0.947729528, alpha: 1) + static let background = UIColor.clear + static let highlightedBackground = UIColor.white.withAlphaComponent(0.1) + static let border = UIColor.white.withAlphaComponent(0.7) + } + + override var isHighlighted: Bool { + didSet { + backgroundColor = isHighlighted ? Color.highlightedBackground : Color.background + } + } + + convenience init(title: String, target: Any, selector: Selector) { + self.init() + + // Default state + setTitle(title, for: []) + setTitleColor(Color.title, for: []) + setTitleColor(Color.disabledTitle, for: .disabled) + titleLabel?.font = .systemFont(ofSize: 20, weight: .semibold) + backgroundColor = Color.background + translatesAutoresizingMaskIntoConstraints = false + heightAnchor.constraint(equalToConstant: 64).isActive = true + layer.cornerRadius = 8 + layer.masksToBounds = true + layer.borderColor = SecondaryButton.Color.border.cgColor + layer.borderWidth = 1.0 + addTarget(target, action: selector, for: .touchUpInside) + } +} diff --git a/www/MoltinCordovaSquareReader.js b/www/MoltinCordovaSquareReader.js index 6f661fe..23ceba7 100644 --- a/www/MoltinCordovaSquareReader.js +++ b/www/MoltinCordovaSquareReader.js @@ -5,8 +5,8 @@ MoltinCordovaSquareReader.prototype.setup = function (successCallback, errorCall cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "setup", []); }; -MoltinCordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (params, successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "authorizeReaderSDKIfNeeded", [params]); +MoltinCordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "authorizeReaderSDKIfNeeded", []); }; MoltinCordovaSquareReader.prototype.startCheckout = function (params, successCallback, errorCallback) { @@ -22,4 +22,8 @@ MoltinCordovaSquareReader.install = function () { return window.squarereader; }; +MoltinCordovaSquareReader.prototype.deauthorize = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "deauthorize", []); +}; + cordova.addConstructor(MoltinCordovaSquareReader.install); From ee9c03a80bffdaa72f04645fc3bc0efa0057bde9 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 14:36:40 +0500 Subject: [PATCH 02/11] Renamed --- package.json | 6 ++-- plugin.xml | 16 +++++----- ...wift => HabibAliCordovaSquareReader.swift} | 2 +- www/HabibAliCordovaSquareReader.js | 29 +++++++++++++++++++ www/MoltinCordovaSquareReader.js | 29 ------------------- 5 files changed, 41 insertions(+), 41 deletions(-) rename src/ios/{MoltinCordovaSquareReader.swift => HabibAliCordovaSquareReader.swift} (97%) create mode 100644 www/HabibAliCordovaSquareReader.js delete mode 100644 www/MoltinCordovaSquareReader.js diff --git a/package.json b/package.json index f70033e..f2a6e10 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "@moltin/cordova-plugin-square-reader", + "name": "@habibali/cordova-plugin-square-reader", "version": "0.2.0", "description": "Cordova Plugin to interface with the Square Reader SDK", "cordova": { - "id": "moltin-cordova-plugin-square-reader", + "id": "habibali-cordova-plugin-square-reader", "platforms": [ "android", "ios" @@ -14,7 +14,7 @@ "cordova-android", "cordova-ios" ], - "author": "Craig Tweedy", + "author": "Habib Ali", "license": "MIT", "publishConfig": { "access": "public" diff --git a/plugin.xml b/plugin.xml index c727818..79e6200 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,20 +1,20 @@ - - MoltinCordovaSquareReader + HabibAliCordovaSquareReader - - + + - - + + @@ -46,7 +46,7 @@ - + - \ No newline at end of file + diff --git a/src/ios/MoltinCordovaSquareReader.swift b/src/ios/HabibAliCordovaSquareReader.swift similarity index 97% rename from src/ios/MoltinCordovaSquareReader.swift rename to src/ios/HabibAliCordovaSquareReader.swift index 5ec8234..dafff26 100644 --- a/src/ios/MoltinCordovaSquareReader.swift +++ b/src/ios/HabibAliCordovaSquareReader.swift @@ -2,7 +2,7 @@ import SquareReaderSDK import CoreLocation import AVKit -@objc(MoltinCordovaSquareReader) class MoltinCordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate,QRAuthorizationViewControllerDelegate { +@objc(HabibAliCordovaSquareReader) class HabibAliCordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate,QRAuthorizationViewControllerDelegate { private lazy var locationManager = CLLocationManager() private var currentCommand: CDVInvokedUrlCommand? diff --git a/www/HabibAliCordovaSquareReader.js b/www/HabibAliCordovaSquareReader.js new file mode 100644 index 0000000..065ef0c --- /dev/null +++ b/www/HabibAliCordovaSquareReader.js @@ -0,0 +1,29 @@ +function HabibAliCordovaSquareReader() { +} + +HabibAliCordovaSquareReader.prototype.setup = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "setup", []); +}; + +HabibAliCordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "authorizeReaderSDKIfNeeded", []); +}; + +HabibAliCordovaSquareReader.prototype.startCheckout = function (params, successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "startCheckout", [params]); +}; + +HabibAliCordovaSquareReader.prototype.pairCardReaders = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "pairCardReaders", []); +}; + +HabibAliCordovaSquareReader.prototype.deauthorize = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "deauthorize", []); +}; + +HabibAliCordovaSquareReader.install = function () { + window.squarereader = new HabibAliCordovaSquareReader(); + return window.squarereader; +}; + +cordova.addConstructor(HabibAliCordovaSquareReader.install); diff --git a/www/MoltinCordovaSquareReader.js b/www/MoltinCordovaSquareReader.js deleted file mode 100644 index 23ceba7..0000000 --- a/www/MoltinCordovaSquareReader.js +++ /dev/null @@ -1,29 +0,0 @@ -function MoltinCordovaSquareReader() { -} - -MoltinCordovaSquareReader.prototype.setup = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "setup", []); -}; - -MoltinCordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "authorizeReaderSDKIfNeeded", []); -}; - -MoltinCordovaSquareReader.prototype.startCheckout = function (params, successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "startCheckout", [params]); -}; - -MoltinCordovaSquareReader.prototype.pairCardReaders = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "pairCardReaders", []); -}; - -MoltinCordovaSquareReader.install = function () { - window.squarereader = new MoltinCordovaSquareReader(); - return window.squarereader; -}; - -MoltinCordovaSquareReader.prototype.deauthorize = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "MoltinCordovaSquareReader", "deauthorize", []); -}; - -cordova.addConstructor(MoltinCordovaSquareReader.install); From 0060af4f78874750fd017112fe9a28b587404920 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 15:47:19 +0500 Subject: [PATCH 03/11] files added --- plugin.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin.xml b/plugin.xml index 79e6200..8c7a4dd 100644 --- a/plugin.xml +++ b/plugin.xml @@ -47,6 +47,8 @@ + + From a7285b4892b4ece490ccbc2c7b9bd94b14ed469f Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 15:56:12 +0500 Subject: [PATCH 04/11] compiler issues resolved --- src/ios/HabibAliCordovaSquareReader.swift | 40 +++++++++++------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ios/HabibAliCordovaSquareReader.swift b/src/ios/HabibAliCordovaSquareReader.swift index dafff26..c208a30 100644 --- a/src/ios/HabibAliCordovaSquareReader.swift +++ b/src/ios/HabibAliCordovaSquareReader.swift @@ -8,30 +8,30 @@ import AVKit private var currentCommand: CDVInvokedUrlCommand? private var locationPermissionCallback: ((Bool) -> ())? - @objc(setup:) - func setup(command: CDVInvokedUrlCommand) { - - self.locationManager.delegate = self + func qrAuthorizationViewController(_ qrAuthorizationViewController: QRAuthorizationViewController, didRecognizeAuthorizationCode code: String) { + self.viewController.dismiss(animated: true, completion: nil) - func qrAuthorizationViewController(_ qrAuthorizationViewController: QRAuthorizationViewController, didRecognizeAuthorizationCode code: String) { - self.viewController.dismiss(animated: true, completion: nil) - - SQRDReaderSDK.shared.authorize(withCode: code) { location, error in - if let authError = error { - // Handle the error - print(authError) - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: authError.localizedDescription), callbackId: self.recentCommand!.callbackId) - } - else { - // Proceed to the main application interface. - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: self.recentCommand!.callbackId) - } + SQRDReaderSDK.shared.authorize(withCode: code) { location, error in + if let authError = error { + // Handle the error + print(authError) + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: authError.localizedDescription), callbackId: self.currentCommand!.callbackId) + } + else { + // Proceed to the main application interface. + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: self.currentCommand!.callbackId) } } + } + + func qrAuthorizationViewControllerDidCancel(_ qrAuthorizationViewController: QRAuthorizationViewController) { + self.viewController.dismiss(animated: true, completion: nil) + } + + @objc(setup:) + func setup(command: CDVInvokedUrlCommand) { - func qrAuthorizationViewControllerDidCancel(_ qrAuthorizationViewController: QRAuthorizationViewController) { - self.viewController.dismiss(animated: true, completion: nil) - } + self.locationManager.delegate = self func requestLocationPermission(callback: @escaping (Bool) -> ()) { switch CLLocationManager.authorizationStatus() { From 2cbed3f599f1e45e79551a9985aad582c4b24693 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 16:19:27 +0500 Subject: [PATCH 05/11] name change --- package.json | 4 +-- plugin.xml | 14 ++++----- ...ader.swift => F3CordovaSquareReader.swift} | 2 +- www/F3CordovaSquareReader.js | 29 +++++++++++++++++++ www/HabibAliCordovaSquareReader.js | 29 ------------------- 5 files changed, 39 insertions(+), 39 deletions(-) rename src/ios/{HabibAliCordovaSquareReader.swift => F3CordovaSquareReader.swift} (97%) create mode 100644 www/F3CordovaSquareReader.js delete mode 100644 www/HabibAliCordovaSquareReader.js diff --git a/package.json b/package.json index f2a6e10..7595e9f 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "@habibali/cordova-plugin-square-reader", + "name": "@f3/cordova-plugin-square-reader", "version": "0.2.0", "description": "Cordova Plugin to interface with the Square Reader SDK", "cordova": { - "id": "habibali-cordova-plugin-square-reader", + "id": "f3-cordova-plugin-square-reader", "platforms": [ "android", "ios" diff --git a/plugin.xml b/plugin.xml index 8c7a4dd..1e17fd3 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,20 +1,20 @@ - - HabibAliCordovaSquareReader + F3CordovaSquareReader - - + + - - + + @@ -46,7 +46,7 @@ - + diff --git a/src/ios/HabibAliCordovaSquareReader.swift b/src/ios/F3CordovaSquareReader.swift similarity index 97% rename from src/ios/HabibAliCordovaSquareReader.swift rename to src/ios/F3CordovaSquareReader.swift index c208a30..e8efd94 100644 --- a/src/ios/HabibAliCordovaSquareReader.swift +++ b/src/ios/F3CordovaSquareReader.swift @@ -2,7 +2,7 @@ import SquareReaderSDK import CoreLocation import AVKit -@objc(HabibAliCordovaSquareReader) class HabibAliCordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate,QRAuthorizationViewControllerDelegate { +@objc(F3CordovaSquareReader) class F3CordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate,QRAuthorizationViewControllerDelegate { private lazy var locationManager = CLLocationManager() private var currentCommand: CDVInvokedUrlCommand? diff --git a/www/F3CordovaSquareReader.js b/www/F3CordovaSquareReader.js new file mode 100644 index 0000000..f87e058 --- /dev/null +++ b/www/F3CordovaSquareReader.js @@ -0,0 +1,29 @@ +function F3CordovaSquareReader() { +} + +F3CordovaSquareReader.prototype.setup = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "setup", []); +}; + +F3CordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "authorizeReaderSDKIfNeeded", []); +}; + +F3CordovaSquareReader.prototype.startCheckout = function (params, successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "startCheckout", [params]); +}; + +F3CordovaSquareReader.prototype.pairCardReaders = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "pairCardReaders", []); +}; + +F3CordovaSquareReader.prototype.deauthorize = function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "deauthorize", []); +}; + +F3CordovaSquareReader.install = function () { + window.squarereader = new F3CordovaSquareReader(); + return window.squarereader; +}; + +cordova.addConstructor(F3CordovaSquareReader.install); diff --git a/www/HabibAliCordovaSquareReader.js b/www/HabibAliCordovaSquareReader.js deleted file mode 100644 index 065ef0c..0000000 --- a/www/HabibAliCordovaSquareReader.js +++ /dev/null @@ -1,29 +0,0 @@ -function HabibAliCordovaSquareReader() { -} - -HabibAliCordovaSquareReader.prototype.setup = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "setup", []); -}; - -HabibAliCordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "authorizeReaderSDKIfNeeded", []); -}; - -HabibAliCordovaSquareReader.prototype.startCheckout = function (params, successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "startCheckout", [params]); -}; - -HabibAliCordovaSquareReader.prototype.pairCardReaders = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "pairCardReaders", []); -}; - -HabibAliCordovaSquareReader.prototype.deauthorize = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "HabibAliCordovaSquareReader", "deauthorize", []); -}; - -HabibAliCordovaSquareReader.install = function () { - window.squarereader = new HabibAliCordovaSquareReader(); - return window.squarereader; -}; - -cordova.addConstructor(HabibAliCordovaSquareReader.install); From 2ef51d0512d40e539212cfa9d0f85de26cdd4857 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 16:45:48 +0500 Subject: [PATCH 06/11] version changed --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 1e17fd3..386c131 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - From 816cb876c575eed049ac8685d9f6f841a92cc125 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 10 Dec 2018 16:46:16 +0500 Subject: [PATCH 07/11] version change --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7595e9f..c80dd22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@f3/cordova-plugin-square-reader", - "version": "0.2.0", + "version": "0.2.1", "description": "Cordova Plugin to interface with the Square Reader SDK", "cordova": { "id": "f3-cordova-plugin-square-reader", From 6c30092cda2a31b2d92be10a2f3b0819b38b0448 Mon Sep 17 00:00:00 2001 From: Habib Ali Date: Mon, 10 Dec 2018 18:04:41 +0500 Subject: [PATCH 08/11] Update README.md --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 21a45ca..5da062f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A Cordova plugin to interface with the native Square Reader POS SDKs. # Install -`$ cordova plugin add @moltin/cordova-plugin-square-reader` +`$ cordova plugin add @f3/cordova-plugin-square-reader` `$ cordova platform ios prepare` @@ -22,11 +22,7 @@ Set up the Square Reader SDK ```ts window['squarereader'].setup(() => { - let params = { - "authCode": "" - }; - - window['squarereader'].authorizeReaderSDKIfNeeded(params, () => { + window['squarereader'].authorizeReaderSDKIfNeeded(() => { alert("Authorised!") }, (err) => { @@ -37,6 +33,8 @@ window['squarereader'].setup(() => { }); ``` +This will ask for location and microphone permission and then it will give QR code reader. User can use this to scan qr code on Square Reader SDK developer portal to authorize + Pair the reader POS system ```ts @@ -61,6 +59,16 @@ window['squarereader'].startCheckout(params, () => { }); ``` +## DeAuthorize + +```ts +window['squarereader'].deauthorize(() => { +alert("Success!") +}, (err) => { +alert(err); +}); +``` + ## Taking Contactless Payments In order to take contactless and card payments, you will need to ensure that you have completed your Square POS business set up on the Square dashboard. As soon as this is complete, the Square Reader system will automatically allow contactless and card payments. From 2c6fe8f04634e085ae1d24c12ad71c109fbdec32 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Tue, 11 Dec 2018 13:25:51 +0500 Subject: [PATCH 09/11] Param added while authorizing --- package.json | 2 +- plugin.xml | 4 +- src/ios/F3CordovaSquareReader.swift | 45 ++-- src/ios/QRAuthorizationViewController.swift | 245 -------------------- src/ios/SecondaryButton.swift | 51 ---- www/F3CordovaSquareReader.js | 4 +- 6 files changed, 20 insertions(+), 331 deletions(-) delete mode 100755 src/ios/QRAuthorizationViewController.swift delete mode 100755 src/ios/SecondaryButton.swift diff --git a/package.json b/package.json index c80dd22..17d5e79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@f3/cordova-plugin-square-reader", - "version": "0.2.1", + "version": "0.2.2", "description": "Cordova Plugin to interface with the Square Reader SDK", "cordova": { "id": "f3-cordova-plugin-square-reader", diff --git a/plugin.xml b/plugin.xml index 386c131..fa0e51a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - @@ -47,8 +47,6 @@ - - diff --git a/src/ios/F3CordovaSquareReader.swift b/src/ios/F3CordovaSquareReader.swift index e8efd94..e7d7d1a 100644 --- a/src/ios/F3CordovaSquareReader.swift +++ b/src/ios/F3CordovaSquareReader.swift @@ -2,31 +2,12 @@ import SquareReaderSDK import CoreLocation import AVKit -@objc(F3CordovaSquareReader) class F3CordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate,QRAuthorizationViewControllerDelegate { +@objc(F3CordovaSquareReader) class F3CordovaSquareReader : CDVPlugin, SQRDCheckoutControllerDelegate, SQRDReaderSettingsControllerDelegate, CLLocationManagerDelegate { private lazy var locationManager = CLLocationManager() private var currentCommand: CDVInvokedUrlCommand? private var locationPermissionCallback: ((Bool) -> ())? - func qrAuthorizationViewController(_ qrAuthorizationViewController: QRAuthorizationViewController, didRecognizeAuthorizationCode code: String) { - self.viewController.dismiss(animated: true, completion: nil) - - SQRDReaderSDK.shared.authorize(withCode: code) { location, error in - if let authError = error { - // Handle the error - print(authError) - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: authError.localizedDescription), callbackId: self.currentCommand!.callbackId) - } - else { - // Proceed to the main application interface. - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: self.currentCommand!.callbackId) - } - } - } - - func qrAuthorizationViewControllerDidCancel(_ qrAuthorizationViewController: QRAuthorizationViewController) { - self.viewController.dismiss(animated: true, completion: nil) - } @objc(setup:) func setup(command: CDVInvokedUrlCommand) { @@ -83,21 +64,27 @@ import AVKit } } - func retrieveAuthorizationCode(command: CDVInvokedUrlCommand) -> String { - return "sq0acp-bFfIC3wV9xKx0cAc_8aoMbMmUtb_j2BNpL2WR55sS6Y" - } - @objc(authorizeReaderSDKIfNeeded:) func authorizeReaderSDKIfNeeded(command: CDVInvokedUrlCommand) { if SQRDReaderSDK.shared.isAuthorized { print("Already authorized.") self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: command.callbackId) } else { - if (QRAuthorizationViewController.canScanQRCodes){ - self.currentCommand = command - let qrAuthorizationViewController = QRAuthorizationViewController() - qrAuthorizationViewController.delegate = self - self.viewController.present(qrAuthorizationViewController, animated: true, completion: nil) + guard let commandParams = command.arguments.first as? [String: Any], + let authCode = commandParams["authCode"] as? String else { + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "No parameters"), callbackId: command.callbackId) + return + } + SQRDReaderSDK.shared.authorize(withCode: authCode) { location, error in + if let authError = error { + // Handle the error + print(authError) + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: authError.localizedDescription), callbackId: command.callbackId) + } + else { + // Proceed to the main application interface. + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: command.callbackId) + } } } } diff --git a/src/ios/QRAuthorizationViewController.swift b/src/ios/QRAuthorizationViewController.swift deleted file mode 100755 index 454729b..0000000 --- a/src/ios/QRAuthorizationViewController.swift +++ /dev/null @@ -1,245 +0,0 @@ -// -// Copyright © 2018 Square, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import UIKit -import AVFoundation -import SquareReaderSDK - -protocol QRAuthorizationViewControllerDelegate: class { - func qrAuthorizationViewController(_ qrAuthorizationViewController: QRAuthorizationViewController, didRecognizeAuthorizationCode code: String) - func qrAuthorizationViewControllerDidCancel(_ qrAuthorizationViewController: QRAuthorizationViewController) -} - -class QRAuthorizationViewController: UIViewController { - public weak var delegate: QRAuthorizationViewControllerDelegate? - - lazy var captureSession = AVCaptureSession() - lazy var previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) - - lazy var labelContainerView = makeContainerView() - lazy var label = makeLabel() - lazy var previewView = makePreviewView() - lazy var cancelButtonContainerView = makeContainerView() - lazy var cancelButton = makeCancelButton() - - // MARK: - State - static var canScanQRCodes: Bool { - return AVCaptureDevice.default(for: .video) != nil - } - - static var isCameraPermissionGranted: Bool { - return AVCaptureDevice.authorizationStatus(for: .video) == .authorized - } - - deinit { - captureSession.stopRunning() - UIDevice.current.endGeneratingDeviceOrientationNotifications() - } - - // MARK: - View lifecycle - override func viewDidLoad() { - super.viewDidLoad() - - if #available(iOS 11.0, *) { - additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 32, bottom: 32, right: 32) - } else { - // Fallback on earlier versions - } - view.backgroundColor = .black - - labelContainerView.addSubview(label) - view.addSubview(previewView) - view.addSubview(labelContainerView) - cancelButtonContainerView.addSubview(cancelButton) - view.addSubview(cancelButtonContainerView) - - // Observe changes to device orientation - UIDevice.current.beginGeneratingDeviceOrientationNotifications() - NotificationCenter.default.addObserver(self, selector: #selector(updateVideoOrientation), name: Notification.Name.UIDeviceOrientationDidChange, object: UIDevice.current) - } - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - - if #available(iOS 11.0, *) { - NSLayoutConstraint.activate([ - previewView.topAnchor.constraint(equalTo: view.topAnchor), - previewView.bottomAnchor.constraint(equalTo: view.bottomAnchor), - previewView.leftAnchor.constraint(equalTo: view.leftAnchor), - previewView.rightAnchor.constraint(equalTo: view.rightAnchor), - - labelContainerView.topAnchor.constraint(equalTo: view.topAnchor), - labelContainerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 80), - labelContainerView.leftAnchor.constraint(equalTo: view.leftAnchor), - labelContainerView.rightAnchor.constraint(equalTo: view.rightAnchor), - - label.bottomAnchor.constraint(equalTo: labelContainerView.bottomAnchor), - label.leftAnchor.constraint(equalTo: labelContainerView.leftAnchor), - label.rightAnchor.constraint(equalTo: labelContainerView.rightAnchor), - label.heightAnchor.constraint(equalToConstant: 80), - - cancelButtonContainerView.topAnchor.constraint(equalTo: cancelButtonContainerView.safeAreaLayoutGuide.bottomAnchor, constant: -96), - cancelButtonContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor), - cancelButtonContainerView.leftAnchor.constraint(equalTo: view.leftAnchor), - cancelButtonContainerView.rightAnchor.constraint(equalTo: view.rightAnchor), - - cancelButton.bottomAnchor.constraint(equalTo: cancelButtonContainerView.safeAreaLayoutGuide.bottomAnchor), - cancelButton.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor), - cancelButton.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor), - ]) - } else { - // Fallback on earlier versions - } - - updatePreviewLayerSize() - } - - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - - if QRAuthorizationViewController.isCameraPermissionGranted { - startCaptureSession() - } else { - requestCameraPermission() - } - } - - override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - - previewLayer.frame = view.bounds - } - - override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { - super.viewWillTransition(to: size, with: coordinator) - - updateVideoOrientation() - } - - override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { - super.traitCollectionDidChange(previousTraitCollection) - - updatePreviewLayerSize() - } - - func updatePreviewLayerSize() { - // Manually set the frame of the layer - view.setNeedsLayout() - } - - @objc func updateVideoOrientation() { - if let connection = previewLayer.connection, connection.isVideoOrientationSupported { - switch UIDevice.current.orientation { - case .portrait: - connection.videoOrientation = .portrait - case .portraitUpsideDown: - connection.videoOrientation = .portraitUpsideDown - case .landscapeLeft: - connection.videoOrientation = .landscapeRight - case .landscapeRight: - connection.videoOrientation = .landscapeLeft - default: - break - } - } - } - - @objc func cancel() { - captureSession.stopRunning() - delegate?.qrAuthorizationViewControllerDidCancel(self) - } -} - -// MARK: - Camera -extension QRAuthorizationViewController: AVCaptureMetadataOutputObjectsDelegate { - func requestCameraPermission() { - AVCaptureDevice.requestAccess(for: .video) { (granted) in - if granted { - DispatchQueue.main.async { - self.startCaptureSession() - } - } else if let url = URL(string: UIApplicationOpenSettingsURLString) { - UIApplication.shared.open(url, options: [:], completionHandler: nil) - } - } - } - - func startCaptureSession() { - // Add the input to the capture session - let captureDevice = AVCaptureDevice.default(for: .video)! - let input = try! AVCaptureDeviceInput(device: captureDevice) - captureSession.addInput(input) - - // Capture metadata output from the session - let captureMetadataOutput = AVCaptureMetadataOutput() - captureSession.addOutput(captureMetadataOutput) - - // Add ourselves as the delegate, only recognize QR codes - captureMetadataOutput.setMetadataObjectsDelegate(self, queue: .main) - captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr] - - // Add the preview layer and start the capture session - captureSession.startRunning() - - // Update the video orientation to match the current device orientation - updateVideoOrientation() - } - - func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { - guard let object = metadataObjects.first as? AVMetadataMachineReadableCodeObject, object.type == .qr, - let code = object.stringValue else { return } - - // Stop capturing frames - captureSession.stopRunning() - - // Notify delegate - delegate?.qrAuthorizationViewController(self, didRecognizeAuthorizationCode: code) - } -} - -// MARK: - UI -extension QRAuthorizationViewController { - private func makeContainerView() -> UIView { - let containerView = UIView() - containerView.backgroundColor = UIColor.black.withAlphaComponent(0.75) - containerView.translatesAutoresizingMaskIntoConstraints = false - return containerView - } - - private func makeLabel() -> UILabel { - let label = UILabel() - label.text = "Scan a QR code." - label.textColor = .white - label.textAlignment = .center - label.font = UIFont.systemFont(ofSize: 24, weight: .semibold) - label.layer.cornerRadius = 8 - label.layer.masksToBounds = true - label.translatesAutoresizingMaskIntoConstraints = false - return label - } - - private func makePreviewView() -> UIView { - let previewView = UIView() - previewLayer.videoGravity = .resizeAspectFill - previewView.layer.insertSublayer(previewLayer, at: 0) - previewView.translatesAutoresizingMaskIntoConstraints = false - return previewView - } - - private func makeCancelButton() -> UIButton { - return SecondaryButton(title: "Cancel", target: self, selector: #selector(cancel)) - } -} diff --git a/src/ios/SecondaryButton.swift b/src/ios/SecondaryButton.swift deleted file mode 100755 index 1011fae..0000000 --- a/src/ios/SecondaryButton.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// Copyright © 2018 Square, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import UIKit - -class SecondaryButton: UIButton { - struct Color { - static let title = UIColor.white - static let disabledTitle = #colorLiteral(red: 0.6424661279, green: 0.7832983136, blue: 0.947729528, alpha: 1) - static let background = UIColor.clear - static let highlightedBackground = UIColor.white.withAlphaComponent(0.1) - static let border = UIColor.white.withAlphaComponent(0.7) - } - - override var isHighlighted: Bool { - didSet { - backgroundColor = isHighlighted ? Color.highlightedBackground : Color.background - } - } - - convenience init(title: String, target: Any, selector: Selector) { - self.init() - - // Default state - setTitle(title, for: []) - setTitleColor(Color.title, for: []) - setTitleColor(Color.disabledTitle, for: .disabled) - titleLabel?.font = .systemFont(ofSize: 20, weight: .semibold) - backgroundColor = Color.background - translatesAutoresizingMaskIntoConstraints = false - heightAnchor.constraint(equalToConstant: 64).isActive = true - layer.cornerRadius = 8 - layer.masksToBounds = true - layer.borderColor = SecondaryButton.Color.border.cgColor - layer.borderWidth = 1.0 - addTarget(target, action: selector, for: .touchUpInside) - } -} diff --git a/www/F3CordovaSquareReader.js b/www/F3CordovaSquareReader.js index f87e058..b7f4a7f 100644 --- a/www/F3CordovaSquareReader.js +++ b/www/F3CordovaSquareReader.js @@ -5,8 +5,8 @@ F3CordovaSquareReader.prototype.setup = function (successCallback, errorCallback cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "setup", []); }; -F3CordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "authorizeReaderSDKIfNeeded", []); +F3CordovaSquareReader.prototype.authorizeReaderSDKIfNeeded = function (params,successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "F3CordovaSquareReader", "authorizeReaderSDKIfNeeded", [params]); }; F3CordovaSquareReader.prototype.startCheckout = function (params, successCallback, errorCallback) { From 13e7a71d60466ccdeae24ab3a0b8deef1b747400 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Tue, 11 Dec 2018 18:42:30 +0500 Subject: [PATCH 10/11] authorized not authorized message is passed while setup --- package.json | 2 +- plugin.xml | 2 +- src/ios/F3CordovaSquareReader.swift | 15 +++++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 17d5e79..adc2f97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@f3/cordova-plugin-square-reader", - "version": "0.2.2", + "version": "0.2.3", "description": "Cordova Plugin to interface with the Square Reader SDK", "cordova": { "id": "f3-cordova-plugin-square-reader", diff --git a/plugin.xml b/plugin.xml index fa0e51a..28c6ce1 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - diff --git a/src/ios/F3CordovaSquareReader.swift b/src/ios/F3CordovaSquareReader.swift index e7d7d1a..b19af82 100644 --- a/src/ios/F3CordovaSquareReader.swift +++ b/src/ios/F3CordovaSquareReader.swift @@ -43,7 +43,14 @@ import AVKit if locationSuccess { requestMicrophonePermission() { microphoneSuccess in if microphoneSuccess { - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: command.callbackId) + var message = "authorized" + if SQRDReaderSDK.shared.isAuthorized{ + message = "authorized" + } + else{ + message = "not authorized" + } + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK,messageAs:message), callbackId: command.callbackId) return } else { self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR), callbackId: command.callbackId) @@ -93,9 +100,9 @@ import AVKit func startCheckout(command: CDVInvokedUrlCommand) { guard let commandParams = command.arguments.first as? [String: Any], - let amount = commandParams["amount"] as? Int else { - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "No parameters"), callbackId: command.callbackId) - return + let amount = commandParams["amount"] as? Int else { + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "No parameters"), callbackId: command.callbackId) + return } // Create an amount of money in the currency of the authorized Square account From 368dfc20c13b4ef08928af9282f4a60a2cffd249 Mon Sep 17 00:00:00 2001 From: HabibAli Date: Mon, 17 Dec 2018 17:54:02 +0500 Subject: [PATCH 11/11] bug resolved --- package.json | 2 +- plugin.xml | 2 +- src/ios/F3CordovaSquareReader.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index adc2f97..b2bbdb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@f3/cordova-plugin-square-reader", - "version": "0.2.3", + "version": "0.2.4", "description": "Cordova Plugin to interface with the Square Reader SDK", "cordova": { "id": "f3-cordova-plugin-square-reader", diff --git a/plugin.xml b/plugin.xml index 28c6ce1..6b089a3 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - diff --git a/src/ios/F3CordovaSquareReader.swift b/src/ios/F3CordovaSquareReader.swift index b19af82..2d66097 100644 --- a/src/ios/F3CordovaSquareReader.swift +++ b/src/ios/F3CordovaSquareReader.swift @@ -170,7 +170,7 @@ import AVKit guard let currentCommand = self.currentCommand else { return } - self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK), callbackId: currentCommand.callbackId) + self.commandDelegate.send(CDVPluginResult(status: CDVCommandStatus_OK, messageAs:result.transactionID), callbackId: currentCommand.callbackId) self.currentCommand = nil }