diff --git a/microservices/authorization/migrations/permissions/list/models/payment-stripe.json b/microservices/authorization/migrations/permissions/list/models/payment-stripe.json index 527ddf37..e89f41d8 100644 --- a/microservices/authorization/migrations/permissions/list/models/payment-stripe.json +++ b/microservices/authorization/migrations/permissions/list/models/payment-stripe.json @@ -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" diff --git a/microservices/payment-stripe/src/constants/agreement-type.ts b/microservices/payment-stripe/src/constants/agreement-type.ts new file mode 100644 index 00000000..1eb818f8 --- /dev/null +++ b/microservices/payment-stripe/src/constants/agreement-type.ts @@ -0,0 +1,9 @@ +/** + * Agreement type + */ +enum AgreementType { + FULL = 'full', + RECIPIENT = 'recipient', +} + +export default AgreementType; diff --git a/microservices/payment-stripe/src/methods/stripe/connect-account.ts b/microservices/payment-stripe/src/methods/stripe/connect-account.ts index a1ff4a47..e63cc2d1 100644 --- a/microservices/payment-stripe/src/methods/stripe/connect-account.ts +++ b/microservices/payment-stripe/src/methods/stripe/connect-account.ts @@ -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'; @@ -24,6 +25,14 @@ class ConnectAccountInput { @IsEnum(BusinessType) @IsUndefinable() businessType?: BusinessType; + + @IsString() + @IsUndefinable() + country?: string; + + @IsEnum(AgreementType) + @IsUndefinable() + serviceAgreement?: AgreementType; } class ConnectAccountOutput { @@ -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 { @@ -51,6 +69,8 @@ const connectAccount = Endpoint.custom( refreshUrl, returnUrl, businessType, + country, + serviceAgreement, ), }; }, diff --git a/microservices/payment-stripe/src/services/payment-gateway/stripe.ts b/microservices/payment-stripe/src/services/payment-gateway/stripe.ts index 330f5154..c8d54ff2 100644 --- a/microservices/payment-stripe/src/services/payment-gateway/stripe.ts +++ b/microservices/payment-stripe/src/services/payment-gateway/stripe.ts @@ -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'; @@ -536,20 +537,37 @@ class Stripe extends Abstract { refreshUrl: string, returnUrl: string, businessType?: BusinessType, + country?: string, + serviceAgreement?: string, ): Promise { 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' }, },