diff --git a/lib/utils/razorpay-utils.d.ts b/lib/utils/razorpay-utils.d.ts index f584875..ac75784 100644 --- a/lib/utils/razorpay-utils.d.ts +++ b/lib/utils/razorpay-utils.d.ts @@ -36,6 +36,30 @@ export interface RazorpayVerifyPaymentLink extends RazorpayWebhook { payment_link_status: string; } +export interface RazorpayVerifyInvoicePayment extends RazorpayWebhook { + /** + * Unique identifier of the invoice generated by Razorpay. + * Example: "inv_KY9Xb8W2Xx1e1A" + */ + invoice_id: string; + + /** + * Internal reference ID (or receipt ID) provided by the business at the time of invoice creation. + * This is typically set using the `reference_id` field. + * Example: "order_rcptid_11" + * + * Note: May be null or undefined if not provided during invoice creation. + */ + invoice_receipt_id: string; + + /** + * Current status of the invoice. + * This will usually be `"paid"` if the payment was completed successfully. + * Example: "paid" + */ + invoice_status: string; +} + export function getDateInSecs(date: string): number export function normalizeDate(date: number | string): number @@ -87,4 +111,4 @@ export function validatePaymentVerification(payload: RazorpayVerifyPayment | Raz */ export function prettify(val: Object): string -export function generateOnboardingSignature(params: any, secret: string): string \ No newline at end of file +export function generateOnboardingSignature(params: any, secret: string): string diff --git a/lib/utils/razorpay-utils.js b/lib/utils/razorpay-utils.js index 6ba6468..05abf70 100644 --- a/lib/utils/razorpay-utils.js +++ b/lib/utils/razorpay-utils.js @@ -138,8 +138,18 @@ function validatePaymentVerification(params={}, signature, secret){ var payload = paymentLinkId + '|' + paymentLinkRefId + '|' + paymentLinkStatus + '|' + paymentId; + }else if(isDefined(params.invoice_id) === true){ + + var invoiceId = params.invoice_id; + var invoiceReceiptId = params.invoice_receipt_id; + var invoiceStatus = params.invoice_status; + + var payload = invoiceId + '|' + invoiceReceiptId + '|' + invoiceStatus + '|' + paymentId; + }else{ - throw new Error('Either order_id or subscription_id is mandatory'); + throw new Error( + 'One of order_id, subscription_id, payment_link_id or razorpay_invoice_id is mandatory for verification.' + ); } return validateWebhookSignature(payload,signature,secret); }; diff --git a/test/utils/razorpay-utils.spec.js b/test/utils/razorpay-utils.spec.js index a5242a3..0276a02 100644 --- a/test/utils/razorpay-utils.spec.js +++ b/test/utils/razorpay-utils.spec.js @@ -159,4 +159,22 @@ describe('Razorpay Utils', () => { 'Validates payment' ); }) + it('Invoice Payment Verfication', () => { + + const respBody = { + 'payment_id':'pay_IH4NVgf4Dreq1l', + 'invoice_id':'inv_KY9Xb8W2Xx1e1A', + 'invoice_receipt_id':'order_rcptid_11', + 'invoice_status':'paid', + }, + correctSignature = '797a05a35631be3d8c88b65d449952cb5314fba3d5fa89709d0c4faf931608be', + wrongSignature = 'sddsfdsfs', + secret = 'EnLs21M47BllR3X8PSFtjtbd'; + + assert.ok( + validatePaymentVerification(respBody, correctSignature, secret) && + !validatePaymentVerification(respBody, wrongSignature, secret), + 'Validates invoice payment' + ); + }) })