Accessing local https service from stripe webhook

元气小坏坏 提交于 2019-12-24 16:22:24

问题


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,

  1. 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.

  1. LocalTunnel: I could not find the way to launch the application after downloading it from the github.

  2. 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.

  1. Install and run localstripe:

    pip3 install --user localstripe
    localstripe
    
  2. 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'`
    
  3. 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

  1. download ngrok and extract in any folder
  2. run ngrok.exe and type following command ngrok http [port] -host-header="localhost:[port]"
  3. Y0u will get a url in ngrok console "Forwording" like https://7755afd8.ngrok.io this url is replacement of localhost:[port]
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!