Skip to content

Commit

Permalink
fix: bug in webhook flow (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: lamnv <[email protected]>
  • Loading branch information
lamnv60533 and liam-guide authored Mar 18, 2024
1 parent 49af963 commit 5a640ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
33 changes: 15 additions & 18 deletions src/dependency/momo/momo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,28 @@ export class MomoService {
const requestId = uuidv4();
const orderId = requestId;
const momoSignatureObj = {
partnerCode: this.partnerCode,
accessKey: this.accessKey,
requestId: requestId,
amount: currentInvoice.total_amount,
extraData: currentInvoice.description,
ipnUrl: this.ipnUrl,
orderId: orderId,
orderInfo: currentInvoice.description,
partnerCode: this.partnerCode,
orderInfo:
currentInvoice.description ||
`payment for invoice id ${request.invoiceId}`,
redirectUrl: this.redirectUrl,
requestId: requestId,
ipnUrl: this.ipnUrl,
extraData: '',
requestType: this.requestType,
};
console.log(momoSignatureObj);

const rawSignature = this.createSignature(momoSignatureObj);
const signature = crypto
.createHmac('sha256', this.secretkey)
.update(rawSignature)
.digest('hex');
const requestBody = JSON.stringify({
partnerCode: this.partnerCode,
accessKey: this.accessKey,
requestId: requestId,
amount: currentInvoice.total_amount,
extraData: currentInvoice.description,
ipnUrl: this.ipnUrl,
orderId: orderId,
orderInfo: currentInvoice.description,
redirectUrl: this.redirectUrl,
requestType: this.requestType,
...momoSignatureObj,
signature: signature,
lang: 'en',
});
Expand Down Expand Up @@ -127,14 +122,16 @@ export class MomoService {
where: { invoice_id: currentInvoice.invoice_id },
order: { created_at: 'DESC' },
});
console.log('========', currentInvoice, latestInvoiceStatus);

if (
latestInvoiceStatus &&
latestInvoiceStatus.status_id === 'NEW' &&
!currentInvoice.payment_order_id
) {
this.logger.log(
'currentInvoice for momo payment order: ',
currentInvoice,
JSON.stringify(currentInvoice),
);
return axiosInstance
.request(options)
Expand Down Expand Up @@ -194,10 +191,10 @@ export class MomoService {
payUrl: momoOrderResult.payUrl,
};
})
.catch(async (error) => {
.catch(async (error: AxiosError) => {
this.logger.error(
'An error occurred when create momo request',
JSON.stringify(error),
JSON.stringify(error.response?.data),
);
await this.orderService.cancelOrder(currentInvoice.order_id, {
isMomo: true,
Expand Down
18 changes: 8 additions & 10 deletions src/feature/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ export class OrderService {
where: { delivery_order_id: orderId },
});
const latestOrderStatus = await this.orderStatusLogRepo.findOne({
where: { order_id: orderId },
where: { order_id: currentOrder.order_id },
order: { logged_at: 'DESC' },
});
const latestDriverStatus = await this.driverStatusLogRepo.findOne({
where: { order_id: orderId },
where: { order_id: currentOrder.order_id },
order: { logged_at: 'DESC' },
});
if (currentOrder) {
Expand All @@ -100,9 +100,7 @@ export class OrderService {
}
return { message: 'Order not existed' };
} catch (error) {
this.logger.error(
`An error occurred while updating order: ${error.message}`,
);
this.logger.error(`An error occurred while updating order: ${error}`);
throw new InternalServerErrorException();
}
}
Expand All @@ -112,7 +110,7 @@ export class OrderService {
latestDriverId,
webhookData,
) {
const PATH_STATUS = webhookData?.path?.status;
const PATH_STATUS = webhookData?.path[2]?.status; // assume 3rd is status record
const SUB_STATUS = webhookData?.sub_status;

const orderStatusLog = new OrderStatusLog();
Expand Down Expand Up @@ -168,7 +166,7 @@ export class OrderService {
const currentDriver = await this.driverRepo.findOne({
where: { reference_id: webhookData.supplier_id, type: driverType },
});
let driverId = currentDriver.driver_id;
let driverId = currentDriver?.driver_id;
if (currentDriver) {
currentDriver.license_plates = webhookData.supplier_plate_number;
currentDriver.phone_number = webhookData.supplier_id;
Expand Down Expand Up @@ -208,13 +206,13 @@ export class OrderService {
// Order_Status_Log with READY and DELIVERING
if (latestOrderStatus === OrderStatus.PROCESSING) {
orderStatusLog.order_status_id = OrderStatus.READY;
await this.orderStatusLogRepo.save(orderStatusLog);
await this.orderStatusLogRepo.save({ ...orderStatusLog });
orderStatusLog.order_status_id = OrderStatus.DELIVERING;
await this.orderStatusLogRepo.save(orderStatusLog);
await this.orderStatusLogRepo.save({ ...orderStatusLog });
} else if (latestOrderStatus === OrderStatus.READY) {
// order_status == READY
// Order_Status_Log with DELIVERING
orderStatusLog.order_status_id = OrderStatus.COMPLETED;
orderStatusLog.order_status_id = OrderStatus.DELIVERING;
await this.orderStatusLogRepo.save(orderStatusLog);
} else {
//Urgent_Action_Needed
Expand Down

0 comments on commit 5a640ae

Please sign in to comment.