-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: payment flow restructure (#37)
* update payment flow, restructure into different files and prepare for unit test, remove unused code files * remove console.log - move rotateToken * chore: code review updates
- Loading branch information
Showing
13 changed files
with
379 additions
and
537 deletions.
There are no files selected for viewing
This file was deleted.
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,33 @@ | ||
import { tabs } from 'webextension-polyfill' | ||
|
||
const getCurrentActiveTabId = async () => { | ||
const activeTabs = await tabs.query({ active: true, currentWindow: true }) | ||
return activeTabs[0].id | ||
} | ||
|
||
export const confirmPayment = async (url: string) => { | ||
const currentTabId = await getCurrentActiveTabId() | ||
|
||
return await new Promise<string>(resolve => { | ||
if (url) { | ||
tabs.create({ url }).then(tab => { | ||
if (tab.id) { | ||
tabs.onUpdated.addListener((tabId, changeInfo) => { | ||
try { | ||
const tabUrl = new URL(changeInfo.url || '') | ||
const interactRef = tabUrl.searchParams.get('interact_ref') | ||
|
||
if (tabId === tab.id && interactRef) { | ||
tabs.update(currentTabId, { active: true }) | ||
tabs.remove(tab.id) | ||
resolve(interactRef) | ||
} | ||
} catch (e) { | ||
throw new Error('Invalid interact ref url.') | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
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,44 @@ | ||
import { AxiosInstance } from 'axios' | ||
|
||
import { getHeaders } from './getHeaders' | ||
|
||
type TCreateQuote = (_params: { | ||
receiver: string | ||
walletAddress: any | ||
sendingUrl: string | ||
token: string | ||
amount: string | ||
instance: AxiosInstance | ||
}) => Promise<any> | ||
|
||
export const createQuote: TCreateQuote = async ({ | ||
receiver, | ||
walletAddress, | ||
sendingUrl, | ||
token, | ||
amount, | ||
instance, | ||
}) => { | ||
const payload = { | ||
method: 'ilp', | ||
receiver, | ||
walletAddress: walletAddress.id, | ||
debitAmount: { | ||
value: amount, // 0.001 USD | ||
assetCode: walletAddress.assetCode, // 'USD' | ||
assetScale: walletAddress.assetScale, // 9 | ||
}, | ||
} | ||
|
||
const quote = await instance.post( | ||
new URL(sendingUrl).origin + '/quotes', | ||
payload, | ||
getHeaders(token), | ||
) | ||
|
||
if (!quote.data.id) { | ||
throw new Error('No quote url id') | ||
} | ||
|
||
return quote.data.id | ||
} |
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,34 @@ | ||
import { AxiosInstance } from 'axios' | ||
|
||
import { getHeaders } from './getHeaders' | ||
|
||
type TGetContinuationRequest = (_params: { | ||
url: string | ||
interactRef: any | ||
token: string | ||
instance: AxiosInstance | ||
}) => Promise<any> | ||
|
||
export const getContinuationRequest: TGetContinuationRequest = async ({ | ||
url, | ||
interactRef, | ||
token, | ||
instance, | ||
}) => { | ||
const continuationRequest = await instance.post( | ||
url, | ||
{ | ||
interact_ref: interactRef, | ||
}, | ||
getHeaders(token), | ||
) | ||
|
||
if (!continuationRequest.data.access_token.value) { | ||
throw new Error('No continuation request') | ||
} | ||
|
||
return { | ||
manageUrl: continuationRequest.data.access_token.manage, | ||
continuationRequestToken: continuationRequest.data.access_token.value, | ||
} | ||
} |
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,5 @@ | ||
export const getHeaders = (gnapToken: string) => ({ | ||
headers: { | ||
Authorization: `GNAP ${gnapToken}`, | ||
}, | ||
}) |
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,36 @@ | ||
import { AxiosInstance } from 'axios' | ||
|
||
type TGetIncomingPaymentGrant = (_params: { | ||
client: string | ||
identifier: string | ||
wallet: Record<string, any> | ||
instance: AxiosInstance | ||
}) => Promise<any> | ||
|
||
export const getIncomingPaymentGrant: TGetIncomingPaymentGrant = async ({ | ||
client, | ||
identifier, | ||
wallet, | ||
instance, | ||
}): Promise<string> => { | ||
const payload = { | ||
access_token: { | ||
access: [ | ||
{ | ||
type: 'incoming-payment', | ||
actions: ['create', 'read', 'list'], | ||
identifier, // receivingPaymentPointerUrl | ||
}, | ||
], | ||
}, | ||
client, // WM_PAYMENT_POINTER_URL | ||
} | ||
|
||
const response = await instance.post(wallet.authServer + '/', payload) | ||
|
||
if (!response.data.access_token.value) { | ||
throw new Error('No client auth') | ||
} | ||
|
||
return response.data.access_token.value | ||
} |
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,30 @@ | ||
import { AxiosInstance } from 'axios' | ||
|
||
import { getHeaders } from './getHeaders' | ||
|
||
type TGetIncomingPaymentUrlId = (_params: { | ||
walletAddress: string | ||
token: string | ||
instance: AxiosInstance | ||
}) => Promise<any> | ||
|
||
export const getIncomingPaymentUrlId: TGetIncomingPaymentUrlId = async ({ | ||
walletAddress, | ||
token, | ||
instance, | ||
}) => { | ||
const incomingPayment = await instance.post( | ||
new URL(walletAddress).origin + '/incoming-payments', | ||
{ | ||
walletAddress, // receivingPaymentPointerUrl | ||
expiresAt: new Date(Date.now() + 6000 * 60 * 10).toISOString(), | ||
}, | ||
getHeaders(token), | ||
) | ||
|
||
if (!incomingPayment?.data?.id) { | ||
throw new Error('No incoming payment id') | ||
} | ||
|
||
return incomingPayment.data.id | ||
} |
Oops, something went wrong.