Skip to content

Commit

Permalink
feat: add payment gateway grpc protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavodalves committed Dec 7, 2023
1 parent 268c7f9 commit 2f5ddb7
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 369 deletions.
2 changes: 2 additions & 0 deletions payment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"typescript": "*"
},
"dependencies": {
"@grpc/grpc-js": "^1.9.12",
"@grpc/proto-loader": "^0.7.10",
"@prisma/client": "^5.4.2",
"amqplib": "^0.10.3",
"cors": "^2.8.5",
Expand Down
2 changes: 1 addition & 1 deletion payment/src/infra/Queue/QueueController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Queue from './Queue';
import ProcessPayment from '../../application/usecase/ProcessPayment';
import OrderCreated from '../../domain/events/OrderCreated';
import { Replace } from '../../helpers/Replace';
import Queue from './queue';


export default class QueueController {
Expand Down
2 changes: 1 addition & 1 deletion payment/src/infra/Queue/RabbitMQQueueAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import amqplib from 'amqplib';
import Queue from './Queue';
import DomainEvent from '../../core/building-blocks/domain-event';
import Queue from './queue';

export default class RabbitMQQueueAdapter implements Queue {
connection: any;
Expand Down
2 changes: 1 addition & 1 deletion payment/src/infra/event/EventManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Queue from '../queue/Queue';
import Queue from '../Queue/queue';
import AggregateRoot from '../../core/building-blocks/aggregate-root';
import DomainEvent from '../../core/building-blocks/domain-event';
import DomainEventManager from '../../domain/service/EventManager';
Expand Down
33 changes: 33 additions & 0 deletions payment/src/infra/gateways/payment/grpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import path from 'path';
import PaymentGateway, { Input, Output } from '../../../application/gateways/Payment';
import { TransactionStatus } from '../../../domain/entities/Transaction';

const protoObject = protoLoader.loadSync(path.resolve(__dirname, 'payment.proto'));
const payment = grpc.loadPackageDefinition(protoObject) as any;

export default class PaymentGatewayGrpc implements PaymentGateway {
private readonly client: any;

constructor(
private readonly serverUrl: string
) {
this.client = new payment.PaymentService(this.serverUrl, grpc.credentials.createInsecure());
}

async createTransaction(input: Input): Promise<Output> {
return new Promise((resolve, reject) => {
this.client.pay(input, (err: any, response: any) => {
if (err) {
reject(err);
} else {
resolve({
tid: response.tid,
status: response.status === 'confirmed' ? TransactionStatus.CONFIRMED : TransactionStatus.RECUSED
});
}
});
});
}
}
15 changes: 15 additions & 0 deletions payment/src/infra/gateways/payment/payment.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

service PaymentService {
rpc Pay(PaymentItem) returns (PaymentResponseItem) {}
}

message PaymentItem {
float value = 1;
string creditCardToken = 2;
}

message PaymentResponseItem {
string tid = 1;
string status = 2;
}
8 changes: 5 additions & 3 deletions payment/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import ProcessPayment from './application/usecase/ProcessPayment';
import { DomainEventManagerAdapter } from './infra/event/EventManager';
import QueueController from './infra/queue/QueueController';
import RabbitMQQueueAdapter from './infra/queue/RabbitMQQueueAdapter';

import FakePaymentGateway from './infra/gateways/payment/fake';
import FakeTransactionRepository from './infra/repositories/transaction';
import { PrismaClient } from '@prisma/client';
import PrismaTransactionRepository from './infra/repositories/transaction';
import PaymentGatewayGrpc from './infra/gateways/payment/grpc';
import QueueController from './infra/Queue/QueueController';
import RabbitMQQueueAdapter from './infra/Queue/RabbitMQQueueAdapter';

async function main () {
const queue = new RabbitMQQueueAdapter();
await queue.connect();
const paymentGateway = new FakePaymentGateway();
const paymentGateway = new PaymentGatewayGrpc('localhost:50051');

const prismaClient = new PrismaClient({ log: ['query']});
const transactionRepository = new PrismaTransactionRepository(prismaClient);
Expand Down
868 changes: 505 additions & 363 deletions payment/yarn.lock

Large diffs are not rendered by default.

0 comments on commit 2f5ddb7

Please sign in to comment.