-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
taskPayment.js
72 lines (64 loc) · 2.23 KB
/
taskPayment.js
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
const Promise = require('bluebird')
const models = require('../../models')
const TransferMail = require('../mail/transfer')
const Stripe = require('stripe')
const stripe = new Stripe(process.env.STRIPE_KEY)
module.exports = Promise.method(function taskPayment (paymentParams) {
return models.Task
.findOne({
where: {
id: paymentParams.taskId
}
},
{ include: [ models.User, models.Order, models.Assign ] }
)
.then(task => {
if (!task) {
throw new Error('find_task_error')
}
return models.Assign.findOne({
where: {
id: task.assigned
},
include: [ models.User ]
}).then(async assign => {
const user = assign.dataValues.User.dataValues
const dest = user.account_id
if (!dest) {
TransferMail.paymentForInvalidAccount(user)
throw new Error('account_destination_invalid')
}
const centavosAmount = paymentParams.value * 100 || task.value * 100
let transferData = {
amount: centavosAmount * 0.92, // 8% base fee
currency: 'usd',
destination: dest,
source_type: 'card',
}
const order = await models.Order.findOne({ where: { TaskId: task.id } })
const coupon = await models.Coupon.findOne({ where: { id: order.couponId } })
if (!coupon || (coupon && coupon.amount < 100)) {
transferData['transfer_group'] = task.transfer_group ? task.transfer_group : `task_${task.id}`
}
return stripe.transfers.create(transferData).then(transfer => {
if (transfer) {
return models.Task.update({ transfer_id: transfer.id }, {
where: {
id: task.id
}
}).then(update => {
if (!update) {
TransferMail.error(user, task, task.value)
throw new Error('update_task_reject')
}
return models.User.findByPk(task.userId).then(taskOwner => {
TransferMail.notifyOwner(taskOwner.dataValues, task, task.value)
TransferMail.success(user, task, task.value)
return transfer
})
})
}
})
})
})
})