#shakilkhan496

1 messages ยท Page 1 of 1 (latest)

boreal siloBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

pseudo topaz
#

I need help with webhook integration on deno

gray rover
#

Can you show me your webhook code? It sounds like the wrong object type was passed in to our function for constructing events

pseudo topaz
#

async function handler(context) {
const signature = context.request.headers.get('Stripe-Signature');

// Use context.request.body().value to get the raw body as Uint8Array.
const rawBody = await context.request.body().value;

let event;
try {
    event = await stripe.webhooks.constructEventAsync(
        rawBody,
        signature,
        signInSecret,
        undefined
    );
} catch (err) {
    console.log(`โŒ Error message: ${err.message}`);
    return new Response(err.message, { status: 400 });
}

// Successfully constructed event
console.log('โœ… Success:', event.id);

// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
    const stripeObject = event.data.object;
    console.log(`๐Ÿ’ฐ PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
    const charge = event.data.object;
    console.log(`๐Ÿ’ต Charge id: ${charge.id}`);
} else {
    console.warn(`๐Ÿคทโ€โ™€๏ธ Unhandled event type: ${event.type}`);
}

return new Response(JSON.stringify({ received: true }), { status: 200 });

}

router.post('/webhookMain', async (context) => {
await handler(context);
});

#

Here is code

#

I have tried in different way , but not solved . file name is server.ts

gray rover
#

So as that comment indicates, you are getting a Uint8Array, not a String or Buffer. Does your framework have a way of giving you the array as one of those? Otherwise you can try converting the Uint8Array to a String

pseudo topaz
#

I do not know. It is deno .

#

here is docs from stripe

#

That gives me different error , .text() is not a function

#

This is console log of raw Body and server log : {โŒ Error message: Webhook payload must be provided as a string or a Buffer (https://nodejs.org/api/buffer.html) instance representing the raw request body.Payload was provided as a parsed JavaScript object instead.
Signature verification is impossible without access to the original signed material.

Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing

1/10/2024, 9:54:57 PM
gcp-us-west1
this is rawBody {
id: "evt_3OX3A6JRdeoMFQJ21HqMg3B4",
object: "event",
api_version: "2023-08-16",
created: 1704898426,
data: {
object: {
id: "pi_3OX3A6JRdeoMFQJ21v4Pydi3",
object: "payment_intent",
amount: 10000,
amount_capturable: 0,
amount_details: { tip: {} },
amount_received: 0,
application: null,
application_fee_amount: null,
automatic_payment_methods: { allow_redirects: "always", enabled: true },
canceled_at: null,
c

GitHub

Node.js library for the Stripe API. . Contribute to stripe/stripe-node development by creating an account on GitHub.

#

so the rawbody is a object

#

This is the problem, I think. How can I solve this??

gray rover
#

But the example uses const body = await request.text(); to get the body

#

And doesn't mention an Uint8Array

pseudo topaz
#

yes I have also used that but that gives a error , .text() is not a function

gray rover
#

Gotcha, sounds like we used a different version of that library in our sample

pseudo topaz
#

I have print the rawbody that is object of trigered event

gray rover
#

I am a bit too busy to look it up at the moment, but I would reccommend looking up how to get a raw request body as a string in that library

#

Awesome, just need to make that a String

#

Or retrieve it as a String

pseudo topaz
#

but here is problem with signature varification

#

How can I do that ?

gray rover
#

Your code looks fine other than passing in the wrong object type

#

If you fix that issue and run in to a different error, we can address that. But for now that is the issue to fix first

pseudo topaz
#

So do I use JSON.stringify to make that string ?

#

I have added the JSON.stringify(rawBody)

#

and got this "Error message: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing"

GitHub

Node.js library for the Stripe API. . Contribute to stripe/stripe-node development by creating an account on GitHub.

#

I don't know what is the issue

#

Here is print "โŒ Error message: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing

1/10/2024, 10:15:16 PM
gcp-us-west1
this is type of body string
1/10/2024, 10:15:16 PM
gcp-us-west1
this is body {"id":"evt_3OX3A6JRdeoMFQJ21HqMg3B4","object":"event","api_version":"2023-08-16","created":1704898426,"data":{"object":{"id":"pi_3OX3A6JRdeoMFQJ21v4Pydi3","object":"payment_intent","amount":10000,"amount_capturable":0,"amount_details":{"tip":{}},"amount_received":0,"application":null,"application_fee_amount":null,"automatic_payment_methods":{"allow_redirects":"always","enabled":true},"canceled_at":null,"cancellation_reason":null,"capture_method":"automatic","client_secret":"pi_3OX3A6JRdeoMFQJ21v4Pydi3_secret_qDGXQqfyuBrkzijMlquAgPOoU","confirmation_method":"automatic","created":1704898426,"currency":"gbp","customer":null,"description":null,"invoice":null,"last_payment_error":null,"latest_charge":null,"livemode":false,"metadata":{"connectedAccountId":"acct_1OWyUiQoTh4gMc3V"},"next_action":null,"on_behalf_of":null,"payment_method":null,"payment_method_configuration_details":{"id":"pmc_1OWmlLJRdeoMFQJ2uSySYDb1","parent":null},"payment_method_options":{"card":{"installments":null,"mandate_opti"

GitHub

Node.js library for the Stripe API. . Contribute to stripe/stripe-node development by creating an account on GitHub.

#

I have checked the type also and that is string now

#

Kindly check this out and let me know, please.

gray rover
#

Can you show me your new code? It sounds like whatever you are doing is changing the body slightly

#

Our signature calculation needs things like whitespace and newline characters to be preserved, so it can be very finnicky unfortunately

pseudo topaz
#

async function handler(context) {
const signature = context.request.headers.get('Stripe-Signature');

// Use context.request.body().value to get the raw body as Uint8Array.
const rawBody = await context.request.body().value;
console.log('this is rawBody',rawBody);
const body = JSON.stringify(rawBody);
console.log('this is body',body);
console.log('this is type of body',typeof(body));
let event;
try {
    event = await stripe.webhooks.constructEventAsync(
        body,
        signature,
        signInSecret,
        undefined
    );
} catch (err) {
    console.log(`โŒ Error message: ${err.message}`);
    return new Response(err.message, { status: 400 });
}

// Successfully constructed event
console.log('โœ… Success:', event.id);

// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
    const stripeObject = event.data.object;
    console.log(`๐Ÿ’ฐ PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
    const charge = event.data.object;
    console.log(`๐Ÿ’ต Charge id: ${charge.id}`);
} else {
    console.warn(`๐Ÿคทโ€โ™€๏ธ Unhandled event type: ${event.type}`);
}

return new Response(JSON.stringify({ received: true }), { status: 200 });

}

router.post('/webhookMain', async (context) => {
await handler(context);
});

#

here is the code

#

I am using deno

gray rover
#

Can you double check tha there is not a text() method? It is looking like it is still supported as far as I can see though unfortunately I don't have that sample set up to check that specific library

pseudo topaz
#

I am checking

#

This is the code "async function handler(context) {
const signature = context.request.headers.get('Stripe-Signature');

// Use context.request.body().value to get the raw body as Uint8Array.
const rawBody = await context.request.body().value;
const body = await context.request.text();
console.log('this is rawBody',rawBody);
// const body = JSON.stringify(rawBody);
console.log('this is body',body);
console.log('this is type of body',typeof(body));
let event;
try {
    event = await stripe.webhooks.constructEventAsync(
        body,
        signature,
        signInSecret,
        undefined
    );
} catch (err) {
    console.log(`โŒ Error message: ${err.message}`);
    return new Response(err.message, { status: 400 });
}

// Successfully constructed event
console.log('โœ… Success:', event.id);

// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
    const stripeObject = event.data.object;
    console.log(`๐Ÿ’ฐ PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
    const charge = event.data.object;
    console.log(`๐Ÿ’ต Charge id: ${charge.id}`);
} else {
    console.warn(`๐Ÿคทโ€โ™€๏ธ Unhandled event type: ${event.type}`);
}

return new Response(JSON.stringify({ received: true }), { status: 200 });

}

router.post('/webhookMain', async (context) => {
await handler(context);
});"

#

On the tutorial they are using .text()

#

but I got that error

#

Can you check sir please?

boreal siloBOT
pseudo topaz
#

Hi bismarck

#

Please check

#

Any one here to help?

oblique tide
#

๐Ÿ‘‹

#

Catching up

pseudo topaz
#

Yes

#

So I am using deno

#

Having an issue with signature verification

oblique tide
#

Okay when you log out your body do you see a buffer type?

pseudo topaz
#

That was object

#

then i parsed as string

oblique tide
#

It should look like binary

#

Not an object or a string

pseudo topaz
#

Okay How can I do that?

oblique tide
#

Also, does your constructEventAsync() method wrap our constructEvent() method from the Node SDK?

oblique tide
pseudo topaz
#

I am using deno

#

async function handler(context) {
const signature = context.request.headers.get('Stripe-Signature');

// Use context.request.body().value to get the raw body as Uint8Array.
const rawBody = await context.request.body().value;
const body = await context.request.text();
console.log('this is rawBody',rawBody);
// const body = JSON.stringify(rawBody);
console.log('this is body',body);
console.log('this is type of body',typeof(body));
let event;
try {
    event = await stripe.webhooks.constructEventAsync(
        body,
        signature,
        signInSecret,
        undefined
    );
} catch (err) {
    console.log(`โŒ Error message: ${err.message}`);
    return new Response(err.message, { status: 400 });
}

// Successfully constructed event
console.log('โœ… Success:', event.id);

// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
    const stripeObject = event.data.object;
    console.log(`๐Ÿ’ฐ PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
    const charge = event.data.object;
    console.log(`๐Ÿ’ต Charge id: ${charge.id}`);
} else {
    console.warn(`๐Ÿคทโ€โ™€๏ธ Unhandled event type: ${event.type}`);
}

return new Response(JSON.stringify({ received: true }), { status: 200 });

}

router.post('/webhookMain', async (context) => {
await handler(context);
});

#

this is code

#

I have written to follow the documents of deno

oblique tide
#

Yeah I'm not familiar with deno sorry

pseudo topaz
#

Here is the docs

#

but when I am using this as .text() method then it returns error

oblique tide
#

Can you show me exactly what you see in the log after you do const body = await request.text(); like in that example?

#

Like log out body after that

pseudo topaz
#

Yes

#

I am testing

#

I got error while using text() method

#

It seems like that is wrong.

gray rover
#

Can you try removing the parentheses? Not a function may mean that it is just a string property

pseudo topaz
#

I do not know how to fix , event on youtube they have showed with .text() on deno

#

You can check this code "async function handler(context) {
const signature = context.request.headers.get('Stripe-Signature');
const newBody = await context.text();
console.log('this is new body', newBody);
// Use context.request.body().value to get the raw body as Uint8Array.
// const rawBody = await context.request.body().value;
const body = await context.request.text();
// console.log('this is rawBody',rawBody);
// const body = JSON.stringify(rawBody);
console.log('this is body',body);
console.log('this is type of body',typeof(body));
let event;
try {
event = await stripe.webhooks.constructEventAsync(
newBody,
signature,
signInSecret,
undefined
);
} catch (err) {
console.log(โŒ Error message: ${err.message});
return new Response(err.message, { status: 400 });
}

// Successfully constructed event
console.log('โœ… Success:', event.id);

// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
    const stripeObject = event.data.object;
    console.log(`๐Ÿ’ฐ PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
    const charge = event.data.object;
    console.log(`๐Ÿ’ต Charge id: ${charge.id}`);
} else {
    console.warn(`๐Ÿคทโ€โ™€๏ธ Unhandled event type: ${event.type}`);
}

return new Response(JSON.stringify({ received: true }), { status: 200 });

}

router.post('/webhookMain', async (context) => {
await handler(context);
});"

oblique tide
#

What do you see if you log out context?

pseudo topaz
#

i am using this " const newBody = await context.text();"

#

Okay let me do it

#

this is context Context {
app: Application {
"#middleware": [
[Function: dispatch] {
router: Router {
"#params": {},
"#stack": [
Layer {
methods: [ "POST" ],
middleware: [ [AsyncFunction (anonymous)] ],
options: {
end: undefined,
sensitive: undefined,
strict: undefined,
ignoreCaptures: undefined
},
paramNames: [],
path: "/webhook",
regexp: /^/webhook[/#?]?$/i
},
Layer {
methods: [ "HEAD", "GET" ],
middleware: [ [AsyncFunction (anonymous)] ],
options: {
end: undefined,
sensitive: undefined,
strict: undefined,
ignoreCaptures: undefined
},
paramNames: [],
path: "/",
regexp: /^/[/#?]?$/i
},
Layer {
methods: [ "POST" ],
middleware: [ [AsyncFunction (anonymous)] ],
options: {
end: undefined,
sensitive: undefined,
strict: undefined,
ignoreCaptures: undefined
},
paramNames: [],
path: "/webhookMain",
regexp: /^/webhookMain[/#?]?$/i
}
]
}
},
[AsyncFunction: allowedMethods]
],
keys: undefined,
proxy: false,
state: {}
},
cookies: SecureCookieMap [],
isUpgradable: false,
respond: true,
request: Request {
hasBody: true,
headers: Headers {
accept: "/; q=0.5, application/xml",
"cache-control": "no-cache",
connection: "close",
"content-length": "2240",
"content-type": "application/json; charset=utf-8",
host: "large-kingfisher-61.deno.dev",
"stripe-signature": "t=1704906123,v1=7dc600c33c969e1837376ae8a3c80d77ecd649f9e275d5333e8f4d3ca13521f0,v0=36b588a675a68df1"... 48 more characters,
"user-agent": "Stripe/1.0 (+https://stripe.com/docs/webhooks)"
},
ip: "54.187.205.235",
ips: [],
method: "POST",
secure: false,
url: "https://large-kingfisher-61.deno.dev/webhookMain"
},
response: Response {
body: undefined,
headers: Headers {},
status: 404,
type: undefined,
writable: true
},
socket: undefined,
state: {}
}

#

Kindly check and let me know please

#

I am on serious problem. I can do in express with no issue but my client is using deno

oblique tide
#

I mean yeah I don't see any text() method there

#

Have you checked your deno version?

#

Maybe you don't have a proper request instance here

pseudo topaz
#

As I am deploying directly in the cloud, it is the latest version.

#

Yes I hope , can you guide me what should be the code ?

oblique tide
#

No I can't. I know nothing about deno as I stated. I'm just looking for info on the web while I also help other people.

pseudo topaz
#

Ok let me check

#

Here is tutorial from stripe

oblique tide
#

Sure but that is from 2 years ago