diff --git a/packages/backend/src/config/app.ts b/packages/backend/src/config/app.ts index b01aaaa088..29a3c7070a 100644 --- a/packages/backend/src/config/app.ts +++ b/packages/backend/src/config/app.ts @@ -132,10 +132,19 @@ export const Config = { incomingPaymentWorkers: envInt('INCOMING_PAYMENT_WORKERS', 1), incomingPaymentWorkerIdle: envInt('INCOMING_PAYMENT_WORKER_IDLE', 200), // milliseconds - pollIncomingPaymentCreatedWebhook: envBool('POLL_INCOMING_PAYMENT_CREATED_WEBHOOK', false), - incomingPaymentCreatedPollTimeout: envInt('INCOMING_PAYMENT_CREATED_POLL_TIMEOUT_MS', 10000), // milliseconds - incomingPaymentCreatedPollFrequency: envInt('INCOMING_PAYMENT_CREATED_POLL_FREQUENCY_MS', 1000), // milliseconds - + pollIncomingPaymentCreatedWebhook: envBool( + 'POLL_INCOMING_PAYMENT_CREATED_WEBHOOK', + false + ), + incomingPaymentCreatedPollTimeout: envInt( + 'INCOMING_PAYMENT_CREATED_POLL_TIMEOUT_MS', + 10000 + ), // milliseconds + incomingPaymentCreatedPollFrequency: envInt( + 'INCOMING_PAYMENT_CREATED_POLL_FREQUENCY_MS', + 1000 + ), // milliseconds + webhookWorkers: envInt('WEBHOOK_WORKERS', 1), webhookWorkerIdle: envInt('WEBHOOK_WORKER_IDLE', 200), // milliseconds webhookUrl: envString('WEBHOOK_URL'), diff --git a/packages/backend/src/open_payments/payment/incoming/errors.ts b/packages/backend/src/open_payments/payment/incoming/errors.ts index e9f520a3bd..76a79c8b44 100644 --- a/packages/backend/src/open_payments/payment/incoming/errors.ts +++ b/packages/backend/src/open_payments/payment/incoming/errors.ts @@ -25,7 +25,7 @@ export const errorToHTTPCode: { [IncomingPaymentError.InvalidExpiry]: 400, [IncomingPaymentError.WrongState]: 409, [IncomingPaymentError.InactiveWalletAddress]: 400, - [IncomingPaymentError.ActionNotPerformed]: 400 + [IncomingPaymentError.ActionNotPerformed]: 403 } export const errorToCode: { @@ -38,7 +38,7 @@ export const errorToCode: { [IncomingPaymentError.InvalidExpiry]: GraphQLErrorCode.BadUserInput, [IncomingPaymentError.WrongState]: GraphQLErrorCode.Conflict, [IncomingPaymentError.InactiveWalletAddress]: GraphQLErrorCode.Inactive, - [IncomingPaymentError.ActionNotPerformed]: GraphQLErrorCode.Timeout, + [IncomingPaymentError.ActionNotPerformed]: GraphQLErrorCode.Timeout } export const errorToMessage: { diff --git a/packages/backend/src/open_payments/payment/incoming/service.ts b/packages/backend/src/open_payments/payment/incoming/service.ts index 76006fd3b3..0b19676d04 100644 --- a/packages/backend/src/open_payments/payment/incoming/service.ts +++ b/packages/backend/src/open_payments/payment/incoming/service.ts @@ -121,7 +121,7 @@ async function createIncomingPayment( return IncomingPaymentError.InvalidAmount } } - + let incomingPayment = await IncomingPayment.query(trx || deps.knex) .insertAndFetch({ walletAddressId: walletAddressId, @@ -148,27 +148,28 @@ async function createIncomingPayment( try { const response = await poll({ - request: async () => getApprovedOrCanceledIncomingPayment(deps, { id: incomingPayment.id }), + request: async () => + getApprovedOrCanceledIncomingPayment(deps, { id: incomingPayment.id }), pollingFrequencyMs: deps.config.incomingPaymentCreatedPollFrequency, timeoutMs: deps.config.incomingPaymentCreatedPollTimeout }) if (response) return response return IncomingPaymentError.ActionNotPerformed - }catch(err) { + } catch (err) { return IncomingPaymentError.ActionNotPerformed } - } -async function getApprovedOrCanceledIncomingPayment(deps: ServiceDependencies, options: GetOptions) { - const incomingPayment = await getIncomingPayment(deps, { id: options.id }) - - if (incomingPayment?.approvedAt || incomingPayment?.cancelledAt) { - return incomingPayment - } - - return undefined +async function getApprovedOrCanceledIncomingPayment( + deps: ServiceDependencies, + options: GetOptions +) { + return IncomingPayment.query(deps.knex) + .get(options) + .withGraphFetched('[asset, walletAddress]') + .whereNotNull('approvedAt') + .orWhereNotNull('cancelledAt') } // Fetch (and lock) an incoming payment for work.