Answer the question
In order to leave comments, you need to log in
Why is the order payment flag not set when using the D7 API?
I ran into a problem when creating payments for an order through D7.
Sometimes, when creating payments (there may be several payments), which fully cover the order amount, the order payment flag is not automatically set.
The code snippet is given below:
<?
$order = Sale\Order::loadByAccountNumber(1000);
$paymentCollection = $order->getPaymentCollection();
$paySystemService = PaySystem\Manager::getObjectById(1);
// цикл для каждой из импортированных оплат
foreach ($importOrderPayments as $importOrderPayment)
{
$paymentExists = find_payment($importOrderPayment['Document']) // определяется существование оплаты: возвращается id оплаты, либо false если оплаты нет
if ($paymentExists === false) // оплаты не существует — создаём
{
$payment = $paymentCollection->createItem();
$payment->setFields([
'PAY_SYSTEM_ID' => $paySystemService->getField("PAY_SYSTEM_ID"),
'PAY_SYSTEM_NAME' => $paySystemService->getField("NAME"),
'DATE_PAID' => \Bitrix\Main\Type\DateTime::createFromTimestamp($importOrderPayment['Date']),
'SUM' => $importOrderPayment['FullPrice'],
'PAY_VOUCHER_NUM' => $importOrderPayment['Document'],
'PAY_VOUCHER_DATE' => \Bitrix\Main\Type\DateTime::createFromTimestamp($importOrderPayment['Date']),
'COMMENTS' => $importOrderPayment['Comments'],
'PAID' => 'Y',
'COMPANY_ID' => 1,
'UPDATED_1C' => 'Y'
]);
}
else // оплата существует - обновим
{
$payment = $paymentCollection->getItemById($paymentExists);
$payment->setFieldNoDemand('SUM', $importOrderPayment['FullPrice']);
$payment->setFields([
'PAY_SYSTEM_ID' => $paySystemService->getField("PAY_SYSTEM_ID"),
'PAY_SYSTEM_NAME' => $paySystemService->getField("NAME"),
'DATE_PAID' => \Bitrix\Main\Type\DateTime::createFromTimestamp($importOrderPayment['Date']),
'PAY_VOUCHER_NUM' => $importOrderPayment['Document'],
'PAY_VOUCHER_DATE' => \Bitrix\Main\Type\DateTime::createFromTimestamp($importOrderPayment['Date']),
'COMMENTS' => $importOrderPayment['Comments'],
'PAID' => 'Y',
'COMPANY_ID' => 1,
'UPDATED_1C' => 'Y'
]);
}
}
$order->save();
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question