当处理charge.refund webhook时,Stripe.Net发票属性为NULL

LMK*_*LMK 2 c# webhooks stripe-payments stripe.net

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

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

仪表板Webhook API版本: 2017-08-15

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

这是Stripe Webhook事件

在此处输入图片说明

这是中断代码(charge.Invoice.SubscriptionId用空引用中断)

在此处输入图片说明

有人遇到过这个问题吗?

谢谢

Nko*_*osi 5

查看源代码Stripe.Mapper<T>.MapFromJson

// 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;
}
Run Code Online (Sandbox Code Playgroud)

使用JSON.Net反序列化JSON,

StripeCharge.Invoice属性标记为[JsonIgnore]attribute的事实。

#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
Run Code Online (Sandbox Code Playgroud)

最后是如何InternalInvoice通过StringOrObject<T>

StringOrObject<StripeInvoice>.Map(value, s => this.InvoiceId = s, o => this.Invoice = o);
Run Code Online (Sandbox Code Playgroud)

您可以在类定义中看到

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果传递的值为a string,则它将Invoiceobject属性设置为null

else if (value is string)
{
    updateId((string)value);
    updateObject(null);
}
Run Code Online (Sandbox Code Playgroud)

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

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