所以我尝试使用 PHP Swift 和 Alamofire 创建托管 strip 帐户。但它根本不起作用。
这是我的 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:无法序列化响应,输入数据为零或零长度。 感谢您的帮助。
最佳答案
看起来您的 Swift/Alamofire 请求需要 JSON 响应,但您的 PHP 代码根本没有发送任何响应:您正在发送 account creation request到 Stripe,但从不输出任何数据。
您可能想要准备一个包含 Swift 代码中所需属性的数组,然后在 PHP 脚本末尾将其输出为 JSON:
echo json_encode($result);
关于php - 使用 php 和 swift 创建托管 strip 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41052140/