Skip to content

Stripe.StripeException: Invalid response object from API #3068

@trevonmckay

Description

@trevonmckay

Describe the bug

I'm using Stripe.net 47.4.0 and many of my operations are failing with the error "Invalid response object from API" but I can see that the request was successfully fulfilled on Stripe. I first use case I was able to work around using the following snippet but now trying to expand to other areas I need a real fix

string sessionSecret;
try
{
    Session session = sessionService.Create(sessionCreateOptions);
    sessionSecret = session.ClientSecret;
}
catch (StripeException stripeException)
{
    if (stripeException.Message.StartsWith("Invalid response object from API:")
        && JsonDocument.Parse(stripeException.StripeResponse.Content).RootElement.TryGetProperty("client_secret", out JsonElement clientSecretElement)
        && clientSecretElement.GetString() is string clientSecretValue)
    {
        sessionSecret = clientSecretValue;
    }
    else
    {
        throw;
    }
}
private async Task<IEnumerable<PaymentIntent>> GetMatchingStripePaymentIntents(IEnumerable<string> requestIds)
{
    if (requestIds.Count() > 10)
    {
        throw new InvalidOperationException("The number of search filters exceeds maximum limit of 10");
    }

    string searchQueryText = string.Join(" OR ", requestIds.Select(requestId => $"metadata[\"verificationRequestId\"]:\"{requestId}\""));

    PaymentIntentService paymentIntentService = new(_stripeClient);
    PaymentIntentSearchOptions searchOptions = new()
    {
        Query = searchQueryText,
    };

    List<PaymentIntent> paymentIntents = new();
    StripeSearchResult<PaymentIntent> searchResult;
    do
    {
        searchResult = await paymentIntentService.SearchAsync(searchOptions);
        paymentIntents.AddRange(searchResult.Where(e => e.Status != "canceled"));
    } while (searchResult.HasMore);

    return paymentIntents;
}

This snippet results in the exception below
Exception: Stripe.StripeException: Invalid response object from API:

Response body

 {
  "object": "search_result",
  "data": [
    {
      "id": "pi_3R4HvCPKt2B4A0jv0Suwf9NN",
      "object": "payment_intent",
      "amount": 14900,
      "amount_capturable": 14900,
      "amount_details": {
        "tip": {}
      },
      "amount_received": 0,
      "application": null,
      "application_fee_amount": null,
      "automatic_payment_methods": null,
      "canceled_at": null,
      "cancellation_reason": null,
      "capture_method": "manual",
      "client_secret": "...",
      "confirmation_method": "automatic",
      "created": 1742372658,
      "currency": "usd",
      "customer": null,
      "description": "...",
      "invoice": null,
      "last_payment_error": null,
      "latest_charge": "ch_3R4HvCPKt2B4A0jv0LIr4IHa",
      "livemode": false,
      "metadata": {
        "$ref": "...",
        "userId": "c33a3176-df7e-4468-9c73-85dbf2d59a67",
        "verificationRequestId": "0195ad70-1935-7695-8c32-9c6e0c423986"
      },
      "next_action": null,
      "on_behalf_of": null,
      "payment_method": "pm_1R4HvBPKt2B4A0jvcEEVJkoT",
      "payment_method_configuration_details": null,
      "payment_method_options": {
        "card": {
          "installments": null,
          "mandate_options": null,
          "network": null,
          "request_three_d_secure": "automatic"
        }
      },
      "payment_method_types": [
        "card"
      ],
      "processing": null,
      "receipt_email": "...",
      "review": null,
      "setup_future_usage": null,
      "shipping": null,
      "source": null,
      "statement_descriptor": null,
      "statement_descriptor_suffix": "VERIFY",
      "status": "requires_capture",
      "transfer_data": null,
      "transfer_group": null
    },
    {
      "id": "pi_3R4HSMPKt2B4A0jv1Zqw8woQ",
      "object": "payment_intent",
      "amount": 14900,
      "amount_capturable": 14900,
      "amount_details": {
        "tip": {}
      },
      "amount_received": 0,
      "application": null,
      "application_fee_amount": null,
      "automatic_payment_methods": null,
      "canceled_at": null,
      "cancellation_reason": null,
      "capture_method": "manual",
      "client_secret": "...",
      "confirmation_method": "automatic",
      "created": 1742370870,
      "currency": "usd",
      "customer": null,
      "description": "OneCredential Verification - Von Estates",
      "invoice": null,
      "last_payment_error": null,
      "latest_charge": "ch_3R4HSMPKt2B4A0jv1NqUVxaM",
      "livemode": false,
      "metadata": {
        "$ref": "...",
        "organizationId": "01958c73-1288-79c2-962d-bf5a6222c0b3",
        "userId": "c33a3176-df7e-4468-9c73-85dbf2d59a67",
        "verificationRequestId": "0195ad56-994a-7f4a-ab80-57845e5eaf92"
      },
      "next_action": null,
      "on_behalf_of": null,
      "payment_method": "pm_1R4HSMPKt2B4A0jvbmSiiMX2",
      "payment_method_configuration_details": null,
      "payment_method_options": {
        "card": {
          "installments": null,
          "mandate_options": null,
          "network": null,
          "request_three_d_secure": "automatic"
        }
      },
      "payment_method_types": [
        "card"
      ],
      "processing": null,
      "receipt_email": "...",
      "review": null,
      "setup_future_usage": null,
      "shipping": null,
      "source": null,
      "statement_descriptor": null,
      "statement_descriptor_suffix": "VERIFY",
      "status": "requires_capture",
      "transfer_data": null,
      "transfer_group": null
    },
    {
      "id": "pi_3R4HPePKt2B4A0jv1ECb3u9j",
      "object": "payment_intent",
      "amount": 14900,
      "amount_capturable": 14900,
      "amount_details": {
        "tip": {}
      },
      "amount_received": 0,
      "application": null,
      "application_fee_amount": null,
      "automatic_payment_methods": null,
      "canceled_at": null,
      "cancellation_reason": null,
      "capture_method": "manual",
      "client_secret": "...",
      "confirmation_method": "automatic",
      "created": 1742370702,
      "currency": "usd",
      "customer": null,
      "description": "...",
      "invoice": null,
      "last_payment_error": null,
      "latest_charge": "ch_3R4HPePKt2B4A0jv1iIdCvCq",
      "livemode": false,
      "metadata": {
        "$ref": "...",
        "organizationId": "01958c73-1288-79c2-962d-bf5a6222c0b3",
        "userId": "c33a3176-df7e-4468-9c73-85dbf2d59a67",
        "verificationRequestId": "0195ad56-994a-7f4a-ab80-57845e5eaf92"
      },
      "next_action": null,
      "on_behalf_of": null,
      "payment_method": "pm_1R4HPePKt2B4A0jvw9WM2wJO",
      "payment_method_configuration_details": null,
      "payment_method_options": {
        "card": {
          "installments": null,
          "mandate_options": null,
          "network": null,
          "request_three_d_secure": "automatic"
        }
      },
      "payment_method_types": [
        "card"
      ],
      "processing": null,
      "receipt_email": "...",
      "review": null,
      "setup_future_usage": null,
      "shipping": null,
      "source": null,
      "statement_descriptor": null,
      "statement_descriptor_suffix": "VERIFY",
      "status": "requires_capture",
      "transfer_data": null,
      "transfer_group": null
    }
  ],
  "has_more": false,
  "next_page": null,
  "url": "/v1/payment_intents/search"
}

To Reproduce

private async Task<IEnumerable<PaymentIntent>> GetMatchingStripePaymentIntents(IEnumerable<string> requestIds)
{
    if (requestIds.Count() > 10)
    {
        throw new InvalidOperationException("The number of search filters exceeds maximum limit of 10");
    }

    string searchQueryText = string.Join(" OR ", requestIds.Select(requestId => $"metadata[\"verificationRequestId\"]:\"{requestId}\""));

    PaymentIntentService paymentIntentService = new(_stripeClient);
    PaymentIntentSearchOptions searchOptions = new()
    {
        Query = searchQueryText,
    };

    List<PaymentIntent> paymentIntents = new();
    StripeSearchResult<PaymentIntent> searchResult;
    do
    {
        searchResult = await paymentIntentService.SearchAsync(searchOptions);
        paymentIntents.AddRange(searchResult.Where(e => e.Status != "canceled"));
    } while (searchResult.HasMore);

    return paymentIntents;
}

Expected behavior

I'm expecting to have a StripeSearchResult<PaymentIntent> object returned.

Code snippets

OS

Windows 11

.NET version

net9.0

Library version

stripe-dotnet 47.4.0

API version

2025-02-24.acacia

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions