Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Refactor and add new features #30

Merged
merged 1 commit into from
Oct 12, 2023
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
138 changes: 9 additions & 129 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# سامانه مودیان
# Moadian - سامانه مودیان مالیانی

کد PHP - لاراول
کد PHP برای اتصال به سامانه مودیان مالیاتی (نظام پایانه های فروشگاهی و سامانه مودیان).

نظام پایانه‌های فروشگاهی و سامانه مودیان
PHP code for connecting to Moadian Maliati Organization (Store Terminals and Taxpayer System).

## Disclaimer
This software is provided as is without any warranty whatsoever, including accuracy and comprehensiveness. We are not associated with Moadian Maliati Organization and we have worked with the API just as simple clients so we are limited in the amount of support and help we can give regarding the functionality of the API. We have however personally used this package and have successfully submitted tens of thousands of invoices.
Expand All @@ -15,129 +15,9 @@ composer require snapp-market-pro/moadian

## Usage

1. Generate a key pair in the admin panel and use the private key as the private key parameter required in `Moadian` class constructor.
2. Get servers public key from the `INFORMATION_SERVER_GET` API endpoint and use it as the public key parameter in `Moadian` class. (see [this issue](https://github.com/Snapp-Market-Pro/moadian/issues/20#issuecomment-1637061790) for sample CURL request)
3. Sample Usage:

```php
<?php

use SnappMarketPro\Moadian\Constants\PacketType;
use SnappMarketPro\Moadian\Dto\InvoiceBodyDto;
use SnappMarketPro\Moadian\Dto\InvoiceDto;
use SnappMarketPro\Moadian\Dto\InvoiceHeaderDto;
use SnappMarketPro\Moadian\Dto\InvoicePaymentDto;
use SnappMarketPro\Moadian\Dto\Packet;
use SnappMarketPro\Moadian\Moadian;
use Datetime;

require_once __DIR__ . '/vendor/autoload.php';

$username = 'شناسه یکتای مالیاتی';
$orgKeyId = '6a2bcd88-a871-4245-a393-2843eafe6e02';
$privateKey = file_get_contents(__DIR__ . '/private.key');
$publicKey = file_get_contents(__DIR__ . '/public.key'); // this is the public key of tax org, not you!

$moadian = new Moadian(
$publicKey,
$privateKey,
$orgKeyId,
$username
);

$taxId = $moadian->generateTaxId(new DateTime(), 1);

$invoiceHeaderDto = (new InvoiceHeaderDto)
->setIndati2m(1000000)
->setIndatim(1000000)
->setInty(1)
->setFt(null)
->setInno(2)
->setIrtaxid(null)
->setScln(null)
->setSetm(1)
->setTins('5555555555')
->setCap(100)
->setBid(null)
->setInsp(100)
->setTvop(0)
->setBpc(null)
->setTax17(0)
->setTaxid($taxId)
->setInp(1)
->setScc(null)
->setIns(3)
->setBillid(null)
->setTprdis(100)
->setTdis(0)
->setTadis(null)
->setTvam(0)
->setTodam(0)
->setTbill(0)
->setTob(null)
->setTinb(null)
->setSbc(null)
->setBbc(null)
->setBpn(null)
->setCrn(null);


$invoiceBodyDto = (new InvoiceBodyDto)
->setSstid('1111111111')
->setSstt('A')
->setMu(23)
->setAm('2')
->setFee('100')
->setCfee(null)
->setCut(null)
->setExr(null)
->setPrdis('100')
->setDis('100')
->setAdis('0')
->setVra('0')
->setVam('0')
->setOdt(null)
->setOdr(null)
->setOdam(null)
->setOlt(null)
->setOlr(null)
->setOlam(null)
->setConsfee(null)
->setSpro(null)
->setBros(null)
->setTcpbs(null)
->setCop(null)
->setBsrn(null)
->setVop(null)
->setTsstam('100');

$invoicePaymentDto = (new InvoicePaymentDto)
->setIinn("1131244211")
->setAcn("2131244212")
->setTrmn("3131244213")
->setTrn("4131244214")
->setPcn(null)
->setPid(null)
->setPdt(null);

$invoiceDto = (new InvoiceDto)
->setHeader($invoiceHeaderDto)
->setBody([$invoiceBodyDto])
->setPayments([$invoicePaymentDto]);

$packet = (new Packet(PacketType::INVOICE_V01, $invoiceDto))
->setFiscalId($username)
->setDataSignature(null)
->setEncryptionKeyId(null)
->setIv(null)
->setSymmetricKey(null);


$token = $moadian->getToken();

$invoice = $moadian
->setToken($token->getToken())
->sendInvoice($packet);
dd($invoice->getBody()->getContents());
```

1. Generate an RSA key pair according to specifications in moadian admin panel.
2. Upload the public key of that key pair and get a username.
3. Following [get-tax-org-public-key](examples/get-tax-org-public-key.php) get Tax Organization public key and key ID and store them somewhere.
4. Construct the `Moadian` class with your private key, tax org public key, tax org key ID, your username and base url of tax org api if needed.
5. Login and set the received token on `Moadian` class.
6. Send Invoices or Inquire their status by reference numbers following [send-invoices](examples/send-invoices.php) or [inquire-by-reference-numbers](examples/inquire-by-reference-numbers.php).
57 changes: 57 additions & 0 deletions examples/get-tax-org-public-key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

$username = 'A1A2B3';
$taxOrgPublicKey = '';
$privateKey = '';
$taxOrgKeyId = '';
$baseURL = 'https://sandboxrc.tax.gov.ir/';

$moadian = new \SnappMarketPro\Moadian\Moadian(
$taxOrgPublicKey,
$privateKey,
$taxOrgKeyId,
$username,
$baseURL
);

$response = $moadian->getServerInformation();

/*
Response looks something like this:
{
"signature": null,
"signatureKeyId": null,
"timestamp": 11111111111,
"result": {
"uid": "XXXX",
"packetType": "SERVER_INFORMATION",
"data": {
"serverTime": 11111111111,
"publicKeys": [
{
"key": "XXXX",
"id": "XXXX",
"algorithm": "RSA",
"purpose": 1
}
]
},
"encryptionKeyId": null,
"symmetricKey": null,
"iv": null
}
}
*/


// Now you should store this public key in some file and use it whenever you want to interact with moadian API.
// No need to this every request, doing it once should be fine until they change server public key, which is unlikely.
// Also save the key ID in some config or env file and pass it to Moadian class from now on.

// You can use this code to store it in pem format
$publicKey = $response['result']['data']['publicKeys'][0]['key'];
$file = fopen("public.key", 'w+');
fwrite($file, "-----BEGIN PUBLIC KEY-----\n");
fwrite($file, chunk_split($publicKey, 64, "\n"));
fwrite($file, "-----END PUBLIC KEY-----\n");
fclose($file);
82 changes: 82 additions & 0 deletions examples/inquire-by-reference-numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

// You got this when you uploaded your public key to moadian
$username = 'A1B2C3';
// See the get-tax-org-public-key.php example
$taxOrgPublicKey = file_get_contents('moadian-public.key');
// See the get-tax-org-public-key.php example
$taxOrgKeyId = 'XXXX-XXXX-XXXX-XXXX';
// This is your private key, you have previously uploaded its corresponding public key to moadian
$privateKey = file_get_contents('private.key');
// Base URL for moadian API. For production, it is the default value for baseUrl parameter in Moadian class constructor
$baseUrl = 'https://sandboxrc.tax.gov.ir/';

$moadian = new \SnappMarketPro\Moadian\Moadian(
$taxOrgPublicKey,
$privateKey,
$taxOrgKeyId,
$username,
$baseUrl
);

// Login first
$token = $moadian->login();
// Save the token for your future requests so that you don't have to login for every inquiry
// Make sure you use the getExpiresAt to get a new token when your old token expires.
$moadian->setToken($token);

$referenceNumbers = ["c1b111c1-b11e-11ad-111e-cbb1c1111111", "c2b222c2-b22e-22ad-222e-cbb2c2222222"];

$response = $moadian->inquireByReferenceNumbers($referenceNumbers);

/*
Response will look like this:
{
"signature": null,
"signatureKeyId": null,
"timestamp": 1111111111111,
"result": {
"uid": null,
"packetType": "INQUIRY_RESULT",
"data": [
{
"referenceNumber": "c1b111c1-b11e-11ad-111e-cbb1c1111111",
"uid": "XXXX",
"status": "FAILED",
"data": {
"error": [
{
"code": "0301",
"message": "XXXX"
}
],
"warning": [
{
"code": "1403",
"message": "XXXX"
}
],
"success": false
},
"packetType": "receive_invoice_confirm",
"fiscalId": "A1B2C3"
},
{
"referenceNumber": "c2b222c2-b22e-22ad-222e-cbb2c2222222",
"uid": "XXXX",
"status": "SUCCESS",
"data": {
"error": [],
"warning": [],
"success": true
},
"packetType": "receive_invoice_confirm",
"fiscalId": "A1B2C3"
}
],
"encryptionKeyId": null,
"symmetricKey": null,
"iv": null
}
}
*/
Loading