DEV Community

peter279k
peter279k

Posted on

Let's use PHP to develop a Web Monetization helper!

[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
  • Packagist link
  • 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

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/',]
Enter fullscreen mode Exit fullscreen mode

Link to Code

[Note]:

GitHub logo peter279k / php-monetization

The Web Monetization feature helper written in PHP

php-monetization

CI-Build

Introduction

  • This is a PHP for Web Monetization helper.

Features

Here are some features about Web Monetization:

  • Validate payment pointer URL.
  • Generate payment pointer URL with given arguments.
  • Generate payment pointer URL with meta tag.
  • Get default payment pointer URL.

Installation

We strongly recoomend using composer to install this package.

The installation steps are as follows:

composer require lee/php-monetization:^1.0

Usage

It's very simple. Here are some usages:

<?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 = 
Enter fullscreen mode Exit fullscreen mode

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 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.

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.
  • The payment pointer URL specification and description is available here. 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 to help me to complete all related static methods with unit tests.

Top comments (0)