Skip to content

Commit

Permalink
feat(terminal): add option to set string token
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlabo committed Mar 8, 2024
1 parent 09a0e2d commit a40926b
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 194 deletions.
21 changes: 19 additions & 2 deletions demo/angular/src/app/terminal/terminal.page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {IonicModule, Platform} from '@ionic/angular';
Expand Down Expand Up @@ -153,9 +153,18 @@ export class TerminalPage {
const eventKeys = Object.keys(TerminalEventsEnum);
eventKeys.forEach(key => {
const handler = StripeTerminal.addListener(TerminalEventsEnum[key], () => {
console.log(key);
this.helper.updateItem(this.eventItems, TerminalEventsEnum[key], true);
});
this.listenerHandlers.push(handler);
if (key === 'RequestedConnectionToken') {
this.listenerHandlers.push(StripeTerminal.addListener(TerminalEventsEnum.RequestedConnectionToken, async () => {
const { secret } = await firstValueFrom(this.http.post<{
secret: string;
}>(environment.api + 'connection/token', {}));
StripeTerminal.setConnectionToken({ token: secret });
}));
}
});

if (type === 'happyPath') {
Expand All @@ -164,7 +173,7 @@ export class TerminalPage {
this.eventItems = JSON.parse(JSON.stringify(cancelPathItems));
}

await StripeTerminal.initialize({ tokenProviderEndpoint: environment.api + 'connection/token', isTest: readerType === TerminalConnectTypes.TapToPay })
await StripeTerminal.initialize({ isTest: readerType === TerminalConnectTypes.TapToPay })
.then(() => this.helper.updateItem(this.eventItems,'initialize', true))
.catch(() => this.helper.updateItem(this.eventItems,'initialize', false));

Expand Down Expand Up @@ -224,6 +233,14 @@ export class TerminalPage {
this.helper.updateItem(this.eventItems, TerminalEventsEnum[key], true);
});
this.listenerHandlers.push(handler);
if (key === 'RequestedConnectionToken') {
this.listenerHandlers.push(StripeTerminal.addListener(TerminalEventsEnum.RequestedConnectionToken, async () => {
const { secret } = await firstValueFrom(this.http.post<{
secret: string;
}>(environment.api + 'connection/token', {}));
StripeTerminal.setConnectionToken({ token: secret });
}));
}
});

this.eventItems = JSON.parse(JSON.stringify(checkDiscoverMethodItems));
Expand Down
85 changes: 71 additions & 14 deletions packages/terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ And update minSdkVersion to 26 And compileSdkVersion to 34 in your `android/app/

## Usage

### use native http client for getting a token

```typescript
(async ()=> {
/**
Expand All @@ -73,19 +75,44 @@ And update minSdkVersion to 26 And compileSdkVersion to 34 in your `android/app/
});
```

### set string token

```typescript
(async ()=> {
// run before StripeTerminal.initialize
StripeTerminal.addListener(TerminalEventsEnum.RequestedConnectionToken, async () => {
const { token } = (await fetch("https://example.com/token")).json();
StripeTerminal.setConnectionToken({ token });
});
});
(async ()=> {
await StripeTerminal.initialize({ isTest: true })
const { readers } = await StripeTerminal.discoverReaders({
type: TerminalConnectTypes.TapToPay,
locationId: "**************",
});
await StripeTerminal.connectReader({
reader: readers[0],
});
await StripeTerminal.collect({ paymentIntent: "**************" });
});
````

## API

<docgen-index>

* [`initialize(...)`](#initialize)
* [`discoverReaders(...)`](#discoverreaders)
* [`setConnectionToken(...)`](#setconnectiontoken)
* [`connectReader(...)`](#connectreader)
* [`getConnectedReader()`](#getconnectedreader)
* [`disconnectReader()`](#disconnectreader)
* [`cancelDiscoverReaders()`](#canceldiscoverreaders)
* [`collect(...)`](#collect)
* [`cancelCollect()`](#cancelcollect)
* [`addListener(TerminalEventsEnum.Loaded, ...)`](#addlistenerterminaleventsenumloaded)
* [`addListener(TerminalEventsEnum.RequestedConnectionToken, ...)`](#addlistenerterminaleventsenumrequestedconnectiontoken)
* [`addListener(TerminalEventsEnum.DiscoveredReaders, ...)`](#addlistenerterminaleventsenumdiscoveredreaders)
* [`addListener(TerminalEventsEnum.ConnectedReader, ...)`](#addlistenerterminaleventsenumconnectedreader)
* [`addListener(TerminalEventsEnum.Completed, ...)`](#addlistenerterminaleventsenumcompleted)
Expand All @@ -103,12 +130,12 @@ And update minSdkVersion to 26 And compileSdkVersion to 34 in your `android/app/
### initialize(...)

```typescript
initialize(options: { tokenProviderEndpoint: string; isTest: boolean; }) => Promise<void>
initialize(options: { tokenProviderEndpoint?: string; isTest: boolean; }) => Promise<void>
```

| Param | Type |
| ------------- | ---------------------------------------------------------------- |
| **`options`** | <code>{ tokenProviderEndpoint: string; isTest: boolean; }</code> |
| Param | Type |
| ------------- | ----------------------------------------------------------------- |
| **`options`** | <code>{ tokenProviderEndpoint?: string; isTest: boolean; }</code> |

--------------------

Expand All @@ -128,6 +155,19 @@ discoverReaders(options: { type: TerminalConnectTypes; locationId?: string; }) =
--------------------


### setConnectionToken(...)

```typescript
setConnectionToken(options: { token: string; }) => Promise<void>
```

| Param | Type |
| ------------- | ------------------------------- |
| **`options`** | <code>{ token: string; }</code> |

--------------------


### connectReader(...)

```typescript
Expand Down Expand Up @@ -208,6 +248,22 @@ addListener(eventName: TerminalEventsEnum.Loaded, listenerFunc: () => void) => P
--------------------


### addListener(TerminalEventsEnum.RequestedConnectionToken, ...)

```typescript
addListener(eventName: TerminalEventsEnum.RequestedConnectionToken, listenerFunc: () => void) => PluginListenerHandle
```

| Param | Type |
| ------------------ | ------------------------------------------------------------------------------------------ |
| **`eventName`** | <code><a href="#terminaleventsenum">TerminalEventsEnum.RequestedConnectionToken</a></code> |
| **`listenerFunc`** | <code>() =&gt; void</code> |

**Returns:** <code><a href="#pluginlistenerhandle">PluginListenerHandle</a></code>

--------------------


### addListener(TerminalEventsEnum.DiscoveredReaders, ...)

```typescript
Expand Down Expand Up @@ -322,15 +378,16 @@ addListener(eventName: TerminalEventsEnum.Failed, listenerFunc: () => void) => P

#### TerminalEventsEnum

| Members | Value |
| ----------------------------- | ---------------------------------------------- |
| **`Loaded`** | <code>'terminalLoaded'</code> |
| **`DiscoveredReaders`** | <code>'terminalDiscoveredReaders'</code> |
| **`CancelDiscoveredReaders`** | <code>'terminalCancelDiscoveredReaders'</code> |
| **`ConnectedReader`** | <code>'terminalConnectedReader'</code> |
| **`DisconnectedReader`** | <code>'terminalDisconnectedReader'</code> |
| **`Completed`** | <code>'terminalCompleted'</code> |
| **`Canceled`** | <code>'terminalCanceled'</code> |
| **`Failed`** | <code>'terminalFailed'</code> |
| Members | Value |
| ------------------------------ | ----------------------------------------------- |
| **`Loaded`** | <code>'terminalLoaded'</code> |
| **`DiscoveredReaders`** | <code>'terminalDiscoveredReaders'</code> |
| **`CancelDiscoveredReaders`** | <code>'terminalCancelDiscoveredReaders'</code> |
| **`ConnectedReader`** | <code>'terminalConnectedReader'</code> |
| **`DisconnectedReader`** | <code>'terminalDisconnectedReader'</code> |
| **`Completed`** | <code>'terminalCompleted'</code> |
| **`Canceled`** | <code>'terminalCanceled'</code> |
| **`Failed`** | <code>'terminalFailed'</code> |
| **`RequestedConnectionToken`** | <code>'terminalRequestedConnectionToken'</code> |

</docgen-api>
Loading

0 comments on commit a40926b

Please sign in to comment.