Skip to content

feat: possibility to create connected accounts with recipient type #204

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

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,22 @@
"out": {
"admin": "allow"
}
},
"country": {
"in": {
"admin": "allow"
},
"out": {
"admin": "allow"
}
},
"serviceAgreement": {
"in": {
"admin": "allow"
},
"out": {
"admin": "allow"
}
}
},
"createdAt": "2023-05-26T13:01:39.186Z"
Expand Down
9 changes: 9 additions & 0 deletions microservices/payment-stripe/src/constants/agreement-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Agreement type
*/
enum AgreementType {
FULL = 'full',
RECIPIENT = 'recipient',
}

export default AgreementType;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Endpoint, IsUndefinable } from '@lomray/microservice-helpers';
import { IsEnum, IsString, Length } from 'class-validator';
import AgreementType from '@constants/agreement-type';
import BusinessType from '@constants/business-type';
import StripeAccountTypes from '@constants/stripe-account-types';
import Stripe from '@services/payment-gateway/stripe';
Expand All @@ -24,6 +25,14 @@ class ConnectAccountInput {
@IsEnum(BusinessType)
@IsUndefinable()
businessType?: BusinessType;

@IsString()
@IsUndefinable()
country?: string;

@IsEnum(AgreementType)
@IsUndefinable()
serviceAgreement?: AgreementType;
}

class ConnectAccountOutput {
Expand All @@ -40,7 +49,16 @@ const connectAccount = Endpoint.custom(
output: ConnectAccountOutput,
description: 'Create new connected account with link',
}),
async ({ userId, email, accountType, refreshUrl, returnUrl, businessType }) => {
async ({
userId,
email,
accountType,
refreshUrl,
returnUrl,
businessType,
country,
serviceAgreement,
}) => {
const service = await Stripe.init();

return {
Expand All @@ -51,6 +69,8 @@ const connectAccount = Endpoint.custom(
refreshUrl,
returnUrl,
businessType,
country,
serviceAgreement,
),
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { validate } from 'class-validator';
import StripeSdk from 'stripe';
import { EntityManager, getManager } from 'typeorm';
import remoteConfig from '@config/remote';
import AgreementType from '@constants/agreement-type';
import BalanceType from '@constants/balance-type';
import BusinessType from '@constants/business-type';
import CouponDuration from '@constants/coupon-duration';
Expand Down Expand Up @@ -536,20 +537,37 @@ class Stripe extends Abstract {
refreshUrl: string,
returnUrl: string,
businessType?: BusinessType,
country?: string,
serviceAgreement?: string,
): Promise<string> {
const customer = await super.getCustomer(userId);
const isRecipient = serviceAgreement === AgreementType.RECIPIENT;

if (!customer.params.accountId) {
const accountCapabilities = isRecipient
? {
capabilities: {
transfers: {
requested: true,
},
},
}
: {};

const stripeConnectAccount: StripeSdk.Account = await this.sdk.accounts.create({
type: accountType,
country: 'US',
country,
...accountCapabilities,
// eslint-disable-next-line camelcase
...(serviceAgreement ? { tos_acceptance: { service_agreement: serviceAgreement } } : {}),
email,
// eslint-disable-next-line camelcase
...(businessType ? { business_type: businessType } : {}),
settings: {
payouts: {
// Recipient accounts cannot have debit_negative_balances = true
// eslint-disable-next-line camelcase
debit_negative_balances: true,
debit_negative_balances: !isRecipient,
// eslint-disable-next-line camelcase
schedule: { interval: 'manual' },
},
Expand Down