braintree / braintree_node

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

Testing gateway.subscription.create fails with Authorization Error #154

Closed Lwdthe1 closed 5 years ago

Lwdthe1 commented 5 years ago

General information

Issue description

I am trying to test my custom integration of purchasing a subscription. Everything works until I try to run gateway.subscription.create({}), which fails with an authorization error. I use the braintree.Test.Nonces.Transactable nonce and fake-valid-no-billing-address-nonce; both succeed to create a payment method and give me a paymentMethodToken.

It'd be nice if the authorization error said more about why the error occurred, but all I get is this: screen shot 2019-01-25 at 10 21 46 am

My code:

        completeSubscriptionTransactionForPayment({ dbPayment, paymentMethodToken, planId }) {
        const subscriptionSale = {
            planId,
            paymentMethodToken: paymentMethodToken,
            // Custom fields must be fields configured in control panel as follows:
            // https://articles.braintreepayments.com/control-panel/custom-fields#creating-a-custom-field
            customFields: { payment_id: `${dbPayment._id}` },
        }

        console.log('Sale data:')
        console.log(subscriptionSale)
        return Q(gateway.subscription.create(subscriptionSale))
            .then((result) => {
                console.log(result)
                return result
            })
            .fail((err) => {
                console.error(err)
                console.error(err.stack)
                throw err
            })
    }

Thanks in advance for your help.

Lwdthe1 commented 5 years ago

Hmmm.... I just removed the customFields and it worked. But I have the custom field that I want to pass in configured in my sandbox:

screen shot 2019-01-25 at 10 32 26 am

Are custom fields not allowed for subscriptions?

crookedneighbor commented 5 years ago

Custom fields are not supported for subscriptions.

You can create a subscription object in your own database that correlates with the Braintree one and include any custom fields you need.

Also, no need to use Q. gateway.subscription.create will return a promise if no callback is provided.

Lwdthe1 commented 5 years ago

Thanks for clarifying.

As for the Q() wrapping, I need to do that because I use Q as my promise library, which uses .fail(), whereas the gateway uses .catch(). Mongoose allows for me to set my preferred promise library, but I have not found a way to do that with Briantree. Is that possible?

crookedneighbor commented 5 years ago

No, there's no global configuration for what promise library to use. According to the Q documentation, you can use catch if using a version of node that supports promises.