Skip to content

Commit

Permalink
release v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Jan 18, 2024
1 parent 51b560e commit 38c45b9
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 88 deletions.
90 changes: 75 additions & 15 deletions deno_dist/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import type {
OTP,
ResponseBalance,
ResponseBody,
ResponseFail,
ResponseUserInfo,
baseHeader,
loginResult,
} from '../types.ts'
import { parseBalanceContext, parseCookieFromMap } from '../utils/parse.ts'
import { parseBalanceContext, parseCookieFromMap, parseUserInfoContext } from '../utils/parse.ts'

export class PayPay {
phone: string = ''
Expand Down Expand Up @@ -46,6 +48,7 @@ export class PayPay {
return {
success: true,
status: 'LoginAlreadySuccess',
context: {},
}
}

Expand All @@ -56,6 +59,7 @@ export class PayPay {
return {
success: true,
status: 'LoginSuccess',
context: {},
}
}

Expand All @@ -67,6 +71,7 @@ export class PayPay {
return {
success: false,
status: 'LoginFailed',
context: {},
}
}
} else {
Expand Down Expand Up @@ -98,12 +103,18 @@ export class PayPay {
return {
success: true,
status: 'LoginSuccess',
context: {
result,
},
}
} else {
if (result['response_type'] === 'ErrorResponse') {
return {
success: false,
status: 'LoginFailed',
context: {
result,
},
}
} else {
this.otp = {
Expand All @@ -114,6 +125,10 @@ export class PayPay {
return {
success: false,
status: 'LoginNeedOTP',
context: {
result,
otp: Object.create(this.otp),
},
}
}
}
Expand All @@ -124,6 +139,14 @@ export class PayPay {
}

async otpLogin(otp: string): Promise<loginResult> {
if (this.isLogged()) {
return {
success: true,
status: 'LoginAlreadySuccess',
context: {},
}
}

if (this.otp.waiting) {
const ctx = {
scope: 'SIGN_IN',
Expand Down Expand Up @@ -154,25 +177,25 @@ export class PayPay {
this.cookie.set('token', result.access_token)
return {
success: true,
status: 'LoginSuccess',
status: 'OTPLoginSuccess',
context: {
result,
},
}
} else {
return {
success: false,
status: 'LoginFailOTP',
status: 'OTPLoginFail',
context: {
result,
},
}
}
} else {
if (this.isLogged()) {
return {
success: true,
status: 'LoginAlreadySuccess',
}
} else {
return {
success: false,
status: 'LoginFailed',
}
return {
success: false,
status: 'LoginDontNeedOTP',
context: {},
}
}
}
Expand All @@ -185,7 +208,14 @@ export class PayPay {
}
}

async getBalance(): Promise<ResponseBalance | undefined> {
async getBalance(): Promise<ResponseBalance | ResponseFail> {
if (!this.isLogged()) {
return {
success: false,
status: 'DontLoggedYet',
}
}

const response = await fetch('https://www.paypay.ne.jp/app/v1/bff/getBalanceInfo', {
method: 'GET',
headers: {
Expand All @@ -195,11 +225,41 @@ export class PayPay {
})

if (!response.ok) {
return undefined
return {
success: false,
status: 'RequestFailed',
}
}

const result = await response.json()

return parseBalanceContext(result)
}

async getUserInfo(): Promise<ResponseUserInfo | ResponseFail> {
if (!this.isLogged()) {
return {
success: false,
status: 'DontLoggedYet',
}
}

const response = await fetch('https://www.paypay.ne.jp/app/v1/getUserProfile', {
method: 'GET',
headers: {
...this.header,
cookie: parseCookieFromMap(this.cookie),
},
})

if (!response.ok) {
return {
success: false,
status: 'RequestFailed',
}
}

const result = await response.json()
return parseUserInfoContext(result)
}
}
8 changes: 6 additions & 2 deletions deno_dist/status/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export const PayPayStatus = {
LoginSuccess: 'LoginSuccess',
LoginNeedOTP: 'LoginNeedOTP',
LoginFailOTP: 'LoginFailOTP',
LoginFailed: 'LoginFailed',
LoginNeedOTP: 'LoginNeedOTP',
LoginDontNeedOTP: 'LoginDontNeedOTP',
OTPLoginSuccess: 'OTPLoginSuccess',
OTPLoginFail: 'OTPLoginFail',
LoginAlreadySuccess: 'LoginAlreadySuccess',
RequestSuccess: 'RequestSuccess',
RequestFailed: 'RequestFailed',
}
63 changes: 46 additions & 17 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Anyone = any

export type baseHeader = {
Accept: string
'User-Agent': string
Expand All @@ -7,6 +10,7 @@ export type baseHeader = {
export type loginResult = {
success: boolean
status: string
context: Record<string, Anyone>
}

export type OTP = {
Expand All @@ -25,23 +29,48 @@ export type FetchContext = {
body?: string
}

export type ResponseBody = any & {}
export type ResponseBody = Anyone & {}

export type ResponseFail = {
success: false
status: string
}

export type ResponseBalance = {
success: true,
total: number,
currency: 'JPY' | string,
updated_at: string,
raw: {
header: {
resultCode: string,
resultMessage: string
},
payload: {
walletSummary: any,
walletDetail: any,
walletDescription: any,
[key: string]: string | boolean
}
success: true
total: number
currency: 'JPY' | string
updated_at: string
raw: {
header: {
resultCode: string
resultMessage: string
}
payload: {
[key: string]: Anyone
}
}
}
}

export type ResponseUserInfo = {
success: true
id: number
user_id: string
state: string
first_name: string
last_name: string
display_name: string
icon_url: string
phone_number: string
email: string
date_of_birth: string
raw: {
header: {
resultCode: string
resultMessage: string
}
payload: {
[key: string]: Anyone
}
}
}
37 changes: 27 additions & 10 deletions deno_dist/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import type { ResponseBalance } from '../index.ts'
import type { Anyone, ResponseBalance, ResponseUserInfo } from '../index.ts'

export function parseCookieFromMap(map: Map<string, string>): string {
return Array.from(map.entries())
.map(([key, value]) => `${key}=${value}`)
.join('; ')
}

export function parseBalanceContext(result: any): ResponseBalance {
return {
success: true,
total: result.payload.walletSummary.allTotalBalanceInfo.balance,
currency: result.payload.walletSummary.allTotalBalanceInfo.currency,
updated_at: result.payload.updatedAt,
raw: result
}
}
export function parseBalanceContext(result: Anyone): ResponseBalance {
return {
success: true,
total: result.payload.walletSummary.allTotalBalanceInfo.balance,
currency: result.payload.walletSummary.allTotalBalanceInfo.currency,
updated_at: result.payload.updatedAt,
raw: result,
}
}

export function parseUserInfoContext(result: Anyone): ResponseUserInfo {
return {
success: true,
id: result.payload.id,
user_id: result.payload.user_defined_id ?? 'unknown',
state: result.payload.state,
first_name: result.payload.first_name,
last_name: result.payload.last_name,
display_name: result.payload.display_name,
icon_url: result.payload.photo_url,
phone_number: result.payload.mobile,
email: result.payload.email,
date_of_birth: result.payload.date_of_birth,
raw: result,
}
}
Loading

0 comments on commit 38c45b9

Please sign in to comment.