KayKay
KayKay

Reputation: 593

Accessing local https service from stripe webhook

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.

Upvotes: 1

Views: 4320

Answers (4)

Abdullah Itbeam
Abdullah Itbeam

Reputation: 1

Although the others answers work, I think they are a bit dated.

Stripe now has a CLI tool that allows you to create a connection between Stripe and your local host. Here are the steps

  1. Create the webhook file that handles the Stripe webhook calls. Let's assume that path to this file is http://localhost/webhook.

  2. Go to stripe.com, go to the dashboard, then click on Developers, and Webhooks, then add a new endpoint. Make sure the URL in that endpoint is the one from step 1 above (i.e., http://localhost/webhook)

  3. Download and install the Stripe CLI locally. Then follow the instructions to login

  4. In your Stripe CLI, run the following command:

    stripe listen --forward-to http://localhost/webhooks.
    
    This will eventually listen to Stripe for any webhooks to your local
    server, and forward them to your sever locally (i.e, it creates a
    bridge between the two)
    
  5. register url in VerifyCsrfToken[Middleware]: class VerifyCsrfToken extends BaseVerifier { protected $except = [ 'webhook' ]; }

  6. Test your work

Upvotes: 0

Greeso
Greeso

Reputation: 8249

Although the others answers work, I think they are a bit dated.

Stripe now has a CLI tool that allows you to create a connection between Stripe and your local host. Here are the steps

  1. Create the webhook file that handles the Stripe webhook calls. Let's assume that path to this file is http://localhost/webhook.

  2. Go to stripe.com, go to the dashboard, then click on Developers, and Webhooks, then add a new endpoint. Make sure the URL in that endpoint is the one from step 1 above (i.e., http://localhost/webhook)

  3. Download and install the Stripe CLI locally. Then follow the instructions to login

  4. In your Stripe CLI, run the following command:

    stripe listen --forward-to http://localhost/webhooks.

    This will eventually listen to Stripe for any webhooks to your local server, and forward them to your sever locally (i.e, it creates a bridge between the two)

  5. Test your work.

The problem with the above solution is it is not going to send back the responses of the webhook back to the Stripe server (because the http://localhost/webhook is private to your network).

If you insist on having responses back to Stripe, then you should either

  1. Map your localhost to a public domain

  2. Use a tunnel, such as ngrok. This answer describes how to use ngrok, but for me, I make the ngrok call this way:

    ngrok http -host-header=localhost 80

    The above call would give me something like https://<some-random-numnber>.ngrok.io

    So in stripe.com, I would have to write the endpoint as

    https://<some-random-numnber>.ngrok.io/<path-to-webhook-response-page>/

Hope this helps

Upvotes: 1

Yogesh Jangir
Yogesh Jangir

Reputation: 37

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;
    }
}

Upvotes: 2

Adrien Verg&#233;
Adrien Verg&#233;

Reputation: 391

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.

Upvotes: 1

Related Questions