Accept Payment This document provides a guide on how to accept payments using the payment gateway. It includes API calls, transaction controllers, and specific implementations for MTN and Airtel payment methods.
To initiate a payment, you can use the following API call. The example below shows how to initiate an MTN MoMo payment.
import axios from "axios" ;
interface PaymentData {
amount : number | null ;
currency : string ;
phoneNumber : string | null ;
description : string | null ;
}
const BASE_URL = 'http://localhost:8000/api/v1/payment' ;
const initiateMtnPayment = async ( paymentData : PaymentData ) => {
try {
const response = await axios. post ( `${ BASE_URL }/mtn-momo-pay` , paymentData, { withCredentials: true });
return response.data;
} catch (error) {
console. error ( "Error during Mtn momo payment" );
throw error;
}
}
import { Request, Response } from 'express' ;
import { getSentTransactions, getReceivedTransaction, completeTransaction, getAllTransactions } from '../services/transaction.service' ;
interface AuthenticatedRequest extends Request {
user : {
id : string ;
};
}
export const getSentTransactionsHandler = async ( req : Request , res : Response ) => {
if ( ! req.user) {
return res. status ( 401 ). json ({ error: 'User not authenticated' });
}
const authReq = req as AuthenticatedRequest ;
try {
const result = await getSentTransactions (authReq.user.id);
res. status ( 200 ). json (result);
} catch (error) {
res. status ( 500 ). json ({ error: 'Error retrieving sent transactions' });
}
};
import { Request, Response } from 'express' ;
import { getSentTransactions, getReceivedTransaction, completeTransaction, getAllTransactions } from '../services/transaction.service' ;
interface AuthenticatedRequest extends Request {
user : {
id : string ;
};
}
export const getReceivedTransactionsHandler = async ( req : Request , res : Response ) => {
if ( ! req.user) {
return res. status ( 401 ). json ({ error: 'User not authenticated' });
}
const authReq = req as AuthenticatedRequest ;
try {
const result = await getReceivedTransaction (authReq.user.id);
res. status ( 200 ). json (result);
} catch (error) {
res. status ( 500 ). json ({ error: 'Error retrieving received transactions' });
}
};
import { Request, Response } from 'express' ;
import { getSentTransactions, getReceivedTransaction, completeTransaction, getAllTransactions } from '../services/transaction.service' ;
export const getAllTransaction = async ( req : Request , res : Response ) => {
if ( ! req.user) {
return res. status ( 401 ). json ({ error: 'User not authenticated' });
}
try {
const result = await getAllTransactions ();
res. status ( 200 ). json (result);
} catch (error) {
res. status ( 500 ). json ({ error: 'Error retrieving all transactions' });
}
};
import { Request, Response } from 'express' ;
import { getSentTransactions, getReceivedTransaction, completeTransaction, getAllTransactions } from '../services/transaction.service' ;
interface AuthenticatedRequest extends Request {
user : {
id : string ;
};
}
export const completeTransactionHandler = async ( req : Request , res : Response ) => {
if ( ! req.user) {
return res. status ( 401 ). json ({ error: 'User not authenticated' });
}
const authReq = req as AuthenticatedRequest ;
try {
const result = await completeTransaction (req.params.id, authReq.user.id);
res. status ( 200 ). json (result);
} catch (error) {
res. status ( 500 ). json ({ error: 'Error completing transaction' });
}
};
This guide provides the necessary information to accept payments using the payment gateway. Follow the provided examples and customize the implementation to fit your specific requirements. If you have any questions or need further assistance, please contact our support team.
Was this page helpful?
Helpful (0)
Not helpful (0)
© Copyright 2024. All rights reserved.