1C-Bitrix Integration with MoeDelo
MoeDelo is a cloud-based accounting and tax service for small businesses. Its target audience is sole proprietors and small LLCs on a simplified tax regime (STS) who sell through a Bitrix-powered online store. Manually transferring orders into MoeDelo is a common pain point: each order requires creating a sale document, adding the counterparty by hand, and generating an invoice separately. MoeDelo provides a public API to automate these operations.
MoeDelo API
API documentation: https://api.moedelo.org/. Authorization via API key (Header: X-DM-Auth-Token). Main resources: counteragents (counterparties), documents/sale (sale documents), invoices (invoices), payments/income (incoming payments).
class MoeDeloClient
{
private string $apiKey;
private string $baseUrl = 'https://api.moedelo.org';
public function request(string $method, string $endpoint, array $body = [], array $query = []): array
{
$url = $this->baseUrl . $endpoint;
if ($query) {
$url .= '?' . http_build_query($query);
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
"X-DM-Auth-Token: {$this->apiKey}",
],
CURLOPT_POSTFIELDS => in_array($method, ['POST', 'PUT', 'PATCH'])
? json_encode($body) : null,
]);
$json = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new \RuntimeException("MoeDelo API {$httpCode}: {$json}");
}
return json_decode($json, true) ?? [];
}
}
Counterparties
For individual customers (B2C orders without business details), create a counterparty with type individual:
public function findOrCreateContragent(\Bitrix\Sale\Order $order): int
{
$props = $order->getPropertyCollection();
$email = $props->getUserEmail();
$name = $props->getBuyerName();
$phone = $props->getPhone();
$inn = $props->getItemByOrderPropertyCode('INN')?->getValue();
// Search by email
$search = $this->client->request('GET', '/api/v1/counteragents', [], ['search' => $email]);
foreach ($search['data'] ?? [] as $ca) {
if ($ca['email'] === $email) {
return $ca['id'];
}
}
// Create
$isLegal = !empty($inn);
$payload = $isLegal ? [
'type' => 'legal_entity',
'name' => $props->getItemByOrderPropertyCode('COMPANY')->getValue(),
'inn' => $inn,
'kpp' => $props->getItemByOrderPropertyCode('KPP')?->getValue() ?? '',
'email' => $email,
'phone' => $phone,
] : [
'type' => 'individual',
'fullname' => $name,
'email' => $email,
'phone' => $phone,
];
$result = $this->client->request('POST', '/api/v1/counteragents', $payload);
return $result['id'];
}
Sale Document (Invoice / Delivery Note)
public function createSaleDocument(\Bitrix\Sale\Order $order, int $contragentId): int
{
$items = [];
foreach ($order->getBasket() as $basketItem) {
// VAT from product properties
$vatRate = $this->resolveVatRate($basketItem->getProductId());
$vatValue = round($basketItem->getPrice() * $basketItem->getQuantity()
* ($vatRate / (100 + $vatRate)), 2);
$items[] = [
'name' => $basketItem->getField('NAME'),
'quantity' => $basketItem->getQuantity(),
'unit' => 'pcs.',
'price' => $basketItem->getPrice(),
'total' => round($basketItem->getPrice() * $basketItem->getQuantity(), 2),
'vat_rate' => $vatRate, // 0, 10, 20
'vat_value' => $vatValue,
];
}
// Order-level discount
$discount = $order->getField('PRICE_DELIVERY') > 0 ? [
'name' => 'Shipping',
'amount' => $order->getField('PRICE_DELIVERY'),
] : null;
$doc = $this->client->request('POST', '/api/v1/documents/sale', [
'date' => date('Y-m-d'),
'number' => $order->getField('ACCOUNT_NUMBER'),
'counteragent_id' => $contragentId,
'items' => $items,
'shipping' => $discount,
'comment' => 'Order from website #' . $order->getField('ACCOUNT_NUMBER'),
]);
return $doc['id'];
}
Invoice for Payment
For B2B orders, generate an invoice after creating the sale document:
public function createInvoice(int $documentId, \Bitrix\Sale\Order $order): string
{
$invoice = $this->client->request('POST', '/api/v1/invoices', [
'document_id' => $documentId,
'due_date' => date('Y-m-d', strtotime('+5 days')),
]);
// Retrieve invoice PDF and attach to the order in Bitrix
$pdfUrl = $invoice['pdf_url'] ?? '';
if ($pdfUrl) {
$this->attachInvoicePdfToOrder($order->getId(), $pdfUrl);
}
return $invoice['number'] ?? '';
}
Incoming Payments
AddEventHandler('sale', 'OnSalePaymentPaid', function(\Bitrix\Sale\Payment $payment) {
$order = $payment->getOrder();
$docId = MoeDeloSyncTable::getDocumentId($order->getId());
if (!$docId) return;
(new MoeDeloSyncService())->registerPayment($docId, $payment);
});
public function registerPayment(int $docId, \Bitrix\Sale\Payment $payment): void
{
$this->client->request('POST', '/api/v1/payments/income', [
'date' => date('Y-m-d'),
'amount' => $payment->getSum(),
'document_id' => $docId,
'type' => $payment->getPaySystem()->getField('CODE') === 'cash'
? 'cash' : 'bank',
'comment' => 'Payment for order #' . $payment->getOrder()->getField('ACCOUNT_NUMBER'),
]);
}
Automation via Events
| Bitrix Event | Action in MoeDelo |
|---|---|
| Order created (status "New") | Create counterparty + sale document |
| Order paid | Record incoming payment |
| Order cancelled | Create refund document |
| Order "Awaiting payment" | Issue invoice (for B2B) |
Scope of Work
- PHP client for MoeDelo API
- Searching and creating counterparties (individuals and legal entities)
- Creating sale documents and invoices
- Recording payments and refunds
- Synchronization table, error handling and retries
Timeline: 3–5 weeks for basic integration. 6–8 weeks including refunds, PDF invoice attachment, and monitoring.







