所以我正在尝试使用PHP Swift和Alamofire创建托管条带帐户。但它根本不起作用。
这是我的PHP代码:
<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("My APIKEY");
$country = $_POST['country'];
$create = \Stripe\Account::create(array(
"country" => $country,
"managed" => true
)
);
?>
这是我的快速代码:
@IBAction func createBtn(_ sender: Any) {
let card = STPCardParams()
card.number = "5200828282828210"
card.expMonth = 4
card.expYear = 2024
card.cvc = "242"
card.currency = "usd"
STPAPIClient.shared().createToken(withCard: card ,completion: {(token, error) -> Void in
if let error = error {
print("ERROR: \(error.localizedDescription)")
}
else if let token = token {
print(token)
self.createUsingToken(token:token)
}
})
}
func createUsingToken(token:STPToken) {
let requestString = "My request URL"
let params = ["token": token.tokenId, "country": "US"]
//This line of code will suffice, but we want a response
Alamofire.request(requestString, method: .post, parameters: params).responseJSON { (response) in
print("REQUEST: \(response.request!)") // original URL request
print("RESPONSE: \(response.response!)") // URL response
print("DATA: \(response.data!)") // server data
print("RESULT: \(response.result)") // result of response serialization
if let JSON = response.result.error {
print("JSON: \(JSON.localizedDescription)")
}
}
}
我从Alamofire收到此错误: JSON:响应无法序列化,输入数据为零或零长度。 谢谢你的帮助。
答案 0 :(得分:0)
看起来您的Swift / Alamofire请求需要JSON响应,但您的PHP代码根本没有发送任何响应:您正在向Stripe发送account creation request但从不输出任何数据。
您可能希望使用Swift代码中所需的属性准备一个数组,然后在PHP脚本的末尾将其输出为JSON:
echo json_encode($result);