Skip to content

Commit

Permalink
chore: Merge pull request #21 from eventOneHQ/master
Browse files Browse the repository at this point in the history
Beta 5
  • Loading branch information
nprail authored Jun 26, 2020
2 parents 624c86f + cdcc5ae commit a421d4c
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 27 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- [Current Project Status](https://github.com/eventOneHQ/capacitor-stripe-terminal/issues/2)
- [Getting Started](#getting_started)
- [iOS Setup](#ios-setup)
- [Usage](#usage)
- [Usage](#usage)
- [Contributing](CONTRIBUTING.md)
- [Authors](#authors)
- [Acknowledgments](#acknowledgement)
Expand Down Expand Up @@ -91,19 +91,17 @@ terminal
// Once the reader is connected, collect a payment intent!
;(async () => {
// subscribe to user instructions - these should be displayed to the user
const waitingSubscription = terminal
const displaySubscription = terminal
.readerDisplayMessage()
.subscribe(message => {
console.log('readerDisplayMessage', message.text)
.subscribe(displayMessage => {
console.log('displayMessage', displayMessage)
})
const inputSubscription = terminal.readerInput().subscribe(message => {
console.log('readerInput', message.text)
const inputSubscription = terminal.readerInput().subscribe(inputOptions => {
console.log('inputOptions', inputOptions)
})

// retrieve the payment intent
const pi = await terminal.retrievePaymentIntent(
'your client secret created server side'
)
await terminal.retrievePaymentIntent('your client secret created server side')

// collect the payment method
await terminal.collectPaymentMethod()
Expand All @@ -112,7 +110,7 @@ terminal
await terminal.processPayment()

// once you are done, make sure to unsubscribe (e.g. in ngOnDestroy)
waitingSubscription.unsubscribe()
displaySubscription.unsubscribe()
inputSubscription.unsubscribe()
})()
```
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CAP_PLUGIN_METHOD(retrievePaymentIntent, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(collectPaymentMethod, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(processPayment, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(clearCachedCredentials, CAPPluginReturnPromise);

)

9 changes: 7 additions & 2 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega
}
}

@objc func clearCachedCredentials(_ call: CAPPluginCall) {
Terminal.shared.clearCachedCredentials()
call.resolve()
}

// helper method to collect a payment intent
// @objc func collectPaymentIntent(_ call: CAPPluginCall) {
// guard let clientSecret = call.getString("clientSecret") else {
Expand Down Expand Up @@ -343,11 +348,11 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega
// MARK: ReaderDisplayDelegate

public func terminal(_: Terminal, didRequestReaderInput inputOptions: ReaderInputOptions = []) {
notifyListeners("didRequestReaderInput", data: ["text": Terminal.stringFromReaderInputOptions(inputOptions)])
notifyListeners("didRequestReaderInput", data: ["value": inputOptions.rawValue])
}

public func terminal(_: Terminal, didRequestReaderDisplayMessage displayMessage: ReaderDisplayMessage) {
notifyListeners("didRequestReaderDisplayMessage", data: ["text": Terminal.stringFromReaderDisplayMessage(displayMessage)])
notifyListeners("didRequestReaderDisplayMessage", data: ["value": displayMessage.rawValue])
}

// MARK: Serializers
Expand Down
112 changes: 103 additions & 9 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Plugin } from '@capacitor/core/dist/esm/definitions'

declare module '@capacitor/core' {
interface PluginRegistry {
StripeTerminal: StripeTerminalInterface
Expand Down Expand Up @@ -208,16 +210,78 @@ export interface ReaderSoftwareUpdate {

/**
* The display messages that a reader may request be displayed by your app.
*
* @category Reader
* @see https://stripe.dev/stripe-terminal-ios/docs/Enums/SCPReaderDisplayMessage
*/
export interface ReaderDisplayMessage {
text: string
export enum ReaderDisplayMessage {
/**
* Retry the presented card.
*/
RetryCard,

/**
* Insert the presented card.
*/
InsertCard,

/**
* Insert or swipe the presented card.
*/
InsertOrSwipeCard,

/**
* Swipe the presented card.
*/
SwipeCard,

/**
* Remove the presented card.
*/
RemoveCard,

/**
* The reader detected multiple contactless cards. Make sure only one contactless card or NFC device is near the reader.
*/
MultipleContactlessCardsDetected,

/**
* The card could not be read. Try another read method on the same card, or use a different card.
*/
TryAnotherReadMethod,

/**
* The card is invalid. Try another card.
*/
TryAnotherCard
}

/**
* This represents all of the input methods available to your user when the reader begins waiting for input.
*
* @category Reader
* @see https://stripe.dev/stripe-terminal-ios/docs/Enums/SCPReaderInputOptions
*/
export interface ReaderInputOptions {
text: string
export enum ReaderInputOptions {
/**
* No input options are available on the reader.
*/
None = 0,

/**
* Swipe a magstripe card.
*/
SwipeCard = 1 << 0,

/**
* Insert a chip card.
*/
InsertCard = 1 << 1,

/**
* Tap a contactless card.
*/
TapCard = 1 << 2
}

export interface PaymentIntent {
Expand All @@ -228,13 +292,43 @@ export interface PaymentIntent {
currency: string
}

export interface StripeTerminalInterface {
setConnectionToken(options: {
token?: string
export interface StripeTerminalInterface extends Plugin {
setConnectionToken(
options: {
token?: string
},
errorMessage?: string
}): Promise<void>
): Promise<void>

initialize(): Promise<void>

getConnectionStatus(): Promise<ConnectionStatus>
discoverReaders(options: DiscoveryConfiguration): Promise<void>

abortDiscoverReaders(): Promise<void>

connectReader(reader: Reader): Promise<{ reader: Reader }>

getConnectedReader(): Promise<{ reader: Reader }>

getConnectionStatus(): Promise<{ status: ConnectionStatus }>

disconnectReader(): Promise<void>

checkForUpdate(): Promise<{ update: ReaderSoftwareUpdate }>

installUpdate(): Promise<void>

abortInstallUpdate(): Promise<void>

retrievePaymentIntent(options: {
clientSecret: string
}): Promise<{ intent: PaymentIntent }>

collectPaymentMethod(): Promise<{ intent: PaymentIntent }>

abortCollectPaymentMethod(): Promise<void>

processPayment(): Promise<{ intent: PaymentIntent }>

clearCachedCredentials(): Promise<void>
}
30 changes: 24 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ export class StripeTerminalPlugin {
})
}

private _listenerToObservable(name: string): Observable<any> {
private _listenerToObservable(
name: string,
transformFunc?: (data: any) => any
): Observable<any> {
return new Observable(subscriber => {
const listener = StripeTerminal.addListener(name, (data: any) => {
subscriber.next(data)
if (transformFunc) {
return subscriber.next(transformFunc(data))
}

return subscriber.next(data)
})

return {
Expand Down Expand Up @@ -197,11 +204,18 @@ export class StripeTerminalPlugin {
}

public readerInput(): Observable<ReaderInputOptions> {
return this._listenerToObservable('didRequestReaderInput')
return this._listenerToObservable('didRequestReaderInput', (data: any) => {
return parseFloat(data.value)
})
}

public readerDisplayMessage(): Observable<ReaderDisplayMessage> {
return this._listenerToObservable('didRequestReaderDisplayMessage')
return this._listenerToObservable(
'didRequestReaderDisplayMessage',
(data: any) => {
return parseFloat(data.value)
}
)
}

public async retrievePaymentIntent(
Expand Down Expand Up @@ -240,7 +254,11 @@ export class StripeTerminalPlugin {
}
}

public addListener(...opts: any[]) {
return StripeTerminal.addListener(...opts)
public async clearCachedCredentials(): Promise<void> {
return StripeTerminal.clearCachedCredentials()
}

public addListener(eventName: string, listenerFunc: Function) {
return StripeTerminal.addListener(eventName, listenerFunc)
}
}

0 comments on commit a421d4c

Please sign in to comment.