swift - 如何从 Braintree Payments php 获取 header 响应

标签 swift braintree braintree-sandbox

我目前使用 Braintree Payment。我能够使用我的 iOS 在仪表板中创建成功的付款,问题是我正在尝试将响应返回给客户端 (iOS),现在返回“”,提前感谢您的帮助。

我现在的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()

最佳答案

完全披露:我在 Braintree 工作。如果您还有任何疑问,请随时联系support .

请记住,在响应中返回任何内容之前,PHP 代码会在您的服务器上进行评估。在这种情况下,Braintree\Transaction::sale 调用会正确计算并将结果保存到您的 $result 变量中。但是,没有其他任何事情发生,您也没有向请求者返回任何内容。

要返回响应,您只需将其打印出来即可。请注意,PHP 默认使用设置为“text/html”的 Content-Type header ,因此如果您不想返回网页,您可能希望将其更改为“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;

关于swift - 如何从 Braintree Payments php 获取 header 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40579276/

相关文章:

swift 4 : SKActionMoveBy goes left instead of right

jquery - 使用 Braintree 自定义表单以编程方式提交

ruby-on-rails - 类地址的父类(super class)不匹配 Braintree

node.js - 如何在 Braintree MarketPlace 中从子商户账户退款?

swift - 在 Swift 中,如何将 CFStringRef[] 和 CFTypeRef[] 转换为 CMutablePointer< COpaquePointer >?

ios - 分手了超大类(class),现在出现 segue 错误 [swift]

swift - 有没有办法在表格 View 中计算警报?

python - Braintree:Python Heroku 服务器

java - Braintree 结账付款失败