简体   繁体   English

从swift 4调用paypal javascript函数

[英]call a paypal javascript function from swift 4

This is the java script for paypal payment How to call this from swift 4 and how to pass the amount to this script, now the amount to pay is hard coded 这是用于贝宝付款的Java脚本如何从swift 4调用此方法以及如何将金额传递给此脚本,现在付款金额已硬编码

paypal.Button.render({

                         env: 'sandbox', // sandbox | production

                         // PayPal Client IDs - replace with your own
                         // Create a PayPal app: https://developer.paypal.com/developer/applications/create
                         client: {
                         sandbox:    'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP4-8aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R',
                         production: '<insert production client id>'
                         },

                         // Show the buyer a 'Pay Now' button in the checkout flow
                         commit: true,


                         // payment() is called when the button is clicked
                         payment: function(data, actions) {

                         // Make a call to the REST api to create the payment
                         return actions.payment.create({
                                                       payment: {
                                                       transactions: [
                                                                      {
                                                                      amount: { total: '0.01', currency: 'USD' }
                                                                      }
                                                                      ]
                                                       }
                                                       });
                         },




                         // onAuthorize() is called when the buyer approves the payment
                         onAuthorize: function(data, actions) {

                         // Make a call to the REST api to execute the payment
                         return actions.payment.execute().then(function() {
                                                               window.alert('Payment Complete!');
                                                               });
                         }

                         }, '#paypal-button-container');

The code which I tried is : 我试过的代码是:

 @IBAction func buttonActionPay(_ sender: UIButton) {

    let htmlPath = Bundle.main.path(forResource: "sampleHTMLCode", ofType: "html")

    let url = URL(fileURLWithPath: htmlPath!)
    let request = URLRequest(url: url)
    webView.load(request)

}

The javascript code above is from paypal developer website and is there any need to edit the code. 上面的javascript代码来自paypal开发人员网站,是否需要编辑代码。

Just change the javascript like this. 只需像这样更改javascript。

<!DOCTYPE html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>

<body>
    <div id="paypal-button-container"></div>


    <script>

       // y = 12;

     function bar(qq){

        var y = qq




        paypal.Button.render({


                             env: 'sandbox', // sandbox | production

                             // PayPal Client IDs - replace with your own
                             // Create a PayPal app: https://developer.paypal.com/developer/applications/create
                             client: {
                             sandbox:    'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP4-8aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R',
                             production: '<insert production client id>'
                             },

                             // Show the buyer a 'Pay Now' button in the checkout flow
                             commit: true,


                             // payment() is called when the button is clicked
                             payment: function(data, actions) {

                             // Make a call to the REST api to create the payment
                             return actions.payment.create({
                                                           payment: {
                                                           transactions: [
                                                                          {
                                                                          amount: { total: y, currency: 'USD' }
                                                                          }
                                                                          ]
                                                           }
                                                           });
                             },


                             // onAuthorize() is called when the buyer approves the payment
                             onAuthorize: function(data, actions) {

                             // Make a call to the REST api to execute the payment
                             return actions.payment.execute().then(function() {
                                                                   window.alert('Payment Complete!');
                                                                   });
                             }

                             }, '#paypal-button-container');
       }


               </script>
    </body>

The Swift code is: Swift代码是:

import UIKit
   import WebKit
   import JavaScriptCore


     class ViewController: 
      UIViewController,WKUIDelegate,WKNavigationDelegate {
      var cont = String()

      var jsContext: JSContext!


         var webView: WKWebView
     required init(coder aDecoder: NSCoder) {
    self.webView = WKWebView(frame: CGRect.zero)
    super.init(coder: aDecoder)!
     }
     override func loadView() {
    super.loadView()

    let userContentController = WKUserContentController()

    let source = "bar('12');"
    let userScript = WKUserScript(source: source, injectionTime: 
    WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
    userContentController.addUserScript(userScript)

    let configuration = WKWebViewConfiguration()
    configuration.userContentController = userContentController
    self.webView = WKWebView(frame: self.view.frame, configuration: 
    configuration)
     }

    override func viewDidLoad() {
    super.viewDidLoad()
   ///


    view.addSubview(webView)

    webView.translatesAutoresizingMaskIntoConstraints = false
    let height = NSLayoutConstraint(item: webView, attribute: .height, 
    relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, 
    constant: 0)
    let width = NSLayoutConstraint(item: webView, attribute: .width, 
     relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, 
     constant: 0)
    view.addConstraints([height, width])

    let path = Bundle.main.path(forResource: "sampleHTMLCode", ofType: 
       "html")!
    let url = URL(fileURLWithPath: path)

    webView.load(URLRequest(url: url))
    webView.allowsBackForwardNavigationGestures = true


    ///
    webView.navigationDelegate = self

      }



  @IBAction func buttonActionPay(_ sender: UIButton) {

    let htmlPath = Bundle.main.path(forResource: "sampleHTMLCode", 
ofType: "html")

    let url = URL(fileURLWithPath: htmlPath!)
    let request = URLRequest(url: url)
    webView.load(request)

     }



     }

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

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