Skip to content

Commit

Permalink
feat(ios): add bluetooth connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlabo committed Oct 7, 2023
1 parent 2a28694 commit b4d99a8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
4 changes: 2 additions & 2 deletions demo/angular/src/app/terminal/terminal.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<ion-item (click)="create('happyPath', terminalConnectTypes.TapToPay)" button="true" detail="true"><ion-label>Expect Happy Path - Tap To Pay</ion-label></ion-item>
<ion-item (click)="create('happyPath', terminalConnectTypes.Internet)" button="true" detail="true"><ion-label>Expect Happy Path - Internet</ion-label></ion-item>
<ion-item (click)="create('happyPath', terminalConnectTypes.Bluetooth)" button="true" detail="true"><ion-label>Expect Happy Path - Bluetooth</ion-label></ion-item>
<ion-item (click)="create('happyPath', terminalConnectTypes.Usb)" button="true" detail="true"><ion-label>Expect Happy Path - Usb</ion-label></ion-item>
<ion-item (click)="create('happyPath', terminalConnectTypes.Usb)" button="true" detail="true" *ngIf="platform.is('android')"><ion-label>Expect Happy Path - Usb</ion-label></ion-item>
<ion-item-divider><ion-label>Cancel Path</ion-label></ion-item-divider>
<ion-item (click)="create('cancelPath', terminalConnectTypes.Internet)" button="true" detail="true"><ion-label>Expect Cancel Path - Internet</ion-label></ion-item>
<ion-item (click)="create('cancelPath', terminalConnectTypes.Bluetooth)" button="true" detail="true"><ion-label>Expect Cancel Path - Bluetooth</ion-label></ion-item>
<ion-item (click)="create('cancelPath', terminalConnectTypes.Usb)" button="true" detail="true"><ion-label>Expect Cancel Path - Usb</ion-label></ion-item>
<ion-item (click)="create('cancelPath', terminalConnectTypes.Usb)" button="true" detail="true" *ngIf="platform.is('android')"><ion-label>Expect Cancel Path - Usb</ion-label></ion-item>
<ion-item (click)="checkDiscoverMethod()" button="true" detail="true"><ion-label>checkDiscoverMethod</ion-label></ion-item>
</ion-list>

Expand Down
3 changes: 2 additions & 1 deletion demo/angular/src/app/terminal/terminal.page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {IonicModule} from '@ionic/angular';
import {IonicModule, Platform} from '@ionic/angular';
import {HttpClient} from '@angular/common/http';
import {HelperService} from '../shared/helper.service';
import {StripeTerminal, TerminalConnectTypes, TerminalEventsEnum} from '@capacitor-community/stripe-terminal';
Expand Down Expand Up @@ -144,6 +144,7 @@ export class TerminalPage {
public terminalConnectTypes = TerminalConnectTypes;
private readonly listenerHandlers: PluginListenerHandle[] = [];
constructor(
public platform: Platform,
private http: HttpClient,
private helper: HelperService,
) { }
Expand Down
53 changes: 51 additions & 2 deletions packages/terminal/ios/Plugin/StripeTerminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Capacitor
import StripeTerminal

public class StripeTerminal: NSObject, DiscoveryDelegate, LocalMobileReaderDelegate {
public class StripeTerminal: NSObject, DiscoveryDelegate, LocalMobileReaderDelegate, BluetoothReaderDelegate {

weak var plugin: StripeTerminalPlugin?
var discoverCancelable: Cancelable?
Expand All @@ -25,22 +25,26 @@ public class StripeTerminal: NSObject, DiscoveryDelegate, LocalMobileReaderDeleg
call.resolve()
}

func discoverReaders(_ call: CAPPluginCall) {
func discoverReaders(_ call: CAPPluginCall) throws {
let connectType = call.getString("type")

if TerminalConnectTypes.TapToPay.rawValue == connectType {
self.type = .localMobile
} else if TerminalConnectTypes.Internet.rawValue == connectType {
self.type = .internet
} else if TerminalConnectTypes.Bluetooth.rawValue == connectType {
self.type = DiscoveryMethod.bluetoothScan
} else {
call.unimplemented(connectType! + " is not support now")
return
}


let config = DiscoveryConfiguration(
discoveryMethod: self.type!,
simulated: self.isTest!
)

self.discoverCall = call
self.locationId = call.getString("locationId")

Expand Down Expand Up @@ -154,6 +158,21 @@ public class StripeTerminal: NSObject, DiscoveryDelegate, LocalMobileReaderDeleg
}
}
}

private func connectBluetoothReader(_ call: CAPPluginCall) {
let config = BluetoothConnectionConfiguration(locationId: self.locationId!)
let reader: JSObject = call.getObject("reader")!
let index: Int = reader["index"] as! Int

Terminal.shared.connectBluetoothReader(self.readers![index], delegate: self, connectionConfig: config) { reader, error in
if let reader = reader {
self.plugin?.notifyListeners(TerminalEvents.ConnectedReader.rawValue, data: [:])
call.resolve()
} else if let error = error {
call.reject(error.localizedDescription)
}
}
}

public func collect(_ call: CAPPluginCall) {
Terminal.shared.retrievePaymentIntent(clientSecret: call.getString("paymentIntent")!) { retrieveResult, retrieveError in
Expand Down Expand Up @@ -188,7 +207,10 @@ public class StripeTerminal: NSObject, DiscoveryDelegate, LocalMobileReaderDeleg
}
call.resolve()
}


// localMobile

public func localMobileReader(_ reader: Reader, didStartInstallingUpdate update: ReaderSoftwareUpdate, cancelable: Cancelable?) {
// TODO
}
Expand All @@ -208,6 +230,33 @@ public class StripeTerminal: NSObject, DiscoveryDelegate, LocalMobileReaderDeleg
public func localMobileReader(_ reader: Reader, didRequestReaderDisplayMessage displayMessage: ReaderDisplayMessage) {
// TODO
}


// bluetooth

public func reader(_: Reader, didReportAvailableUpdate update: ReaderSoftwareUpdate) {
// TODO
}

public func reader(_: Reader, didStartInstallingUpdate update: ReaderSoftwareUpdate, cancelable: Cancelable?) {
// TODO
}

public func reader(_: Reader, didReportReaderSoftwareUpdateProgress progress: Float) {
// TODO
}

public func reader(_: Reader, didFinishInstallingUpdate update: ReaderSoftwareUpdate?, error: Error?) {
// TODO
}

public func reader(_: Reader, didRequestReaderInput inputOptions: ReaderInputOptions = []) {
// TODO
}

public func reader(_: Reader, didRequestReaderDisplayMessage displayMessage: ReaderDisplayMessage) {
// TODO
}
}

class APIClient: ConnectionTokenProvider {
Expand Down

0 comments on commit b4d99a8

Please sign in to comment.