function cw_gift_payment_run_processor($params, $return)
{
    $cart =& cw_session_register('cart');
    extract($params);
    if ($params['payment_data']['processor'] != 'payment_gift_certificate') {
        if (!empty($cart['info']['applied_giftcerts'])) {
            cw_gift_giftcerts_process($cart, $doc_ids);
        }
        return $return;
    }
    global $app_catalogs;
    $payment_id = $payment_data['payment_id'];
    $gc_id = trim($userinfo['gc_id']);
    $top_message =& cw_session_register('top_message');
    // Check non existing/updated Gift certificate in the applied_giftcerts array
    if (!empty($cart['info']['applied_giftcerts'])) {
        $_invalid_gcs = cw_gift_check_applied_giftcerts($payment_id);
        if (!empty($_invalid_gcs) && is_array($_invalid_gcs)) {
            $top_message = array('type' => 'E', 'content' => cw_get_langvar_by_name('err_gc_invalid_gcs', array('invalid_gcs' => implode(', ', $_invalid_gcs))));
            cw_header_location($app_catalogs['customer'] . "/index.php?target=cart&mode=checkout&err=fields&payment_id=" . $payment_id);
        }
    }
    if ($cart['info']['total'] == 0) {
        cw_gift_giftcerts_process($cart, $doc_ids);
        return array('code' => 3);
    }
    $gc_error_code = cw_giftcert_check($gc_id);
    if ($gc_error_code == 1) {
        $top_message = array('content' => cw_get_langvar_by_name("err_filling_form"), 'type' => 'E');
        cw_header_location($app_catalogs['customer'] . "/index.php?target=cart&mode=checkout&err=fields&payment_id=" . $payment_id);
    }
    if ($gc_error_code == 2) {
        $top_message = array('content' => cw_get_langvar_by_name('err_gc_used'), 'type' => 'E');
        cw_header_location($app_catalogs['customer'] . '/index.php?target=cart&mode=checkout&err=fields&payment_id=' . $payment_id);
    }
    $gc = cw_giftcert_data($gc_id, true);
    if (empty($gc)) {
        $top_message = array('content' => cw_get_langvar_by_name('err_gc_not_found'), 'type' => 'E');
        cw_header_location($app_catalogs['customer'] . '/index.php?target=cart&mode=checkout&err=fields&payment_id=' . $payment_id);
    }
    $gc_applied = cw_giftcert_apply($gc);
    if (!$gc_applied) {
        $top_message = array('content' => cw_get_langvar_by_name('txt_gc_not_enough_money'), 'type' => 'E');
        cw_header_location($app_catalogs['customer'] . '/index.php?target=cart&mode=checkout&err=fields&payment_id=' . $payment_id);
    }
    $cart['info']['applied_giftcerts'][count($cart['info']['applied_giftcerts']) - 1]['giftcert_cost'] = $cart['info']['total'];
    $cart['info']['giftcert_discount'] += $cart['info']['total'];
    $cart['info']['total'] = 0;
    if ($cart['orders']) {
        foreach ($cart['orders'] as $k => $v) {
            $cart['orders'][$k]['info']['total'] = 0;
        }
    }
    cw_gift_giftcerts_process($cart, $doc_ids);
    $products = cw_call('cw_products_in_cart', array($cart, $userinfo));
    $cart = cw_func_call('cw_cart_calc', array('cart' => $cart, 'products' => $products, 'userinfo' => $userinfo));
    return array('code' => 3);
}
<?php

// Apply giftcert to the cart (AJAX)
if ($action == 'apply_gc') {
    global $app_catalogs;
    $cart =& cw_session_register('cart', array());
    if (empty($cart)) {
        return;
    }
    $top_message =& cw_session_register('top_message');
    $gc_id = trim($gc_id);
    $gc_error_code = cw_giftcert_check($gc_id);
    if ($gc_error_code == 1) {
        $top_message = array('content' => cw_get_langvar_by_name("err_filling_form"), 'type' => 'E', 'anchor' => 'coupon');
    }
    if ($gc_error_code == 2) {
        $top_message = array('content' => cw_get_langvar_by_name('err_gc_used'), 'type' => 'E', 'anchor' => 'coupon');
    }
    if (!$gc_error_code) {
        $gc = cw_giftcert_data($gc_id, true);
        if (empty($gc)) {
            $top_message = array('content' => cw_get_langvar_by_name('err_gc_not_found'), 'type' => 'E', 'anchor' => 'coupon');
        } else {
            $gc_applied = cw_giftcert_apply($gc);
            if (!$gc_applied) {
                $top_message = array('content' => cw_get_langvar_by_name('txt_gc_not_enough_money'), 'type' => 'E', 'anchor' => 'coupon');
            }
        }
    }
    cw_session_save();
    global $action;