How to improve Stripe payments in Power Pages

My recent post, Anatomy of the Stripe payments in Power Pages, got some interest. I looked under the hood of this out-of-the-box integration and raised a few points on the limitations that come with that. In this post, I want to show how you might work around some of these and improve Stripe payments in Power Pages.

Specifically, I’ll show you how:

  • Capture additional information about payment processing results.
  • Handle failed payments on “the server” and improve the visibility of such events.

The idea is simple – enhance the “Payment” table with additional fields and populate them using Power Automate cloud flow as a Stripe web hook. I describe here the bare minimum changes for the demo, but you may want to capture more information and do better error handling, etc.

So, let’s create a new Power Platform solution where we will include the enhancements.

Enhance the Payment table

I added to the solution an existing “Payment” table and a new column “Payment status code” (single line of text). I will use this and the other two existing columns on the table to capture the payment processing result.

Create cloud flow as a WebHook

In the solution, I created a new instant cloud flow with a trigger of type “HTTP Request”. It’s set to be available to anyone who calls it, and I defined an empty schema.

For the next step, you need to know the structure of the webhook payload, so for now, let’s just add a “Compose” action to store the payload (body) of the request and return to it after we set up Stripe.

Save the new flow and then copy the “HTTP POST URL” value – this is what Stripe will call on the payment events we will define next.

Register WebHook with Stripe

Now, go to the Stripe developer section and find the “Webhook” section, where you should already see a few webhooks registered by the out-of-the-box integration. We will add another one – paste the URL to the flow created earlier into the “Enpoint URL” field and then select “payment_intent.payment_failed” as the event type to track.

If you followed the official documentation or the helpful video by Nick Doelman, you should have the Stripe payment page in your Power Pages portal set up already. Let’s use it to trigger a failed payment. You need to initiate a payment using a test Stripe card (e.g. 4000 0000 0000 0002). As expected, it will fail, displaying an error to the user on the front end:

What we are more interested in is what happened at the backend. At the backend, you should see your flow triggered by Stripe and capture the payload of the failed payment event!

Capture the results in the Payment table

We have everything now to capture these details in the Payment table. Let’s return to our cloud flow and edit it.

Remove the “Compose” action; it served us well. As the next step of the cloud flow, we will find the record in the Payment table that has the “Payment identifier” field value as the payment intent reference from the webhook request data: triggerBody()?['data']?['object']?['id']

Then, the other step is to update this record with the payment event information we received:

Please note the values set by this action as:

  • Row ID – the first record of the previous List operation (you may want to do it better by checking the record count, etc): first(outputs('List_rows')?['body/value'])?['pp_paymentid']
  • Payment statusFailed
  • Payment status code – the failure_code from the event data: first(triggerBody()?['data']?['object']?['charges']?['data'])?['failure_code']
  • Payment status reason – the failure_message from the event data: first(triggerBody()?['data']?['object']?['charges']?['data'])?['failure_message']

By triggering the failed payment again, we can observe (in a basic model drive app I trust you to implement) the data captured as below:

Summary

Now you know how to implement handling of Stripe payment events on the server side. I demonstrated it in the failed payment scenario, but the same approach applies to almost any other event generated by Stripe during the processing of transactions.

We have seen how you can enhance the base Payment table to capture the additional information, register a Power Automate cloud flow as a webhook on the Stripe portal and use it to populate the additional information.

If you liked the post, like it, follow and link to it. reach out to me at experts@technomancy.com.au and follow on LinkedIn.

3 comments

Leave a comment