首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Stripe.Net发票属性在处理charge.refund web钩子时为空。

Stripe.Net发票属性在处理charge.refund web钩子时为空。
EN

Stack Overflow用户
提问于 2018-07-21 17:55:32
回答 1查看 636关注 0票数 2

我正在使用Stripe.Net来处理付款。当我开始测试"charge.refund“网页钩子时,我在代码后面得到了空发票属性,但是在Stripeweb钩子事件中存在发票值,并且我确认发票也是存在的仪表板。

注意到Stripe.Net中的版本和配置的webhook是不同的。

仪表板Web钩子API版本:2017-08-15

Stripe.Net版本: 16.12.0.0 (它支持2018-02-06)。

下面是Stripe webhook事件

这是破译代码(使用空引用的charge.Invoice.SubscriptionId中断)

以前有人遇到过这个问题吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-21 18:19:36

看着Stripe.Mapper.MapFromJson

代码语言:javascript
复制
// the ResponseJson on a list method is the entire list (as json) returned from stripe.
// the ObjectJson is so we can store only the json for a single object in the list on that entity for
// logging and/or debugging
public static T MapFromJson(string json, string parentToken = null, StripeResponse stripeResponse = null)
{
    var jsonToParse = string.IsNullOrEmpty(parentToken) ? json : JObject.Parse(json).SelectToken(parentToken).ToString();

    var result = JsonConvert.DeserializeObject<T>(jsonToParse);

    // if necessary, we might need to apply the stripe response to nested properties for StripeList<T>
    ApplyStripeResponse(json, stripeResponse, result);

    return result;
}

public static T MapFromJson(StripeResponse stripeResponse, string parentToken = null)
{
    return MapFromJson(stripeResponse.ResponseJson, parentToken, stripeResponse);
}

private static void ApplyStripeResponse(string json, StripeResponse stripeResponse, object obj)
{
    if (stripeResponse == null)
    {
        return;
    }

    foreach (var property in obj.GetType().GetRuntimeProperties())
    {
        if (property.Name == nameof(StripeResponse))
        {
            property.SetValue(obj, stripeResponse);
        }
    }

    stripeResponse.ObjectJson = json;
}

它使用JSON.Net反序列化JSON,

事实是 attribute

代码语言:javascript
复制
#region Expandable Invoice

/// <summary>
/// ID of the invoice this charge is for if one exists.
/// </summary>
public string InvoiceId { get; set; }

[JsonIgnore]
public StripeInvoice Invoice { get; set; }

[JsonProperty("invoice")]
internal object InternalInvoice
{
    set
    {
        StringOrObject<StripeInvoice>.Map(value, s => this.InvoiceId = s, o => this.Invoice = o);
    }
}
#endregion

以及如何通过InternalInvoice映射StringOrObject<T>属性。

代码语言:javascript
复制
StringOrObject<StripeInvoice>.Map(value, s => this.InvoiceId = s, o => this.Invoice = o);

您可以在类定义中看到

代码语言:javascript
复制
internal static class StringOrObject<T>
    where T : StripeEntityWithId
{
    public static void Map(object value, Action<string> updateId, Action<T> updateObject)
    {
        if (value is JObject)
        {
            T item = ((JToken)value).ToObject<T>();
            updateId(item.Id);
            updateObject(item);
        }
        else if (value is string)
        {
            updateId((string)value);
            updateObject(null);
        }
    }
}

如果传递的值是string,则将Invoice对象属性设置为null。

代码语言:javascript
复制
else if (value is string)
{
    updateId((string)value);
    updateObject(null);
}

因此,您描述的行为是根据所显示的数据和代码设计的。

您可能需要提取InvoiceId并尝试检索它(发票)以使用其成员。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51458690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档