diff --git a/src/dependency/momo/momo.service.ts b/src/dependency/momo/momo.service.ts index e929662..df18fc5 100644 --- a/src/dependency/momo/momo.service.ts +++ b/src/dependency/momo/momo.service.ts @@ -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', }); @@ -127,6 +122,8 @@ export class MomoService { where: { invoice_id: currentInvoice.invoice_id }, order: { created_at: 'DESC' }, }); + console.log('========', currentInvoice, latestInvoiceStatus); + if ( latestInvoiceStatus && latestInvoiceStatus.status_id === 'NEW' && @@ -134,7 +131,7 @@ export class MomoService { ) { this.logger.log( 'currentInvoice for momo payment order: ', - currentInvoice, + JSON.stringify(currentInvoice), ); return axiosInstance .request(options) @@ -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, diff --git a/src/feature/order/order.service.ts b/src/feature/order/order.service.ts index c4e1d0d..51f4e89 100644 --- a/src/feature/order/order.service.ts +++ b/src/feature/order/order.service.ts @@ -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) { @@ -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(); } } @@ -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(); @@ -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; @@ -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