-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c77435a
commit faee9b7
Showing
6 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 35 additions & 4 deletions
39
processor/src/libs/fastify/hooks/paypal-webhook-verification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
import { PaypalAPI } from '../../../clients/paypal.client'; | ||
import { config } from '../../../config/config'; | ||
import { FastifyRequest } from 'fastify'; | ||
import { ErrorAuthErrorResponse } from '@commercetools/connect-payments-sdk'; | ||
import { NotificationPayloadDTO } from '../../../dtos/paypal-payment.dto'; | ||
import { IncomingHttpHeaders } from 'http'; | ||
import { config } from '../../../config/config'; | ||
|
||
export class WebhookVerificationHook { | ||
constructor() {} | ||
paypalClient: PaypalAPI; | ||
constructor() { | ||
this.paypalClient = new PaypalAPI(); | ||
} | ||
|
||
public authenticate() { | ||
return async (request: FastifyRequest) => { | ||
const data = request.body as NotificationPayloadDTO; | ||
// implement | ||
if (!data.resource) { | ||
throw new ErrorAuthErrorResponse('Unexpected payload'); | ||
} | ||
|
||
const verifyNotificationPayload = { | ||
auth_algo: this.verifyHeader(request.headers, 'paypal-auth-algo'), | ||
cert_url: this.verifyHeader(request.headers, 'paypal-cert-url'), | ||
transmission_id: this.verifyHeader(request.headers, 'paypal-transmission-id'), | ||
transmission_sig: this.verifyHeader(request.headers, 'paypal-transmission-sig'), | ||
transmission_time: this.verifyHeader(request.headers, 'paypal-transmission-time'), | ||
webhook_id: config.paypalWebhookId, | ||
webhook_event: data, | ||
}; | ||
|
||
const validator = await this.paypalClient.verifyNotification(verifyNotificationPayload); | ||
if (validator.verification_status === 'FAILURE') { | ||
throw new ErrorAuthErrorResponse('Webhook signature is not valid'); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
private verifyHeader(headers: IncomingHttpHeaders, key: string): string { | ||
const value = headers[key]; | ||
|
||
if (!value || Array.isArray(value)) { | ||
throw new Error('Could not retrieve correct header'); | ||
} | ||
|
||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters