-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Callback module, service, and controller; add callback handlin…
…g in MpesaExpressController with Redis integration for payment status updates
- Loading branch information
Showing
7 changed files
with
64 additions
and
6,445 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,49 @@ | ||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; | ||
import { Controller, Get, Post, Body, Patch, Param, Delete, Logger } from '@nestjs/common'; | ||
import { MpesaExpressService } from './mpesa-express.service'; | ||
import { CreateMpesaExpressDto } from './dto/create-mpesa-express.dto'; | ||
import Redis from 'ioredis'; | ||
import { RedisService } from '@liaoliaots/nestjs-redis'; | ||
|
||
@Controller('mpesa') | ||
export class MpesaExpressController { | ||
constructor(private readonly mpesaExpressService: MpesaExpressService) {} | ||
private readonly redis: Redis | null; | ||
private logger = new Logger('MpesaExpressController'); | ||
constructor( | ||
private mpesaExpressService: MpesaExpressService, | ||
private readonly redisService: RedisService | ||
) {} | ||
|
||
@Post('/stkpush') | ||
create(@Body() createMpesaExpressDto: CreateMpesaExpressDto) { | ||
return this.mpesaExpressService.stkPush(createMpesaExpressDto); | ||
} | ||
|
||
@Post('/callback') | ||
async handleCallback(@Body() callBackData: any) { | ||
this.logger.debug(`Callback data: ${JSON.stringify(callBackData)}`); | ||
const redisClient = this.redisService.getOrThrow(); | ||
|
||
const { Body: { stkCallback } } = callBackData; | ||
|
||
const { MerchantRequestID, CheckoutRequestID, ResultCode, ResultDesc } = stkCallback; | ||
|
||
const payment = await redisClient.get(MerchantRequestID); | ||
// if (!payment || payment === null || payment === undefined) { | ||
// this.logger.error('Payment not found, was it cached?'); | ||
// } | ||
|
||
if (payment) { | ||
this.logger.debug(`Payment found: ${payment}`); | ||
const parsedData = JSON.parse(payment); | ||
|
||
if(ResultCode === 0) { | ||
parsedData.status = 'COMPLETED'; | ||
} else { | ||
parsedData.status = 'FAILED'; | ||
} | ||
await redisClient.set(MerchantRequestID, JSON.stringify(parsedData)); | ||
} else { | ||
this.logger.error('Payment not found, was it cached?'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters