klsheng / myinvois-php-sdk
MyInvois SDK for PHP
1.0.5
2024-08-15 03:47 UTC
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- ext-openssl: *
- guzzlehttp/guzzle: ~7.0|~6.0|~5.0|~4.0
- psr/http-client: ^1.0 || ^2.0
- sabre/xml: ^4.0
Requires (Dev)
- phpunit/phpunit: ~8|~9
README
一个面向对象的PHP库,用于创建由MyInvois系统支持的定制UBL v2.1格式。
此SDK最初需要UBL-Invoice包。然而,MyInvois系统并不完全支持UBL v2.1格式,因此根据原始作者重新创建UBL包以支持MyInvois系统。
待办事项
- 以税务系统用户身份登录
- 以中介系统用户身份登录
- 获取所有文档类型
- 获取文档类型
- 获取文档类型版本
- 获取通知
- 验证纳税人的TIN
- 提交文件(发票)
- 提交文件(贷项通知)
- 提交文件(借项通知)
- 提交文件(退款通知)
- 提交文件(自开发票)
- 提交文件(自开贷项通知)
- 提交文件(自开借项通知)
- 提交文件(自开退款通知)
- 取消文件
- 拒绝文件
- 获取最近文件
- 获取提交
- 获取文件
- 获取文件详细信息
- 搜索文件
- 数字签名
- 必填字段验证
- 获取文件的二维码URL
如何获取沙箱的客户端ID和客户端密钥?
要获取您的客户端ID和客户端密钥,请将以下信息通过电子邮件请求发送至sdkmyinvois@hasil.gov.my
- 纳税人TIN
- 商业登记号码
- 公司名称
- 公司电子邮件地址
- ERP系统名称
安装和用法
此包需要PHP 7.4或更高版本。
安装此扩展的首选方法是composer。
要安装,请运行以下之一
$ composer require klsheng/myinvois-php-sdk "*"
或
"klsheng/myinvois-php-sdk": "*"
将其添加到您的composer.json
文件的require
部分。
依赖项
此包需要以下扩展才能正常工作
如果您使用Composer,这些依赖项应该会自动处理。如果您手动安装,请确保这些扩展可用。
用法
您可以在CreateDocumentExample处参考示例,创建由MyInvois系统支持的UBL v2.1文档。
示例用法
以税务系统用户身份登录
use Klsheng\Myinvois\MyInvoisClient; $prodMode = false; $client = new MyInvoisClient('client_id', 'client_secret', $prodMode); $client->login(); $access_token = $client->getAccessToken(); // Store $access_token somewhere to re-use it again within 1hour // OR $client->setAccessToken('access_token');
以中介系统用户身份登录
use Klsheng\Myinvois\MyInvoisClient; $prodMode = false; $client = new MyInvoisClient('client_id', 'client_secret', $prodMode); $client->login($onbehalfof); $access_token = $client->getAccessToken(); // Store $access_token somewhere to re-use it again within 1hour // OR $client->setAccessToken('access_token'); $client->setOnbehalfof($onbehalfof);
获取所有文档类型
$response = $client->getAllDocumentTypes();
获取文档类型
$response = $client->getDocumentType($id);
获取文档类型版本
$response = $client->getDocumentTypeVersion($id, $versionId);
获取通知
$response = $client->getNotifications(); // OR $dateFrom = new \DateTime('2015-02-13T14:20:10Z'); $dateTo = '2015-02-13T14:20:10Z'; $type = '2'; $language = 'en'; $status = 'delivered'; $channel = 'email'; $pageNo = 3; $pageSize = 20; $response = $client->getNotifications($dateFrom, $dateTo, $type, $language, $status, $channel, $pageNo, $pageSize);
验证纳税人的TIN
$tin = 'C00000000000'; $idType = 'NRIC'; $idValue = '770625015324'; $response = $client->validateTaxPayerTin($tin, $idType, $idValue);
提交JSON文档
use Klsheng\Myinvois\Helper\MyInvoisHelper; use Klsheng\Myinvois\Example\Ubl\CreateDocumentExample; use Klsheng\Myinvois\Ubl\Constant\InvoiceTypeCodes; $id = 'INV20240418105410'; $supplier = [ 'TIN' => 'C00000000000', 'BRN' => '0000000-T', ]; $customer = [ 'TIN' => 'C00000000000', 'BRN' => '0000000-T', ]; $delivery = [ 'TIN' => 'C00000000000', 'BRN' => '0000000-T', ]; // Example contains hardcoded test data, you may need to modify it yourself $example = new CreateDocumentExample(); $invoice = $example->createJsonDocument(InvoiceTypeCodes::INVOICE, $id, $supplier, $customer, $delivery, true, '/path/to/eInvoice.crt', '/path/to/eInvoice.key'); // If you are using p12 or pfx file, you may use following code to generate document /* $invoice = $example->createJsonDocument(InvoiceTypeCodes::INVOICE, $id, $supplier, $customer, $delivery, true, '/path/to/eInvoice.p12', null, 'passphrase'); */ // In case there is DS326 error, you may use following code to re-arrange issuer key sequences // Default sequence -> ['CN', 'E', 'OU', 'O', 'C'] /* $invoice = $example->createJsonDocument(InvoiceTypeCodes::INVOICE, $id, $supplier, $customer, $delivery, true, '/path/to/eInvoice.p12', null, 'passphrase', ['C', 'O', 'OU', 'E', 'CN']); */ $documents = []; $document = MyInvoisHelper::getSubmitDocument($id, $invoice); $documents[] = $document; $response = $client->submitDocument($documents);
提交XML文档
use Klsheng\Myinvois\Helper\MyInvoisHelper; use Klsheng\Myinvois\Example\Ubl\CreateDocumentExample; use Klsheng\Myinvois\Ubl\Constant\InvoiceTypeCodes; $id = 'INV20240418105410'; $supplier = [ 'TIN' => 'C00000000000', 'BRN' => '0000000-T', ]; $customer = [ 'TIN' => 'C00000000000', 'BRN' => '0000000-T', ]; $delivery = [ 'TIN' => 'C00000000000', 'BRN' => '0000000-T', ]; // Example contains hardcoded test data, you may need to modify it yourself $example = new CreateDocumentExample(); $invoice = $example->createXmlDocument(InvoiceTypeCodes::INVOICE, $id, $supplier, $customer, $delivery, true, '/path/to/eInvoice.crt', '/path/to/eInvoice.key'); // If you are using p12 or pfx file, you may use following code to generate document /* $invoice = $example->createXmlDocument(InvoiceTypeCodes::INVOICE, $id, $supplier, $customer, $delivery, true, '/path/to/eInvoice.p12', null, 'passphrase'); */ // In case there is DS326 error, you may use following code to re-arrange issuer key sequences // Default sequence -> ['CN', 'E', 'OU', 'O', 'C'] /* $invoice = $example->createXmlDocument(InvoiceTypeCodes::INVOICE, $id, $supplier, $customer, $delivery, true, '/path/to/eInvoice.p12', null, 'passphrase', ['C', 'O', 'OU', 'E', 'CN']); */ $documents = []; $document = MyInvoisHelper::getSubmitDocument($id, $invoice); $documents[] = $document; $response = $client->submitDocument($documents);
取消文件
$reason = 'Customer refund'; $response = $client->cancelDocument($id, $reason);
拒绝文件
$reason = 'Customer reject'; $response = $client->rejectDocument($id, $reason);
获取最近文件
$response = $client->getRecentDocuments(); // OR $pageNo = 3; $pageSize = 20; $submissionDateFrom = '2022-11-25T01:59:10Z'; $submissionDateTo = new \DateTime('2022-12-22T23:59:59Z'); $issueDateFrom = null; $issueDateTo = null; $direction = 'Sent'; $status = 'Valid'; $documentType = '01'; $receiverId = 'A12345678'; $receiverIdType = 'PASSPORT'; $receiverTin = 'C2584563200'; $issuerId = null; $issuerIdType = null; $issuerTin = null; $response = $client->getRecentDocuments($pageNo, $pageSize, $submissionDateFrom, $submissionDateTo, $issueDateFrom, $issueDateTo, $direction, $status, $documentType, $receiverId, $receiverIdType, $receiverTin, $issuerId, $issuerIdType, $issuerTin);
获取提交
$response = $client->getSubmission($tid);
获取文件
$response = $client->getDocument($id);
获取文件详细信息
$response = $client->getDocumentDetail($id);
搜索文件
$response = $client->searchDocuments(); // OR $id = 'F9D425P6DS7D8IU'; $submissionDateFrom = null; $submissionDateTo = null; $continuationToken = 'Y4RWK9617T0TJNRBF4CSVGQG10'; $pageSize = 100; $issueDateFrom = null; $issueDateTo = null; $direction = 'Sent'; $status = 'Valid'; $documentType = '01'; $receiverId = null; $receiverIdType = null; $receiverTin = null; $issuerTin = null; $response = $client->searchDocuments($id, $submissionDateFrom, $submissionDateTo, $continuationToken, $pageSize, $issueDateFrom, $issueDateTo, $direction, $status, $documentType, $receiverId, $receiverIdType, $receiverTin, $issuerTin);
获取文件的二维码URL
use Klsheng\Myinvois\MyInvoisClient; $prodMode = false; $client = new MyInvoisClient('client_id', 'client_secret', $prodMode); $id = '0000000000000'; // Document's UUID $longId = '11111111111111'; // Document's Long Id $url = $client->generateDocumentQrCodeUrl($id, $longId);