2016-11-13 65 views
0

我目前使用Braintree付款。我能夠使用我的iOS在儀表板中創建成功的付款問題是我正在嘗試返回客戶端(iOS)的回覆,現在它返回「」,感謝您提前給予幫助。如何從Braintree付款獲得標題回覆php

我當前的PHP

<?php 
require_once("../includes/braintree_init.php"); 

//$amount = $_POST["amount"]; 
//$nonce = $_POST["payment_method_nonce"]; 
$nonce = "fake-valid-nonce"; 
$amount = "10"; 

$result = Braintree\Transaction::sale([ 
    'amount' => $amount, 
    'paymentMethodNonce' => $nonce 
]); 

我的客戶

URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in 
      // TODO: Handle success or failure 
      let responseData = String(data: data!, encoding: String.Encoding.utf8) 
      // Log the response in console 
      print(responseData); 

      // Display the result in an alert view 
      DispatchQueue.main.async(execute: { 
       let alertResponse = UIAlertController(title: "Result", message: "\(responseData)", preferredStyle: UIAlertControllerStyle.alert) 

       // add an action to the alert (button) 
       alertResponse.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 

       // show the alert 
       self.present(alertResponse, animated: true, completion: nil) 

      }) 

      } .resume() 

回答

1

全面披露:我在布倫特裏工作。如果您有任何其他問題,請隨時聯繫support

請記住,PHP代碼在響應中返回任何內容之前會在您的服務器上進行評估。在這種情況下,Braintree\Transaction::sale呼叫評估正確,並將結果保存到您的$result變量中。然而,沒有其他的事情發生,你沒有回報你的請求者。

要返回響應,您可以簡單地打印出來。請注意,PHP默認使用Content-Type頭設置爲「text/html」,所以如果你不想返回一個網頁,你可能會想把它改成「application/json」之類的東西,或者其他什麼是最適合你的。

$result = Braintree\Transaction::sale([ 
    'amount' => $amount, 
    'paymentMethodNonce' => $nonce 
]); 

$processed_result = // you could serialize the result here, into JSON, for example 
header('Content-Type: application/json'); 
print $processed_result; 
+0

你能給我一個json序列化的例子爲「$ processed_result =」 – pprevalon