Skip to content

Instantly share code, notes, and snippets.

@houssenedao
Last active November 16, 2021 09:16
Show Gist options
  • Save houssenedao/6fd0b5958ec1ed36fa8cdd64628ffbda to your computer and use it in GitHub Desktop.
Save houssenedao/6fd0b5958ec1ed36fa8cdd64628ffbda to your computer and use it in GitHub Desktop.
Nestjs - CinetPayService
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { HttpRequestService } from './http-request.service';
interface ICinetpayConfig {
apikey: string;
siteId: string;
lang?: string;
currency?: string;
channels?: PaymentChannel;
notifyUrl?: string;
returnUrl?: string;
}
interface ICinetpaySignature {
amount: number;
transactionId: string;
firstName: string;
lastName: string;
externalId: string;
description: string;
metadata?: any;
}
interface ICinetpayInitResponse {
code: string;
message: string;
description: string;
data: ICinetpayResponseData;
api_response_id: string;
}
interface ICinetpayResponseData {
payment_token: string;
payment_url: string;
}
interface ICinetpayErrorResponse {
code: string;
message: string;
description: string;
api_response_id: string;
}
export interface ICinetpayInitPaymentResponse extends ICinetpayInitResponse, ICinetpayErrorResponse {}
type PaymentChannel = 'ALL' | 'MOBILE_MONEY' | 'CREDIT_CARD';
@Injectable()
export class CinetpayService {
/**
* API Key.
*
* @private
*/
private apikey: string;
/**
* Site ID.
*
* @private
*/
private siteId: string;
/**
* lang.
*
* @private
*/
private lang = 'fr';
/**
* Currency.
*
* @private
*/
private currency = 'XOF';
/**
* channel.
*
* @private
*/
private channels = 'MOBILE_MONEY';
/**
* Notify url.
*
* @private
*/
private notifyUrl: string;
/**
* Return url.
*
* @private
*/
private returnUrl: string;
/**
* Cinetpay service constructor.
*
* @param config
* @param http
*/
constructor(private readonly config: ConfigService, private readonly http: HttpRequestService) {}
/**
* Set configuration.
*
* @param apikey
* @param siteId
* @param language
* @param currency
*/
init({ apikey, siteId, lang, currency, channels, notifyUrl, returnUrl }: ICinetpayConfig) {
// validation
if (!apikey && !siteId) {
throw Error(`API Key and Site Id is required`);
}
if (lang) {
this.lang = lang;
}
if (currency) {
this.currency = currency;
}
if (channels) {
this.channels = channels;
}
if (notifyUrl) {
this.notifyUrl = notifyUrl;
}
if (returnUrl) {
this.returnUrl = returnUrl;
}
// set values.
this.apikey = apikey;
this.siteId = siteId;
// return instance.
return this;
}
/**
* Init cinetpay payment.
*
* @param amount
* @param description
* @param transactionId
* @param metadata
* @param firstName
* @param lastName
* @param externalId
*/
async initPayment({
amount,
description,
transactionId,
metadata,
firstName,
lastName,
externalId,
}: ICinetpaySignature): Promise<ICinetpayInitPaymentResponse> {
try {
const requestBody = {
apikey: this.apikey,
site_id: this.siteId,
amount,
transaction_id: transactionId,
currency: this.currency,
description,
customer_id: externalId,
customer_name: lastName,
customer_surname: firstName,
channels: this.channels,
lang: this.lang,
metadata,
notify_url: this.notifyUrl,
return_url: this.returnUrl,
};
Logger.log(`INIT PAYMENT REQUEST BODY ${JSON.stringify(requestBody)}`, CinetpayService.name);
const response = this.http.send<ICinetpayInitPaymentResponse>({
method: 'post',
url: `${this.config.get('service.cinetpay.endpoint')}/payment`,
data: requestBody,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
Logger.log(`CINETPAY RESPONSE ${JSON.stringify(response)}`, CinetpayService.name);
return response;
} catch (e) {
Logger.error(e, CinetpayService.name);
throw new InternalServerErrorException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment