Search code examples
node.jsbraintree

How to get a customer's ID from a webhook with Braintree?


How can I get a customer's ID from a webhook notification from Braintree?

For example, I can do the following to get their ID:

gateway.webhookNotification.parse(sampleNotification.bt_signature, sampleNotification.bt_payload, (err, webhookNotification) => {
     let customerId = webhookNotification.subject.subscription.transactions[0].customer.id;
});

But this requires the user to have at least one transaction first. How can I get a customer's ID if they do not have any transactions yet (eg. a new user)?

Here is the response when logging webhookNotification:

WebhookNotification {
   timestamp: '2017-04-30T11:01:33Z',
   kind: 'subscription_charged_unsuccessfully',
   subject: { 
      subscription: { 
         id: 'jywgnr', 
         transactions: [], 
         addOns: [], 
         discounts: [] 
       }
   },
   subscription: Subscription { 
      id: 'jywgnr', 
      transactions: [], 
      addOns: [], 
      discounts: [] 
   } 
}

Solution

  • Looks like you are using the subscription webhook. That appears to return a payment method token in the payload, which you can use in paymentMethod.find() call.

    From that paymentMethod.find('token') result object, you can retrieve the customer_id.

    EDIT: Looks like because the subscription was unsuccessful, there is no payment method token in that webhook. There is, though, a subscription ID, which you can use in a Subscription.find() API call, which will certainly return a result object that contains a payment method token.