Guest User

Untitled

a guest
Jul 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. //
  2. // CustomWebViewController.swift
  3. // WibmoPay
  4. //
  5. // Created by Hemavathi on 10/07/18.
  6. //
  7.  
  8. import UIKit
  9. import WebKit
  10.  
  11. let kJavascriptFileName = "WibmoPayWebKitPostData"
  12.  
  13. class CustomWebViewController: UIViewController,WKUIDelegate {
  14. @IBOutlet weak var LoadingView: UIView!
  15. var webView: WKWebView!
  16. var merchantDetails: WPMerchantDetails?
  17. var postedParams = false
  18. @IBOutlet weak var loadActivityIndicator: UIActivityIndicatorView!
  19.  
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. self.navigationItem.hidesBackButton = true
  23. self.navigationItem.title = "Pay"
  24. let cancelBarBtn = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(CustomWebViewController.cancelTransaction(_:)))
  25. self.navigationItem.setRightBarButton(cancelBarBtn, animated: true)
  26. self.configureWebView()
  27. }
  28.  
  29. /*
  30. Dismiss the controller and return from sdk to app
  31. */
  32. @objc private func cancelTransaction(_ sender: UIButton) {
  33. self.dismiss(animated: true, completion: nil)
  34. }
  35.  
  36. /*
  37. Injected JavaScript html for showing webview
  38. */
  39. private func configureWebView(){
  40. let webConfiguration = WKWebViewConfiguration()
  41. self.webView = WKWebView(frame: self.LoadingView.frame,configuration: webConfiguration)
  42. self.LoadingView.addSubview(self.webView)
  43. webView.addSubview(self.loadActivityIndicator)
  44. webView.contentMode = .scaleToFill
  45. //webView.allowsBackForwardNavigationGestures = false
  46. //webView.autoresizesSubviews = true
  47. webView.scrollView.bounces = false
  48. //webView.scrollView.isScrollEnabled = false
  49. //automaticallyAdjustsScrollViewInsets = true
  50. webView.navigationDelegate = self
  51.  
  52. let frameworkBundle = Bundle(for: WibmoPaySdk.self)
  53. let resourceBundle = Bundle(url: frameworkBundle.url(forResource: "WibmoPay", withExtension: "bundle")!)
  54. let javascriptFilePath = resourceBundle?.path(forResource: kJavascriptFileName, ofType: "html")
  55. let javascriptFileContents = try? String(contentsOfFile: javascriptFilePath!)
  56. self.webView.navigationDelegate = self
  57. self.webView.loadHTMLString(javascriptFileContents!, baseURL: frameworkBundle.bundleURL)
  58. }
  59. }
  60.  
  61. /*
  62. WKWebView delegates
  63. */
  64. extension CustomWebViewController : WKNavigationDelegate {
  65.  
  66. /*
  67. when URL is completed loaded this method is invoked
  68. */
  69. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  70. if postedParams == false {
  71. self.makePostRequest()
  72. }
  73. self.loadActivityIndicator.stopAnimating()
  74. self.LoadingView.isHidden = false
  75. }
  76.  
  77. func makePostRequest() {
  78. var url = ""
  79. let postString: [String : String] = [SDKConstant.KEY_REQUEST_PARAM_CHECKSUM : merchantDetails!.generateCheckSumUsingSHA512,
  80. SDKConstant.KEY_REQUEST_PARAM_MERCHANT_NAME : merchantDetails!.merchantName,
  81. SDKConstant.KEY_REQUEST_PARAM_MERCHANT_TXN_ID : merchantDetails!.merchantTxnId,
  82. SDKConstant.KEY_REQUEST_PARAM_MERCHANT_AMOUNT : merchantDetails!.merchantTxnAmount,
  83. SDKConstant.KEY_REQUEST_PARAM_MERCHANT_MESSAGE : merchantDetails!.merchantMessage,
  84. SDKConstant.KEY_REQUEST_PARAM_MERCHANT_PAYMENT_OPTION : merchantDetails!.merchantPaymentOption,
  85. SDKConstant.KEY_REQUEST_PARAM_PAYER_NAME : merchantDetails!.payerName,
  86. SDKConstant.KEY_REQUEST_PARAM_PAYER_MOBILE_NO : merchantDetails!.payerMobile,
  87. SDKConstant.KEY_REQUEST_PARAM_PAYER_EMAIL_ID : merchantDetails!.payerEmailId]
  88.  
  89. var postParams = ""
  90. for param in postString {
  91. if let urlEncodedValue = param.value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
  92. postParams += "(param.key):'(urlEncodedValue)',"
  93. }
  94. }
  95. postParams = String(postParams.dropLast())
  96. if let wibmoPayURL = WibmoPaySdk.sdkManager.wibmoPayUrl{
  97. print("URL:(String(describing: wibmoPayURL))")
  98. url = "(wibmoPayURL)(SDKConstant.API_PAYMENT_URL)"
  99. }else{
  100. WibmoPayError.inCorrectMerchantDetails(missingData: "URL is empty")
  101. }
  102. let jsEvaluator = "post('(url)',{(postParams)});"
  103. print(jsEvaluator)
  104. self.webView.evaluateJavaScript(jsEvaluator) { (result, error) in
  105. if let error = error {
  106. print(error)
  107. }
  108. }
  109. postedParams = true
  110.  
  111. print("webView URL:(String(describing: webView.url))")
  112. }
  113.  
  114. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  115. self.loadActivityIndicator.startAnimating()
  116. self.LoadingView.isHidden = true
  117. print("(String(describing: webView.url))")
  118. var merchantName: String!
  119. var orderId: String!
  120. var merchantTxnId: String!
  121. var status: String!
  122. var paymentMode: String!
  123. var amount: String!
  124. var commission: String!
  125. var channel: String!
  126.  
  127. if let redirectedUrl = webView.url, let merchantCbUrl = merchantDetails?.merchantCallbackUrl {
  128. if(redirectedUrl.absoluteString.hasPrefix(merchantCbUrl)) {
  129. print("redirect url matching(String(describing: redirectedUrl.host))")
  130. if let responseData = redirectedUrl.query {
  131. let params = responseData.components(separatedBy: "&")
  132. for param in params {
  133. let keyValue = param.components(separatedBy: "=")
  134. print("keyValue(String(describing: keyValue))")
  135.  
  136. if keyValue.first == "merchant_name"{
  137. merchantName = keyValue.last
  138. print(keyValue , "keyValue.first = (merchantName)")
  139. continue
  140. } else if keyValue.first == "order_id"{
  141. orderId = keyValue.last
  142. print(keyValue , "keyValue.first = (orderId)")
  143. continue
  144. } else if keyValue.first == "merchant_txn_id"{
  145. merchantTxnId = keyValue.last
  146. print(keyValue , "keyValue.first = (merchantTxnId)")
  147. continue
  148. } else if keyValue.first == "status"{
  149. status = keyValue.last
  150. print(keyValue , "keyValue.first = (status)")
  151. continue
  152. } else if keyValue.first == "payment_mode"{
  153. paymentMode = keyValue.last
  154. print(keyValue , "keyValue.first = (paymentMode)")
  155. continue
  156. } else if keyValue.first == "amount"{
  157. amount = keyValue.last
  158. print(keyValue , "keyValue.first = (amount)")
  159. continue
  160. }else if keyValue.first == "commission"{
  161. commission = keyValue.last
  162. print(keyValue , "keyValue.first = (commission)")
  163. continue
  164. }else if keyValue.first == "channel"{
  165. channel = keyValue.last
  166. print(keyValue , "keyValue.first = (channel)")
  167. continue
  168. } else {
  169. print("key and value missing")
  170. }
  171. }
  172.  
  173. let merchantResponse = WibmoPayResponse(merchant_name: merchantName, order_id: orderId, merchant_txn_id: merchantTxnId, status: status, payment_mode: paymentMode, amount: amount, commission: commission, channel: channel)
  174. WibmoPaySdk.sdkManager.transactionCompletionBlock?(merchantResponse,nil)
  175. print("send data success")
  176. self.webView!.stopLoading()
  177. self.dismiss(animated: true, completion: nil)
  178. }
  179. }else{
  180. print("redirectedUrl not matching")
  181. }
  182. }
  183. }
  184.  
  185. // func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  186. // self.loadActivityIndicator.stopAnimating()
  187. // }
  188.  
  189. override var shouldAutorotate: Bool{
  190. return false
  191. }
  192.  
  193. }
Add Comment
Please, Sign In to add comment