dta38159 2016-11-10 22:27
浏览 38

在Braintree事务中发布变量undefined

I am setting up payment processing for my OSX desktop app in swift. Since there is no SDK for OSX, I am using a Web View via the PHP SDK for Braintree. I want to pass some POST data so that the price of a product is coming dynamically from my App, but it seems to not be recognized and is underfined according to PHP errors.
I know how to send POST variable to a PHP script and have done it many times, but I have a feeling something might not be right with my swift code. My PHP error is: "Notice: Undefined variable: price"

PayPal.swift

import Cocoa
import WebKit

class PayPal: NSViewController {

@IBOutlet weak var paypalWebView: WebView?

override func viewWillAppear() {

    self.view.window!.title = "Payment"
}

override func viewDidLoad() {
    super.viewDidLoad()

    setupPP()

}

func setupPP(){

    let request: NSURL = NSURL(string: "http://")!

    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: request as URL)

    urlRequest.httpMethod = "POST"
    let postString = "price=45"
    urlRequest.httpBody = postString.data(using: String.Encoding.utf8);

    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
        (data, response, error) -> Void in

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {

            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")

        }

        let responseString = String(data: data!, encoding: .utf8)
        print("responseString = \(responseString)")

    }

    task.resume()

    let requesting = NSURLRequest(url: request as URL)

    DispatchQueue.main.async(){
        self.paypalWebView?.mainFrame.load(requesting as URLRequest)
    }

}

}

Braintree PHP Script

if(isset($_POST["price"])){

$price = $_POST["price"];

}

<?php $tr_data = Braintree_TransparentRedirect::transactionData(
                array('redirectUrl' => "http://" ,
                'transaction' => array('amount' => $price, 'type' => 'sale'))) ?>

Also the Paypal View that is essentially a web view is being activated when a user clicks on a button via a segue. I don't know if that would have anything to do with it or not?

  • 写回答

1条回答 默认 最新

  • doufeixi6014 2016-11-11 16:40
    关注

    I ended up getting it to work!

    Paypal.swift

    import Cocoa
    import WebKit
    
    class PayPal: NSViewController {
    
    @IBOutlet weak var paypalWebView: WebView?
    
    override func viewWillAppear() {
    
        self.view.window!.title = "Payment"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        setupPP()
    
    }
    
    
    func setupPP(){
    
        let request: NSURL = NSURL(string: "http://")!
    
        let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: request as URL)
    
        urlRequest.httpMethod = "POST"
        let postString = "amount=29.95"
        urlRequest.httpBody = postString.data(using: String.Encoding.utf8);
    
        let session = URLSession.shared
        let task = session.dataTask(with: urlRequest as URLRequest) {
            (data, response, error) -> Void in
    
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
    
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
    
            }
    
            let responseString = String(data: data!, encoding: .utf8)
            print("responseString = \(responseString)")
    
        }
    
        paypalWebView?.mainFrame.load(urlRequest as URLRequest)
    
        task.resume()
    
    
    }
    
    }
    

    PHP Script

    <?php
                if (isset ($_POST['amount'])) {
    
                        $amount = $_POST['amount'];
                }
    
                $tr_data = Braintree_TransparentRedirect::transactionData(
                    array('redirectUrl' => "http://" . $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH),
    
                    'transaction' => array('amount' => $amount, 'type' => 'sale'))) 
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀