Skip to content

Commit

Permalink
add basic setup for ListStoredPaymentMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
peelar committed Apr 18, 2024
1 parent 09aaeb8 commit bbe6f8d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
10 changes: 10 additions & 0 deletions graphql/fragments/ListStoredPaymentMethodsEvent.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fragment ListStoredPaymentMethodsEvent on ListStoredPaymentMethods {
__typename
issuingPrincipal {
__typename
... on App {
id
name
}
}
}
5 changes: 5 additions & 0 deletions graphql/subscriptions/ListStoredPaymentMethods.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
subscription ListStoredPaymentMethods {
event {
...ListStoredPaymentMethodsEvent
}
}
17 changes: 17 additions & 0 deletions src/modules/webhooks/list-stored-payment-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type ListStoredPaymentMethodsEventFragment } from "generated/graphql";

import { createLogger } from "@/lib/logger";
import { type ListStoredPaymentMethodsResponse } from "@/schemas/ListStoredPaymentMethods/ListStoredPaymentMethodsResponse.mjs";

export class ListStoredPaymentMethodsService {
private logger = createLogger({
name: "ListStoredPaymentMethodsService",
});

async execute(
_payload: ListStoredPaymentMethodsEventFragment,
): Promise<ListStoredPaymentMethodsResponse> {
// TODO
return {};
}
}
14 changes: 14 additions & 0 deletions src/modules/webhooks/webhook-manager-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import { TransactionRefundRequestedService } from "./transaction-refund-requeste

import { PaymentGatewayInitializeSessionService } from "./payment-gateway-initialize-session";
import { TransactionInitializeSessionService } from "./transaction-initialize-session";
import { ListStoredPaymentMethodsService } from "./list-stored-payment-methods";
import { createServerClient } from "@/lib/create-graphq-client";
import { type PaymentGatewayInitializeSessionResponse } from "@/pages/api/webhooks/payment-gateway-initialize-session";
import { type TransactionCancelationRequestedResponse } from "@/schemas/TransactionCancelationRequested/TransactionCancelationRequestedResponse.mjs";
import { type TransactionInitializeSessionResponse } from "@/schemas/TransactionInitializeSession/TransactionInitializeSessionResponse.mjs";
import { type TransactionProcessSessionResponse } from "@/schemas/TransactionProcessSession/TransactionProcessSessionResponse.mjs";
import { type TransactionRefundRequestedResponse } from "@/schemas/TransactionRefundRequested/TransactionRefundRequestedResponse.mjs";
import {
type ListStoredPaymentMethodsEventFragment,
type PaymentGatewayInitializeSessionEventFragment,
type TransactionCancelationRequestedEventFragment,
type TransactionInitializeSessionEventFragment,
type TransactionProcessSessionEventFragment,
type TransactionRefundRequestedEventFragment,
} from "generated/graphql";
import { type ListStoredPaymentMethodsResponse } from "@/schemas/ListStoredPaymentMethods/ListStoredPaymentMethodsResponse.mjs";

export interface PaymentsWebhooks {
transactionInitializeSession: (
Expand All @@ -32,6 +35,9 @@ export interface PaymentsWebhooks {
transactionCancelationRequested: (
payload: TransactionCancelationRequestedEventFragment,
) => Promise<TransactionCancelationRequestedResponse>;
listStoredPaymentMethods: (
payload: ListStoredPaymentMethodsEventFragment,
) => Promise<ListStoredPaymentMethodsResponse>;
}

export class AppWebhookManager implements PaymentsWebhooks {
Expand All @@ -41,6 +47,14 @@ export class AppWebhookManager implements PaymentsWebhooks {
this.apiClient = apiClient;
}

async listStoredPaymentMethods(
payload: ListStoredPaymentMethodsEventFragment,
): Promise<ListStoredPaymentMethodsResponse> {
const service = new ListStoredPaymentMethodsService();

return service.execute(payload);
}

async transactionInitializeSession(
payload: TransactionInitializeSessionEventFragment,
): Promise<TransactionInitializeSessionResponse> {
Expand Down
71 changes: 71 additions & 0 deletions src/pages/api/webhooks/list-stored-payment-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { SaleorSyncWebhook } from "@saleor/app-sdk/handlers/next";
import { createLogger } from "@/lib/logger";
import { SynchronousWebhookResponseBuilder } from "@/lib/webhook-response-builder";

import { getAuthorizeConfig } from "@/modules/authorize-net/authorize-net-config";
import { AuthorizeWebhookManager } from "@/modules/authorize-net/webhook/authorize-net-webhook-manager";
import { createAppWebhookManager } from "@/modules/webhooks/webhook-manager-service";
import { errorUtils } from "@/error-utils";
import { saleorApp } from "@/saleor-app";
import { type ListStoredPaymentMethodsResponse } from "@/schemas/ListStoredPaymentMethods/ListStoredPaymentMethodsResponse.mjs";
import {
UntypedListStoredPaymentMethodsDocument,
type ListStoredPaymentMethodsEventFragment,
} from "generated/graphql";

export const config = {
api: {
bodyParser: false,
},
};

export const listStoredPaymentMethodsSyncWebhook =
new SaleorSyncWebhook<ListStoredPaymentMethodsEventFragment>({
name: "ListStoredPaymentMethods",
apl: saleorApp.apl,
event: "TRANSACTION_REFUND_REQUESTED",
query: UntypedListStoredPaymentMethodsDocument,
webhookPath: "/api/webhooks/list-stored-payment-methods",
});

const logger = createLogger({
name: "listStoredPaymentMethodsSyncWebhook",
});

class WebhookResponseBuilder extends SynchronousWebhookResponseBuilder<ListStoredPaymentMethodsResponse> {}

export default listStoredPaymentMethodsSyncWebhook.createHandler(
async (req, res, { authData, ...ctx }) => {
const responseBuilder = new WebhookResponseBuilder(res);
logger.debug({ payload: ctx.payload }, "handler called");

try {
const authorizeConfig = getAuthorizeConfig();
const authorizeWebhookManager = new AuthorizeWebhookManager({
appConfig: authorizeConfig,
});

await authorizeWebhookManager.register();

const appWebhookManager = await createAppWebhookManager({
authData,
authorizeConfig,
});

const response = await appWebhookManager.listStoredPaymentMethods(ctx.payload);
// eslint-disable-next-line @saleor/saleor-app/logger-leak
logger.info({ response }, "Responding with:");
return responseBuilder.ok(response);
} catch (error) {
const normalizedError = errorUtils.normalize(error);
errorUtils.capture(normalizedError);
logger.error(normalizedError);

return responseBuilder.ok({
result: "REFUND_FAILURE",
pspReference: "", // todo: add
message: normalizedError.message,
});
}
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"anyOf": [
{
"type": "object"
}
]
}

0 comments on commit bbe6f8d

Please sign in to comment.