-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathuserBankAccountCreate.ts
More file actions
62 lines (53 loc) · 1.68 KB
/
Copy pathuserBankAccountCreate.ts
File metadata and controls
62 lines (53 loc) · 1.68 KB
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
import models from '../../models'
import stripeModule from '../../client/payment/stripe'
const stripe = stripeModule()
import { currencyMap } from '../util/currency-map'
const currentModels = models as any
const getCurrency = (country: string) => {
return currencyMap[country]
}
type UserBankAccountCreateParams = {
userParams: {
id: number
country: string
currency?: string
}
bankAccountParams: {
country?: string
currency?: string
account_holder_type: string
account_holder_name: string
routing_number: string
account_number: string
}
}
export async function userBankAccountCreate({
userParams,
bankAccountParams
}: UserBankAccountCreateParams) {
const userCountry = userParams.country
const userCurrency = userParams.currency || getCurrency(userCountry)
const data = await currentModels.User.findOne({
where: { id: userParams.id }
})
if (data.dataValues.account_id) {
const bankAccounts = await stripe.accounts.listExternalAccounts(data.dataValues.account_id, {
object: 'bank_account'
})
if (bankAccounts.data.length) {
return bankAccounts.data[0]
}
const account = await stripe.accounts.createExternalAccount(data.dataValues.account_id, {
external_account: {
object: 'bank_account',
country: bankAccountParams.country || userCountry,
currency: bankAccountParams.currency || userCurrency,
account_holder_type: bankAccountParams.account_holder_type,
account_holder_name: bankAccountParams.account_holder_name,
routing_number: bankAccountParams.routing_number,
account_number: bankAccountParams.account_number
}
})
return account
}
}