pprevalon pprevalon - 20 days ago 10
Swift Question

How to get a header response from Braintree Payments php

I currently using Braintree Payment. Im able to create a successful payment in the dashboard using my iOS the problem is I'm trying to return back to client(iOS) the response, right now it is return " ", thank you for the help in advance.

My current 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
]);


my client

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()

Answer

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

Remember, PHP code is evaluated on your server before anything is returned in a response. In this case, the Braintree\Transaction::sale call evaluates properly and the result is saved into your $result variable. However, nothing else happens and you return nothing to your requester.

To return the response, you can simply print it out. Note that PHP defaults to using a Content-Type header set to "text/html", so if you don't want to return a webpage, you'll likely want to change it to something like "application/json", or whatever is most appropriate for you.

$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;
Comments