chargebee / chargebee-typescript

Typescript library for the Chargebee API.
https://apidocs.chargebee.com/docs/api?lang=typescript
MIT License
23 stars 16 forks source link

portal_session.create returning invalid response #49

Open ankero opened 7 months ago

ankero commented 7 months ago

Hi,

The client.portal_session.create().request() is returning an invalid response. It is only including the responseHeaders to the response. The server is returning the portal session, but the library has a bug in core.js responseHandler on line response = new result_1.Result(response, responseHeaders); that causes the response to be dropped and only the responseHeaders get returned.

Reproduce just by calling that API with this library.

Here's the request I made to produce this issue:

const response = await chargebeeClient.portal_session.create({
        customer: {
          id: customerId,
        },
      }).request()

Workaround can be done with direct API call, for example:

  private async createPortalSession(
    customerId: string,
  ): Promise<PortalSession> {
    try {
      /**
       * this.chargebeeClient.portal_session.create().request() is
       * broken. So we'll use axios until its fixed.
       */
      const { data } = await axios.post(
        `https://${this.apiAuth?.site}.chargebee.com/api/v2/portal_sessions?customer[id]=${customerId}`,
        {},
        {
          headers: {
            Authorization: `Basic ${Buffer.from(
              `${this.apiAuth?.api_key}:`,
            ).toString("base64")}`,
            "Content-Type": "application/json",
          },
        },
      );
      // @ts-ignore response
      return data.portal_session as PortalSession;
    } catch (error) {
      console.error("Failed to create portal session", error);
      throw error;
    }
  }
cb-sriramthiagarajan commented 6 months ago

Hi @ankero, I'm sorry about the delay in responding to this. I'm able to see this working correctly using the SDK. Sharing the code snipped that works for me:

const response = await chargebee.portal_session
      .create({
        customer: {
          id: "customer_id"
        }
      })
      .request();
console.log(response.portal_session);

Could you give this a try and let us know if it works for you? Please share the error details if you see any.

cb-sriramthiagarajan commented 5 months ago

Hi @ankero, did you get a chance to try this out? Let me know if you're still facing any issues.