-
Notifications
You must be signed in to change notification settings - Fork 0
/
payeasyPaid.ts
112 lines (108 loc) · 2.56 KB
/
payeasyPaid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as z from 'zod'
import { extract } from './extract'
import { Parser } from './parser'
/**
* ペイジー払い込みのご確認
*/
const payeasyPaidSchema = z.object({
type: z.literal('payeasyPaid'),
/**
* 受付番号
*/
receiptNumber: z.string(),
/**
* 払込日時
*/
paidOn: z.string(),
/**
* 払込先
*/
payee: z.string(),
/**
* お客さま番号
*/
customerNumber: z.string().optional(),
/**
* 納付番号
*/
paymentNumber: z.string().optional(),
/**
* お名前
*/
name: z.string(),
/**
* 払込内容
*/
detail: z.string(),
/**
* 払込金額
*/
amount: z.number(),
/**
* 払込手数料
*/
fee: z.number(),
})
export type PayeasyPaid = z.infer<typeof payeasyPaidSchema>
export const payeasyPaidParser: Parser<PayeasyPaid> = ({ subject, text }) => {
if (subject !== 'ペイジー払い込みのご確認') {
return
}
const receiptNumber = extract(text, /受付番号: (?<receiptNumber>\d+)/)
.map(_ => _.receiptNumber)
.pop()
const paidOn = extract(
text,
/払込日時: (?<year>\d{4})\/(?<month>\d{2})\/(?<day>\d{2})\s(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})/,
)
.map(
_ =>
`${_.year}-${_.month}-${_.day}T${_.hour}:${_.minute}:${_.second}+09:00`,
)
.pop()
const payee = extract(text, /払込先: (?<payee>.+)/)
.map(_ => _.payee)
.pop()
const customerNumber = extract(text, /お客さま番号: (?<customerNumber>\d+)/)
.map(_ => _.customerNumber)
.pop()
const paymentNumber = extract(text, /納付番号: (?<paymentNumber>\d+)/)
.map(_ => _.paymentNumber)
.pop()
const name = extract(text, /お名前: (?<name>.*)/)
.map(_ => _.name)
.pop()
const detail = extract(text, /払込内容: (?<detail>.+)/)
.map(_ => _.detail)
.pop()
const amount = extract(text, /払込金額: (?<amount>[\d,]+)円/)
.map(_ => parseInt(_.amount.replace(/,/g, '')))
.filter(_ => !Number.isNaN(_))
.pop()
const fee = extract(text, /払込手数料: (?<fee>[\d,]+)円/)
.map(_ => parseInt(_.fee.replace(/,/g, '')))
.filter(_ => !Number.isNaN(_))
.pop()
if (
typeof receiptNumber === 'string' &&
typeof paidOn === 'string' &&
typeof payee === 'string' &&
typeof detail === 'string' &&
typeof amount === 'number' &&
typeof fee === 'number'
) {
return {
type: 'payeasyPaid',
receiptNumber,
paidOn,
payee,
customerNumber,
paymentNumber,
name: name ?? '',
detail,
amount,
fee,
}
}
return
}