Skip to content

Commit

Permalink
Merge pull request #5 from expcapitaldev/feat/ios-sdk-update
Browse files Browse the repository at this point in the history
feat(ios): sdk updated, support for Checkout events
  • Loading branch information
expcapitaldev authored Apr 13, 2022
2 parents bf835ea + 2eb91f9 commit 3711ac0
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 24 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

### Version 3.0.0 (13/04/2022)

- feat(ios): support checkout events

### Version 2.1.0 (15/0/2022)

- fix: add null check for event handler
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.0",
"version": "3.0.0",
"name": "cordova-plugin-trustly",
"cordova_name": "Cordova Trustly Plugin",
"description": "Cordova Trustly Plugin",
Expand Down
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-trustly"
version="2.1.0">
version="3.0.0">

<name>Trustly Cordova Plugin</name>
<description>Add Trustly support to your cordova apps</description>
Expand Down Expand Up @@ -61,6 +61,7 @@

<header-file src="src/ios/CDVTrustly-Bridging-Header.h" />
<source-file src="src/ios/CDVTrustlyPlugin.swift" />
<source-file src="src/ios/CDVTrustlyCheckoutDelegate.swift" />
<source-file src="src/ios/CDVTrustlyOpenURLScheme.swift" />
<source-file src="src/ios/CDVTrustlyWKWebView.swift" />

Expand Down
44 changes: 44 additions & 0 deletions src/ios/CDVTrustlyCheckoutDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Trustly Group AB
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

import Foundation

public enum TrustlyCheckoutEvent: String {
case openURLScheme = "onTrustlyCheckoutRedirect"
case success = "onTrustlyCheckoutSuccess"
case error = "onTrustlyCheckoutError"
case abort = "onTrustlyCheckoutAbort"
}


public protocol TrustlyCheckoutDelegate: class {
/// Called when Checkout receives a request to open a URL Scheme.
func onTrustlyCheckoutRequstToOpenURLScheme(urlScheme: String)
/// Called when Checkout transaction is complete.
func onTrustlyCheckoutSuccessfull(urlString: String?)
/// Called when the users abort the Checkout.
func onTrustlyCheckoutAbort(urlString: String?)
/// Called when an error occurs in the Checkout.
func onTrustlyCheckoutError()
}
84 changes: 72 additions & 12 deletions src/ios/CDVTrustlyOpenURLScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,91 @@

import Foundation
import WebKit
import UIKit

/**
Will try to open the URL, then return result in callback
:param: JSON
*/
public class TrustlyWKScriptOpenURLScheme: NSObject, WKScriptMessageHandler {

public static let NAME = "trustlyOpenURLScheme"
weak var trustlyCheckoutDelegate: TrustlyCheckoutDelegate?

var webView: WKWebView

public init(webView: WKWebView) {
/// Name of the "native bridge" that will be used to communicate with the web view.
public static let NAME = "trustlySDKBridge"

init(webView: WKWebView) {
self.webView = webView
}

/**
Function to handle messages from the web client rendered in the Web View.
*/
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let parsed = getParsedJSON(object: message.body as AnyObject),
let callback: String = parsed.object(forKey: "callback") as? String,
let urlscheme: String = parsed.object(forKey: "urlscheme") as? String
{
UIApplication.shared.openURL(NSURL(string: urlscheme)! as URL)
let js: String = String(format: "%@", [callback, urlscheme])

guard let parsedCheckoutEventObject = getParsedJSON(object: message.body as AnyObject) else {
print("TRUSTLY SDK: Message posted from script handler has an invalid format")
return
}

guard let eventType = parsedCheckoutEventObject.object(forKey: "type") as? String else {
print("TRUSTLY SDK: Found no type property on checkout event")
return
}

guard let trustlyCheckoutEvent = TrustlyCheckoutEvent(rawValue: eventType) else {
print("TRUSTLY SDK: Checkout event type not recognized")
return
}

let url: String? = parsedCheckoutEventObject.object(forKey: "url") as? String ?? nil

/// Check if the SDK user have opted into using TrustlyCheckoutEventDelegate
if trustlyCheckoutDelegate != nil {
handleCheckoutEvent(checkoutEvent: trustlyCheckoutEvent, url: url)
return
}

//Only allow redirect events to reach legacy code below, all other event types were introduced with the TrustlyCheckoutDelegate.
if trustlyCheckoutEvent != .openURLScheme {
return
}

/// Handle the message the legacy way to ensure backwards compability.
if let urlScheme = parsedCheckoutEventObject.object(forKey: "url") as? String {

if let url = URL(string: urlScheme) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}

let js: String = String(format: "%@", ["", urlScheme])
webView.evaluateJavaScript(js, completionHandler: nil)
}
}

/**
Validate and call the correct delegate method for the event.
- Parameter jsonObject: The json object sent from the Web Client.
*/
func handleCheckoutEvent(checkoutEvent: TrustlyCheckoutEvent, url: String?) {

switch checkoutEvent {
case .openURLScheme:
if let urlSchemeString = url as String? {
self.trustlyCheckoutDelegate?.onTrustlyCheckoutRequstToOpenURLScheme(urlScheme: urlSchemeString)
}
case .success:
self.trustlyCheckoutDelegate?.onTrustlyCheckoutSuccessfull(urlString: url)
case .error:
self.trustlyCheckoutDelegate?.onTrustlyCheckoutError()
case .abort:
self.trustlyCheckoutDelegate?.onTrustlyCheckoutAbort(urlString: url)
}

}

/**
Helper function that will try to parse AnyObject to JSON and return as NSDictionary
:param: AnyObject
Expand Down
29 changes: 19 additions & 10 deletions src/ios/CDVTrustlyWKWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ import WebKit
import SafariServices

public class TrustlyWKWebView: UIView, WKNavigationDelegate, WKUIDelegate, SFSafariViewControllerDelegate {
var trustlyView: WKWebView?
var endUrls: [String]
var completionBlock: ((String) -> Void)

var webView: WKWebView?
var trustlyWKScriptHandler: TrustlyWKScriptOpenURLScheme!

public weak var delegate: TrustlyCheckoutDelegate? {
didSet {
trustlyWKScriptHandler.trustlyCheckoutDelegate = delegate
}
}

public init?(checkoutUrl: String, endUrls: [String], frame: CGRect, completionBlock: @escaping ((String) -> Void)) {
self.endUrls = endUrls
self.completionBlock = completionBlock
Expand All @@ -41,20 +49,21 @@ public class TrustlyWKWebView: UIView, WKNavigationDelegate, WKUIDelegate, SFSaf
configuration.userContentController = userContentController
configuration.preferences.javaScriptCanOpenWindowsAutomatically = true

trustlyView = WKWebView(frame: frame, configuration: configuration)
guard let trustlyView = trustlyView else { return nil }
webView = WKWebView(frame: frame, configuration: configuration)
guard let webView = webView else { return nil }

webView.navigationDelegate = self
webView.uiDelegate = self

trustlyView.navigationDelegate = self
trustlyView.uiDelegate = self
trustlyWKScriptHandler = TrustlyWKScriptOpenURLScheme(webView: webView)
userContentController.add(trustlyWKScriptHandler, name: TrustlyWKScriptOpenURLScheme.NAME)

userContentController.add(
TrustlyWKScriptOpenURLScheme(webView: trustlyView), name: TrustlyWKScriptOpenURLScheme.NAME)
if let url = URL(string: checkoutUrl) {
trustlyView.load(URLRequest(url: url))
trustlyView.allowsBackForwardNavigationGestures = true
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}

addSubview(trustlyView)
addSubview(webView)
}

public required init?(coder aDecoder: NSCoder) {
Expand Down

0 comments on commit 3711ac0

Please sign in to comment.