Skip to content

Commit

Permalink
refactor(payment-service): refractor package.json and modify readmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sumit-tuteja committed Sep 1, 2021
1 parent 3fcd46d commit e4cf971
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 50 deletions.
199 changes: 154 additions & 45 deletions services/payment-service/README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,184 @@
# payment-service

This application is generated using [LoopBack 4 CLI](https://loopback.io/doc/en/lb4/Command-line-interface.html) with the
[initial project layout](https://loopback.io/doc/en/lb4/Loopback-application-layout.html).
[![LoopBack](<https://github.com/strongloop/loopback-next/raw/master/docs/site/imgs/branding/Powered-by-LoopBack-Badge-(blue)[email protected]>)](http://loopback.io/)

## Install dependencies
A Loopback Microservice primarily used for payment implementation to charge the payments for
any client application.

By default, dependencies were installed when this application was generated.
Whenever dependencies in `package.json` are changed, run the following command:
## Installation

```sh
npm install
```bash
npm i @sourceloop/payment-service
```

To only install resolved dependencies in `package-lock.json`:
## Usage

- Create a new Loopback4 Application (If you don't have one already)
`lb4 testapp`
- Install the in mail service
`npm i @sourceloop/payment-service`
- Set the [environment variables](#environment-variables).
- Run the [migrations](#migrations).
- Add the `PaymentServiceComponent` to your Loopback4 Application (in `application.ts`).
```typescript
// import the PaymentServiceComponent
import {PaymentServiceComponent} from '@sourceloop/payment-service';
// add Component for PaymentServiceComponent
this.component(PaymentServiceComponent);
```
**Binding the Providers**

```typescript
//import Providers
import {
GatewayBindings,
GatewayProvider,
RazorpayBindings,
RazorpayProvider,
StripeBindings,
StripeProvider,
} from 'payment-service/dist/providers';
//Bind the providers
this.bind(StripeBindings.Config).to({dataKey: '', publishKey: ''});
this.bind(StripeBindings.StripeHelper).toProvider(StripeProvider);
this.bind(RazorpayBindings.RazorpayConfig).to({dataKey: '', publishKey: ''});
this.bind(RazorpayBindings.RazorpayHelper).toProvider(RazorpayProvider);
this.bind(GatewayBindings.GatewayHelper).toProvider(GatewayProvider);
```

```sh
npm ci
- Set up a [Loopback4 Datasource](https://loopback.io/doc/en/lb4/DataSource.html) with `dataSourceName` property set to `PaymentDatasourceName`. You can see an example datasource [here](#setting-up-a-datasource).
- Start the application
`npm start`

### Setting up a `DataSource`

Here is a sample Implementation `DataSource` implementation using environment variables and PostgreSQL as the data source.

```typescript
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';
import {PaymentDatasourceName} from '@sourceloop/payment-service';

const config = {
name: PaymentDatasourceName,
connector: 'postgresql',
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
schema: process.env.DB_SCHEMA,
};

@lifeCycleObserver('datasource')
export class InmailDataSource
extends juggler.DataSource
implements LifeCycleObserver
{
static dataSourceName = PaymentDatasourceName;
static readonly defaultConfig = config;

constructor(
@inject(`datasources.config.${PaymentDatasourceName}`, {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
```

## Run the application
### Migration

The migrations required for this service are processed during the installation automatically if you set the `PAYMENT_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. The migrations use [`db-migrate`](https://www.npmjs.com/package/db-migrate) with [`db-migrate-pg`](https://www.npmjs.com/package/db-migrate-pg) driver for migrations, so you will have to install these packages to use auto-migration. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `PAYMENT_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

### Setting Environment Variables

```sh
npm start
Do not forget to set Environment variables. The examples below show a common configuration for a PostgreSQL Database running locally.

```environment
NODE_ENV=dev
LOG_LEVEL=DEBUG
HOST=0.0.0.0
PORT=3000
DB_HOST=localhost
DB_PORT=5432
DB_USER=pg_service_user
DB_PASSWORD=pg_service_user_password
DB_DATABASE=payment_db
DB_SCHEMA=public
JWT_SECRET=super_secret_string
JWT_ISSUER=https://authentication.service
```

You can also run `node .` to skip the build step.
| Name | Required | Default Value | Description |
| ------------- | -------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `NODE_ENV` | Y | | Node environment value, i.e. `dev`, `test`, `prod` |
| `LOG_LEVEL` | Y | | Log level value, i.e. `error`, `warn`, `info`, `verbose`, `debug` |
| `HOST` | Y | | Host for the service to run under, i.e. `0.0.0.0` |
| `PORT` | Y | `3000` | Port for the service to listen on. |
| `DB_HOST` | Y | | Hostname for the database server. |
| `DB_PORT` | Y | | Port for the database server. |
| `DB_USER` | Y | | User for the database. |
| `DB_PASSWORD` | Y | | Password for the database user. |
| `DB_DATABASE` | Y | | Database to connect to on the database server. |
| `DB_SCHEMA` | Y | `public` | Database schema used for the data source. In PostgreSQL, this will be `public` unless a schema is made explicitly for the service. |
| `JWT_SECRET` | Y | | Symmetric signing key of the JWT token. |
| `JWT_ISSUER` | Y | | Issuer of the JWT token. |

Open http://127.0.0.1:3000 in your browser.
### API Documentation

## Rebuild the project
#### Common Headers

To incrementally build the project:
Authorization: Bearer <token> where <token> is a JWT token signed using JWT issuer and secret.
`Content-Type: application/json` in the response and in request if the API method is NOT GET

```sh
npm run build
```
#### Common Request path Parameters

To force a full build by cleaning up cached artifacts:
{version}: Defines the API Version

```sh
npm run rebuild
```
### Common Responses

## Fix code style and formatting issues
200: Successful Response. Response body varies w.r.t API
401: Unauthorized: The JWT token is missing or invalid
403: Forbidden : Not allowed to execute the concerned API
404: Entity Not Found
400: Bad Request (Error message varies w.r.t API)
201: No content: Empty Response

```sh
npm run lint
```
#### API Details

To automatically fix such issues:
##### POST /payment-gateways

```sh
npm run lint:fix
```
Create a payment gateway.

## Other useful commands
##### POST /place-order-and-pay

- `npm run migrate`: Migrate database schemas for models
- `npm run openapi-spec`: Generate OpenAPI spec into a file
- `npm run docker:build`: Build a Docker image for this application
- `npm run docker:run`: Run this application inside a Docker container
Create an order and initiate transaction for the selected payment gateway, this will create order and initiate payment process.

## Tests
##### POST /orders

```sh
npm test
```
Creating orders manually.

##### GET /transactions/orderid/{id}

Pass order id in {id} for manually created orders or retry the payment in case of failure.

##### POST /transactions/refund/{id}

Pass transactions ID in {id} to initiate a refund.

##### GET /orders

Get list of all orders.

##### GET /transactions

Get list of all transactions.

##### GET /transactions/{id}

## What's next
Get details of particular transaction.

Please check out [LoopBack 4 documentation](https://loopback.io/doc/en/lb4/) to
understand how you can continue to add features to this application.
##### GET /orders/{id}

[![LoopBack](https://github.com/strongloop/loopback-next/raw/master/docs/site/imgs/branding/Powered-by-LoopBack-Badge-(blue)[email protected])](http://loopback.io/)
Get details of particular order.
19 changes: 14 additions & 5 deletions services/payment-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "0.0.1",
"description": "services",
"keywords": [
"loopback-application",
"loopback-extension",
"loopback"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engines": {
"node": ">=10.16"
"node": ">=10"
},
"scripts": {
"build": "lb-tsc",
Expand Down Expand Up @@ -39,10 +39,11 @@
},
"repository": {
"type": "git",
"url": ""
"url": "https://github.com/sourcefuse/loopback4-microservice-catalog.git",
"directory": "services/payment-service"
},
"author": " <[email protected]>",
"license": "",
"author": "Sourcefuse",
"license": "MIT",
"files": [
"README.md",
"dist",
Expand All @@ -52,6 +53,10 @@
"database.json",
"!*/__tests__"
],
"peerDependencies": {
"db-migrate": "< 1.0.0 || ^1.0.0",
"db-migrate-pg": "^1.0.0"
},
"dependencies": {
"@loopback/boot": "^3.4.0",
"@loopback/context": "^3.16.0",
Expand Down Expand Up @@ -82,5 +87,9 @@
"typescript": "~4.2.4",
"widdershins": "^4.0.1",
"source-map-support": "^0.5.19"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
}
}

0 comments on commit e4cf971

Please sign in to comment.