braintree / braintree_node

Braintree Node.js library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
334 stars 104 forks source link

`gateway.plan.all()` without plans created leads to `TypeError: Cannot set property 'success' of undefined` #206

Closed PierreZiegler closed 2 years ago

PierreZiegler commented 2 years ago

General information

Issue description

When trying to get all plans while not having plans create, TypeError: Cannot set property 'success' of undefined is thrown.

My Code (from https://developer.paypal.com/braintree/docs/reference/request/plan/all):

// ...

const response = await this.getGateway().plan.all()

// ...

Error:

TypeError: Cannot set property 'success' of undefined
    at /usr/src/app/node_modules/braintree/lib/braintree/plan_gateway.js:27:28
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at BraintreeService.getDistributionPlan (/usr/src/app/src/payment/braintree.service.ts:135:23)
    at PaymentController.getDistributionPlan (/usr/src/app/src/payment/payment.controller.ts:45:12)
    at /usr/src/app/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    at /usr/src/app/node_modules/@nestjs/core/router/router-proxy.js:9:17

node_modules/braintree/lib/braintree/plan_gateway.js:19-33:

      .then((response) => {
        if (!response.success) {
          return response;
        }

        // NEXT_MAJOR_VERSION all the other server sdks return the collection directly, rather
        // than a response object. For now, add the plans and success properties to the collection
        // for backwards compatibility. We can drop these at the next major version.
        const collection = response.plans;

        collection.success = response.success;
        collection.plans = response.plans;

        return collection;
      });

So it seems like response.plans is not set when there are no plans.

Expected Behavior

If there are no plans, I would expect an empty array [] or a proper error message.

hollabaq86 commented 2 years ago

👋 @PierreZiegler thanks for reaching out. Since we direct folks to create Plans via the Control Panel, I'm curious how large of a use case this is... Can you elaborate on your workflow that results in you're needing to call plans.all for an account that has no plans?

I'm going to forward this on to the team responsible for our Recurring Billing product because we may want to surface this scenario differently, such as a validation error or 404. But if you have the time, I'd love to know more on your use case.

hollabaq86 commented 2 years ago

Wanted to provide an update, the engineering team responsible for this part of the API has merged a fix so that we're receiving an empty array when no plans exist for this type request. This should be deployed soon, so I'm going to go ahead an close this.