Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1.  
  2. 2-та класа които отварят webview (android, ios) и го конфигурират да приема самоподписани (unsecure) https страници, които се ползват само в дебъг
  3. адресите които получават са:
  4.  
  5. при DEBUG: https://mdpay-test.fibank.bg/ecomm/ClientHandler?trans_id=1231231
  6. при RELEASE: https://mdpay.fibank.bg/ecomm/ClientHandler?trans_id=
  7.  
  8. (trans_id се взима след заявка към urbo сървъра (makeOrder заявка))
  9.  
  10. // ANDROID
  11.  
  12. package com.urboapp.android.urbo.view.parking;
  13.  
  14. import android.content.DialogInterface;
  15. import android.graphics.Bitmap;
  16. import android.net.http.SslError;
  17. import android.os.Bundle;
  18. import android.support.v7.app.AlertDialog;
  19. import android.support.v7.app.AppCompatActivity;
  20. import android.content.Intent;
  21. import android.webkit.SslErrorHandler;
  22. import android.webkit.WebResourceRequest;
  23. import android.webkit.WebSettings;
  24. import android.webkit.WebView;
  25. import android.webkit.WebViewClient;
  26.  
  27. import com.urboapp.android.urbo.R;
  28.  
  29. public class CardEntryActivity extends AppCompatActivity {
  30.  
  31. public static final int SUCCESS = 13;
  32. public static final int FAILURE = 14;
  33.  
  34. public long parkingStart;
  35. public long orderId;
  36.  
  37. void dismissWithResult(int result) {
  38. Intent data = new Intent();
  39. data.putExtra("PARKING_START", parkingStart);
  40. data.putExtra("ORDER_ID", orderId);
  41. setResult(result, data);
  42. finish();
  43. }
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_card_entry);
  49.  
  50. WebView myWebView = (WebView) findViewById(R.id.webview);
  51. WebSettings webSettings = myWebView.getSettings();
  52. webSettings.setJavaScriptEnabled(true);
  53.  
  54. myWebView.setWebViewClient(new WebViewClient() {
  55. @Override
  56. public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
  57. final AlertDialog.Builder builder = new AlertDialog.Builder(CardEntryActivity.this);
  58. builder.setMessage("The SSL certificate of this page is invalid. Do you want to proceed anyway?");
  59. builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
  60. @Override
  61. public void onClick(DialogInterface dialog, int which) {
  62. handler.proceed();
  63. }
  64. });
  65. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  66. @Override
  67. public void onClick(DialogInterface dialog, int which) {
  68. handler.cancel();
  69. dismissWithResult(FAILURE);
  70. }
  71. });
  72. final AlertDialog dialog = builder.create();
  73. dialog.show();
  74. }
  75.  
  76. @Override
  77. public void onPageFinished(WebView view, String url) {
  78. if (url.contains("frantishex")) {
  79. if (url.contains("success"))
  80. dismissWithResult(SUCCESS);
  81. else
  82. dismissWithResult(FAILURE);
  83. }
  84. }
  85. });
  86.  
  87. // Get the Intent that started this activity and extract the string
  88. Intent intent = getIntent();
  89. String url = intent.getStringExtra("CARD_ENTRY_URL");
  90. parkingStart = intent.getLongExtra("PARKING_START", 0);
  91. orderId = intent.getLongExtra("ORDER_ID", 0);
  92. myWebView.loadUrl(url);
  93. }
  94. }
  95.  
  96.  
  97. /// IOS
  98.  
  99.  
  100. import UIKit
  101.  
  102. class CardEntryViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDelegate {
  103.  
  104. @IBOutlet weak var closeButton: UIButton!
  105. @IBOutlet weak var webView: UIWebView!
  106.  
  107. enum PaymentResult: Int {
  108. case success = 0
  109. case cancel = 1
  110. case failed = 2
  111. }
  112.  
  113. typealias CompletionBlock = (_ result: PaymentResult) -> Void
  114.  
  115. var urlString: String?
  116. var orderId: Int?
  117. var completeHandler: CompletionBlock?
  118.  
  119. override func viewDidLoad() {
  120. super.viewDidLoad()
  121.  
  122. if let urlString = self.urlString {
  123. let url = URL(string: urlString)
  124. NSLog("bank payment request: %@", urlString)
  125. let urlRequest = URLRequest(url: url!)
  126. let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)!
  127. urlConnection.start()
  128. webView.loadRequest(urlRequest)
  129. webView.delegate = self
  130. }
  131. }
  132.  
  133. override func didReceiveMemoryWarning() {
  134. super.didReceiveMemoryWarning()
  135. // Dispose of any resources that can be recreated.
  136. }
  137.  
  138. func webView(_ view: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
  139. return true
  140. }
  141.  
  142. func connection(_ connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: URLProtectionSpace) -> Bool{
  143. return true
  144. }
  145.  
  146. @IBAction func didPressColoseButton(_ sender: Any) {
  147. //self.dismiss(animated: true, completion: nil)
  148. if let closeHandler = completeHandler {
  149. closeHandler(PaymentResult.cancel)
  150. }
  151. }
  152.  
  153. func webViewDidFinishLoad(_ webView: UIWebView) {
  154. let href = webView.stringByEvaluatingJavaScript(from: "window.location.href")
  155. if let url = href {
  156. if url.lowercased().range(of:"frantishex") != nil {
  157. if url.lowercased().range(of:"success") != nil {
  158. if let closeHandler = completeHandler {
  159. closeHandler(PaymentResult.success)
  160. }
  161. }
  162. else {
  163. if let closeHandler = completeHandler {
  164. closeHandler(PaymentResult.failed)
  165. }
  166. }
  167. }
  168. }
  169. }
  170.  
  171. func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge){
  172. if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
  173. let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
  174. challenge.sender!.use(credential, for: challenge)
  175. } else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic {
  176. let defaultCredentials: URLCredential = URLCredential(user: "username", password: "password", persistence:URLCredential.Persistence.forSession)
  177. challenge.sender!.use(defaultCredentials, for: challenge)
  178. } else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM {
  179.  
  180. } else{
  181. challenge.sender!.performDefaultHandling!(for: challenge)
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement