Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Bump dependencies and add QR code output options" #14

Merged
merged 17 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1]
php: [8.3, 8.2, 8.1]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
name: PHP ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
Expand All @@ -22,7 +22,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
extensions: dom, curl, libxml, mbstring, zip, pcntl, bcmath, intl, gd, exif, iconv, fileinfo
coverage: pcov

- name: Setup problem matchers
Expand All @@ -40,4 +40,4 @@ jobs:
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.php_cs
.php_cs.cache
.phpunit.result.cache
.phpunit.cache
build
composer.lock
coverage
Expand Down
48 changes: 33 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# PromptPay QR Code Generator - PHP


![Example](assets/promptpay-qr.png)

[![Latest Version on Packagist](https://img.shields.io/packagist/v/farzai/promptpay.svg?style=flat-square)](https://packagist.org/packages/farzai/promptpay)
[![Tests](https://img.shields.io/github/actions/workflow/status/farzai/promptpay-qr-php/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/farzai/promptpay-qr-php/actions/workflows/run-tests.yml)
[![codecov](https://codecov.io/gh/farzai/promptpay-qr-php/branch/main/graph/badge.svg)](https://codecov.io/gh/farzai/promptpay-qr-php)
[![Total Downloads](https://img.shields.io/packagist/dt/farzai/promptpay.svg?style=flat-square)](https://packagist.org/packages/farzai/promptpay)


This library that allows you to create PromptPay QR codes.
You can generate a QR code for receiving payments through PromptPay, which is a popular payment method in Thailand.

Expand All @@ -14,8 +18,6 @@ You can generate a QR code for receiving payments through PromptPay, which is a
- PHP 8.0 or higher

## Installation (For CLI)
![Example CLI](assets/promptpay-qr-cli.png)


You can install the package globally via composer:

Expand Down Expand Up @@ -43,8 +45,14 @@ For Windows
```

## Usage

```bash
promptpay 0988888888 --amount 100
$ promptpay <phone-number> <amount>
```

For example, to generate a QR code for receiving 100 THB from the phone number 0988888888:
```bash
$ promptpay 0988888888 100
```


Expand All @@ -53,7 +61,6 @@ promptpay 0988888888 --amount 100

## Installation (For PHP Application)

![Example](assets/promptpay-qr.png)

You can install the package via composer:

Expand All @@ -63,21 +70,32 @@ composer require farzai/promptpay

## Usage

For example, to generate a QR code for receiving 100 THB from the phone number 0988888888:

```php
use Farzai\PromptPay\Generator;
use Farzai\PromptPay\PromptPay;
use Farzai\PromptPay\Outputs\FilesystemOutput;

$generator = new Generator();
// Generate a QR code for receiving 100 THB from the phone number 0988888888
$imageUri = PromptPay::to('0988888888')
->amount(100)
->toDataUri('png');

$qrCode = $generator->generate(
target: "098-888-8888",
amount: 100
);
// Display the QR code
echo '<img src="' . $imageUri . '" />';
```

Or you can save the QR code to a file:

```php
use Farzai\PromptPay\PromptPay;
use Farzai\PromptPay\Outputs\FilesystemOutput;

// Next, you can save the image to a file:
$qrCode->save('qrcode.png');
$imagePath = PromptPay::to('0988888888')
->amount(100)
->toFile('qr-code.png');

// Or insert it directly into a template:
echo '<img src="' . $qrCode->asDataUri() . '" />';
echo 'QR code saved to ' . $imagePath;
```

## Testing
Expand All @@ -92,7 +110,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re

## Contributing

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
Please see [CONTRIBUTING](https://github.com/farzai/.github/blob/main/CONTRIBUTING.md) for details.

## Security Vulnerabilities

Expand Down
Binary file removed assets/promptpay-qr-cli.png
Binary file not shown.
Binary file modified assets/promptpay-qr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions bin/promptpay
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {


$application = new Symfony\Component\Console\Application();

$application->add(
$command = new Farzai\PromptPay\Commands\CreateQrCode()
);
Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
"authors": [
{
"name": "parsilver",
"email": "parkorn@farzai.com"
"role": "Developer"
}
],
"require": {
"php": "^8.1",
"endroid/qr-code": "^4.8",
"nyholm/psr7": "^1.8",
"psr/http-message": "^1.0|^2.0",
"symfony/console": "^6.2"
"endroid/qr-code": "^5.0.0",
"symfony/console": "^6.0"
},
"require-dev": {
"pestphp/pest": "^1.20",
"pestphp/pest": "^2.34.0",
"laravel/pint": "^1.2",
"spatie/ray": "^1.28",
"farzai/transport": "^1.3",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0"
Expand Down
58 changes: 21 additions & 37 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="Farzai Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Farzai Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
22 changes: 9 additions & 13 deletions src/Commands/CreateQrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@

namespace Farzai\PromptPay\Commands;

use Farzai\PromptPay\Generator;
use Farzai\PromptPay\PromptPay;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class CreateQrCode extends Command
{
protected static $defaultName = 'farzai:promptpay:create-qr-code';

protected function configure()
{
$this
->setName('create-qr-code')
->setDescription('Create QR Code PromptPay for receive')
->addArgument('target', InputArgument::OPTIONAL, 'Target (phone number, citizen id, e-wallet id)')
->addOption('amount', 'a', InputOption::VALUE_OPTIONAL, 'Amount (optional)');
->addArgument('amount', InputArgument::OPTIONAL, 'Amount to receive');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$target = $input->getArgument('target');
$amount = $input->getOption('amount') ?? null;
$amount = $input->getArgument('amount') ?? null;

if (! $target) {
$target = $this->ask(
Expand All @@ -51,15 +49,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln([
...$lines,
'====================================',
'==============================================',
'',
]);

$generator = new Generator();

$generator
->generate($target, $amount)
->asConsole($output);
PromptPay::to($target)
->amount($amount)
->toConsole($output);

return Command::SUCCESS;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Contracts/OutputInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Farzai\PromptPay\Contracts;

interface OutputInterface
{
/**
* Write payload to output
*/
public function write(string $payload): mixed;
}
31 changes: 4 additions & 27 deletions src/Contracts/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,15 @@

namespace Farzai\PromptPay\Contracts;

use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use Symfony\Component\Console\Output\OutputInterface;

interface QrCode
{
/**
* Write qr code to file
*/
public function save(string $path): void;

/**
* Convert qr to data uri
*/
public function asDataUri(): string;

/**
* Convert qr to base64
*/
public function asBase64(): string;

/**
* Convert qr to png
*/
public function asPng(): string;

/**
* Convert qr to response
* Write qr code to output
*/
public function toPsrResponse(): PsrResponseInterface;
public function writeTo(OutputInterface $output): mixed;

/**
* Convert qr to console
* Get qr code payload
*/
public function asConsole(OutputInterface $output): void;
public function __toString(): string;
}
14 changes: 11 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ public function __construct()
}

/**
* @param int|float $amount
* Generate qr code
*/
public function generate(string $target, $amount = null): QrCodeContract
public function generate(string $recipient, $amount = null): QrCodeContract
{
// Remove non-numeric characters
$recipient = preg_replace('/\D/', '', $recipient);

// Validate target
if (! preg_match('/^[0-9]{10,15}$/', $recipient)) {
throw new \InvalidArgumentException('Invalid recipient, must be 10-15 digits');
}

$payload = $this->payloadGenerator->generate(
$target, $amount,
$recipient, $amount,
);

return new QrCode($payload);
Expand Down
Loading