Implement Midtrans payment gateway in Flutter by using Snap method

Requirements:

  • This tutorial requires you need to know BloC knowledge. If not, please read this document first.

Direction:

  1. Register a midtrans account.
  2. Create a simple backend with 2 endpoints /productand /purchase
  3. Create a flutter project.
  4. Obtain transaction token from midtrans.
  5. Use webview_flutter to wrap snap.js
  6. Handle postMessage in javascript channel.

Register midtrans account:

midtrans access keys

Create a simple backend:

  • In this tutorial, I use Python-Flask for the backend side ’cause it’s a fast development language. You can use Golang or anything else depends on your requirements.
  • GET /product endpoint: Fake to return a list of product
  • POST /purchase endpoint: the purpose of this endpoint is to create a transaction token from midtrans.

Create a sample Flutter project:

  • Open snap.dart screen after retrieving transaction token when pressing BUY NOW button. By handling callback methods (_loadHtmlFromAssets()) of snap payment and use postMessage as an interaction channel between webview and native code, we can easily receive messages from midtrans.

Thanks for reading my article, all source code is available at https://github.com/trongdth/midtrans.git. Feel free to contact me via trongdth@gmail.com if there is any problem.

Full demo:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Trong Dinh Thai Hoang

Trong Dinh Thai Hoang

39 Followers

I’m a peaceful person who wants to make friend with people around the world.