问题
I am integrating a payment system using Stripe. In the process, I need to test the webhooks in my local dev. machine before I ship it to QA. I have tried the following,
- Ultrahook: however when starting the ultrahook it said, authenticated <myaccount>, but did not give any "Forwarding activated..." message. When I tried to access the url from stripe or web, it did not work. Details below,
local url: https : //localhost/xxx/yyy/zzz ultrahook command: ultrahook -k localhost https : //localhost.com/xxx/yyy/zzz hook url used in stripe: http : //localhost.arivanza.ultrahook.com/xxx/yyy/zzz
I have also tried, https : //localhost.com/, but the request does not come through from the hook when tested from stripe.
LocalTunnel: I could not find the way to launch the application after downloading it from the github.
PageKite: It by default opens up localhost:80, not sure how to open up the https://localhost.com
Any help would be greatly appreciated.
回答1:
If you need to receive webhooks on your local dev machine (let's say, on localhost:1234/api/url
), you could use a local "mock" Stripe server, like localstripe. Once lauched, it will act like Stripe and send events if you configure it to.
Install and run localstripe:
pip3 install --user localstripe localstripe
Configure your program to use
localhost:8420
(localstripe) instead of the real Stripe (api.stripe.com
). For instance with a Python program:import stripe stripe.api_key = 'sk_test_anythingyouwant' stripe.api_base = 'http://localhost:8420'`
Configure localstripe to send webhook events to your program:
curl localhost:8420/_config/webhooks/mywebhook1 \ -d url=http://localhost:1234/api/url -d secret=whsec_s3cr3t
Not all events are implemented in localstripe, and it could behave slightly differently from real Stripe. But it allows you to test your application in a local sandbox, without touching actual Stripe servers.
回答2:
Hi I have tried by self. Please follow following steps
- download ngrok and extract in any folder
- run ngrok.exe and type following command ngrok http [port] -host-header="localhost:[port]"
- Y0u will get a url in ngrok console "Forwording" like https://7755afd8.ngrok.io this url is replacement of localhost:[port]
- You can use no https://7755afd8.ngrok.io/index.html
Code example for stripe webhook using asp.net:
var postdata =new StreamReader(HttpContext.Request.InputStream).ReadToEnd();
var data = JObject.Parse(postdata);
var eventid = data["id"].ToString();
var eventdata = StripeHelper.GetStripeEvent(eventid);
if(eventdata!=null)
{
switch(eventdata.Type)
{
case "charge.succeeded":
//charged event
break;
case "customer.source.created":
//card added
break;
case "customer.source.deleted":
//card deleted
break;
case "customer.subscription.trial_will_end":
//trial will end
break;
}
}
来源:https://stackoverflow.com/questions/34592547/accessing-local-https-service-from-stripe-webhook