rikudou / czqrpayment
捷克账户的二维码支付库
Requires
- php: ^7.3 | ^8.0
- rikudou/iban: ^1.1.1
- rikudou/qr-payment-interface: ^1.0
- rikudou/qr-payment-qr-code-provider: ^1.0
Requires (Dev)
- endroid/qr-code: ^3.2
- friendsofphp/php-cs-fixer: ^2.18
- php-coveralls/php-coveralls: ^2.1
- phpstan/phpstan: ^0.12.82
- phpunit/phpunit: ^9.5
Suggests
- endroid/qr-code: For getting the qr code image
README
一个简单的库,用于生成捷克共和国的二维码支付码。所有方法均在源代码中进行了文档说明。
使用 Symfony?请参阅 QR Payment Bundle。
安装
通过 composer: composer require rikudou/czqrpayment
使用方法
您可以为实现 \Rikudou\Iban\Iban\IbanInterface 接口的对象创建二维码支付,或从账户号和银行账户(然后转换为 \Rikudou\Iban\Iban\CzechIbanAdapter 对象)创建。
有关接口和类的描述,请参阅 rikudou/iban,这些类允许您例如验证 iban 和/或银行账户号和银行代码。
从 IbanInterface 实现类
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\Iban\Iban\IBAN; use Rikudou\Iban\Iban\CzechIbanAdapter; // initialized with IBAN directly $payment = new QrPayment(new IBAN('CZ5530300000001325090010')); // initialized from Czech account number and bank code $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030')); // the IBAN classes don't use strict typing so you can also use implicit conversion like this // beware of bank codes that start with zero, those need to always be supplied as a string (like 0300) $payment = new QrPayment(new CzechIbanAdapter(1325090010, 3030));
直接从账户号和银行代码
<?php use Rikudou\CzQrPayment\QrPayment; $payment = QrPayment::fromAccountAndBankCode('1325090010', '3030'); // the class does not use strict typing so you can also use implicit conversion like this // beware of bank codes that start with zero, those need to always be supplied as a string (like 0300) $payment = QrPayment::fromAccountAndBankCode(1325090010, 3030);
设置支付详情
设置支付详情有两种方法。您可以在关联数组中设置,或者使用类中提供的方法。
使用关联数组
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\CzQrPayment\Options\QrPaymentOptions; use Rikudou\Iban\Iban\CzechIbanAdapter; $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030'), [ QrPaymentOptions::VARIABLE_SYMBOL => 123456, QrPaymentOptions::AMOUNT => 100, QrPaymentOptions::CURRENCY => "CZK", QrPaymentOptions::DUE_DATE => date("Y-m-d", strtotime("+14 days")) ]); // or you can assign the options later via setOptions() $payment->setOptions([ QrPaymentOptions::VARIABLE_SYMBOL => 123456, QrPaymentOptions::AMOUNT => 100, QrPaymentOptions::CURRENCY => "CZK", QrPaymentOptions::DUE_DATE => date("Y-m-d", strtotime("+14 days")), QrPaymentOptions::INSTANT_PAYMENT => true, ]);
使用方法
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\Iban\Iban\CzechIbanAdapter; $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030')); $payment ->setVariableSymbol(123456) ->setAmount(100) ->setCurrency("CZK") ->setDueDate(new DateTimeImmutable('+14 days')) ->setInstantPayment(true);
异常
抛出异常的描述
__construct()InvalidArgumentException- 如果您在构造函数中提供了选项,而其中任何一个选项不存在\Rikudou\CzQrPayment\Exception\InvalidValueException- 如果选项中的任何一个包含星号TypeError- 如果任何值在标准 php 转换后无法分配给属性
setOptions()- 与构造函数相同getQrString()\Rikudou\CzQrPayment\Exception\InvalidValueException- 如果选项中的任何一个包含星号或 iban 无效
getQrImage()\Rikudou\CzQrPayment\Exception\MissingLibraryException- 如果缺少 endroid/qr-code 库
所有这些异常(除 InvalidArgumentException 和 TypeError 之外)都扩展了基本 \Rikudou\CzQrPayment\Exception\QrPaymentException。
二维码图像
此库提供了使用其姐妹库 rikudou/qr-payment-qr-code-provider 的多种二维码图像实现。如果安装了任何受支持的二维码生成库,则方法 getQrCode() 将返回一个 \Rikudou\QrPaymentQrCodeProvider\QrCode 实例,可用于获取包含生成的二维码支付数据的图像。
<?php use Rikudou\CzQrPayment\QrPayment; use Endroid\QrCode\QrCode; $payment = new QrPayment(...); $qrCode = $payment->getQrCode(); // get the raw image data and display them in the browser header('Content-Type: image/png'); echo $qrCode->getRawString(); // use in an img html tag echo "<img src='{$qrCode->getDataUri()}'>"; // write to a file $qrCode->writeToFile('/tmp/some-file.png'); // get the raw object from the underlying system $raw = $qrCode->getRawObject(); // let's assume we're using endroid/qr-code v4 assert($raw instanceof QrCode); // do some custom transformations $raw->setLabelFontSize(15); // the object is still referenced by the adapter, meaning we can now render it the same way as before echo "<img src='{$qrCode->getDataUri()}'>";
公共方法列表
构造函数
参数
\Rikudou\Iban\Iban\IbanInterface $iban必需 - 支付的 IBANarray|null $options- 选项数组。可以使用辅助类QrPaymentOptions用于选项名称。
示例
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\CzQrPayment\Options\QrPaymentOptions; use Rikudou\Iban\Iban\CzechIbanAdapter; $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030')); // or with options $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030'), [ QrPaymentOptions::AMOUNT => 100 ]);
setOptions()
设置选项,如果您不想在构造函数中设置它们,则很有用。
参数
array $options必需 - 与构造函数参数$options相同
返回
返回自身,您可以使用此方法进行链式调用。
示例
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\CzQrPayment\Options\QrPaymentOptions; use Rikudou\Iban\Iban\CzechIbanAdapter; $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030')); $payment->setOptions([ QrPaymentOptions::AMOUNT => 100 ]);
getIban()
返回 iban。
返回
\Rikudou\Iban\Iban\IbanInterface
示例
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\Iban\Iban\CzechIbanAdapter; $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030')); $myIBAN = $payment->getIban();
getQrString()
返回应在 QR 图像中编码的字符串。
返回
字符串
示例
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\CzQrPayment\Options\QrPaymentOptions; use Rikudou\Iban\Iban\CzechIbanAdapter; $payment = new QrPayment(new CzechIbanAdapter('1325090010', '3030'), [ QrPaymentOptions::AMOUNT => 100, QrPaymentOptions::VARIABLE_SYMBOL => 1502, QrPaymentOptions::DUE_DATE => new DateTimeImmutable('+14 days'), ]); $qrString = $payment->getQrString(); // SPD*1.0*ACC:CZ5530300000001325090010*AM:100.00*CC:CZK*X-PER:7*X-VS:1502*DT:20210413
静态 fromAccountAndBankCode()
返回一个从账户和银行代码创建的新支付对象实例。基本上是new QrPayment(new CzechIbanAdapter())的别名。
参数
string $accountNumber必需 - 账号string $bankCode必需 - 银行代码
返回
返回自身,您可以使用此方法进行链式调用。
示例
<?php use Rikudou\CzQrPayment\QrPayment; $payment = QrPayment::fromAccountAndBankCode('1325090010', '3030'); // do all the other stuff
getQrImage()
通过建议的第三方库返回QR码。
返回
\Endroid\QrCode\QrCode
示例
<?php use Rikudou\CzQrPayment\QrPayment; use Rikudou\CzQrPayment\Options\QrPaymentOptions; $payment = QrPayment::fromAccountAndBankCode('1325090010', '3030')->setOptions([ QrPaymentOptions::AMOUNT => 100 ]); header('Content-Type: image/png'); echo $payment->getQrImage()->writeString();
选项
这是您可以设置的选项列表。
int $variableSymbol- 变量符号,没有默认值int $specificSymbol- 特定符号,没有默认值int $constantSymbol- 常量符号,没有默认值string $currency- 货币的ISO 4217代码,默认为CZKstring $comment- 支付注释,没有默认值int $repeat- 如果支付失败,应重复支付的天数,默认为7string $internalId- 支付的内部ID,没有默认值DateTimeInterface dueDate- 支付的到期日,没有默认值float $amount- 支付金额,不应超过2位小数,没有默认值string $country- 国家ISO 3166-1 alpha-2代码,默认为CZbool $instantPayment- 是否应将支付作为即时支付而不是标准支付(取决于银行支持)
所有这些选项都可以使用QrPaymentOptions辅助类作为构造函数的常量、setOptions()或作为方法来设置。
例如,可以使用常量QrPaymentOptions::AMOUNT或使用方法setAmount()在数组中设置amount。