简体   繁体   English

如何在 Flutter 中实现 3D Secure(由 Visa / MasterCard SecureCode 验证)?

[英]How to implement 3D Secure (Verified by Visa / MasterCard SecureCode) in Flutter?

The existing implementation 3DS is such that during checkout and payment stage, it involves redirecting the customer away from your app, to a bank / card issuer website where the customer can enter their previously set-up password to authenticate that they are indeed the card holder.现有的 3DS 实现是这样的,在结账和付款阶段,它涉及将客户从您的应用程序重定向到银行/发卡机构网站,客户可以在其中输入他们之前设置的密码以验证他们确实是持卡人. The website will then redirect the customer back to your website with information needed to complete the transaction.然后,该网站会将客户重定向回您的网站,并提供完成交易所需的信息。 How can I read the response of the bank server to the redirection?如何读取银行服务器对重定向的响应? I tried to do this via the webview_flutter, but can only get URL redirects.我试图通过 webview_flutter 做到这一点,但只能获得 URL 重定向。

Widget _view3ds(PaymentAnswer answer) {
  final url = answer.model['AcsUrl'];
  return Scaffold(
    appBar: AppBar(
      title: const Text('Pay'),
    ),
    body: WebView(
      initialUrl: url,
      initialPostParameters: answer.paramPost3DS(),
      javascriptMode: JavascriptMode.unrestricted,
      onPageFinished: _pageFinished
    ),
  );
}

void _pageFinished(String url)  {
  print(url);
  url.response ??? // Is there a way to get to the server response? Maybe there is another plugin that allows this?
}

There is a swift code that works great:有一个很好用的 swift 代码:

/// Handle result from 3DS form
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let urlString = request.url?.absoluteString
        if (urlString == Constants.cloudpaymentsURL) {
            var response: String? = nil
            if let aBody = request.httpBody {
                response = String(data: aBody, encoding: .ascii)
            }
            let responseDictionary = parse(response: response)
            webView.removeFromSuperview()
            post3ds(transactionId: responseDictionary?["MD"] as! String, paRes: responseDictionary?["PaRes"] as! String)
            return false
        }
        return true
    }

Can i get something like this on pure flutter?我可以在纯颤振上得到这样的东西吗?

You need to understand the flows that are happening in your payment integration.您需要了解支付集成中发生的流程。 Basically there is the app, the server and the payment gateway.基本上有应用程序、服务器和支付网关。

Your app may initiate a payment to your server and your server may then redirect this call to payment gateway.您的应用程序可能会向您的服务器发起付款,然后您的服务器可能会将此调用重定向到支付网关。 Alternatively, your app may directly call a link on a payment gateway (this is implementation dependent).或者,您的应用程序可以直接调用支付网关上的链接(这取决于实现)。 In either case, the call to the payment gateway will most likely be initiated with a callbackUrl parameter of some sort.在任何一种情况下,对支付网关的调用很可能是使用某种callbackUrl参数启动的。 This callbackUrl will be used by the payment gateway to notify you of any success or failure.支付网关将使用此callbackUrl来通知您任何成功或失败。

In most cases if not all, the callbackUrl is a publicly accessible url and hence will most probably be the your servers (your application is not publicly accessible for the payment gateway to invoke any endpoints on).在大多数情况下,如果不是全部,callbackUrl 是一个可公开访问的 url,因此很可能是您的服务器(您的应用程序不可公开访问以供支付网关调用任何端点)。 Note that you app still has no access to this data as the payment gateway calls your server directly.请注意,您的应用程序仍然无法访问此数据,因为支付网关直接调用您的服务器。 Your server will then execute an HTTP Redirect which will be reflected by the app going to a success or error page.然后,您的服务器将执行 HTTP 重定向,该重定向将通过应用程序转到成功或错误页面来反映。

This redirect url is the key.这个重定向 url 是关键。 It should hold either a transactionId that u can later use to query the server for status or a general success/failure format with sufficient info for your next work.它应该包含一个您以后可以用来查询服务器状态的 transactionId,或者包含为您的下一个工作提供足够信息的一般成功/失败格式。

Also please understand that the above is only an example.另外请理解,以上只是一个例子。 You will have to figure out how the payment flow is implemented in your server.您必须弄清楚付款流程是如何在您的服务器中实现的。

To obtain a handle onto the navigation functions of a WKWebView use the navigation delegate: https://developer.apple.com/documentation/webkit/wknavigationdelegate要获得 WKWebView 导航功能的句柄,请使用导航委托: https : //developer.apple.com/documentation/webkit/wknavigationdelegate

Specifically you will need to look at the delegate methods and choose an appropriate handle to capture the url.具体来说,您需要查看委托方法并选择合适的句柄来捕获 url。 I would assume the following will help you:我认为以下内容会对您有所帮助:

func webView(WKWebView, didReceiveServerRedirectForProvisionalNavigation: WKNavigation!)

You should parse the url with RegExp.您应该使用 RegExp 解析 url。 I think it should be "order_id" or something similar in case of successful transaction我认为在成功交易的情况下它应该是“order_id”或类似的东西

if (url.contains('order_id')) {
  flutterWebViewPlugin.close();
  RegExp regExp = RegExp("order_id=(.*)");
  var token = regExp.firstMatch(url)?.group(1);
  ...
} else {
  print('rejected transaction');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM