-
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
ff93a0e
commit 619eb76
Showing
7 changed files
with
130 additions
and
2 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 |
---|---|---|
|
@@ -8,6 +8,9 @@ project(':capacitor-mlkit-barcode-scanning').projectDir = new File('../node_modu | |
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') | ||
|
||
|
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,99 @@ | ||
import { CapacitorNotifications } from "@capacitor/background-runner"; | ||
|
||
addEventListener("checkPaymentsInFlight", async (resolve, reject, _args) => { | ||
Check failure on line 3 in src/runners/background.ts GitHub Actions / code_quality
Check failure on line 3 in src/runners/background.ts GitHub Actions / code_quality
Check failure on line 3 in src/runners/background.ts GitHub Actions / code_quality
|
||
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); | ||
// fixme change back to InFlight | ||
if (payment && payment.status === "Succeeded") { | ||
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; |