Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/banzeh/cielo
Browse files Browse the repository at this point in the history
  • Loading branch information
banzeh committed Jun 17, 2020
2 parents a4ebfd0 + 1e642b6 commit 9487649
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .versionrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"types": [
{"type": "feat", "section": "Funcionalidades"},
{"type": "fix", "section": "Errors Corrigidos"},
{"type": "fix", "section": "Erros Corrigidos"},
{"type": "chore", "hidden": true},
{"type": "docs", "hidden": true},
{"type": "style", "hidden": true},
Expand Down
8 changes: 1 addition & 7 deletions src/class/bank-slip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ export class BankSlip {
}

public create(request: BankSlipCreateRequestModel): Promise<BankSlipCreateResponseModel> {
const options: IHttpRequestOptions = this.util.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path: '/1/sales',
hostname: this.cieloTransactionParams.hostnameTransacao,
});

return this.util.request<BankSlipCreateResponseModel>(options, request);
return this.util.postToSales<BankSlipCreateResponseModel, BankSlipCreateRequestModel>(request);
}
}
7 changes: 1 addition & 6 deletions src/class/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ export class Card {
}

public createTokenizedCard(request: TokenizeRequestModel): Promise<TokenizeResponseModel> {
const options: IHttpRequestOptions = this.util.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path: '/1/card',
hostname: this.cieloTransactionParams.hostnameTransacao,
});
return this.util.request<TokenizeResponseModel>(options, request);
return this.util.post<TokenizeResponseModel, TokenizeRequestModel>({ path: '/1/card' }, request);
}
}
48 changes: 18 additions & 30 deletions src/class/consult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,52 @@ import { RecurrentPaymentConsultResponseModel } from '../models/recurrent-paymen

export class Consult {
private cieloTransactionParams: CieloTransactionInterface;
private util: Utils;


constructor(transaction: CieloTransactionInterface) {
this.cieloTransactionParams = transaction;
this.util = new Utils(transaction);
}

public paymentId(params: ConsultTransactionPaymentIdRequestModel): Promise<TransactionCreditCardResponseModel> {
const util = new Utils(this.cieloTransactionParams);
const options: IHttpRequestOptions = util.getHttpRequestOptions({
method: HttpRequestMethodEnum.GET,
const options = {
path: `/1/sales/${params.paymentId}`,
hostname: this.cieloTransactionParams.hostnameQuery,
});
};

return util.request<TransactionCreditCardResponseModel>(options, {});
return this.util.get<TransactionCreditCardResponseModel>(options);
}

public merchantOrderId(params: ConsultTransactionMerchantOrderIdRequestModel): Promise<ConsultMerchantOrderIdResponseModel> {
const util = new Utils(this.cieloTransactionParams);
const options: IHttpRequestOptions = util.getHttpRequestOptions({
method: HttpRequestMethodEnum.GET,
const options = {
path: `/1/sales?merchantOrderId=${params.merchantOrderId}`,
hostname: this.cieloTransactionParams.hostnameQuery,
});
};

return util.request<ConsultMerchantOrderIdResponseModel>(options, {});
return this.util.get<ConsultMerchantOrderIdResponseModel>(options);
}

public recurrent(params: ConsultTransactionRecurrentPaymentIdRequestModel): Promise<RecurrentPaymentConsultResponseModel> {
const util = new Utils(this.cieloTransactionParams);
const options: IHttpRequestOptions = util.getHttpRequestOptions({
method: HttpRequestMethodEnum.GET,
const options = {
path: `/1/RecurrentPayment/${params.recurrentPaymentId}`,
hostname: this.cieloTransactionParams.hostnameQuery,
});
};

return util.request<RecurrentPaymentConsultResponseModel>(options, {});
return this.util.get<RecurrentPaymentConsultResponseModel>(options);
}

public bin(params: ConsultBinRequestModel): Promise<ConsultBinResponseModel> {
const util = new Utils(this.cieloTransactionParams);
const options: IHttpRequestOptions = util.getHttpRequestOptions({
method: HttpRequestMethodEnum.GET,
const options = {
path: `/1/cardBin/${params.cardBin}`,
hostname: this.cieloTransactionParams.hostnameQuery,
});
};

return util.request<ConsultBinResponseModel>(options, {});
return this.util.get<ConsultBinResponseModel>(options);
}

public cardtoken(params: ConsultTokenRequestModel): Promise<ConsultTokenResponseModel> {
const util = new Utils(this.cieloTransactionParams);
const options: IHttpRequestOptions = util.getHttpRequestOptions({
method: HttpRequestMethodEnum.GET,
const options = {
path: `/1/card/${params.cardToken}`,
hostname: this.cieloTransactionParams.hostnameQuery,
});
};

return util.request<ConsultTokenResponseModel>(options, {});
return this.util.get<ConsultTokenResponseModel>(options);
}

}
7 changes: 1 addition & 6 deletions src/class/creditcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export class CreditCard {
}

public transaction(transaction: TransactionCreditCardRequestModel): Promise<TransactionCreditCardResponseModel> {
const options: IHttpRequestOptions = this.util.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path: "/1/sales",
hostname: this.cieloTransactionParams.hostnameTransacao,
});
return this.util.request<TransactionCreditCardResponseModel>(options, transaction);
return this.util.postToSales<TransactionCreditCardResponseModel, TransactionCreditCardRequestModel>(transaction);
}

public captureSaleTransaction(transaction: CaptureRequestModel): Promise<CaptureResponseModel> {
Expand Down
7 changes: 1 addition & 6 deletions src/class/debit-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export class DebitCard {
}

public createSimpleTransaction(transaction: DebitCardSimpleTransactionRequestModel): Promise<DebitCardSimpleTransactionResponseModel> {
const options: IHttpRequestOptions = this.util.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path: "/1/sales",
hostname: this.cieloTransactionParams.hostnameTransacao,
});
return this.util.request<DebitCardSimpleTransactionResponseModel>(options, transaction);
return this.util.postToSales<DebitCardSimpleTransactionResponseModel, DebitCardSimpleTransactionRequestModel>(transaction);
}
}
8 changes: 1 addition & 7 deletions src/class/eletronic-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ export class EletronicTransfer {
}

public create(request: EletronicTransferCreateRequestModel): Promise<EletronicTransferCreateResponseModel> {
const options: IHttpRequestOptions = this.util.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path: '/1/sales',
hostname: this.cieloTransactionParams.hostnameTransacao,
});

return this.util.request<EletronicTransferCreateResponseModel>(options, request);
return this.util.postToSales<EletronicTransferCreateResponseModel, EletronicTransferCreateRequestModel>(request);
}
}
8 changes: 1 addition & 7 deletions src/class/recurrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ export class Recurrent {
}

public create(params: RecurrentCreateModel): Promise<RecurrentCreateResponse> {
const options: IHttpRequestOptions = this.util.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path: '/1/sales/',
hostname: this.cieloTransactionParams.hostnameTransacao,
});

return this.util.request<RecurrentCreateResponse>(options, params);
return this.util.postToSales<RecurrentCreateResponse, RecurrentCreateModel>(params);
}

public modifyCustomer(params: RecurrentModifyCustomerModel): Promise<IHttpResponse> {
Expand Down
34 changes: 33 additions & 1 deletion src/class/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ export class Utils {
this.cieloConstructor = params;
}

public get<T>(params: { path: string }): Promise<T> {
const hostname = this.cieloConstructor.hostnameQuery;
const { path } = params;
const method = HttpRequestMethodEnum.GET;

const options: IHttpRequestOptions = this.getHttpRequestOptions({
path,
hostname,
method,
});
return this.request<T>(options, {});
}

public postToSales<T, U>(data: U): Promise<T> {
return this.post<T, U>({ path: '/1/sales/' }, data);
}

/**
* Realiza um post na API da Cielo
* @param params path do post
* @param data payload de envio
*/
public post<T, U>(params: { path: string }, data: U):Promise<T> {
const { path } = params;
const options: IHttpRequestOptions = this.getHttpRequestOptions({
method: HttpRequestMethodEnum.POST,
path,
hostname: this.cieloConstructor.hostnameTransacao,
});
return this.request<T>(options, data);
}

public getHttpRequestOptions(params: { hostname: string, path: string, method: HttpRequestMethodEnum }): IHttpRequestOptions {
return {
method: params.method,
Expand Down Expand Up @@ -53,7 +85,7 @@ export class Utils {

res.on('end', () => {
const response = (chunks.length > 0 && this.validateJSON(chunks)) ? JSON.parse(chunks) : '';
if (res.statusCode && [200, 201].indexOf(res.statusCode) === -1) return reject(this.parseHttpRequestError(options, data, response));
if (res.statusCode && [200, 201].indexOf(res.statusCode) === -1) return reject(this.parseHttpRequestError(options, data, res));
if (options.method === 'PUT' && chunks.length === 0) return resolve(this.parseHttpPutResponse(res));
return resolve({
...this.parseHttpPutResponse(res),
Expand Down
4 changes: 2 additions & 2 deletions src/models/payment.request.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface PaymentRequestModel {
/**
* Texto impresso na fatura bancaria comprador - Exclusivo para VISA/MASTER - não permite caracteres especiais - Ver Anexo
*/
softDescriptor: string;
softDescriptor?: string;
/**
* Dados do cartão
*/
Expand Down Expand Up @@ -63,7 +63,7 @@ export interface PaymentRequestModel {
/**
* URI para onde o usuário será redirecionado após o fim do pagamento
*/
ReturnUrl?: string;
returnUrl?: string;

/**
* Informações de recorrência de pagamento
Expand Down
Loading

0 comments on commit 9487649

Please sign in to comment.