-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Background android app pending payments
- Loading branch information
1 parent
6f628cf
commit 36e54bc
Showing
7 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capa | |
include ':capacitor-app' | ||
project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/app/android') | ||
|
||
include ':capacitor-background-runner' | ||
project(':capacitor-background-runner').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/background-runner/android') | ||
|
||
include ':capacitor-browser' | ||
project(':capacitor-browser').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/browser/android') | ||
|
||
|
@@ -14,12 +17,12 @@ project(':capacitor-clipboard').projectDir = new File('../node_modules/.pnpm/@ca | |
include ':capacitor-filesystem' | ||
project(':capacitor-filesystem').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/filesystem/android') | ||
|
||
include ':capacitor-share' | ||
project(':capacitor-share').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/share/android') | ||
|
||
include ':capacitor-haptics' | ||
project(':capacitor-haptics').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/haptics/android') | ||
|
||
include ':capacitor-share' | ||
project(':capacitor-share').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/share/android') | ||
|
||
include ':capacitor-toast' | ||
project(':capacitor-toast').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/toast/android') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { CapacitorNotifications } from "@capacitor/background-runner"; | ||
|
||
addEventListener("checkPaymentsInFlight", async (resolve, reject, _args) => { | ||
try { | ||
await checkPaymentsInFlight(); | ||
resolve(); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
|
||
interface OutboundPayment { | ||
status: string; | ||
} | ||
|
||
async function checkPaymentsInFlight() { | ||
console.log("checkPaymentsInFlight"); | ||
const db = await openDatabase(); | ||
|
||
const transaction = db.transaction("wallet_store", "readonly"); | ||
const store = transaction.objectStore("wallet_store"); | ||
|
||
// Get keys prefixed with "payment_outbound" | ||
const keys = await getAllKeysWithPrefix(store, "payment_outbound"); | ||
|
||
for (const key of keys) { | ||
const payment = await get(store, key); | ||
console.log(payment.status); | ||
if (payment && payment.status === "InFlight") { | ||
showNotification(); | ||
break; | ||
} | ||
} | ||
transaction.commit(); | ||
} | ||
|
||
function openDatabase(): Promise<IDBDatabase> { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open("wallet"); | ||
request.onsuccess = () => resolve(request.result); | ||
request.onerror = () => reject(request.error); | ||
}); | ||
} | ||
|
||
function getAllKeysWithPrefix( | ||
store: IDBObjectStore, | ||
prefix: string | ||
): Promise<string[]> { | ||
return new Promise((resolve, reject) => { | ||
const keys: string[] = []; | ||
const cursorRequest = store.openKeyCursor(); | ||
|
||
cursorRequest.onsuccess = function (event) { | ||
const cursor = (event.target as IDBRequest).result as IDBCursor; | ||
if (cursor) { | ||
if (cursor.key.toString().startsWith(prefix)) { | ||
keys.push(cursor.key.toString()); | ||
} | ||
cursor.continue(); | ||
} else { | ||
resolve(keys); | ||
} | ||
}; | ||
|
||
cursorRequest.onerror = function () { | ||
reject(cursorRequest.error); | ||
}; | ||
}); | ||
} | ||
|
||
function get(store: IDBObjectStore, key: string): Promise<OutboundPayment> { | ||
return new Promise((resolve, reject) => { | ||
const request = store.get(key); | ||
request.onsuccess = () => resolve(request.result); | ||
request.onerror = () => reject(request.error); | ||
}); | ||
} | ||
|
||
function showNotification() { | ||
// generate random id | ||
const id = Math.random() * 100_000_000; | ||
|
||
// send notification in 5 seconds | ||
const scheduleDate = new Date(); | ||
scheduleDate.setSeconds(scheduleDate.getSeconds() + 5); | ||
|
||
// todo make pretty | ||
CapacitorNotifications.schedule([ | ||
{ | ||
id, | ||
title: "You have a payment in flight", | ||
body: "Open Mutiny to make sure it completes.", | ||
scheduleAt: scheduleDate | ||
} | ||
]); | ||
} | ||
|
||
export default checkPaymentsInFlight; |