Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 1x 1x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 4x 3x 6x 10x 10x 2x 8x 5x 5x 5x 8x 4x 8x 8x 8x 4x 4x 1x 1x 1x 1x 8x 8x 3x 3x 3x 3x 1x 2x 2x 7x 7x 2x 5x 7x 6x 5x 4x 3x 3x 1x 2x 1x 1x 1x 1x 1x 1x 7x 7x 6x 1x 7x 7x 2x 5x 5x 4x 3x 1x 2x 2x 1x 2x 2x 3x 3x 1x 1x 3x 2x 1x | 'use strict';
const _ = require('lodash');
const CE = use('C2C/Exceptions');
const Config = use('Config');
const statuses = Config.get('core.shared.general.paymentStatuses');
const Id = use('C2C/Id');
class PaymentService {
static get inject() {
return [
'C2C/Repositories/PaymentRepo',
'C2C/Repositories/PaymentTipRepo',
'C2C/Repositories/OrderRepo',
'C2C/Repositories/CurrencyRepo',
'C2C/Repositories/PaymentMethodRepo',
'C2C/Repositories/PaymentStatusRepo',
'C2C/Repositories/BookingRepo',
];
}
constructor(
paymentRepo,
paymentTipRepo,
orderRepo,
currencyRepo,
paymentMethodRepo,
paymentStatusRepo,
bookingRepo,
) {
this.paymentRepo = paymentRepo;
this.paymentTipRepo = paymentTipRepo;
this.orderRepo = orderRepo;
this.currencyRepo = currencyRepo;
this.paymentMethodRepo = paymentMethodRepo;
this.paymentStatusRepo = paymentStatusRepo;
this.bookingRepo = bookingRepo;
}
getByPage(params) {
return this.paymentRepo.getPages(params);
}
getCountBy(conditions) {
return this.paymentRepo.countBy(conditions);
}
getSumSucceedAmountBy(conditions) {
return this.paymentRepo.sumSucceedBy(conditions, 'amount');
}
async update(conditions, payload) {
const payment = await this.paymentRepo.findOne({ ...conditions });
if (!payment) {
throw CE.NotFoundException.raise('errors.notFound');
}
this._checkStatusToUpdate(payload.statusId, payment.statusId);
payment.merge(payload);
await payment.save();
return payment;
}
_checkStatusToUpdate(newStatus, status) {
if (!Number.isInteger(newStatus)) {
newStatus = status;
}
let checkResult = false;
let validStatuses = [];
switch (status) {
case statuses.waiting.id:
validStatuses = [statuses.waiting.id, statuses.succeed.id];
break;
case statuses.succeed.id:
validStatuses = [statuses.refunded.id];
break;
case statuses.canceled.id:
validStatuses = [];
break;
}
checkResult = _.includes(validStatuses, newStatus);
if (!checkResult) {
throw CE.BadRequestException.raise('errors.invalidStatus');
}
}
async addTip({ paymentId, bookingId, tipData }) {
// check staffId valid
const currentStaffIds = await this.orderRepo.pluck('staffId', {
bookingId,
});
tipData.forEach(({ staffId }) => {
if (!_.isNull(staffId) && !_.includes(currentStaffIds, staffId)) {
throw CE.NotFoundException.raise('errors.notFound');
}
});
await this.paymentTipRepo.deleteMany({ paymentId });
return this.paymentTipRepo.createMany(tipData);
}
async getOneOrFail(conditions) {
const payment = await this.paymentRepo.findOne({ ...conditions });
if (!payment) {
throw CE.NotFoundException.raise('errors.notFound');
}
return payment;
}
async createPaymentForSystem({
// sourceId,
bookingId,
amount,
systemId,
paymentMethodCode,
currencyCode,
paymentStatusCode,
metadata,
}) {
const booking = await this._getBookingByBookingIdAndSystemId(bookingId, systemId);
const paymentMethod = this.paymentMethodRepo.findByCodeOrFail(paymentMethodCode);
const currency = this.currencyRepo.findByCodeOrFail(currencyCode);
const status = this.paymentStatusRepo.findByCodeOrFail(paymentStatusCode);
// a target system booking just has only a payment
const payment = await this.paymentRepo.findOne({ bookingId: booking.id });
if (payment) {
throw CE.BadRequestException.raise('errors.otherPaymentExists');
}
// just allow create payment with Succeed, Refunded or Error status
if (!_.includes([statuses.succeed.id, statuses.refunded.id, statuses.error.id], status.id)) {
throw CE.BadRequestException.raise('errors.invalidPaymentStatus');
}
let balance = 0;
Eif (status.id === statuses.error.id) {
balance = amount;
}
const paymentData = {
code: Id.makeId(),
attempt: 0,
bookingId: booking.id,
bookingSourceId: booking.sourceId,
amount,
balance,
systemCustomer: booking.systemCustomer,
systemId: systemId,
methodId: paymentMethod.id,
currencyId: currency.id,
statusId: status.id,
userId: booking.userId,
isRefund: 0,
notes: '',
metadata,
};
return this.paymentRepo.create(paymentData);
}
async _getBookingByBookingIdAndSystemId(bookingId, systemId) {
const booking = await this.bookingRepo.findOne({
id: bookingId,
systemId,
});
if (booking) {
return booking;
}
throw CE.NotFoundException.raise('errors.bookingOfTargetSystemNotExist');
}
async updateFromSystem({ paymentCode, systemId, updatedData }) {
const payment = await this.paymentRepo.findOne({
code: paymentCode,
systemId,
});
if (!payment) {
throw CE.NotFoundException.raise('errors.paymentNotExists');
}
// update payment status
const paymentStatusCode = _.get(updatedData, 'paymentStatusCode');
if (paymentStatusCode) {
const status = this.paymentStatusRepo.findByCodeOrFail(paymentStatusCode);
// just allow update payment status to Succeed, Refunded or Error
if (!_.includes([statuses.succeed.id, statuses.refunded.id, statuses.error.id], status.id)) {
throw CE.BadRequestException.raise('errors.invalidPaymentStatus');
}
let balance = 0;
if (status.id === statuses.error.id) {
balance = payment.amount;
}
payment.statusId = status.id;
payment.balance = balance;
}
// update payment method
const paymentMethodCode = _.get(updatedData, 'paymentMethodCode');
if (paymentMethodCode) {
const paymentMethod = this.paymentMethodRepo.findByCodeOrFail(paymentMethodCode);
payment.methodId = paymentMethod.id;
}
if (!_.isEmpty(payment.dirty)) {
await payment.save();
}
}
}
module.exports = PaymentService;
|