您好,我收到错误 responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
我猜这是由我传递到后端服务器的金额引起的。但是,我无法找出原因。如果我处理全额退款(不使用金额和货币),则一切正常。
下面的代码是在 iOS 中。
print(countOrders)
var requestString = "http://xxxxx.com/fullRefund.php"
var params = ["chargeId": chargeId]
let amountStr = String(Int(sellingPrice * Double(quantity)))
if countOrders > 1 {
requestString = "http://xxxxx.com/partialRefund.php"
params = ["chargeId": chargeId, "amount": amountStr, "currency": currency]
}
print(requestString)
print(chargeId)
print(amountStr)
print(currency)
Alamofire.request(requestString, method: .post, parameters: params).responseJSON { (response) in
switch response.result {
case .success(_):
break
case .failure(let error):
print(error)
break
}
}
每个输入在打印时都应该正确
2
http://xxxxx.com/partialRefund.php
ch_1BDdTTLYXQrQQLvfRzXnzLsh
4083
CNY
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
这是 Stripe 的收费详情
ID: ch_1BDdTTLYXQrQQLvfRzXnzLsh
Amount: ¥6,366.00 CNY → $7,391.84 HKD
下面的代码是partialRefund.php。我猜想数量有问题,因为错误显示 inputDataNilOrZeroLength
<?php
require_once('stripe-php/init.php');
\Stripe\Stripe::setApiKey('sk_test_keyyyyyyyyyyyyyyyy');
$chargeId = $_POST['chargeId'];
$amount = $_POST['amount']; // this line is deleted in fullRefund.php
$currency = $_POST['currency']; // this line is deleted in fullRefund.php
try {
$re = \Stripe\Refund::create(
array(
"charge" => $chargeId,
"amount" => $amount*100, // this line is deleted in fullRefund.php
"currency" => $currency // this line is deleted in fullRefund.php
)
);
$json = array(
'status' => 'Success'
);
echo json_encode($json);
} catch(\Stripe\Error\Card $e) {
$json = array(
'status' => 'Failure',
'message' => $e->getMessage()
);
echo json_encode($json);
}
?>
最佳答案
看起来您在对 iOS 应用程序的响应中遇到了错误,而不是在 PHP 脚本中出现错误。您可能想尝试 switching to responseString
看看是否可以解决问题,或者尝试在 echo
JSON 之前添加 Content-Type header :
header('Content-Type: application/json');
关于php - Stripe - 通过 PHP、iOS 和 Alamofire 进行部分退款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46777478/