braintree / braintree_node

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

Different error for invalid payloads #129

Closed kylealwyn closed 6 years ago

kylealwyn commented 6 years ago

Not a bug, just a small suggestion after integrating.

I had this code after some copy-pasta while moving over from Stripe:

const results = await braintree.customer.create({
      paymentMethodNonce: nonce,
     // `metadata` instead of `customFields`
      metadata: {
        user_id: user.id,
      },
});

It's misleading to throw an AuthorizationError when sending invalid fields in a gateway call. Something along the lines of MalformedPayload or BadRequest seems like it would be more helpful. It would then mimic more closely http statuses 401 Unauthorized and 400 Bad Request or 422 UnprocessableEntity. The misnamed (or extra in my case) fields seem to have nothing to do with Authorization, which led me down the rabbit hole of thinking my keys were wrong.

I understand now it's in the documentation but still believe to be a misnomer. Thanks!

crookedneighbor commented 6 years ago

Looks like the problem is that we're not actually verifying the keys being passed into create before making the request: https://github.com/braintree/braintree_node/blob/90ebc69922a15a9d0c7d7fb94695a0a9ebcf6714/lib/braintree/customer_gateway.js#L17

We do this in the other sdks and should make the change here to use invalid key error. Thanks for reporting it.

crookedneighbor commented 6 years ago

This is fixed (finally) in version 2.11.0 https://github.com/braintree/braintree_node/blob/master/CHANGELOG.md#2110