我正在 Swift 的 Native iOS App 中实施安全支付解决方案。
基本上,当我开始支付请求时,如果支付卡注册了 3D 检查,我会在响应中收到“PaReq”和“ACSURL”。
然后我需要做java脚本POST。
一旦用户完成操作,我需要提取“PaRes”并在下一次服务调用中发送它。
这是使用 UIWebView 在 Objective-C 中的工作解决方案。我想在 Swift 中使用 WKWebView 实现相同的事情,因为 UIWebView 已被弃用。
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.secureViewContainer.frame.size.width ,self.secureViewContainer.frame.size.height)];
webView.delegate = self;
[self.view addSubview:webView];
NSString *termURL = @"https://www.apple.com";
self.webViewURL = @"";
self.webViewHTMLString = [NSString stringWithFormat:@"<html><body><form name='redirectToIssuerForm' id='redirectToIssuerForm' action='%@' method='post'><input type='hidden' name='PaReq' value='%@' /><input type='hidden' name='TermUrl' value='%@' /><input type='hidden' name='MD' value='' /><input type='submit' id='submitButton' value='Click here to continue' /></form><script>function submitForm(){document.getElementById('submitButton').style.display='none'; document.getElementById('submitButton').click();} window.onload = (function(){submitForm();});</script></body></html>",self.acsurl,self.pareq,termURL];
[webView loadHTMLString:self.webViewHTMLString baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
if([webView.request.URL.absoluteString isEqualToString:@"about:blank"]){
[webView stringByEvaluatingJavaScriptFromString:@"submitForm()"];
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if([ [NSString stringWithFormat:@"%@",request.URL] isEqualToString:@"https://www.apple.com/"]){
NSData *data = request.HTTPBody;
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"\n\n\n 3D Secure - dataString: %@", dataString);
return NO;
}
else{
return YES;
}
}
这是我尝试使用 WKWebView 迁移到 Swift 的内容:
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.secureViewContainer.frame.size.height))
self.webView = WKWebView (frame: customFrame , configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
self.secureViewContainer.addSubview(webView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let termURLString = "https://www.apple.com"
var paReqString = response.PaRequest
var acsurlString = response.ACSURL
// create HTML String
let webViewHTMLString = String(format:"<html><body><form name='redirectToIssuerForm' id='redirectToIssuerForm' action='%@' method='post'><input type='hidden' name='PaReq' value='%@' /><input type='hidden' name='TermUrl' value='%@' /><input type='hidden' name='MD' value='' /><input type='submit' id='submitButton' value='Click here to continue' /></form><script>function submitForm(){document.getElementById('submitButton').style.display='none'; document.getElementById('submitButton').click();} window.onload = (function(){submitForm();});</script></body></html>",acsurlString,paReqString,termURLString)
// Load HTML String
webView.loadHTMLString(webViewHTMLString, baseURL: nil)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.url?.absoluteString == "about:blank" {
webView.evaluateJavaScript("submitForm()", completionHandler: {
result in
print("\n\n\n Result - \(result)\n\n\n")
})
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url,url.absoluteString.hasPrefix("https://www.apple.com/") {
let data = navigationAction.request.httpBody
print("\n\n data : \(String(describing: data))")
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
问题是“结果”和“数据”都是零。
1:我会在 webView(_:decidePolicyFor:decisionHandler:) (或)在 webView:didFinishNavigation: 中获得 java 脚本结果吗?
2:在 UIWebView 工作解决方案中,我可以将 termURL 更改为任何 url,如“ https://www.apple.com ”,我仍然可以获得数据,有人可以解释一下这个 Javascript POST 是如何工作的吗?
3:请指点我一个指南/教程,以了解更多关于 JavaScript 和 Native iOS App 交互的信息。