Skip to content

Commit

Permalink
feat: possibility to create connected accounts with recipient type (#204
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vackca authored Apr 29, 2024
1 parent 84e83b5 commit 360ae2c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
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

0 comments on commit 360ae2c

Please sign in to comment.