braintree / braintree-web-drop-in

Braintree Drop-in for the web
MIT License
200 stars 126 forks source link

PaymentMethod is not available upon initialization with a selected vaulted method #646

Closed dminkovsky closed 4 years ago

dminkovsky commented 4 years ago

General information

Issue description

Upon initializing the drop in with authorization set to a client token generated using a customer ID for a customer with vaulted payment methods, the drop in displays the existing payment methods. In this situation, paymentMethodRequestable is not fired, and isPaymentMethodRequestable() needs to be called to detect whether requestPaymentMethod() can be called.

However, unlike the paymentMethodRequestable event, which provides handlers with the PaymentMethod ("CreditCard" | "PayPal"), isPaymentMethodRequestable() just returns a boolean with no payment method type.

Is there a way to find out what the payment type is when using isPaymentMethodRequestable() on initialization, so that the interface between these two ways is consistent?

My use case is that I am displaying a submit button that says "Use this card" or "Use this PayPal account" depending on payment method, and cannot make this distinction when a vaulted payment method exists upon initialization.

crookedneighbor commented 4 years ago

👋

You can simply call requestPaymentMethod after you've determined that is requestable.

braintree.dropin.create({
  authorization: 'CLIENT_TOKEN_ENCODED_WITH_CUSTOMER_ID',
  container: '#dropin-container'
}).then(function (dropinInstance) {
  if (dropinInstance.isPaymentMethodRequestable()) {
    // This will be true if you generated the client token
    // with a customer ID and there is a saved payment method
    // available to tokenize with that customer.
    dropinInstance.requestPaymentMethod().then(function (payload) {
      // inspect type of payment method: payload.type
    });
  }

  // finish setting up drop-in
});
dminkovsky commented 4 years ago

That's a good approach, thanks.