Skip to content

Commit

Permalink
Merge pull request #68 from saleor/feat/add-events-to-report
Browse files Browse the repository at this point in the history
feat: add events to report
  • Loading branch information
peelar committed Jan 17, 2024
2 parents 60c9fa4 + 391632c commit a8a9458
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-spoons-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-app-authorize-net": minor
---

Added support for priorAuthCapture.created and void.created Authorize events.
5 changes: 5 additions & 0 deletions .changeset/strange-brooms-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-app-authorize-net": patch
---

Fixed the missing argument TypeScript issue in `webhook-manager-service.ts`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getAuthorizeConfig, type AuthorizeConfig } from "../authorize-net-confi
import { AuthorizeNetInvalidWebhookSignatureError } from "../authorize-net-error";
import { authorizeNetEventSchema } from "./authorize-net-webhook-client";
import { MissingAuthDataError } from "./authorize-net-webhook-errors";
import { AuthorizeNetWebhookTransactionSynchronizer } from "./authorize-net-webhook-transaction-synchronizer";
import { TransactionEventReporter } from "./transaction-reporter";
import { saleorApp } from "@/saleor-app";
import { createLogger } from "@/lib/logger";
import { createServerClient } from "@/lib/create-graphq-client";
Expand Down Expand Up @@ -109,20 +109,20 @@ export class AuthorizeNetWebhookHandler {

private async processAuthorizeWebhook(eventPayload: EventPayload) {
const authData = await this.getAuthData();

const client = createServerClient(authData.saleorApiUrl, authData.token);

const synchronizer = new AuthorizeNetWebhookTransactionSynchronizer({
const reporter = new TransactionEventReporter({
client,
});
return synchronizer.synchronizeTransaction(eventPayload);

return reporter.reportEvent(eventPayload);
}

async handle() {
await this.verifyWebhook();
const eventPayload = await this.parseWebhookBody();
await this.processAuthorizeWebhook(eventPayload);

this.logger.info("Finished processing webhook");
this.logger.debug("Finished processing webhook");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,45 @@ import {
type TransactionEventReportMutation,
type TransactionEventReportMutationVariables,
} from "generated/graphql";
import { createLogger } from "@/lib/logger";

const TransactionEventReportMutationError = AuthorizeNetError.subclass(
"TransactionEventReportMutationError",
);

const TransactionEventReportUnsupportedTypeError = AuthorizeNetError.subclass(
"TransactionEventReportUnsupportedTypeError",
);

/**
* @description This class is used to synchronize Authorize.net transactions with Saleor transactions
*/
export class AuthorizeNetWebhookTransactionSynchronizer {
export class TransactionEventReporter {
private client: Client;
private logger = createLogger({
name: "TransactionEventReporter",
});

constructor({ client }: { client: Client }) {
this.client = client;
}

private mapEventType(authorizeTransactionType: AuthorizeNetEvent): TransactionEventTypeEnum {
this.logger.debug({ authorizeTransactionType }, "Mapping Authorize transaction event type");
switch (authorizeTransactionType) {
// todo:
case "net.authorize.payment.priorAuthCapture.created":
return TransactionEventTypeEnum.ChargeSuccess;
case "net.authorize.payment.void.created":
return TransactionEventTypeEnum.ChargeFailure;
default:
return TransactionEventTypeEnum.AuthorizationActionRequired;
throw new TransactionEventReportUnsupportedTypeError(
`${authorizeTransactionType} is not a supported transaction event type`,
);
}
}

private async transactionEventReport(variables: TransactionEventReportMutationVariables) {
const { error: mutationError } = await this.client
const { data, error: mutationError } = await this.client
.mutation<TransactionEventReportMutation>(TransactionEventReportDocument, variables)
.toPromise();

Expand All @@ -44,14 +58,16 @@ export class AuthorizeNetWebhookTransactionSynchronizer {
{ cause: mutationError.message },
);
}

this.logger.trace({ data }, "Transaction event response");
}

private getAuthorizeTransaction({ id }: { id: string }) {
const transactionDetailsClient = new TransactionDetailsClient();
return transactionDetailsClient.getTransactionDetailsRequest({ transactionId: id });
}

async synchronizeTransaction(eventPayload: EventPayload) {
async reportEvent(eventPayload: EventPayload) {
const id = eventPayload.payload.id;
const authorizeTransaction = await this.getAuthorizeTransaction({ id });

Expand All @@ -61,13 +77,19 @@ export class AuthorizeNetWebhookTransactionSynchronizer {

const type = this.mapEventType(eventPayload.eventType);

await this.transactionEventReport({
const eventReportPayload = {
amount: authorizeTransaction.transaction.authAmount,
availableActions: [],
pspReference: authorizeTransactionId,
time: authorizeTransaction.transaction.submitTimeLocal,
time: eventPayload.eventDate,
transactionId: saleorTransactionId,
type,
});
};

this.logger.debug({ eventReportPayload }, "Reporting transaction event");

await this.transactionEventReport(eventReportPayload);

this.logger.info("Successfully synchronized Saleor transaction with Authorize transaction");
}
}
1 change: 1 addition & 0 deletions src/modules/webhooks/transaction-process-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class TransactionProcessSessionService {
saleorTransactionId: string;
authorizeTransactionId: string;
}) {
// todo: do I even need it? or can I read transactionid from pspreference?
const metadataManager = new TransactionMetadataManager({ apiClient: this.apiClient });

await metadataManager.saveTransactionId({
Expand Down
4 changes: 1 addition & 3 deletions src/modules/webhooks/webhook-manager-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export class AppWebhookManager implements PaymentsWebhooks {
async transactionCancelationRequested(
payload: TransactionCancelationRequestedEventFragment,
): Promise<TransactionCancelationRequestedResponse> {
const service = new TransactionCancelationRequestedService({
apiClient: this.apiClient,
});
const service = new TransactionCancelationRequestedService();

return service.execute(payload);
}
Expand Down

0 comments on commit a8a9458

Please sign in to comment.