Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

(feature) Referral program (#190)

Andrii Romasiunada22093e3ec
diff --git a/.env.example b/.env.example
index 8b7caada0..36b38b30c 100644
--- a/.env.example
+++ b/.env.example
@@ -85,3 +85,7 @@ SENTRY_DSN=''
# Cloudflare Proxy
CLOUDFLARE_PROXY_ENABLED=false
+
+# Paypal (used for payouts to referrers)
+PAYPAL_CLIENT_ID=
+PAYPAL_CLIENT_SECRET=
diff --git a/apps/production/src/auth/auth.controller.ts b/apps/production/src/auth/auth.controller.ts
index a58f4afdb..06a27811a 100644
--- a/apps/production/src/auth/auth.controller.ts
+++ b/apps/production/src/auth/auth.controller.ts
@@ -107,9 +107,12 @@ export class AuthController {
throw new ConflictException(i18n.t('auth.passwordSameAsEmail'))
}
+ const referrerID = await this.authService.getReferrerId(body.refCode)
+
const newUser = await this.authService.createUnverifiedUser(
body.email,
body.password,
+ referrerID,
)
const jwtTokens = await this.authService.generateJwtTokens(newUser.id, true)
@@ -489,9 +492,15 @@ export class AuthController {
): Promise<any> {
const ip = getIPFromHeaders(headers) || reqIP || ''
- const { hash, provider } = body
+ const { hash, provider, refCode } = body
- return this.authService.authenticateSSO(hash, headers, ip, provider)
+ return this.authService.authenticateSSO(
+ hash,
+ headers,
+ ip,
+ provider,
+ refCode,
+ )
}
@ApiOperation({ summary: 'Link SSO provider to an existing account' })
diff --git a/apps/production/src/auth/auth.service.ts b/apps/production/src/auth/auth.service.ts
index 94f34bdc6..e940c664d 100644
--- a/apps/production/src/auth/auth.service.ts
+++ b/apps/production/src/auth/auth.service.ts
@@ -148,12 +148,14 @@ export class AuthService {
public async createUnverifiedUser(
email: string,
password: string,
+ referrerID?: string,
): Promise<User> {
const hashedPassword = await this.hashPassword(password)
const user = await this.userService.createUser({
email,
password: hashedPassword, // Using the password field is incorrect.
+ referrerID,
})
await this.sendVerificationEmail(user.id, user.email)
@@ -502,7 +504,7 @@ export class AuthService {
}
}
- async registerUserGoogle(sub: string, email: string) {
+ async registerUserGoogle(sub: string, email: string, referrerId?: string) {
const query: UserGoogleDTO = {
googleId: sub,
trialEndDate: dayjs
@@ -513,6 +515,7 @@ export class AuthService {
isActive: true,
emailRequests: 0,
email,
+ referrerId,
}
const userWithSameEmail = await this.userService.findOneWhere({
@@ -578,24 +581,57 @@ export class AuthService {
return payload
}
+ async getReferrerId(refCode?: string): Promise<string | undefined> {
+ if (!refCode) {
+ return undefined
+ }
+
+ try {
+ let referrerID
+
+ if (refCode) {
+ const referrer = await this.userService.findOneWhere({
+ refCode,
+ })
+
+ if (referrer) {
+ referrerID = referrer.id
+ }
+ }
+
+ return referrerID
+ } catch (reason) {
+ console.error(
+ `[ERROR][AuthService -> getReferrerId]: ${reason} - ${refCode}`,
+ )
+ return undefined
+ }
+ }
+
async authenticateSSO(
ssoHash: string,
headers: unknown,
ip: string,
provider: SSOProviders,
+ refCode?: string,
) {
if (provider === SSOProviders.GOOGLE) {
- return this.authenticateGoogle(ssoHash, headers, ip)
+ return this.authenticateGoogle(ssoHash, headers, ip, refCode)
}
if (provider === SSOProviders.GITHUB) {
- return this.authenticateGithub(ssoHash, headers, ip)
+ return this.authenticateGithub(ssoHash, headers, ip, refCode)
}
throw new BadRequestException('Unknown SSO provider supplied')
}
- async authenticateGoogle(ssoHash: string, headers: unknown, ip: string) {
+ async authenticateGoogle(
+ ssoHash: string,
+ headers: unknown,
+ ip: string,
+ refCode?: string,
+ ) {
const { sub, email } = await this.processHash(ssoHash)
try {
@@ -604,7 +640,9 @@ export class AuthService {
})
if (!user) {
- return await this.registerUserGoogle(sub, email)
+ const referrerId = await this.getReferrerId(refCode)
+
+ return await this.registerUserGoogle(sub, email, referrerId)
}
return await this.handleExistingUserGoogle(user, headers, ip)
@@ -858,7 +896,7 @@ export class AuthService {
)
}
- async registerUserGithub(id: number, email: string) {
+ async registerUserGithub(id: number, email: string, referrerId?: string) {
const query: UserGithubDTO = {
githubId: id,
trialEndDate: dayjs
@@ -869,6 +907,7 @@ export class AuthService {
isActive: true,
emailRequests: 0,
email,
+ referrerId,
}
const userWithSameEmail = await this.userService.findOneWhere({
@@ -966,7 +1005,12 @@ export class AuthService {
})
}
- async authenticateGithub(ssoHash: string, headers: unknown, ip: string) {
+ async authenticateGithub(
+ ssoHash: string,
+ headers: unknown,
+ ip: string,
+ refCode?: string,
+ ) {
const { id, email } = await this.processHash(ssoHash)
try {
@@ -975,7 +1019,9 @@ export class AuthService {
})
if (!user) {
- return await this.registerUserGithub(id, email)
+ const referrerId = await this.getReferrerId(refCode)
+
+ return await this.registerUserGithub(id, email, referrerId)
}
return await this.handleExistingUserGithub(user, headers, ip)
diff --git a/apps/production/src/auth/dtos/register.dto.ts b/apps/production/src/auth/dtos/register.dto.ts
index 05c4d0e39..d0e977624 100644
--- a/apps/production/src/auth/dtos/register.dto.ts
+++ b/apps/production/src/auth/dtos/register.dto.ts
@@ -5,6 +5,7 @@ import {
IsBoolean,
MaxLength,
MinLength,
+ IsOptional,
} from 'class-validator'
export class RegisterRequestDto {
@@ -34,6 +35,13 @@ export class RegisterRequestDto {
@IsNotEmpty({ message: 'This field is required.' })
@IsBoolean({ message: 'Please enter the valid value (true or false).' })
public readonly checkIfLeaked: boolean
+
+ @ApiProperty({
+ description: 'Affiliate code',
+ example: 'ABCDEFGH',
+ })
+ @IsOptional()
+ public readonly refCode?: string
}
export class RegisterResponseDto {
diff --git a/apps/production/src/auth/dtos/sso-get-jwt-by-hash.dto.ts b/apps/production/src/auth/dtos/sso-get-jwt-by-hash.dto.ts
index cc58875d2..cd9de2d6d 100644
--- a/apps/production/src/auth/dtos/sso-get-jwt-by-hash.dto.ts
+++ b/apps/production/src/auth/dtos/sso-get-jwt-by-hash.dto.ts
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsEnum, IsString } from 'class-validator'
+import { IsEnum, IsString, IsOptional } from 'class-validator'
import { SSOProviders } from './sso-generate.dto'
@@ -17,4 +17,11 @@ export class SSOGetJWTByHashDto {
})
@IsEnum(SSOProviders)
provider: SSOProviders
+
+ @ApiProperty({
+ description: 'Affiliate code',
+ example: 'ABCDEFGH',
+ })
+ @IsOptional()
+ refCode?: string
}
diff --git a/apps/production/src/common/constants.ts b/apps/production/src/common/constants.ts
index a55b6d83d..a26e4cc03 100644
--- a/apps/production/src/common/constants.ts
+++ b/apps/production/src/common/constants.ts
@@ -6,7 +6,8 @@ import * as _toNumber from 'lodash/toNumber'
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config()
-const { CLICKHOUSE_DATABASE } = process.env
+const { CLICKHOUSE_DATABASE, PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET } =
+ process.env
const redis = new Redis(
_toNumber(process.env.REDIS_PORT),
@@ -207,4 +208,6 @@ export {
PERFORMANCE_COLUMNS,
sentryIgnoreErrors,
isProxiedByCloudflare,
+ PAYPAL_CLIENT_ID,
+ PAYPAL_CLIENT_SECRET,
}
diff --git a/apps/production/src/common/templates/en/password-change_information.html b/apps/production/src/common/templates/en/password-change_information.html
index 5cde9ebca..24d2565f7 100644
--- a/apps/production/src/common/templates/en/password-change_information.html
+++ b/apps/production/src/common/templates/en/password-change_information.html
@@ -57,7 +57,7 @@
letter-spacing: -1px;
"
>
- You password has been updated
+ Your password has been updated
</span>
</font>
<div style="height: 38px; line-height: 38px; font-size: 36px;">&nbsp;</div>
diff --git a/apps/production/src/common/templates/en/paypal-email-update.html b/apps/production/src/common/templates/en/paypal-email-update.html
new file mode 100644
index 000000000..caba958c3
--- /dev/null
+++ b/apps/production/src/common/templates/en/paypal-email-update.html
@@ -0,0 +1,158 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>swetrix.com password have been updated</title>
+ <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet" />
+ </head>
+ <body style="margin: 0; padding: 0; background-color: #f0f4f7;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="background: #f0f4f7; min-width: 340px; font-size: 1px; line-height: normal;">
+ <tr>
+ <td align="center" valign="top" bgcolor="#f0f4f7">
+ <!--[if (gte mso 9)|(IE)]>
+ <table border="0" cellspacing="0" cellpadding="0">
+ <tr><td align="center" valign="top" width="750"><![endif]-->
+ <table cellpadding="0" cellspacing="0" border="0" width="750" style="width: 100%; max-width: 750px; min-width: 340px; background: #f0f4f7;">
+ <tr>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ <td align="center" valign="top" style="background: #ffffff;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="right" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 23px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 40px; line-height: 40px; font-size: 38px;">&nbsp;</div>
+ <a href="https://swetrix.com" target="_blank" style="display: block; max-width: 173px;text-decoration: none;">
+ <img src="https://swetrix.com/assets/logo_blue.png" alt="Swetrix" width="175" border="0" style="display: block; width: 175px; font-size: 32px;" />
+ </a>
+ <div style="height: 40px; line-height: 40px; font-size: 40px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font align="center" style="font-size: 52px; line-height: 55px; font-weight: 300; letter-spacing: -1.5px;">
+ <span
+ align="center"
+ style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; font-size: 44px; color: #1a1a1a;"
+ >
+ PayPal email address update
+ </span>
+ </font>
+ <div style="height: 38px; line-height: 38px; font-size: 36px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font face="'Source Sans Pro', sans-serif" color="#6d6d6d" style="font-size: 17px; line-height: 23px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #6d6d6d;
+ font-size: 17px;
+ line-height: 23px;
+ "
+ >
+ Your affiliate payout email address has been changed<br />
+ If you have not done so, please contact us at [email protected]
+ </span>
+ </font>
+ <div style="height: 30px; line-height: 30px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 32px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com/terms"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Terms of Service
+ </a>
+ </span>
+ </font>
+ <font face="'Source Sans Pro', sans-serif" color="#3c4858" style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ &nbsp;&nbsp;|&nbsp;&nbsp;
+ </span>
+ </font>
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com/privacy"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Privacy Policy
+ </a>
+ </span>
+ </font>
+ </td>
+ </tr>
+ </table>
+ <div style="height: 25px; line-height: 25px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ </tr>
+ </table>
+ <!--[if (gte mso 9)|(IE)]>
+ </td></tr>
+ </table><![endif]-->
+ </td>
+ </tr>
+ </table>
+ </body>
+</html>
diff --git a/apps/production/src/common/utils.ts b/apps/production/src/common/utils.ts
index 5d80df001..5d24cc708 100644
--- a/apps/production/src/common/utils.ts
+++ b/apps/production/src/common/utils.ts
@@ -130,6 +130,13 @@ const generateRecoveryCode = () =>
capitalization: 'uppercase',
})
+const generateRefCode = () =>
+ randomstring.generate({
+ length: 8,
+ charset: 'alphanumeric',
+ capitalization: 'uppercase',
+ })
+
const millisecondsToSeconds = (milliseconds: number) => milliseconds / 1000
const generateRandomString = (length: number): string =>
@@ -245,4 +252,5 @@ export {
lookup,
getGeoDetails,
getIPFromHeaders,
+ generateRefCode,
}
diff --git a/apps/production/src/mailer/letter.ts b/apps/production/src/mailer/letter.ts
index 9e3623924..073ce0bf5 100644
--- a/apps/production/src/mailer/letter.ts
+++ b/apps/production/src/mailer/letter.ts
@@ -15,4 +15,5 @@ export enum LetterTemplate {
TrialEndsTomorrow = 'trial-ends-tomorrow',
ProjectSubscriberInvitation = 'project-subscriber-invitation',
ProjectTransfer = 'project-transfer',
+ PayPalEmailUpdate = 'paypal-email-update',
}
diff --git a/apps/production/src/mailer/mailer.service.ts b/apps/production/src/mailer/mailer.service.ts
index 9ef7a69b9..aa3d58dbf 100644
--- a/apps/production/src/mailer/mailer.service.ts
+++ b/apps/production/src/mailer/mailer.service.ts
@@ -92,6 +92,11 @@ const metaInfoJson = {
`A Swetrix user offers to transfer ${p.name} project to your account.`,
},
},
+ [LetterTemplate.PayPalEmailUpdate]: {
+ subject: {
+ en: () => 'Your PayPal email has been updated',
+ },
+ },
}
interface Params {
diff --git a/apps/production/src/payouts/entities/payouts.entity.ts b/apps/production/src/payouts/entities/payouts.entity.ts
new file mode 100644
index 000000000..9ec95f9a3
--- /dev/null
+++ b/apps/production/src/payouts/entities/payouts.entity.ts
@@ -0,0 +1,76 @@
+import { Entity, PrimaryGeneratedColumn, ManyToOne, Column } from 'typeorm'
+import { User } from '../../user/entities/user.entity'
+
+export enum PayoutStatus {
+ /**
+ * The payout is ready to be paid on the next payout date.
+ * This status is assigned when we received the next payment for the subscription from the user.
+ */
+ processing = 'processing',
+ /**
+ * The payout is pending and will be paid automatically unless user cancels their subscriptions or makes a refund or chargeback.
+ */
+ pending = 'pending',
+ /**
+ * The payout was paid to the referrer.
+ */
+ paid = 'paid',
+ /**
+ * The payout was suspended for some reason. It won't be paid automatically and will require manual intervention.
+ */
+ suspended = 'suspended',
+}
+
+@Entity()
+export class Payout {
+ @PrimaryGeneratedColumn('uuid')
+ id: string
+
+ // The date when the payout was created
+ @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
+ created: Date
+
+ // The amount of the payout
+ @Column({
+ type: 'decimal',
+ })
+ amount: number
+
+ // The currency of the payout
+ @Column({
+ type: 'varchar',
+ length: 3,
+ })
+ currency: string
+
+ // The referral ID (user which was referred)
+ @Column('varchar', { length: 36 })
+ referralId: string
+
+ // The status of the payout
+ @Column({
+ type: 'enum',
+ enum: PayoutStatus,
+ default: PayoutStatus.pending,
+ })
+ status: PayoutStatus
+
+ // The date when the payout was paid
+ @Column({
+ type: 'timestamp',
+ nullable: true,
+ })
+ paidAt: Date
+
+ // The transaction ID of the payout
+ @Column({
+ type: 'varchar',
+ length: 50,
+ nullable: true,
+ })
+ transactionId: string
+
+ // The referrer (user which receives the payout)
+ @ManyToOne(() => User, user => user.payouts)
+ user: User
+}
diff --git a/apps/production/src/payouts/payouts.module.ts b/apps/production/src/payouts/payouts.module.ts
new file mode 100644
index 000000000..131228fb2
--- /dev/null
+++ b/apps/production/src/payouts/payouts.module.ts
@@ -0,0 +1,13 @@
+import { Module } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
+
+import { PayoutsService } from './payouts.service'
+import { Payout } from './entities/payouts.entity'
+import { AppLoggerModule } from '../logger/logger.module'
+
+@Module({
+ imports: [TypeOrmModule.forFeature([Payout]), AppLoggerModule],
+ providers: [PayoutsService],
+ exports: [PayoutsService],
+})
+export class PayoutsModule {}
diff --git a/apps/production/src/payouts/payouts.service.ts b/apps/production/src/payouts/payouts.service.ts
new file mode 100644
index 000000000..8f42e3c08
--- /dev/null
+++ b/apps/production/src/payouts/payouts.service.ts
@@ -0,0 +1,71 @@
+import { Injectable } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { Repository } from 'typeorm'
+import { Payout, PayoutStatus } from './entities/payouts.entity'
+import { Pagination, PaginationOptionsInterface } from '../common/pagination'
+
+@Injectable()
+export class PayoutsService {
+ constructor(
+ @InjectRepository(Payout)
+ private payoutsRepository: Repository<Payout>,
+ ) {}
+
+ async create(
+ payout: Pick<Payout, 'amount' | 'currency' | 'referralId' | 'user'>,
+ ): Promise<Payout> {
+ return this.payoutsRepository.save(payout)
+ }
+
+ async update(criteria: any, update: Record<string, unknown>): Promise<any> {
+ return this.payoutsRepository.update(criteria, update)
+ }
+
+ async delete(id: string): Promise<any> {
+ return this.payoutsRepository.delete(id)
+ }
+
+ async find(params: any): Promise<Payout[]> {
+ return this.payoutsRepository.find(params)
+ }
+
+ async findOne(where: any, params: any = {}): Promise<Payout> {
+ return this.payoutsRepository.findOne({
+ where,
+ ...params,
+ })
+ }
+
+ async sumAmountByReferrerId(
+ referrerId: string,
+ status: PayoutStatus,
+ ): Promise<number> {
+ const result = await this.payoutsRepository
+ .createQueryBuilder('payout')
+ .select('SUM(payout.amount)', 'sum')
+ .where('payout.userId = :referrerId', { referrerId })
+ .andWhere('payout.status = :status', { status })
+ .getRawOne()
+
+ return result.sum
+ }
+
+ async paginate(
+ options: PaginationOptionsInterface,
+ where: Record<string, unknown> | undefined,
+ ): Promise<Pagination<Payout>> {
+ const [results, total] = await this.payoutsRepository.findAndCount({
+ take: options.take || 100,
+ skip: options.skip || 0,
+ where,
+ order: {
+ created: 'DESC',
+ },
+ })
+
+ return new Pagination<Payout>({
+ results,
+ total,
+ })
+ }
+}
diff --git a/apps/production/src/project/entity/project.entity.ts b/apps/production/src/project/entity/project.entity.ts
index 7e7d0c67f..ed7b02204 100644
--- a/apps/production/src/project/entity/project.entity.ts
+++ b/apps/production/src/project/entity/project.entity.ts
@@ -8,7 +8,7 @@ import {
} from 'typeorm'
import { ApiProperty } from '@nestjs/swagger'
-import { Alert } from 'src/alert/entity/alert.entity'
+import { Alert } from '../../alert/entity/alert.entity'
import { User } from '../../user/entities/user.entity'
import { ProjectShare } from './project-share.entity'
import { ExtensionToProject } from '../../marketplace/extensions/entities/extension-to-project.entity'
diff --git a/apps/production/src/task-manager/task-manager.module.ts b/apps/production/src/task-manager/task-manager.module.ts
index 5a9acc3d8..fda77cc00 100644
--- a/apps/production/src/task-manager/task-manager.module.ts
+++ b/apps/production/src/task-manager/task-manager.module.ts
@@ -8,6 +8,7 @@ import { MailerModule } from '../mailer/mailer.module'
import { UserModule } from '../user/user.module'
import { ProjectModule } from '../project/project.module'
import { AnalyticsModule } from '../analytics/analytics.module'
+import { PayoutsModule } from '../payouts/payouts.module'
import { ActionTokensModule } from '../action-tokens/action-tokens.module'
import { AppLoggerModule } from '../logger/logger.module'
import { Message } from '../integrations/telegram/entities/message.entity'
@@ -23,6 +24,7 @@ import { Message } from '../integrations/telegram/entities/message.entity'
ExtensionsModule,
AppLoggerModule,
TypeOrmModule.forFeature([Message]),
+ PayoutsModule,
],
providers: [TaskManagerService, TelegramService],
exports: [TaskManagerService],
diff --git a/apps/production/src/task-manager/task-manager.service.ts b/apps/production/src/task-manager/task-manager.service.ts
index ee97deb04..58a568228 100644
--- a/apps/production/src/task-manager/task-manager.service.ts
+++ b/apps/production/src/task-manager/task-manager.service.ts
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common'
import { Cron, CronExpression } from '@nestjs/schedule'
import { IsNull, LessThan, In, Not, Between, MoreThan, Like } from 'typeorm'
+import * as Paypal from '@paypal/payouts-sdk'
import * as bcrypt from 'bcrypt'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
@@ -11,6 +12,8 @@ import * as _size from 'lodash/size'
import * as _map from 'lodash/map'
import * as _now from 'lodash/now'
import * as _find from 'lodash/find'
+import * as _toNumber from 'lodash/toNumber'
+import * as _reduce from 'lodash/reduce'
import { AlertService } from '../alert/alert.service'
import { QueryCondition, QueryMetric, QueryTime } from '../alert/dto/alert.dto'
@@ -25,6 +28,8 @@ import { ActionTokensService } from '../action-tokens/action-tokens.service'
import { ActionTokenType } from '../action-tokens/action-token.entity'
import { LetterTemplate } from '../mailer/letter'
import { AnalyticsService } from '../analytics/analytics.service'
+import { PayoutsService } from '../payouts/payouts.service'
+import { PayoutStatus } from '../payouts/entities/payouts.entity'
import {
ACCOUNT_PLANS,
PlanCode,
@@ -45,12 +50,25 @@ import {
PROJECT_INVITE_EXPIRE,
REDIS_LOG_CAPTCHA_CACHE_KEY,
JWT_REFRESH_TOKEN_LIFETIME,
+ PAYPAL_CLIENT_ID,
+ PAYPAL_CLIENT_SECRET,
+ isDevelopment,
} from '../common/constants'
import { getRandomTip } from '../common/utils'
import { AppLoggerService } from '../logger/logger.service'
dayjs.extend(utc)
+let paypalClient
+
+if (PAYPAL_CLIENT_ID && PAYPAL_CLIENT_SECRET) {
+ const environment = new Paypal.core.SandboxEnvironment(
+ PAYPAL_CLIENT_ID,
+ PAYPAL_CLIENT_SECRET,
+ )
+ paypalClient = new Paypal.core.PayPalHttpClient(environment)
+}
+
const getQueryTime = (time: QueryTime): number => {
if (time === QueryTime.LAST_15_MINUTES) return 15 * 60
if (time === QueryTime.LAST_30_MINUTES) return 30 * 60
@@ -91,6 +109,7 @@ export class TaskManagerService {
private readonly extensionsService: ExtensionsService,
private readonly logger: AppLoggerService,
private readonly telegramService: TelegramService,
+ private readonly payoutsService: PayoutsService,
) {}
@Cron(CronExpression.EVERY_MINUTE)
@@ -1102,4 +1121,111 @@ export class TaskManagerService {
await this.userService.deleteRefreshTokensWhere(where)
}
+
+ @Cron(CronExpression.EVERY_1ST_DAY_OF_MONTH_AT_NOON)
+ async payReferrers(): Promise<void> {
+ if (isDevelopment || !paypalClient) {
+ return
+ }
+
+ const payoutsToProcess = await this.payoutsService.find({
+ where: {
+ status: PayoutStatus.processing,
+ },
+ relations: ['user'],
+ })
+
+ if (_isEmpty(payoutsToProcess)) {
+ return
+ }
+
+ // A map of emails to pay and their amounts
+ const payouts = {}
+
+ for (let i = 0; i < _size(payoutsToProcess); ++i) {
+ const key = payoutsToProcess[i].user.paypalPaymentsEmail
+
+ if (!key) {
+ continue
+ }
+
+ if (!payouts[key]) {
+ payouts[key] = {
+ amount: 0,
+ payoutsIds: [],
+ }
+ }
+
+ payouts[key].amount += _toNumber(payoutsToProcess[i].amount)
+ payouts[key].payoutsIds.push(payoutsToProcess[i].id)
+ }
+
+ const requestBody = {
+ sender_batch_header: {
+ recipient_type: 'EMAIL',
+ email_message: 'Swetrix referral program payout.',
+ note: 'Swetrix referral program payout.',
+ },
+ items: _reduce(
+ payouts,
+ (acc, value, key) => {
+ acc.push({
+ recipient_type: 'EMAIL',
+ amount: {
+ value: value.amount.toFixed(2),
+ currency: 'USD',
+ },
+ receiver: key,
+ note: `Your Swetrix $${value.amount.toFixed(2)} payout.`,
+ })
+
+ return acc
+ },
+ [],
+ ),
+ }
+
+ // Send the request to PayPal
+ const request = new Paypal.payouts.PayoutsPostRequest()
+ request.requestBody(requestBody)
+
+ const response = await paypalClient.execute(request)
+
+ if (response.statusCode !== 201) {
+ console.error(
+ `[CRON](payReferrers) An error occured while executing a request to pay referrers: ${JSON.stringify(
+ response,
+ null,
+ 2,
+ )}`,
+ )
+ console.error(`Payouts: ${JSON.stringify(payouts, null, 2)}`)
+
+ // Update the payouts in the DB
+ await this.payoutsService.update(
+ {
+ where: {
+ id: In(_map(payoutsToProcess, 'id')),
+ },
+ },
+ {
+ status: PayoutStatus.suspended,
+ },
+ )
+ return
+ }
+
+ // Update the payouts in the DB
+ await this.payoutsService.update(
+ {
+ where: {
+ id: In(_map(payoutsToProcess, 'id')),
+ },
+ },
+ {
+ status: PayoutStatus.paid,
+ transactionId: response.result?.batch_header?.payout_batch_id,
+ },
+ )
+ }
}
diff --git a/apps/production/src/user/dto/user-github.dto.ts b/apps/production/src/user/dto/user-github.dto.ts
index 9a205397a..91a232fa0 100644
--- a/apps/production/src/user/dto/user-github.dto.ts
+++ b/apps/production/src/user/dto/user-github.dto.ts
@@ -23,4 +23,7 @@ export class UserGithubDTO {
@ApiProperty({ example: true, required: false })
isActive: boolean
+
+ @ApiProperty({ example: 'uuid-of-referrer', required: false })
+ referrerId?: string
}
diff --git a/apps/production/src/user/dto/user-google.dto.ts b/apps/production/src/user/dto/user-google.dto.ts
index 025fb1e0b..5fb31392c 100644
--- a/apps/production/src/user/dto/user-google.dto.ts
+++ b/apps/production/src/user/dto/user-google.dto.ts
@@ -23,4 +23,7 @@ export class UserGoogleDTO {
@ApiProperty({ example: true, required: false })
isActive: boolean
+
+ @ApiProperty({ example: 'uuid-of-referrer', required: false })
+ referrerId?: string
}
diff --git a/apps/production/src/user/entities/user.entity.ts b/apps/production/src/user/entities/user.entity.ts
index 2b918cb03..baf36faf9 100644
--- a/apps/production/src/user/entities/user.entity.ts
+++ b/apps/production/src/user/entities/user.entity.ts
@@ -11,6 +11,7 @@ import { Project } from '../../project/entity/project.entity'
import { ProjectShare } from '../../project/entity/project-share.entity'
import { Extension } from '../../marketplace/extensions/entities/extension.entity'
import { ExtensionToUser } from '../../marketplace/extensions/entities/extension-to-user.entity'
+import { Payout } from '../../payouts/entities/payouts.entity'
import { Comment } from '../../marketplace/comments/entities/comment.entity'
import { Complaint } from '../../marketplace/complaints/entities/complaint.entity'
import { RefreshToken } from './refresh-token.entity'
@@ -219,6 +220,20 @@ export class User {
@Column({ default: false })
showLiveVisitorsInTitle: boolean
+ @Column('varchar', { length: 8, default: null })
+ refCode: string | null
+
+ @Column('varchar', { default: null })
+ referrerID: string | null
+
+ @Column('varchar', {
+ length: 254,
+ unique: true,
+ default: null,
+ nullable: true,
+ })
+ paypalPaymentsEmail: string | null
+
@BeforeUpdate()
updateTimestamp() {
this.updated = new Date()
@@ -227,6 +242,9 @@ export class User {
@OneToMany(() => Project, project => project.admin)
projects: Project[]
+ @OneToMany(() => Payout, payout => payout.user)
+ payouts: Payout[]
+
@OneToMany(() => ProjectShare, sharedProjects => sharedProjects.user)
sharedProjects: ProjectShare[]
diff --git a/apps/production/src/user/user.controller.ts b/apps/production/src/user/user.controller.ts
index a545a33b9..7df23ed3b 100644
--- a/apps/production/src/user/user.controller.ts
+++ b/apps/production/src/user/user.controller.ts
@@ -15,6 +15,7 @@ import {
ConflictException,
Headers,
Ip,
+ Patch,
} from '@nestjs/common'
import { Request } from 'express'
import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
@@ -68,6 +69,7 @@ import {
checkRateLimit,
getGeoDetails,
getIPFromHeaders,
+ generateRefCode,
} from '../common/utils'
import { IUsageInfo, IMetaInfo } from './interfaces'
@@ -197,6 +199,40 @@ export class UserController {
return this.userService.update(userId, { receiveLoginNotifications })
}
+ @Patch('/set-paypal-email')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async setPaypalEmail(
+ @CurrentUserId() userId: string,
+ @Body('paypalPaymentsEmail') paypalPaymentsEmail: string,
+ @Headers() headers,
+ @Ip() reqIP,
+ ): Promise<User> {
+ this.logger.log(
+ { userId, paypalPaymentsEmail },
+ 'PATCH /user/set-paypal-email',
+ )
+
+ const ip = getIPFromHeaders(headers) || reqIP || ''
+
+ await checkRateLimit(ip, 'set-paypal-email', 10, 3600)
+
+ const user = await this.userService.findOne(userId)
+
+ if (!user) {
+ throw new BadRequestException('User not found')
+ }
+
+ await this.mailerService.sendEmail(
+ user.email,
+ LetterTemplate.PayPalEmailUpdate,
+ )
+
+ return this.userService.update(userId, {
+ paypalPaymentsEmail: paypalPaymentsEmail || null,
+ })
+ }
+
@Post('/api-key')
@UseGuards(JwtAccessTokenGuard, RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
@@ -605,6 +641,8 @@ export class UserController {
'registeredWithGithub',
'apiKey',
'emailRequests',
+ 'refCode',
+ 'referrerID',
])
await this.userService.update(id, userToUpdate)
@@ -627,6 +665,87 @@ export class UserController {
}
}
+ // @Get('payouts/list')
+ // @ApiQuery({ name: 'take', required: false })
+ // @ApiQuery({ name: 'skip', required: false })
+ // @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ // @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ // async getPayoutsList(
+ // @CurrentUserId() id: string,
+ // @Query('take') take: number | undefined,
+ // @Query('skip') skip: number | undefined,
+ // ): Promise<Pagination<Payout> | Payout[]> {
+ // this.logger.log({ id, take, skip }, 'GET /user/payouts/list')
+
+ // const user = await this.userService.findOneWhere({ id })
+
+ // if (!user) {
+ // throw new BadRequestException('User not found')
+ // }
+
+ // return this.userService.getPayoutsList(user)
+ // }
+
+ @Get('referrals')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async getReferralsList(@CurrentUserId() id: string): Promise<any> {
+ this.logger.log({ id }, 'GET /user/referrals')
+
+ const user = await this.userService.findOneWhere({ id })
+
+ if (!user) {
+ throw new BadRequestException('User not found')
+ }
+
+ return this.userService.getReferralsList(user)
+ }
+
+ @Get('payouts/info')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async getPayoutsInfo(@CurrentUserId() id: string): Promise<any> {
+ this.logger.log({ id }, 'GET /user/payouts/info')
+
+ const user = await this.userService.findOneWhere({ id })
+
+ if (!user) {
+ throw new BadRequestException('User not found')
+ }
+
+ return this.userService.getPayoutsInfo(user)
+ }
+
+ @Post('generate-ref-code')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async generateRefCode(@CurrentUserId() id: string): Promise<any> {
+ this.logger.log({ id }, 'POST /user/generate-ref-code')
+
+ const user = await this.userService.findOneWhere({ id })
+
+ if (!user) {
+ throw new BadRequestException('User not found')
+ }
+
+ if (user.refCode) {
+ throw new BadRequestException('Referral code already exists')
+ }
+
+ let refCode = generateRefCode()
+
+ // eslint-disable-next-line no-await-in-loop
+ while (!(await this.userService.isRefCodeUnique(refCode))) {
+ refCode = generateRefCode()
+ }
+
+ await this.userService.update(id, { refCode })
+
+ return {
+ refCode,
+ }
+ }
+
@Get('/export')
@UseGuards(JwtAccessTokenGuard, RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
diff --git a/apps/production/src/user/user.module.ts b/apps/production/src/user/user.module.ts
index faabf717c..2d99b822b 100644
--- a/apps/production/src/user/user.module.ts
+++ b/apps/production/src/user/user.module.ts
@@ -8,6 +8,7 @@ import { User } from './entities/user.entity'
import { ActionTokensModule } from '../action-tokens/action-tokens.module'
import { MailerModule } from '../mailer/mailer.module'
import { AuthModule } from '../auth/auth.module'
+import { PayoutsModule } from '../payouts/payouts.module'
import { AppLoggerModule } from '../logger/logger.module'
import { ProjectModule } from '../project/project.module'
import { RefreshToken } from './entities/refresh-token.entity'
@@ -22,6 +23,7 @@ import { Message } from '../integrations/telegram/entities/message.entity'
forwardRef(() => AuthModule),
AppLoggerModule,
ProjectModule,
+ PayoutsModule,
],
providers: [UserService, TelegramService],
exports: [UserService],
diff --git a/apps/production/src/user/user.service.ts b/apps/production/src/user/user.service.ts
index db1ebe52b..e0bdacdee 100644
--- a/apps/production/src/user/user.service.ts
+++ b/apps/production/src/user/user.service.ts
@@ -4,21 +4,25 @@ import {
InternalServerErrorException,
} from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
-import { Repository } from 'typeorm'
+import { In, Repository } from 'typeorm'
import axios from 'axios'
import * as dayjs from 'dayjs'
import * as _isEmpty from 'lodash/isEmpty'
import * as _size from 'lodash/size'
import * as _omit from 'lodash/omit'
import * as _isNull from 'lodash/isNull'
+import * as _toNumber from 'lodash/toNumber'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
+import { PayoutsService } from '../payouts/payouts.service'
import {
User,
ACCOUNT_PLANS,
TRIAL_DURATION,
BillingFrequency,
+ PlanCode,
} from './entities/user.entity'
+import { PayoutStatus } from '../payouts/entities/payouts.entity'
import { UserProfileDTO } from './dto/user.dto'
import { RefreshToken } from './entities/refresh-token.entity'
import { DeleteFeedback } from './entities/delete-feedback.entity'
@@ -90,6 +94,7 @@ export class UserService {
private readonly refreshTokenRepository: Repository<RefreshToken>,
@InjectRepository(DeleteFeedback)
private readonly deleteFeedbackRepository: Repository<DeleteFeedback>,
+ private readonly payoutsService: PayoutsService,
) {}
async create(
@@ -134,8 +139,8 @@ export class UserService {
return this.usersRepository.delete(id)
}
- async count(): Promise<number> {
- return this.usersRepository.count()
+ async count(options?: any): Promise<number> {
+ return this.usersRepository.count(options)
}
omitSensitiveData(user: Partial<User>): Partial<User> {
@@ -153,7 +158,7 @@ export class UserService {
return this.usersRepository.findOne({ where, relations })
}
- findOne(id: string, params: object = {}): Promise<User> {
+ findOne(id: string, params: any = {}): Promise<User> {
return this.usersRepository.findOne({
where: { id },
...params,
@@ -174,7 +179,7 @@ export class UserService {
})
}
- find(params: object): Promise<User[]> {
+ find(params: any): Promise<User[]> {
return this.usersRepository.find(params)
}
@@ -236,7 +241,9 @@ export class UserService {
return this.usersRepository.findOne({ email })
}
- public async createUser(user: Pick<User, 'email' | 'password'>) {
+ public async createUser(
+ user: Pick<User, 'email' | 'password' | 'referrerID'>,
+ ) {
return this.usersRepository.save({
...user,
trialEndDate: dayjs
@@ -495,4 +502,87 @@ export class UserService {
await this.update(id, updateParams)
}
+
+ async getPayoutsList(user: User, take = 20, skip = 0) {
+ return this.payoutsService.paginate(
+ {
+ take,
+ skip,
+ },
+ {
+ user: user.id,
+ },
+ )
+ }
+
+ async getReferralsList(user: User): Promise<Partial<User>[]> {
+ return this.usersRepository
+ .createQueryBuilder('user')
+ .select([
+ 'user.planCode',
+ 'user.created',
+ 'user.billingFrequency',
+ 'user.tierCurrency',
+ ])
+ .where('user.referrerID = :id', { id: user.id })
+ .andWhere('user.planCode != :none', { none: PlanCode.none })
+ .andWhere('user.planCode != :trial', { trial: PlanCode.trial })
+ .andWhere('user.planCode != :free', { free: PlanCode.free })
+ .orderBy('user.created', 'DESC')
+ .getMany()
+ }
+
+ async getPayoutsInfo(user: User): Promise<any> {
+ const { id } = user
+
+ const trials = await this.count({
+ where: {
+ referrerID: id,
+ planCode: In([PlanCode.trial, PlanCode.none]),
+ },
+ })
+
+ const subscribers = _toNumber(
+ (
+ await this.usersRepository
+ .createQueryBuilder('user')
+ .select('COUNT(user.id)', 'count')
+ .where('user.referrerID = :id', { id })
+ .andWhere('user.planCode != :none', { none: PlanCode.none })
+ .andWhere('user.planCode != :trial', { trial: PlanCode.trial })
+ .getRawOne()
+ )?.count,
+ )
+
+ let paid = await this.payoutsService.sumAmountByReferrerId(
+ id,
+ PayoutStatus.paid,
+ )
+ let nextPayout = await this.payoutsService.sumAmountByReferrerId(
+ id,
+ PayoutStatus.processing,
+ )
+ let pending = await this.payoutsService.sumAmountByReferrerId(
+ id,
+ PayoutStatus.pending,
+ )
+
+ paid = paid ?? 0
+ nextPayout = nextPayout ?? 0
+ pending = pending ?? 0
+
+ return {
+ trials,
+ subscribers,
+ paid,
+ nextPayout,
+ pending,
+ }
+ }
+
+ async isRefCodeUnique(code: string): Promise<boolean> {
+ const user = await this.findOneWhere({ refCode: code })
+
+ return !user
+ }
}
diff --git a/apps/production/src/webhook/webhook.controller.ts b/apps/production/src/webhook/webhook.controller.ts
index e8de6e016..107d46322 100644
--- a/apps/production/src/webhook/webhook.controller.ts
+++ b/apps/production/src/webhook/webhook.controller.ts
@@ -137,6 +137,57 @@ export class WebhookController {
// break
// }
+ case 'subscription_payment_succeeded': {
+ const {
+ subscription_id: subID,
+ balance_earnings: balanceEarnings,
+ balance_currency: balanceCurrency,
+ } = body
+
+ const subscriber = await this.userService.findOneWhere({ subID })
+
+ if (!subscriber) {
+ this.logger.error(
+ `[subscription_payment_succeeded] Cannot find the subscriber with subID: ${subID}\nBody: ${JSON.stringify(
+ body,
+ null,
+ 2,
+ )}`,
+ )
+ return
+ }
+
+ // That user was not referred by anyone
+ if (!subscriber.referrerID) {
+ return
+ }
+
+ const referrer = await this.userService.findOneWhere({
+ id: subscriber.referrerID,
+ })
+
+ if (!referrer) {
+ this.logger.error(
+ `[subscription_payment_succeeded] Cannot find the referrer with ID: ${subscriber.referrerID}`,
+ )
+ return
+ }
+
+ await this.webhookService.setReferralPayoutsToProcessing(
+ subscriber.id,
+ referrer,
+ )
+
+ await this.webhookService.addPayoutForUser(
+ referrer,
+ subscriber.id,
+ Number(balanceEarnings),
+ balanceCurrency,
+ )
+
+ break
+ }
+
case 'subscription_payment_failed': {
const { subscription_id: subID, attempt_number: attemptNumber } = body
diff --git a/apps/production/src/webhook/webhook.module.ts b/apps/production/src/webhook/webhook.module.ts
index ea4dd3df8..7e982b8f2 100644
--- a/apps/production/src/webhook/webhook.module.ts
+++ b/apps/production/src/webhook/webhook.module.ts
@@ -1,13 +1,19 @@
import { Module, forwardRef } from '@nestjs/common'
-import { ProjectModule } from 'src/project/project.module'
+import { ProjectModule } from '../project/project.module'
+import { PayoutsModule } from '../payouts/payouts.module'
import { WebhookController } from './webhook.controller'
import { UserModule } from '../user/user.module'
import { AppLoggerModule } from '../logger/logger.module'
import { WebhookService } from './webhook.service'
@Module({
- imports: [forwardRef(() => UserModule), ProjectModule, AppLoggerModule],
+ imports: [
+ forwardRef(() => UserModule),
+ ProjectModule,
+ PayoutsModule,
+ AppLoggerModule,
+ ],
providers: [WebhookService],
exports: [WebhookService],
controllers: [WebhookController],
diff --git a/apps/production/src/webhook/webhook.service.ts b/apps/production/src/webhook/webhook.service.ts
index 8b8eef255..747b4c226 100644
--- a/apps/production/src/webhook/webhook.service.ts
+++ b/apps/production/src/webhook/webhook.service.ts
@@ -10,6 +10,9 @@ import * as _includes from 'lodash/includes'
import * as _keys from 'lodash/keys'
import * as _isArray from 'lodash/isArray'
import { AppLoggerService } from '../logger/logger.service'
+import { PayoutsService } from '../payouts/payouts.service'
+import { PayoutStatus } from '../payouts/entities/payouts.entity'
+import { User } from '../user/entities/user.entity'
const PADDLE_PUB_KEY = `-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqFcHslKkXcJlTYg4FL6j
@@ -39,7 +42,10 @@ const paddleWhitelistIPs = [
@Injectable()
export class WebhookService {
- constructor(private readonly logger: AppLoggerService) {}
+ constructor(
+ private readonly logger: AppLoggerService,
+ private readonly payoutsService: PayoutsService,
+ ) {}
public ksort(obj: Record<string, unknown>): Record<string, unknown> {
const keys = _keys(obj).sort()
@@ -90,4 +96,33 @@ export class WebhookService {
throw new ForbiddenException('You have no access to this endpoint')
}
}
+
+ async addPayoutForUser(
+ referrer: User,
+ referralId: string,
+ amount: number,
+ currency: string,
+ ): Promise<any> {
+ return this.payoutsService.create({
+ amount,
+ currency,
+ referralId,
+ user: referrer,
+ })
+ }
+
+ async setReferralPayoutsToProcessing(
+ referralId: string,
+ referrer: User,
+ ): Promise<any> {
+ return this.payoutsService.update(
+ {
+ referralId,
+ user: referrer,
+ },
+ {
+ status: PayoutStatus.processing,
+ },
+ )
+ }
}
diff --git a/migrations/mysql/2023_10_01.sql b/migrations/mysql/2023_10_01.sql
new file mode 100644
index 000000000..f854ecd9e
--- /dev/null
+++ b/migrations/mysql/2023_10_01.sql
@@ -0,0 +1,16 @@
+CREATE TABLE IF NOT EXISTS `payout` (
+ `id` varchar(36) NOT NULL,
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ `amount` decimal(10,0) NOT NULL,
+ `currency` varchar(3) NOT NULL,
+ `referralId` varchar(36) NOT NULL,
+ `status` enum('processing','pending','paid','suspended') DEFAULT 'pending',
+ `paidAt` timestamp NULL DEFAULT NULL,
+ `transactionId` varchar(50) DEFAULT NULL,
+ `userId` varchar(36) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+alter table user add column `referrerID` varchar(255) DEFAULT NULL after `receiveLoginNotifications`;
+alter table user add column `paypalPaymentsEmail` varchar(254) DEFAULT NULL after `referrerID`;
+alter table user add column `refCode` varchar(8) DEFAULT NULL after `paypalPaymentsEmail`;
diff --git a/package.json b/package.json
index 78aca42b5..1ec307f2b 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"@nestjs/schedule": "^3.0.3",
"@nestjs/swagger": "^7.1.10",
"@nestjs/typeorm": "^10.0.0",
+ "@paypal/payouts-sdk": "^1.1.1",
"@sentry/node": "^7.67.0",
"axios": "^1.5.0",
"bcrypt": "^5.1.1",