├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package.json ├── src ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts ├── dtos │ └── account.dto.ts └── main.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── testing-formats ├── create-account.testing.json └── create-payment.testing.json ├── tsconfig.build.json ├── tsconfig.json └── yarn.lock /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | tsconfigRootDir : __dirname, 6 | sourceType: 'module', 7 | }, 8 | plugins: ['@typescript-eslint/eslint-plugin'], 9 | extends: [ 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:prettier/recommended', 12 | ], 13 | root: true, 14 | env: { 15 | node: true, 16 | jest: true, 17 | }, 18 | ignorePatterns: ['.eslintrc.js'], 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', 23 | '@typescript-eslint/no-explicit-any': 'off', 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json 36 | 37 | # environment files 38 | .env -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | 19 | Support us 20 | 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Installation 30 | 31 | ```bash 32 | $ npm install 33 | ``` 34 | 35 | ## Running the app 36 | 37 | ```bash 38 | # development 39 | $ npm run start 40 | 41 | # watch mode 42 | $ npm run start:dev 43 | 44 | # production mode 45 | $ npm run start:prod 46 | ``` 47 | 48 | ## Test 49 | 50 | ```bash 51 | # unit tests 52 | $ npm run test 53 | 54 | # e2e tests 55 | $ npm run test:e2e 56 | 57 | # test coverage 58 | $ npm run test:cov 59 | ``` 60 | 61 | ## Support 62 | 63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 64 | 65 | ## Stay in touch 66 | 67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 68 | - Website - [https://nestjs.com](https://nestjs.com/) 69 | - Twitter - [@nestframework](https://twitter.com/nestframework) 70 | 71 | ## License 72 | 73 | Nest is [MIT licensed](LICENSE). 74 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/nest-cli", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stripe-with-charge-nestjs", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^9.0.0", 25 | "@nestjs/core": "^9.0.0", 26 | "@nestjs/platform-express": "^9.0.0", 27 | "dotenv": "^16.0.3", 28 | "reflect-metadata": "^0.1.13", 29 | "rimraf": "^3.0.2", 30 | "rxjs": "^7.2.0", 31 | "stripe": "^11.1.0" 32 | }, 33 | "devDependencies": { 34 | "@nestjs/cli": "^9.0.0", 35 | "@nestjs/schematics": "^9.0.0", 36 | "@nestjs/testing": "^9.0.0", 37 | "@types/express": "^4.17.13", 38 | "@types/jest": "28.1.8", 39 | "@types/node": "^16.0.0", 40 | "@types/supertest": "^2.0.11", 41 | "@typescript-eslint/eslint-plugin": "^5.0.0", 42 | "@typescript-eslint/parser": "^5.0.0", 43 | "eslint": "^8.0.1", 44 | "eslint-config-prettier": "^8.3.0", 45 | "eslint-plugin-prettier": "^4.0.0", 46 | "jest": "28.1.3", 47 | "prettier": "^2.3.2", 48 | "source-map-support": "^0.5.20", 49 | "supertest": "^6.1.3", 50 | "ts-jest": "28.0.8", 51 | "ts-loader": "^9.2.3", 52 | "ts-node": "^10.0.0", 53 | "tsconfig-paths": "4.1.0", 54 | "typescript": "^4.7.4" 55 | }, 56 | "jest": { 57 | "moduleFileExtensions": [ 58 | "js", 59 | "json", 60 | "ts" 61 | ], 62 | "rootDir": "src", 63 | "testRegex": ".*\\.spec\\.ts$", 64 | "transform": { 65 | "^.+\\.(t|j)s$": "ts-jest" 66 | }, 67 | "collectCoverageFrom": [ 68 | "**/*.(t|j)s" 69 | ], 70 | "coverageDirectory": "../coverage", 71 | "testEnvironment": "node" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Body, Controller, Get, Post, Res, Param, Put } from '@nestjs/common'; 2 | import { Response } from 'express'; 3 | import { AppService } from './app.service'; 4 | import { AccountDto } from './dtos/account.dto'; 5 | 6 | @Controller('/api/v1/stripe') 7 | export class AppController { 8 | constructor(private readonly appService: AppService) {} 9 | 10 | @Get() 11 | getHello(): string { 12 | return this.appService.getHello(); 13 | } 14 | 15 | @Post('/account/create') 16 | async createAccount(@Res() res: Response) { 17 | const response = await this.appService.createAccount(); 18 | res.send(response); 19 | } 20 | 21 | @Post('/account/create/new') 22 | async createNewAccount(@Res() res: Response) { 23 | const account = await this.appService.createNewAccount(); 24 | res.send(account); 25 | } 26 | 27 | @Post('/account-link/create/:id') 28 | async createAccountLink(@Res() res: Response, @Param() params) { 29 | console.log(params.id); 30 | const response = await this.appService.createAccountLink(params.id); 31 | res.send(response); 32 | } 33 | 34 | @Get('/account/retrieve/all') 35 | async getAllAccount(@Res() res: Response) { 36 | const connectedAcc = await this.appService.getAccounts(); 37 | res.send(connectedAcc); 38 | } 39 | 40 | @Get('/account/retrieve/:accId') 41 | async getAccount(@Res() res: Response, @Param() param) { 42 | const connectedAcc = await this.appService.getAccount(param.accId); 43 | res.send(connectedAcc); 44 | } 45 | 46 | @Put('/account/update/:accId') 47 | async updateAccount(@Res() res: Response, @Param() params) { 48 | const { accId } = params; 49 | const response = await this.appService.updateAccount(accId); 50 | res.send(response); 51 | } 52 | 53 | @Post('/payment/create') 54 | async doPayment(@Res() res: Response) { 55 | const session = await this.appService.doPayment(); 56 | res.send(session.url); 57 | } 58 | 59 | @Get('/payment/success') 60 | paymentSuccess(@Res() res: Response) { 61 | res.send('

Payment Successfull

'); 62 | } 63 | 64 | @Get('/payment/cancel') 65 | paymentCancel(@Res() res: Response) { 66 | res.send('

Payment Canceled

'); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | @Module({ 6 | imports: [], 7 | controllers: [AppController], 8 | providers: [AppService], 9 | }) 10 | export class AppModule {} 11 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, Res } from '@nestjs/common'; 2 | import Stripe from 'stripe'; 3 | import { AccountDto } from './dtos/account.dto'; 4 | import 'dotenv/config'; 5 | 6 | @Injectable() 7 | export class AppService { 8 | stripe = new Stripe(process.env.STRIPE_SECRET, { 9 | apiVersion: '2022-11-15', 10 | }); 11 | 12 | calculateChargeInCents = (items): number => { 13 | let total = items.map((item) => { 14 | return item.unitPrice * item.quantity; // considering unitPrice in USD 15 | }); 16 | const getSum = (total, value) => { 17 | return total + value; 18 | }; 19 | 20 | let totalAmount = total.reduce(getSum); 21 | console.log(totalAmount); 22 | let commission = 4 / 100; 23 | let charge = totalAmount * commission * 100; 24 | 25 | return charge; 26 | }; 27 | 28 | getHello(): string { 29 | return 'Hello from stripe payment'; 30 | } 31 | 32 | createAccount = async () => { 33 | const account = await this.stripe.accounts.create({ 34 | country: 'US', 35 | type: 'custom', 36 | capabilities: { 37 | card_payments: { requested: true }, 38 | transfers: { requested: true }, 39 | }, 40 | }); 41 | return account; 42 | }; 43 | 44 | async updateAccount(accId: string) { 45 | const account = await this.stripe.accounts.update(accId, { 46 | tos_acceptance: { date: 1609798905, ip: '8.8.8.8' }, 47 | }); 48 | 49 | return account; 50 | } 51 | 52 | async createAccountLink(acc_id: string) { 53 | const accountLink = await this.stripe.accountLinks.create({ 54 | account: acc_id, 55 | refresh_url: 'http://localhost:8008/api/v1/stripe/reauth', 56 | return_url: 'http://localhost:8008/api/v1/stripe/return', 57 | type: 'account_onboarding', 58 | }); 59 | 60 | console.log(accountLink); 61 | return accountLink; 62 | } 63 | 64 | async doPayment() { 65 | const items = [ 66 | { 67 | id: 1, 68 | name: 'Every Padel Åby Arena, GOTHENBURG, SWEDEN', 69 | description: 70 | "Play padel at Every Padel in Gothenburg, Sweden in one of the world's finest padel facilities. Every Padel's newly opened padel centre in Gothenburg offers a total of 25 top-class padel courts.", 71 | images: [ 72 | 'https://www.padelrumors.com/wp-content/uploads/2022/01/everypadel.jpg', 73 | ], 74 | quantity: 1, // we may set 1 match 75 | unitPrice: 380, // per match 76 | }, 77 | ]; 78 | 79 | const session = await this.stripe.checkout.sessions.create({ 80 | line_items: items.map((item) => { 81 | return { 82 | quantity: item.quantity, 83 | price_data: { 84 | currency: 'usd', 85 | unit_amount: item.unitPrice * 100, 86 | product_data: { 87 | name: item.name, 88 | description: item.description, 89 | images: item.images, 90 | }, 91 | }, 92 | }; 93 | }), 94 | mode: 'payment', 95 | success_url: 'http://localhost:8008/api/v1/stripe/payment/success', 96 | cancel_url: 'http://localhost:8008/api/v1/stripe/payment/cancel', 97 | payment_intent_data: { 98 | application_fee_amount: this.calculateChargeInCents(items), 99 | transfer_data: { 100 | destination: 'acct_1M7ZGND76FV95Zzs', 101 | }, 102 | }, 103 | }); 104 | 105 | return session; 106 | } 107 | 108 | async getAccounts() { 109 | const response = await this.stripe.accounts.list(); 110 | return response; 111 | } 112 | 113 | async getAccount(accId) { 114 | const response = await this.stripe.accounts.list(); 115 | return response.data.filter((acc) => acc.id === accId)[0]; 116 | } 117 | 118 | // creating acc with all info 119 | createNewAccount = async () => { 120 | const account = await this.stripe.accounts.create({ 121 | country: 'US', 122 | type: 'custom', 123 | capabilities: { 124 | card_payments: { requested: true }, 125 | transfers: { requested: true }, 126 | }, 127 | business_type: 'individual', 128 | individual: { 129 | first_name: 'Charles', 130 | last_name: 'Jerde', 131 | email: 'karson87@yahoo.com', 132 | dob: { 133 | day: 24, 134 | month: 6, 135 | year: 1953, 136 | }, 137 | address: { 138 | line1: '215 Gusikowski Flats', 139 | city: 'Jonesboro', 140 | postal_code: '76538', 141 | state: 'TX', // see documentation must cause state shorthand 142 | country: 'US', 143 | }, 144 | phone: '+1-907-677-6064', 145 | ssn_last_4: '0000', 146 | }, 147 | email: 'karson87@yahoo.com', 148 | external_account: { 149 | object: 'bank_account', 150 | account_number: '000999999991', 151 | routing_number: '110000000', 152 | country: 'US', 153 | currency: 'usd', 154 | }, 155 | business_profile: { 156 | mcc: '7997', // see documentation; this code is for country club 157 | support_url: 'www.xyz.com', 158 | url: 'www.xyz.com', 159 | }, 160 | tos_acceptance: { 161 | date: 1609798905, 162 | ip: '8.8.8.8', 163 | }, 164 | }); 165 | return account; 166 | }; 167 | } 168 | -------------------------------------------------------------------------------- /src/dtos/account.dto.ts: -------------------------------------------------------------------------------- 1 | export class AccountDto { 2 | business_type: string; 3 | individual: { 4 | first_name: string; 5 | last_name: string; 6 | email: string; 7 | dob: { 8 | day: number; 9 | month: number; 10 | year: number; 11 | }; 12 | address: { 13 | line1: string; 14 | city: string; 15 | postal_code: string; 16 | state: string; // see documentation must cause state shorthand, such as Texas == TX 17 | country: string; 18 | }; 19 | phone: string; 20 | ssn_last_4: string; 21 | }; 22 | email: string; 23 | external_account: { 24 | object: string; 25 | account_number: string; 26 | routing_number: string; 27 | country: string; 28 | currency: string; 29 | }; 30 | business_profile: { 31 | mcc: string; // see documentation; this code is for country club 32 | support_url: string; 33 | url: string; 34 | }; 35 | } 36 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(8008); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /testing-formats/create-account.testing.json: -------------------------------------------------------------------------------- 1 | { 2 | "country": "US", 3 | "type": "custom", 4 | "capabilities": { 5 | "card_payments": { "requested": true }, 6 | "transfers": { "requested": true } 7 | }, 8 | "business_type": "individual", 9 | "individual": { 10 | "first_name": "Viola", 11 | "last_name": "Adams V", 12 | "email": "fhamill@hotmail.com", 13 | "dob": { 14 | "day": 9, 15 | "month": 5, 16 | "year": 1995 17 | }, 18 | "address": { 19 | "line1": "215 Gusikowski Flats", 20 | "city": "Jonesboro", 21 | "postal_code": "76538", 22 | "state": "TX", 23 | "country": "US" 24 | }, 25 | "phone": "+1-339-893-7199", 26 | "ssn_last_4": "0000" 27 | }, 28 | "email": "fhamill@hotmail.com", 29 | "external_account": { 30 | "object": "bank_account", 31 | "account_number": "000999999991", 32 | "routing_number": "110000000", 33 | "country": "US", 34 | "currency": "usd" 35 | }, 36 | "business_profile": { 37 | "mcc": "7997", 38 | "support_url": "www.xyz.com", 39 | "url": "www.xyz.com" 40 | }, 41 | "tos_acceptance": { 42 | "date": 1609798905, 43 | "ip": "8.8.8.8" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /testing-formats/create-payment.testing.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | --------------------------------------------------------------------------------