Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 2 additions & 0 deletions localenv/mock-account-servicing-entity/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('webhooks', function (table) {
table.jsonb('metadata').nullable()
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('webhooks', function (table) {
table.dropColumn('metadata')
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema
.alterTable('incomingPayments', function (table) {
table.enum('initiatedBy', ['CARD', 'OPEN_PAYMENTS', 'ADMIN'])
})
.then(() => {
return Promise.all([
knex.raw(
`UPDATE "incomingPayments" SET "initiatedBy" = 'OPEN_PAYMENTS' WHERE "client" IS NOT NULL`
),
knex.raw(
`UPDATE "incomingPayments" SET "initiatedBy" = 'ADMIN' WHERE "client" IS NULL`
)
])
})
.then(() => {
return knex.raw(
`ALTER TABLE "incomingPayments" ALTER COLUMN "initiatedBy" SET NOT NULL`
)
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('incomingPayments', function (table) {
table.dropColumn('initiatedBy')
})
}
15 changes: 14 additions & 1 deletion packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,20 @@ export const Config = {
'SEND_TENANT_WEBHOOKS_TO_OPERATOR',
false
),
cardServiceUrl: process.env.CARD_SERVICE_URL
cardServiceUrl: optional(envString, 'CARD_SERVICE_URL'),
posServiceUrl: optional(envString, 'POS_SERVICE_URL')
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function optional<T extends (...args: any[]) => ReturnType<T>>(
envGetter: T,
envVar: string
): ReturnType<T> | undefined {
try {
return envGetter(envVar)
} catch (err) {
return undefined
}
}

function parseRedisTlsConfig(
Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/graphql/generated/graphql.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/backend/src/graphql/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '../../open_payments/payment/combined/model'
import { getPageTests } from './page.test'
import { createTenant } from '../../tests/tenant'
import { IncomingPaymentInitiationReason } from '../../open_payments/payment/incoming/model'

describe('Payment', (): void => {
let deps: IocContract<AppServices>
Expand Down Expand Up @@ -84,7 +85,8 @@ describe('Payment', (): void => {
const incomingPayment = await createIncomingPayment(deps, {
walletAddressId: inWalletAddressId,
client: client,
tenantId: Config.operatorTenantId
tenantId: Config.operatorTenantId,
initiationReason: IncomingPaymentInitiationReason.OpenPayments
})

const query = await appContainer.apolloClient
Expand Down
152 changes: 144 additions & 8 deletions packages/backend/src/graphql/resolvers/incoming_payment.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ApolloError, gql } from '@apollo/client'
import { getPageTests } from './page.test'
import { createTestApp, TestContainer } from '../../tests/app'
import {
createApolloClient,
createTestApp,
TestContainer
} from '../../tests/app'
import { IocContract } from '@adonisjs/fold'
import { AppServices } from '../../app'
import { initIocContainer } from '../..'
Expand All @@ -14,6 +18,7 @@ import { v4 as uuid } from 'uuid'
import { IncomingPaymentService } from '../../open_payments/payment/incoming/service'
import { AccountingService } from '../../accounting/service'
import {
IncomingPaymentInitiationReason,
IncomingPayment as IncomingPaymentModel,
IncomingPaymentState
} from '../../open_payments/payment/incoming/model'
Expand All @@ -29,6 +34,7 @@ import {
} from '../../open_payments/payment/incoming/errors'
import { Amount, serializeAmount } from '../../open_payments/amount'
import { GraphQLErrorCode } from '../errors'
import { createTenant } from '../../tests/tenant'

describe('Incoming Payment Resolver', (): void => {
let deps: IocContract<AppServices>
Expand Down Expand Up @@ -81,7 +87,8 @@ describe('Incoming Payment Resolver', (): void => {
description: `IncomingPayment`,
externalRef: '#123'
},
tenantId
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
}),
pagedQuery: 'incomingPayments',
parent: {
Expand Down Expand Up @@ -122,7 +129,8 @@ describe('Incoming Payment Resolver', (): void => {
metadata,
expiresAt,
incomingAmount,
tenantId
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})

const createSpy = jest
Expand Down Expand Up @@ -171,7 +179,11 @@ describe('Incoming Payment Resolver', (): void => {
query.data?.createIncomingPayment
)

expect(createSpy).toHaveBeenCalledWith({ ...input, tenantId })
expect(createSpy).toHaveBeenCalledWith({
...input,
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})
expect(query).toEqual({
__typename: 'IncomingPaymentResponse',
payment: {
Expand Down Expand Up @@ -241,7 +253,11 @@ describe('Incoming Payment Resolver', (): void => {
})
)
}
expect(createSpy).toHaveBeenCalledWith({ ...input, tenantId })
expect(createSpy).toHaveBeenCalledWith({
...input,
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})
})

test('Internal server error', async (): Promise<void> => {
Expand Down Expand Up @@ -288,7 +304,125 @@ describe('Incoming Payment Resolver', (): void => {
}
expect(createSpy).toHaveBeenCalledWith({
...input,
tenantId
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})
})

describe('tenant boundaries', (): void => {
test('operator can label incoming payment as card payment', async (): Promise<void> => {
const payment = await createIncomingPayment(deps, {
walletAddressId,
client,
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})

const createSpy = jest
.spyOn(incomingPaymentService, 'create')
.mockResolvedValueOnce(payment)

const input = {
walletAddressId,
isCardPayment: true
}

const query = await appContainer.apolloClient
.query({
query: gql`
mutation CreateIncomingPayment(
$input: CreateIncomingPaymentInput!
) {
createIncomingPayment(input: $input) {
payment {
id
walletAddressId
}
}
}
`,
variables: { input }
})
.then(
(query): IncomingPaymentResponse =>
query.data?.createIncomingPayment
)

expect(createSpy).toHaveBeenCalledWith({
walletAddressId: input.walletAddressId,
tenantId,
initiationReason: IncomingPaymentInitiationReason.Card
})
expect(query).toEqual({
__typename: 'IncomingPaymentResponse',
payment: {
__typename: 'IncomingPayment',
id: payment.id,
walletAddressId
}
})
})

test('tenant cannot label incoming payment as card payment', async (): Promise<void> => {
const tenant = await createTenant(deps)
const tenantWalletAddress = await createWalletAddress(deps, {
tenantId: tenant.id
})
const payment = await createIncomingPayment(deps, {
walletAddressId: tenantWalletAddress.id,
client,
tenantId: tenant.id,
initiationReason: IncomingPaymentInitiationReason.Admin
})

const createSpy = jest
.spyOn(incomingPaymentService, 'create')
.mockResolvedValueOnce(payment)

const input = {
walletAddressId,
isCardPayment: true
}

const tenantedApolloClient = await createApolloClient(
appContainer.container,
appContainer.app,
tenant.id
)
const query = await tenantedApolloClient
.query({
query: gql`
mutation CreateIncomingPayment(
$input: CreateIncomingPaymentInput!
) {
createIncomingPayment(input: $input) {
payment {
id
walletAddressId
}
}
}
`,
variables: { input }
})
.then(
(query): IncomingPaymentResponse =>
query.data?.createIncomingPayment
)

expect(createSpy).toHaveBeenCalledWith({
walletAddressId: input.walletAddressId,
tenantId: tenant.id,
initiationReason: IncomingPaymentInitiationReason.Admin
})
expect(query).toEqual({
__typename: 'IncomingPaymentResponse',
payment: {
__typename: 'IncomingPayment',
id: payment.id,
walletAddressId: tenantWalletAddress.id
}
})
})
})
})
Expand All @@ -307,7 +441,8 @@ describe('Incoming Payment Resolver', (): void => {
assetCode: asset.code,
assetScale: asset.scale
},
tenantId
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})
}

Expand Down Expand Up @@ -484,7 +619,8 @@ describe('Incoming Payment Resolver', (): void => {
metadata,
expiresAt,
incomingAmount,
tenantId
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})
const input = {
id: payment.id,
Expand Down
18 changes: 16 additions & 2 deletions packages/backend/src/graphql/resolvers/incoming_payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
IncomingPayment as SchemaIncomingPayment,
QueryResolvers
} from '../generated/graphql'
import { IncomingPayment } from '../../open_payments/payment/incoming/model'
import {
IncomingPayment,
IncomingPaymentInitiationReason
} from '../../open_payments/payment/incoming/model'
import {
isIncomingPaymentError,
errorToCode,
Expand Down Expand Up @@ -97,14 +100,25 @@ export const createIncomingPayment: MutationResolvers<ForTenantIdContext>['creat
throw new Error('Missing tenant id to create incoming payment')
}

if (!ctx.isOperator && args.input.isCardPayment) {
ctx.logger.warn(
{ input: args.input, tenant: ctx.tenant },
'non-operator cannot create card payment'
)
}

const incomingPaymentOrError = await incomingPaymentService.create({
walletAddressId: args.input.walletAddressId,
expiresAt: !args.input.expiresAt
? undefined
: new Date(args.input.expiresAt),
incomingAmount: args.input.incomingAmount,
metadata: args.input.metadata,
tenantId
tenantId,
initiationReason:
ctx.isOperator && args.input.isCardPayment
? IncomingPaymentInitiationReason.Card
: IncomingPaymentInitiationReason.Admin
})
if (isIncomingPaymentError(incomingPaymentOrError)) {
throw new GraphQLError(errorToMessage[incomingPaymentOrError], {
Expand Down
Loading
Loading