Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payment-service): adds payment-service feature #257

Merged
merged 22 commits into from
Sep 3, 2021

Conversation

sumit-tuteja
Copy link
Contributor

@sumit-tuteja sumit-tuteja commented Jul 13, 2021

Description

Adds payment-service that supports configurable multiple payment gateways.

Fixes #256 #212 #275 #285

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • Performed a self-review of my own code
  • Code conforms with the style guide

})
chargeResponse: DataObject<{}>,
): Promise<unknown> {
console.log(this.req.query, 'query');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why console.log ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

): Promise<any> {
console.log(id);
const Order = await this.ordersRepository.findById(id);
return this.res.send(await this.gatewayHelper.create(Order));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why using this.res.send ?

@akshatdubeysf
Copy link
Contributor

@sumiter92 please look into the sonar issues as well.

@sumit-tuteja sumit-tuteja force-pushed the feature/payment-service branch from 793814c to 1429002 Compare August 4, 2021 09:28
const newOrder = await this.ordersRepository.create(orderEntity);
return this.res.redirect(
redirectStatusCode,
`http://localhost:3000/transactions/orderid/${newOrder.id}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a hardcoded redirect here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sumiter92 this needs to come from DB or env

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link

@anujkalka anujkalka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs lots of improvements, summary

  1. DB need to be revisited, needs lots of improvement to caputre data
  2. Use DB transactions on all payment operations. and rollback on errors
  3. Exception handling not implemented anywhere
  4. Returning HTML in api responses
  5. Classes need to be restructured. it should not have gateway. name checks in code like if stripe do this if razor do ths.. this is not extendable. This can be improved using passing gateway object in constructer and calling functions from parent class ..
  6. striepe and razor classes should only have gateway related operations. All DB operations shoudl be writen seperatly and can be called seperatly, as same data need to be stored with any payment gateway.

) {}

async create(payorder: Orders) {
if (payorder.paymentmethod === 'razorpay') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not generic, static checks for payment gateways.. insted of this, payment gateway should be provided argument in constructer and the generic method of this provider gateway would call the payment gateway's method via that constructer refrence.. This makes this class generic to be used with any payment gatewy. This gateway class would be initialized with specific payment gateway class when it is to be initilized

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored done

gatewayId,
);
const gatewayType = paymentGateway.gatewayType;
if (gatewayType === 'stripe') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same,, static check of gateway should not be there.. not extandable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

});
const transRes = transactions[0]?.res;
if (payorder?.status === 'paid') {
return `<html>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning HTML. from API ??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes in one case we are returning, because card number was handled from payment gateway side and we are not storing card information , in frontend request , we are sending 302 temporary redirect to backend for payment and after successful payment , it can be redirected to frontend again with successurl through env and yes by default template is set but can be changed according to requirement and comes from db now

}
const razorPayOptions = {
amount: payorder.totalAmount, // amount in the smallest currency unit
currency: 'INR',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static currency ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed , now its dynamic

currency: 'INR',
};
if (transactions.length === 0) {
await instance.orders.create(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Payment method class should only have functions related to payment gateway intraction
  2. All db related operations should be handeled seperatly, as same wouldbe used in other payment gateways also.
  3. While dealing with payment and database.. always use transactions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this instance is of razorpay and create order on razorpay database , our order is created in transactions controller


refund: async (transactionId: string) => {
const transaction = await this.transactionsRepository.findById(
transactionId,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use transactions whereever possible

);
const paymentId = transaction?.res?.chargeResponse?.id;
const refund = await instance.payments.refund(paymentId);
transaction.res.refundDetails = refund;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excption handling

await this.transactionsRepository.create(transactionData);
}
return `<html>
<title>Stripe Payment Demo</title>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML should not be returned

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored

const transaction = await this.transactionsRepository.findById(
transactionId,
);
const paymentId = await transaction?.res?.chargeResponse?.id;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as in razorpay

  1. DB operations should be seperate from payment, as same operations are happenign ere
  2. use DB transacions and rollback on failover
  3. no exception handling
  4. dont return HTML

@@ -0,0 +1,31 @@
CREATE SCHEMA IF NOT EXISTS main;

CREATE TABLE main.orders (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB need to be revisited if this is to be made generic, many important fields are missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are adding only payment related fields and storing different gateway response in json format in one column , if we want to add more column according to business needs we can extend these models

@@ -0,0 +1,81 @@
{
"name": "payment-service",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name should be @sourceloop/payment-service

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"@loopback/rest-explorer": "^3.2.1",
"@loopback/service-proxy": "^3.1.1",
"@sourceloop/core": "^1.0.0",
"@types/uuid": "^8.3.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types should be in dev dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"@loopback/eslint-config": "^10.1.1",
"eslint": "^7.23.0",
"typescript": "~4.2.3"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add commitizen, husky hooks and commit lint.

@akshatdubeysf
Copy link
Contributor

@sumiter92 also look into the sonar issues.

@sumit-tuteja sumit-tuteja force-pushed the feature/payment-service branch from 1831e52 to e4cf971 Compare September 1, 2021 10:04
@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 3, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
2.9% 2.9% Duplication

@akshatdubeysf akshatdubeysf merged commit e571834 into sourcefuse:master Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Payment Service with Multiple Gateways support
5 participants