#shakilkhan496
1 messages ยท Page 1 of 1 (latest)
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.
- shakil-signature-verification, 1 hour ago, 42 messages
- shakilkhan496, 4 hours ago, 59 messages
- shakilkhan496, 8 hours ago, 6 messages
I need help with webhook integration on deno
Can you show me your webhook code? It sounds like the wrong object type was passed in to our function for constructing events
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
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
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
so the rawbody is a object
This is the problem, I think. How can I solve this??
But the example uses const body = await request.text(); to get the body
And doesn't mention an Uint8Array
yes I have also used that but that gives a error , .text() is not a function
Gotcha, sounds like we used a different version of that library in our sample
I have print the rawbody that is object of trigered event
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
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
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"
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"
I have checked the type also and that is string now
Kindly check this out and let me know, please.
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
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
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
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?
Okay when you log out your body do you see a buffer type?
Okay How can I do that?
Also, does your constructEventAsync() method wrap our constructEvent() method from the Node SDK?
That depends on how your server receives requests
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
Yeah I'm not familiar with deno sorry
Here is the docs
but when I am using this as .text() method then it returns error
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
Yes
I am testing
I got error while using text() method
It seems like that is wrong.
Can you try removing the parentheses? Not a function may mean that it is just a string property
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);
});"
What do you see if you log out context?
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
I mean yeah I don't see any text() method there
Have you checked your deno version?
Does seem like you want one of these methods: https://docs.deno.com/deploy/api/runtime-request#methods
Maybe you don't have a proper request instance here
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 ?
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.
Ok let me check
Part two of how to handle webhooks using stripe-node in a Deno application. We recommend starting with the part one (https://youtu.be/epCHqHEdz8I).
In this episode, CJ walks you through handling webhooks using stripe-node in a Deno application.
Presenter
CJ Avilla - Developer Advocate at Stripe - https://twitter.com/cjav_dev
Tab...
Here is tutorial from stripe
Sure but that is from 2 years ago