php - Stripe - 通过 PHP、iOS 和 Alamofire 进行部分退款

标签 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);
}
?>

最佳答案

看起来您在对 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/

相关文章:

php - WordPress 查询更新未更新所有帖子

perl - 如何将 Perl 对象转换为 JSON,反之亦然

sql-server - OPENJSON 在 SQL Server 中不起作用?

swift - 尝试在 Swift 中使用 `min()` 函数时出现编译错误

ios - 当用户在 uipageviewcontroller 的最后一页时设置按钮的可见性

php - 使用 jquery 和 php 将数据与上传图像一起保存到 mysql

php - MYSQL基于复选框和按复选框进行列选择

php - 当 cron 作业设置为 "run every 5 minutes"时,它是否从添加之日起每 5 分钟运行一次?

javascript - 匹配来自两个单独 JSON feed 的值

swift - 如何在 Swift 的泛型函​​数中制作 let 参数的可变副本