首页 > 解决方案 > Stripe STPCustomerDeserializer 无法解析客户 Swift

问题描述

从 Stripe API 返回的客户 JSON 无法使用 STPCustomerDeserializer(jsonResponse: data) 解析,定位“id”字段时返回 nil。记录数据表明 id 不为零。

客户对象与 Stripe API 返回的格式完全相同。错误消息“来自 Stripe 的响应未能解析为有效的 JSON。”

调试反序列化器表明它在这里失败:

// required fields
NSString *stripeId = [dict stp_stringForKey:@"id"];
if (!stripeId) {
    return nil; <<< returning here
}

在 >>>

- (nullable NSString *)stp_stringForKey:(NSString *)key {
    id value = self[key];
    if (value && [value isKindOfClass:[NSString class]]) {
        return value;
    }
    return nil; << returning here
}

我曾尝试以不同格式传递客户数据,但无济于事。

我看到有人在这里问类似的问题无法读取数据,因为它的格式不正确 JSON & SWIFT 3

["customer": {
    "account_balance" = 0;
    address = "<null>";
    balance = 0;
    created = ***;
    currency = "<null>";
    "default_source" = “***”;
    delinquent = 0;
    description = "<null>";
    discount = "<null>";
    email = “***”;
    id = "cus_***";
    "invoice_prefix" = ***;
    "invoice_settings" =     {
        "custom_fields" = "<null>";
        "default_payment_method" = "<null>";
        footer = "<null>";
    };
    livemode = 1;
    metadata =     {
    };
    name = "<null>";
    object = customer;
    phone = "<null>";
    "preferred_locales" =     (
    );
    shipping = "<null>";
    sources =     {
        data =         (
                        {
                "address_city" = "<null>";
                "address_country" = GB;
                "address_line1" = "<null>";
                "address_line1_check" = "<null>";
                "address_line2" = "<null>";
                "address_state" = "<null>";
                "address_zip" = "<null>";
                "address_zip_check" = "<null>";
                brand = Visa;
                country = GB;
                customer = "cus_***";
                "cvc_check" = pass;
                "dynamic_last4" = "<null>";
                "exp_month" = ***;
                "exp_year" = ***;
                fingerprint = ***;
                funding = debit;
                id = "card_***";
                last4 = ***;
                metadata =                 {
                };
                name = "<null>";
                object = card;
                "tokenization_method" = "<null>";
            }
        );
        "has_more" = 0;
        object = list;
        "total_count" = 1;
        url = "/v1/customers/cus_***/sources";
    };
    subscriptions =     {
        data =         (
        );
        "has_more" = 0;
        object = list;
        "total_count" = 0;
        url = "/v1/customers/cus_***/subscriptions";
    };
    "tax_exempt" = none;
    "tax_ids" =     {
        data =         (
        );
        "has_more" = 0;
        object = list;
        "total_count" = 0;
        url = "/v1/customers/cus_***/tax_ids";
    };
    "tax_info" = "<null>";
    "tax_info_verification" = "<null>";
}]

标签: iosswiftstripe-payments

解决方案


我修好了它!data["customer"]我只需要使用as下标到 json中Any


推荐阅读