Let's build a Web Service with Web Monetization X PHP Binding!
Tags: gftwhackathon
Published: 2020-06-03 17:25:56

---
title: Let's build a Web Service with Web Monetization X PHP Binding!
published: true
description: My submission to the Grant For The Web x DEV Hackathon!
tags: gftwhackathon
---

[Instructions]:

In this Hackathon, it can learn about how to develop some popular web service with Web Monetization features and PHP binding.

## What I built

Here are some stuffs I build for this dead simple web service:

- Using Bootstrap to develop all required templates.
- Using PHP 7.2+ version with Slim 4 Framework to build a web service.
- Using the Twig view template to be back-end rendering exclusive contents.
- Using PHP session to record current Web Monetization status event.
- In development mode, using mkcert command to create self-signed certificate easily.
- In development mode, using the stunnel command to create SSL tunnel and make local PHP web bulit-in server can be served on HTTPS. (It's important for Web Monetization because this only supports over HTTP.)
- In production mode, using the [certbot](https://certbot.eff.org/) to create HTTPS easily.
- Both development mode and production mode are on Ubuntu 18.04.

### Submission Category: 

Here are some categories included in this Hackathon:

- Foundational Technology: using the PHP to be back-end binding.
- Experiment stuffs: let all some stuffs and put them together to be a development environment.
- Production stuffs: After completing this service in development environment, I also provide the deployment steps to publish in production mode.
- Automatic Building Steps: I want to save my life, so I develop a simple Bash script to build my development environment easily.

## Demo

Firstly, visiting the following website via [web-monetization.peterli.website](https://web-monetization.peterli.website/), and it will present following captured picture:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4q0az5gvet4p7106hfon.png)

Second, the Home and Introduction don't have the web-monetized feature, they're the simple page.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/84d8jmp0gglypdher7x6.png)

Third, click the Blog link. By default, this page with web-monetized is disabled. And it will only look at the warning message about "This is the locked section one, You've to enable and pay with Web Monetization to unlock it :)!".

And the danger message says: "The Web Monetization has been disabled!".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zwh69gr4bwhrd72gn5n9.png)

To enable the web-monetized, we can just click the "Enable Web Monetization".

Once click this button, it will refresh this page and it will look at these exclusive contents:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u7swj2f977x18b4wjvaq.png)

If people thinks this is enough to do web monetization, they can click "Disable Web Monetization" button.

If people want to do web monetization for about whiles, they don't have to click disabled button.

They can enable web-monetization and leave this page directly.

The web service will use PHP session to remember this until the session has been expired.

If people want to know how much amounts are sent to this author, they can use the "Calculate Web Monetization" to present total amounts:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eh8wq3243rjz2iakbfx4.png)

If people want to stop knowing the amount information, just click "Stop Calculating Web Monetization".

Then visiting the donation page:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ra0rq98tzlod4dd8f2z7.png)

It's alternative way to do donation.

Sometimes most of developers have the PayPal donation button or other payment donation buttons to let sponsor pay some dollars.

Every payment donation buttons need to have the minimum dollars to let sponsor pay. For example, 1, 5 or 10 USD dollars at least.

This is not very convenient for every sponsor and it's up to sponsor determine how much amounts paid for developer.

The donation page are developed for these sponsors.

If some sponsors want to pay small amounts to developers, it can just click "Enable Donation Page".

And this donation page will enable the web monetization page.

Once enabling web monetization page, it will start paying some amounts and present current amount they pay for this developer.

Once the sponsor thinks it's good enough to do this payment, they can click "Disable Donation Page" at any moments!

Finally, visiting History page. In this page, it can show how much amounts that this website owner has made.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kmekqm4d9gt8tbwykxng.png)

I think amounts should be public and it can let every user and sponsor know the total amounts of this website.

## Link to Code

- All codes are located on [web-monetization-demo](https://github.com/peter279k/web-monetization-demo).
Feel free to fork and use them :).
- It also provides a completed [README](https://github.com/peter279k/web-monetization-demo/blob/master/README.md) for every back-end developers to develop a awesome web-based application services steps by steps.

## How I built it 

- I think these exclusive contents should be hidden.
And I use the Twig view to complete this issue.
- To sync transaction history for every micro payment sent to Coil , I still use the AJAX to do POST method to store every amount detail.
- To let developers decide to enable/disable web monetization, every page I develop has the enable and disable button.

## Additional Resources/Info 

Here are some useful references I look at:

- [Coil](https://coil.com/)
- [Web Monetization Docs](https://webmonetization.org/docs)
- [Slim Framework](http://www.slimframework.com/)
- [Slim Framework tutorial](https://odan.github.io/2019/11/05/slim4-tutorial.html#deployment)
- [Uphold](https://uphold.com/)
- [DigitalOcean](https://www.digitalocean.com/)
- [Certbot](https://certbot.eff.org/#ubuntutrusty-apache)