Skip to content

Commit

Permalink
feat: expect sessionId instead of cartId in webhook event
Browse files Browse the repository at this point in the history
  • Loading branch information
a11rew committed Nov 5, 2024
1 parent 3a87014 commit 80ce292
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions packages/plugin/src/services/paystack-payment-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
PaymentProviderError,
PaymentProviderSessionResponse,
MedusaContainer,
ICartModuleService,
WebhookActionResult,
CreatePaymentProviderSession,
UpdatePaymentProviderSession,
Expand Down Expand Up @@ -58,7 +57,6 @@ type PaystackPaymentProviderSessionResponse = PaymentProviderSessionResponse & {
class PaystackPaymentProcessor extends AbstractPaymentProvider {
static identifier = "paystack";

protected readonly cartService: ICartModuleService;
protected readonly configuration: PaystackPaymentProcessorConfig;
protected readonly paystack: Paystack;
protected readonly debug: boolean;
Expand All @@ -81,16 +79,6 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {
disable_retries: options.disable_retries,
});
this.debug = Boolean(options.debug);

// Container is just an object - https://docs.medusajs.com/development/fundamentals/dependency-injection#in-classes
this.cartService = container.cartService;

if (this.cartService.retrieveCart === undefined) {
throw new MedusaError(
MedusaError.Types.UNEXPECTED_STATE,
"Your Medusa installation contains an unsupported cartService implementation. Update your Medusa installation.",
);
}
}

/**
Expand All @@ -111,6 +99,12 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {

const validatedCurrencyCode = formatCurrencyCode(currency_code);

if (!email) {
return this.buildError("Email is required", {
detail: "Email is required to initiate a Paystack payment",
});
}

const { data, status, message } =
await this.paystack.transaction.initialize({
amount: Number(amount), // Paystack expects amount in lowest denomination - https://paystack.com/docs/payments/accept-payments/#initialize-transaction-1
Expand Down Expand Up @@ -352,22 +346,24 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {
* Handles incoming webhook events from Paystack
*/
async getWebhookActionAndData({
data,
data: { event, data },
rawData,
headers,
}: {
data: {
event: string;
amount: number;
metadata?: Record<string, any>;
data: {
amount: number;
metadata?: Record<string, any>;
};
};
rawData: string | Buffer;
headers: Record<string, unknown>;
}): Promise<WebhookActionResult> {
if (this.debug) {
console.info(
"PS_P_Debug: Handling webhook event",
JSON.stringify({ data, rawData, headers }, null, 2),
JSON.stringify({ data, headers }, null, 2),
);
}

Expand All @@ -394,17 +390,17 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {
}

// Validate event type
if (data.event !== "charge.success") {
if (event !== "charge.success") {
return {
action: PaymentActions.NOT_SUPPORTED,
};
}

const cartId = data.metadata?.cart_id;
const sessionId = data.metadata?.session_id;

if (!cartId) {
if (!sessionId) {
console.error(
"PS_P_Debug: No cart_id found in webhook transaction metadata",
"PS_P_Debug: No sessionId found in webhook transaction metadata",
);
return {
action: PaymentActions.NOT_SUPPORTED,
Expand All @@ -414,7 +410,7 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {
return {
action: PaymentActions.AUTHORIZED,
data: {
session_id: cartId,
session_id: sessionId,
amount: data.amount,
},
};
Expand Down

0 comments on commit 80ce292

Please sign in to comment.