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

Searching by custom fields #6

Closed anderslyman closed 3 years ago

anderslyman commented 4 years ago

When using the .list() function, how do I specify a custom field to search by?

Naive approach (this line: 'cf_custom_field[is]': customValue yields a syntax error):

  chargebee.subscription
      .list({
        limit: 2,
        plan_id: {in: ['my-app', 'my-app-(premium)']}
        'cf_custom_field[is]': customValue
      })
      .request((error, result) => {
        if (error) {
          console.log(error);
        } else {
          // Do something
        }
      });

Refer: https://support.chargebee.com/support/discussions/topics/316925 Note that I have contacted support to enable this feature on our account - just need some direction performing the search with this library.

benzittlau commented 4 years ago

Interesting that this was just posted 7 minutes ago, as I'm hitting the same issue with customer custom fields. I attempted using this syntax:

  chargebee.customer
      .list({
          cf_custom_field: { is: customValue },
      })

And it appears it just silently drops the criteria and returns the full list (actually a pretty scary behaviour if you don't discover that's what's happening).

Going to try and trace through the source a bit to see if I can find a way to pass them through here.

Semi-related, I was able to pass through the custom field when creating a customer as follows:

   export interface ChargebeeCustomerCreateParams extends _customer.create_params {
      cf_custom_field: number;
    }
...
    const requestWrapper = this.chargebee.customer.create({
      first_name: user.firstName,
      cf_custom_field: customValue,
      last_name: user.lastName,
      email: user.email,
      company: account.name,
    } as ChargebeeCustomerCreateParams);
anderslyman commented 4 years ago

@benzittlau I heard back from the chargebee team, here's the syntax:

  chargebee.subscription.list({
    limit: 2,
    plan_id: {in: ['plan-a', 'plan-b']}
  })
  .param({
    cf_custom_field: customValue
  })

It might actually be: 'cf_custom_field[is]': customValue

I can compile with either syntax, which is progress (version 2.0.4). Running the query immediately fails with core.js:4081 ERROR Error: Uncaught (in promise): ReferenceError: Buffer is not defined so I'm not sure which it actually is, but maybe it'll get you going.

benzittlau commented 4 years ago

@anderslyman Thanks for reminding me of this thread. I was able to resolve my issue late last week (also working with Chargebee support) by tracing the issue back to the custom field type. Only single line text custom fields are supported for filtering via the API, and my field was initially a number. Once that was resolved, I was able to use the following code:

    export interface ChargebeeCustomerListParams extends _customer.customer_list_params {
      cf_parakeeto_account_id: { is: string };
    }
    const requestWrapper = this.chargebee.customer.list({
      cf_parakeeto_account_id: { is: `${userAccount.account.id}` },
    } as ChargebeeCustomerListParams);
anderslyman commented 4 years ago

@benzittlau thank you! That compiles for me as well, but I still get a Buffer is not defined error as described here: https://github.com/chargebee/chargebee-typescript/issues/3

Hopefully support will be able to help with that one, no reply yet though...

anderslyman commented 3 years ago

This error was due to running the library in the front-end, which is not supported. I moved it to an AWS lambda function and everything works as intended.