Stripe - 使用PHP,iOS和Alamofire部分退款

时间:2017-10-16 19:10:28

标签: php json swift alamofire stripe-payments

嗨我收到错误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);
}
?>

1 个答案:

答案 0 :(得分:0)

看起来你的回复 iOS应用程序时出错,而不是进入你的PHP脚本。您可能需要尝试使用switching to responseString来查看是否修复了它,或者尝试在{JEN} echo之前添加Content-Type标头:

header('Content-Type: application/json');