-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathpaymentRequestsTransfersFetch.ts
More file actions
70 lines (68 loc) · 2.09 KB
/
Copy pathpaymentRequestsTransfersFetch.ts
File metadata and controls
70 lines (68 loc) · 2.09 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
63
64
65
66
67
68
69
70
import requestPromise from 'request-promise'
import stripe from '../../client/payment/stripe'
import models from '../../models'
const currentModels = models as any
const stripeInstance = stripe()
export async function transferFetch(id: number) {
if (id) {
const transfer = await currentModels.Transfer.findOne({
where: { id },
include: [
currentModels.Task,
{
model: currentModels.User,
as: 'User'
},
{
model: currentModels.User,
as: 'destination'
}
]
})
if (transfer.paypal_payout_id) {
const paypalCredentials = await requestPromise({
method: 'POST',
uri: `${process.env.PAYPAL_HOST}/v1/oauth2/token`,
headers: {
Accept: 'application/json',
'Accept-Language': 'en_US',
Authorization:
'Basic ' +
Buffer.from(process.env.PAYPAL_CLIENT + ':' + process.env.PAYPAL_SECRET).toString(
'base64'
),
'Content-Type': 'application/json',
grant_type: 'client_credentials'
},
form: {
grant_type: 'client_credentials'
}
})
const paypalToken = JSON.parse(paypalCredentials)['access_token']
try {
const paypalTransfer = await requestPromise({
method: 'GET',
uri: `${process.env.PAYPAL_HOST}/v1/payments/payouts/${transfer.paypal_payout_id}`,
headers: {
Accept: '*/*',
'Accept-Language': 'en_US',
Prefer: 'return=representation',
Authorization: 'Bearer ' + paypalToken,
'Content-Type': 'application/json'
},
json: true
})
transfer.dataValues.paypalTransfer = paypalTransfer
} catch (error) {
console.error('Error fetching PayPal transfer:', error)
}
}
if (transfer.transfer_id) {
const stripeTransfer = await stripeInstance.transfers.retrieve(transfer.transfer_id)
if (stripeTransfer) {
transfer.dataValues.stripeTransfer = stripeTransfer
}
}
return transfer
}
}