Получить ссылку на оплату заказа. Битрикс API

Получаем ссылку на оплату заказа по ID заказа.

use Bitrix\Main\Loader;
use Bitrix\Sale;

Loader::includeModule("sale");

$orderId = 49; // $orderId - заменить на ID искомого заказа

$order = Sale\Order::load($orderId);

$paymentLink = false;

$paymentCollection = $order->getPaymentCollection();

foreach ($paymentCollection as $payment) {

    $paySystemService = \Bitrix\Sale\PaySystem\Manager::getObjectById($payment->getPaymentSystemId());

    if (!empty($paySystemService)) {
        $arPaySysAction = $paySystemService->getFieldsValues();

        if (
            $paySystemService->getField('NEW_WINDOW') === 'N'
            || $paySystemService->getField('ID')
            == \Bitrix\Sale\PaySystem\Manager::getInnerPaySystemId()
        ) {
            $initResult = $paySystemService->initiatePay($payment, null, \Bitrix\Sale\PaySystem\BaseServiceHandler::STRING);

            if ($initResult->isSuccess()) {
                $arPaySysAction['BUFFERED_OUTPUT'] = $initResult->getTemplate();

                preg_match('/<FORM[^>]+ACTION="([^"]+)"[^>]*>/i', $arPaySysAction['BUFFERED_OUTPUT'], $matches);

                $paymentLink = $matches[1];
            } else {
                $arPaySysAction["ERROR"] = $initResult->getErrorMessages();
            }
        }
    }
}

print_r($paymentLink);

Или

use Bitrix\Main\Loader;
use Bitrix\Sale;

Loader::includeModule("sale");
$orderId = 48;
$order = Sale\Order::load($orderId);
$paymentCollection = $order->getPaymentCollection();
$payment = $paymentCollection[0];
$service = \Bitrix\Sale\PaySystem\Manager::getObjectById($payment->getPaymentSystemId());
$context = \Bitrix\Main\Application::getInstance()->getContext();
$URL_PAYMENT = $service->initiatePay($payment, $context->getRequest(),2)->getPaymentUrl();

print_r($URL_PAYMENT);