Let's use PHP to develop a Web Monetization helper!
Tags: gftwhackathon
Published: 2020-06-14 04:35:35

---
title: Let's use PHP to develop a Web Monetization helper!
published: true
description: This is a simple PHP project to generate some Web Monetization features easily.
tags: gftwhackathon
---

[Instructions]: In this tutorial, I present a PHP package and how to generate some web monetization features on back-end easily :).

## What I built

Here are some links and description for what I built:

- [php-monetiation link](https://github.com/peter279k/php-monetization)
- [Packagist link](https://packagist.org/packages/lee/php-monetization)
- Validate payment pointer URL.
- Generate payment pointer URL with given arguments.
- Generate payment pointer URL with meta tag.
- Get default payment pointer URL.


### Submission Category: 

This PHP package is about Foundational Technology.

## Demo

- These following sample codes include above features.
- Here are some sample codes and their results:

```PHP
<?php

require_once __DIR__ . '/vendor/autoload.php';

use Lee\WebMonetization\WebMonetization;

// Generate payment pointer URL with meta tag
$paymentPointerUrl = '$ilp.uphold.com/alice';
$metaTag = WebMonetization::generateMetaTag($paymentPointerUrl);

echo $metaTag; // <meta name="monetization" content="$ilp.uphold.com/alice">

// Validate payment pointer URL
$validateResult = WebMonetization::validatePaymentPointer($paymentPointerUrl);

echo $validateResult; // true

// Generate payment pointer
$interLedger = 'ilp';
$domainName = 'uphold';
$tldName = 'com';
$userTokenName = 'alice';
$paymentPointerUrl = WebMonetization::generatePaymentPointer($interLedger, $domainName, $tldName, $userTokenName);

echo $paymentPointerUrl; // $ilp.uphold.com/alice

// Get default payment pointer urls
$webMonetization = new WebMonetization();
$paymentPointerUrls = $webMonetization->getDefaultPaymentPointerUrls();

echo $paymentPointerUrls; // [ '$ilp.uphold.com/', '$ilp.gatehub.net/', '$pay.stronghold.co/',]
```

## Link to Code

[Note]: {% github https://github.com/peter279k/php-monetization %}


## How I built it 

- I use the PCRE (Per Compatibility Regular Expression) to validate specific payment pointer URL on PHP.
And it can call `WebMonetization::validatePaymentPointer` to complete above specifictaion.
- I create configuration array wit PHP to define a default payment pointer URL prefix lists.
These lists are as follows:
    - $ilp.uphold.com/
    - $ilp.gatehub.net/
    - $pay.stronghold.co/
It can call `WebMonetization::getDefaultPaymentPointerUrls` method to complete above specification.
- To crate completed payment pointer URL string easily, I also create a method named `WebMonetization::generatePaymentPointer` to complete above specification.
- To create payment pointer URL with meta tag string easily, I also create a method named `WebMonetization::generateMetaTag` to complete this specification.
- To make all of static methods verified and reliable, I also develop unit tests with PHPUnit to complete above specifications.
All of unit tests are available on [WebMonetizationTest](https://github.com/peter279k/php-monetization/blob/master/tests/WebMonetizationTest.php) class.
- To make sure all of above static methods are verified and reliable, I also integrate the GitHub Action Workflows to complete this issue about do automatic PHPUnit tests for every incoming commits.
The latest GitHub Action Workflows status is available [here](https://github.com/peter279k/php-monetization/actions?query=workflow%3ACI-Build).

## Additional Resources/Info 
Here are some useful resources and info to help me to complete this simple and useful PHP packages:

- PCRE reference and function usages on [official PHP website](https://www.php.net/manual/en/book.pcre.php).
- The payment pointer URL specification and description is available [here](https://paymentpointers.org/). And it can help me to know more details about generating payment pointer URL :).
- To complete all of static methods I develop on `WebMonetization` class reliably, I also want to say thanks for [PHPUnit](https://phpunit.de/) to help me to complete all related static methods with unit tests.