Skip to content

Commit

Permalink
fix(payme): pay request payload (#1479)
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnh12 authored and tuanddd committed Sep 8, 2023
1 parent 88b8407 commit 2a7bfef
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 15 deletions.
42 changes: 42 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

call_lefthook()
{
dir="$(git rev-parse --show-toplevel)"
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')

if lefthook -h >/dev/null 2>&1
then
lefthook "$@"
elif test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif command -v npx >/dev/null 2>&1
then
npx @evilmartians/lefthook "$@"
else
echo "Can't find lefthook in PATH"
fi
}

call_lefthook run "pre-commit" "$@"
42 changes: 42 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

call_lefthook()
{
dir="$(git rev-parse --show-toplevel)"
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')

if lefthook -h >/dev/null 2>&1
then
lefthook "$@"
elif test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif command -v npx >/dev/null 2>&1
then
npx @evilmartians/lefthook "$@"
else
echo "Can't find lefthook in PATH"
fi
}

call_lefthook run "prepare-commit-msg" "$@"
11 changes: 2 additions & 9 deletions src/adapters/mochi-pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,7 @@ class MochiPay extends Fetcher {
return data
}

public async generatePaymentCode(body: {
profileId: string
amount: string
token: string
type: "paylink" | "payme"
note?: string
recipient_id?: string
}) {
public async generatePaymentCode(body: any) {
return await this.jsonFetch(`${MOCHI_PAY_API_BASE_URL}/pay-requests`, {
method: "POST",
body: {
Expand Down Expand Up @@ -165,7 +158,7 @@ class MochiPay extends Fetcher {

async getListTx(
profileId: string,
query: { action?: string; page?: number; size?: number },
query: { status?: string; action?: string; page?: number; size?: number },
) {
return await this.jsonFetch(
`${MOCHI_PAY_API_BASE_URL}/profile/${profileId}/transactions`,
Expand Down
6 changes: 4 additions & 2 deletions src/commands/balances/index/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,14 @@ const txnsFetcher: Record<
platform: string,
) => Promise<any>
> = {
[BalanceType.Offchain]: (profileId) => mochiPay.getListTx(profileId, {}),
[BalanceType.Offchain]: (profileId) =>
mochiPay.getListTx(profileId, { status: "success" }),
[BalanceType.Onchain]: (profileId, _, address, type) =>
defi.getWalletTxns(profileId, address, type),
[BalanceType.Cex]: (profile_id, platform) =>
defi.getDexTxns(profile_id, platform),
[BalanceType.All]: (profileId) => mochiPay.getListTx(profileId, {}),
[BalanceType.All]: (profileId) =>
mochiPay.getListTx(profileId, { status: "success" }),
}

async function getTxns(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/pay/link/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export async function run({
amount.toLocaleString().replaceAll(",", ""),
decimal,
).toString(),
token,
tokenId: balance?.token.id,
note,
type: "paylink",
})
Expand Down
53 changes: 53 additions & 0 deletions src/commands/pay/me/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export async function run({

const t = await getToken(token)

const parsedTarget = await parseTargetAndPlatform({ platform, target })

const res: any = await mochiPay.generatePaymentCode({
profileId,
amount: parseUnits(
Expand All @@ -66,6 +68,8 @@ export async function run({
token,
type: "payme",
note,
target: parsedTarget.target,
target_platform: parsedTarget.platform,
})
// api error
if (!res.ok) {
Expand Down Expand Up @@ -186,6 +190,55 @@ export async function run({
})
}

async function parseTargetAndPlatform({
platform,
target,
}: {
platform?: string
target?: string
}) {
const res = { target: "", platform }
if (!platform || !target) return res
// discord
if (platform === "discord") {
const targetId = target.replaceAll(/<|>/g, "")
const pfRes = await profile.getByDiscord(targetId)
if (pfRes.err) {
throw new APIError({
description: `[getByDiscord] API error with status ${pfRes.status_code}`,
curl: "",
})
}
res.target = pfRes.id
}

// telegram
if (platform === "telegram") {
const pfRes = await profile.getByTelegramUsername(target)
if (pfRes.err) {
throw new APIError({
description: `[getByTelegram] API error with status ${pfRes.status_code}`,
curl: "",
})
}
res.target = pfRes.id
}

//email
if (platform === "mail") {
const pfRes = await profile.getByEmail(target)
if (pfRes.err) {
throw new APIError({
description: `[getByEmail] API error with status ${pfRes.status_code}`,
curl: "",
})
}
res.target = pfRes.id
}

return res
}

// DO NOT DELETE: use this after mochi-notification support payme usecase
async function sendNotification({
platform,
Expand Down
15 changes: 12 additions & 3 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ export interface RequestOffchainTransferRequest {
platform?: string
recipients?: string[]
sender?: string

/** AmountString string `json:"amount_string"` */
token?: string
transfer_type?: string
Expand Down Expand Up @@ -1033,9 +1034,7 @@ export interface ResponseCoinMarketItemData {
price_change_percentage_24h?: number
price_change_percentage_24h_in_currency?: number
price_change_percentage_7d_in_currency?: number
sparkline_in_7d?: {
price?: number[]
}
sparkline_in_7d?: { price?: number[] }
symbol?: string
}

Expand Down Expand Up @@ -1115,17 +1114,21 @@ export interface ResponseCreateUserTokenSupportRequest {

export interface ResponseDataFilterConfigByReaction {
data?: ResponseRoleReactionResponse

/** page index */
page?: number

/** page size */
size?: number
total?: number
}

export interface ResponseDataListRoleReactionResponse {
data?: ResponseListRoleReactionResponse

/** page index */
page?: number

/** page size */
size?: number
total?: number
Expand Down Expand Up @@ -1184,6 +1187,7 @@ export interface ResponseDiscordGuildResponse {
id?: string
name?: string
owner?: boolean

/** @example 0 */
permissions?: string
}
Expand Down Expand Up @@ -1375,8 +1379,10 @@ export interface ResponseGetInvestListResponse {

export interface ResponseGetLevelRoleConfigsResponse {
data?: ModelGuildConfigLevelRole[]

/** page index */
page?: number

/** page size */
size?: number
total?: number
Expand Down Expand Up @@ -1490,6 +1496,7 @@ export interface ResponseGetTrackingWalletsResponse {

export interface ResponseGetTrendingSearch {
coins?: ResponseGetTrendingSearchCoin[]

/** this field coingecko return empty */
exchanges?: object
}
Expand Down Expand Up @@ -2008,6 +2015,7 @@ export interface ResponseOnchainInvestDataResponse {
export interface ResponsePaginationResponse {
/** page index */
page?: number

/** page size */
size?: number
total?: number
Expand Down Expand Up @@ -2251,6 +2259,7 @@ export interface UtilPagination {

export interface UuidNullUUID {
uuid?: string

/** Valid is true if UUID is not NULL */
valid?: boolean
}

0 comments on commit 2a7bfef

Please sign in to comment.