capacitor-community / stripe

Stripe Mobile SDK wrapper for Capacitor
MIT License
184 stars 75 forks source link

@capacitor-community/stripe-terminal TokenProvider - Allow Callback To Handle Http Token Fetch Request #335

Closed miccav-eventlink closed 3 months ago

miccav-eventlink commented 3 months ago

Is your feature request related to a problem? Please describe.

I'd like a way to control how the terminal connection token is fetched. Specifically how the http request and error handling is utilized.

Describe the solution you'd like

Allowing the call back to handle the request as seen below.

Describe alternatives you've considered

There wasn't any alternatives that could be considered since the request is handled in the TokenProvider class without any way for it to be modified.

Additional context

Here's a crude example:

public class CustomTokenProvider implements ConnectionTokenProvider {

    public void GetTokenFunction(ConnectiontokenCallback callback) = null;

    @override
    public void fetchConnectionToken(ConnectionTokenCallback callback) {
        try {
            GetTokenFunction(callback);
        } catch (Exception e) {
            callback.onError(e.ToString() or something);
        }
    }
}

TokenProvider tokenProvider = new CustomTokenProvider(function(callback) {
    doApiCall.then((res) => callback.onsuccess(res.data.Token););
});

if (!Terminal.isInitialized()) {
    Terminal.initTerminal(this.contextSupplier.get().getApplicationContext(), logLevel, tokenProvider, listener);
}

Terminal.getInstance();

Usage Product

If you adopt this plugin, please let us know which product you are using.

Product Name: Product URL : Using Function:

rdlabo commented 3 months ago

Thanks for the issue. Interesting suggestion, what problem do you hope to solve by this? Do you want to change the API request destination in the middle of a request? Or do you want to do error handling?

yllaw commented 3 months ago

I also would like this feature.

The ideal case would be that StripeTerminal.Initializehas an option fetchConnectionToken: () => Promise<string>where the returning string is the token. Then this token is used to initialize the StripeTerminal package.

This way we can delegate our own http-client layer so we can add headers, data, etc that our backend server endpoint may require.

rdlabo commented 3 months ago

I see. It is certainly preferable to make the request on the web and pass it as a string, considering the granting of Headers, etc. But If StripeTerminal.Initialize have that method, it is undesirable because a wrapper Class is required.

Currently considering:

StripeTerminal.addListener(TerminalEventsEnum.RequestedConnectionToken, async () => {
  const token = await getToken();  // Users Method
  StripeTerminal.setConnectionToken({token});
});
rdlabo commented 3 months ago

Released: https://github.com/capacitor-community/stripe/releases/tag/v5.4.5 Docs: https://github.com/capacitor-community/stripe/tree/v5.4.5/packages/terminal#set-string-token

Please try this.