rikudou / skqrpayment
斯洛伐克账户的QR支付库
Requires
- php: ^7.3 || ^8.0
- rikudou/iban: ^1.0
- rikudou/qr-payment-interface: ^1.0
- rikudou/qr-payment-qr-code-provider: ^1.0
Requires (Dev)
- endroid/qr-code: ^3.9
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.32
- phpunit/phpunit: ^8.5
Suggests
- endroid/qr-code: For getting QR code image
- rikudou/pay-by-square-decoder: If you want to decode Pay By Square encoded data
README
此库生成一个字符串,可以嵌入到QR码中,并且可以被所有斯洛伐克银行识别。
此库要求在底层系统上存在xz
二进制文件。
安装
composer require rikudou/skqrpayment
使用方法
使用或不用IBAN创建一个实例。所有IBAN必须是\Rikudou\Iban\Iban\IbanInterface
的实例。
<?php use rikudou\SkQrPayment\QrPayment; use Rikudou\Iban\Iban\IBAN; use rikudou\SkQrPayment\Iban\IbanBicPair; // without IBANs $payment = new QrPayment(); // one IBAN $payment = new QrPayment(new IBAN('SK1234567890123456')); // multiple IBANs, there can be as many as you want $payment = new QrPayment( new IBAN('SK1234567890123456'), new IBAN('SK1234567890123456'), new IbanBicPair('SK1234567890123456', 'BANKBIC') );
目前有三种IbanInterface
实现
\Rikudou\Iban\Iban\IBAN
- 接受IBAN作为字符串的通用IBAN类\rikudou\SkQrPayment\Iban\IbanBicPair
- 如果银行不在映射中,允许您提供自己的BIC(Swift)代码\Rikudou\Iban\Iban\CzechIbanAdapter
- 可以从本地捷克格式(账户号和银行代码)构建的IBAN
您还可以从字符串构造新的实例
<?php use rikudou\SkQrPayment\QrPayment; $payment = QrPayment::fromIBAN('SK1234567890123456');
构建后更改IBAN
如果您想在构建后添加/删除IBAN,请使用addIban()
、removeIban()
和setIbans()
方法。
<?php use rikudou\SkQrPayment\QrPayment; use Rikudou\Iban\Iban\IBAN; use rikudou\SkQrPayment\Iban\IbanBicPair; $payment = new QrPayment(); $iban1 = new IBAN('SK1234567890123456'); $iban2 = new IBAN('SK6543210987654321'); $payment ->addIban($iban1) ->addIban($iban2); // object now contains both IBANs $payment ->removeIban($iban2); // only the first IBAN is now present in the object // You don't have to store the object, the ibans are considered the same if the string representation is the same: $payment ->removeIban(new IbanBicPair('SK1234567890123456')); // the object now doesn't contain any IBAN $payment->setIbans([ $iban1, $iban2 ]);
设置选项
您可以使用setOptions()
方法或相应的setter设置所有选项。
您可以使用
\rikudou\SkQrPayment\Payment\QrPaymentOptions
常量作为选项名称
通过setOptions()
设置选项
<?php use rikudou\SkQrPayment\QrPayment; use rikudou\SkQrPayment\Payment\QrPaymentOptions; use Rikudou\Iban\Iban\IBAN; $payment = new QrPayment(); $payment->setOptions([ QrPaymentOptions::AMOUNT => 100, QrPaymentOptions::COMMENT => 'payment', QrPaymentOptions::CONSTANT_SYMBOL => 123, QrPaymentOptions::COUNTRY => 'SK', QrPaymentOptions::CURRENCY => 'EUR', QrPaymentOptions::DUE_DATE => new DateTime('+1 week'), QrPaymentOptions::INTERNAL_ID => '456', QrPaymentOptions::PAYEE_NAME => 'John Doe', QrPaymentOptions::SPECIFIC_SYMBOL => 789, QrPaymentOptions::VARIABLE_SYMBOL => 012, QrPaymentOptions::XZ_PATH => '/path/to/xz', QrPaymentOptions::IBANS => [ new IBAN('SK1234567890123456') ] ]);
使用流畅的setter进行设置
<?php use rikudou\SkQrPayment\QrPayment; use Rikudou\Iban\Iban\IBAN; $payment = new QrPayment(); $payment ->setAmount(100) ->setComment('payment') ->setConstantSymbol('123') ->setCountry('SK') ->setCurrency('EUR') ->setDueDate(new DateTime('+1 week')) ->setInternalId('456') ->setPayeeName('John Doe') ->setSpecificSymbol('789') ->setVariableSymbol('012') ->setXzBinary('/path/to/xz') ->setIbans([ new IBAN('SK01234567890123456') ]);
默认值
提供了一些选项的默认值
currency
- EURcountry
- SKdueDate
- 当前日期和时间
此外,这些属性不是必需的
variableSymbol
specificSymbol
constantSymbol
comment
internalId
payeeName
QR码图片
此库通过其姐妹库rikudou/qr-payment-qr-code-provider提供了许多QR码图片的实现。如果已安装任何受支持的QR码生成库,则方法getQrCode()
将返回一个\Rikudou\QrPaymentQrCodeProvider\QrCode
的实例,该实例可用于获取包含生成的QR支付数据的图片。
<?php use rikudou\SkQrPayment\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()}'>";
xz二进制文件
由于“通过Square支付”标准使用lzma1,它没有PHP绑定,因此需要调用xz二进制文件。
如果您将其放在标准PATH中,此库应能够自己找到它,如果没有,您必须使用setXzBinary()
手动设置路径。
如果您想实现获取二进制路径的自定义逻辑,您可以创建一个实现\rikudou\SkQrPayment\Xz\XzBinaryLocatorInterface
的自定义类,并通过在支付对象中使用setXzBinaryLocator()
设置对象。
获取结果
一旦您配置了所有选项,只需调用getQrString()
并使用您喜欢的QR码库处理它即可。
或者,如果您使用 endroid/qr-code
,可以调用 getQrImage()
来获取 \Endroid\QrCode\QrCode
实例。
<?php use rikudou\SkQrPayment\QrPayment; use Rikudou\Iban\Iban\IBAN; $payment = new QrPayment(new IBAN('SK6807200002891987426353')); $payment ->setAmount(500) ->setVariableSymbol('123456') ->setDueDate(new DateTime('+1 week')) ; $qrString = $payment->getQrString(); // $qrString now holds the string to embed inside the QR code, in this example: // 0004U0001M8GLP3E8KPT058IQ99QISMB02IH36MOD4BCKQQGVDE4641AOA2NURPCOPSALFG0LPG1C6N0E2JMC7RG2F4L2E57OCSHOUROGHOC8VTTPHHRFHU6VFTM8N80 $qrCode = $payment->getQrImage(); // send to browser header('Content-Type: ' . $qrCode->getContentType()); echo $qrCode->writeString();
异常
所有异常都扩展自基本类 \rikudou\SkQrPayment\Exception\QrPaymentException
。
\rikudou\SkQrPayment\Exception\DictionaryNotFoundException
- 当您没有提供 BIC 且没有给定国家代码的映射(目前只有CZ
和SK
)时- 继承自
\rikudou\SkQrPayment\Exception\BicNotFoundException
- 继承自
\rikudou\SkQrPayment\Exception\BicNotFoundException
- 当您没有提供 BIC 且在提供的映射中找不到银行时\rikudou\SkQrPayment\Exception\InvalidTypeException
- 当您尝试使用fromIBAN()
创建实例且 IBAN 不是一个QrPaymentInterface
实例或字符串时\rikudou\SkQrPayment\Exception\QrPaymentException
- 基本异常- 当您调用
getQrImage()
但没有安装endroid/qr-code
库时 - 当您调用
getQrString()
而没有提供任何 IBAN 时 - 当您调用
getQrString()
或getXzBinary()
且文件系统中没有xz
二进制文件时 - 当您使用不是
IbanInterface
实例或字符串的参数创建IbanBicPair
实例时 - 当您创建
IbanBicPair
实例或调用getQrString()
时,任何 IBAN 都不是有效的
- 当您调用