Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe.Response<Stripe.Charge> does not contain header information #1204

Closed
joeythomaschaske opened this issue Jul 28, 2021 · 1 comment · Fixed by #1206
Closed

Stripe.Response<Stripe.Charge> does not contain header information #1204

joeythomaschaske opened this issue Jul 28, 2021 · 1 comment · Fixed by #1206
Assignees

Comments

@joeythomaschaske
Copy link

joeythomaschaske commented Jul 28, 2021

The charges.create method returns a Stripe.Response<Stripe.Charge> object. The Stripe.Response object is supposed to have a headers attribute like so

export type Response<T> = T & {
  headers: {[key: string]: string};
  lastResponse: {
    requestId: string;
    statusCode: number;
    apiVersion?: string;
    idempotencyKey?: string;
    stripeAccount?: string;
  };
};

In reality, this attribute does not exist in the raw JSON.

In a conversation on the stripe discord server it appears the header information is actually stored on the lastResponse object. But again looking at the Stripe.Response definition, it is not present on that object.

That leads us to having to rely on undocumented attributes and telling typescript to ignore them

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.log(`Last Response: ${JSON.stringify(charge.lastResponse?.headers)}`);
@joeythomaschaske
Copy link
Author

One thing that is odd is that when the charges.create throws an exception, the error object contains the header information

{
    "type": "StripeInvalidRequestError",
    "raw": {
        "code": "token_in_use",
        "doc_url": "https://stripe.com/docs/error-codes/token-in-use",
        "message": "There is currently another in-progress request using this Stripe token (that probably means you clicked twice, and the other charge is still going through): ba_1JFKbuHaVrFFEQvKxUkfstDG. This token can not be used again if that charge is successful.'",
        "type": "invalid_request_error",
        "headers": {
            "server": "nginx",
            "date": "Wed, 28 Jul 2021 16:14:28 GMT",
            "content-type": "application/json",
            "content-length": "420",
            "connection": "keep-alive",
            "access-control-allow-credentials": "true",
            "access-control-allow-methods": "GET, POST, HEAD, OPTIONS, DELETE",
            "access-control-allow-origin": "*",
            "access-control-expose-headers": "Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required",
            "access-control-max-age": "300",
            "cache-control": "no-cache, no-store",
            "idempotency-key": "cc9be33b-5660-4ce1-88d6-6a57fe9bf26c-cc9be33b-5660-4ce1-88d6-6a57fe9bf26c-db3ba87b-81e4-4471-a1b4-19a2de81c5f7-2021-07-28",
            "idempotent-replayed": "true",
            "original-request": "req_pYbvZJ0OXzpfKn",
            "request-id": "req_cyGe9oAOWKMUVa",
            "stripe-should-retry": "false",
            "stripe-version": "2020-08-27",
            "x-stripe-c-cost": "0",
            "strict-transport-security": "max-age=31556926; includeSubDomains; preload"
        },
        "statusCode": 400,
        "requestId": "req_cyGe9oAOWKMUVa"
    },
    "rawType": "invalid_request_error",
    "code": "token_in_use",
    "doc_url": "https://stripe.com/docs/error-codes/token-in-use",
    "headers": {
        "server": "nginx",
        "date": "Wed, 28 Jul 2021 16:14:28 GMT",
        "content-type": "application/json",
        "content-length": "420",
        "connection": "keep-alive",
        "access-control-allow-credentials": "true",
        "access-control-allow-methods": "GET, POST, HEAD, OPTIONS, DELETE",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required",
        "access-control-max-age": "300",
        "cache-control": "no-cache, no-store",
        "idempotency-key": "cc9be33b-5660-4ce1-88d6-6a57fe9bf26c-cc9be33b-5660-4ce1-88d6-6a57fe9bf26c-db3ba87b-81e4-4471-a1b4-19a2de81c5f7-2021-07-28",
        "idempotent-replayed": "true",
        "original-request": "req_pYbvZJ0OXzpfKn",
        "request-id": "req_cyGe9oAOWKMUVa",
        "stripe-should-retry": "false",
        "stripe-version": "2020-08-27",
        "x-stripe-c-cost": "0",
        "strict-transport-security": "max-age=31556926; includeSubDomains; preload"
    },
    "requestId": "req_cyGe9oAOWKMUVa",
    "statusCode": 400
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants