Пример #1
0
/**
 * Create a license key.
 *
 * @api
 *
 * @param array $args        {
 *
 * @type string $key         The license key to be used. If empty, one will be
 *       generated.
 * @type int    $transaction Transaction ID. If empty, one will be manually
 *       generated
 * @type int    $product     Product ID.
 * @type int    $customer    Customer ID.
 * @type string $status      The key's status. Accepts 'active', 'expired',
 *       'disabled'
 * @type float  $paid        If manually generating a transaction, the amount
 *       paid.
 * @type int    $limit       Activation limit.
 * @type string $expires     Expiration date. Pass null or empty string for
 *       forever.
 * @type string $date        When the transaction occurred. GMT.
 * }
 *
 * @return \ITELIC\Key|WP_Error
 */
function itelic_create_key($args)
{
    $defaults = array('key' => '', 'transaction' => '', 'product' => '', 'customer' => '', 'status' => '', 'paid' => '');
    $args = ITUtility::merge_defaults($args, $defaults);
    $product = itelic_get_product($args['product']);
    if (!$product->has_feature('licensing')) {
        return new WP_Error('invalid_product', __("Product does not have licensing enabled.", \ITELIC\Plugin::SLUG));
    }
    $customer = it_exchange_get_customer($args['customer']);
    if (!$customer) {
        return new WP_Error('invalid_customer', __("Invalid customer", \ITELIC\Plugin::SLUG));
    }
    $transaction = it_exchange_get_transaction($args['transaction']);
    if (!$args['transaction']) {
        if (!function_exists('it_exchange_manual_purchases_addon_transaction_uniqid')) {
            return new WP_Error('no_manual_purchases', __("Manual purchases add-on is not installed.", \ITELIC\Plugin::SLUG));
        }
        // Grab default currency
        $settings = it_exchange_get_option('settings_general');
        $currency = $settings['default-currency'];
        $description = array();
        $product_id = $product->ID;
        $itemized_data = apply_filters('it_exchange_add_itemized_data_to_cart_product', array(), $product_id);
        if (!is_serialized($itemized_data)) {
            $itemized_data = maybe_serialize($itemized_data);
        }
        $key = $product_id . '-' . md5($itemized_data);
        $products[$key]['product_base_price'] = $product->get_feature('base-price');
        $products[$key]['product_subtotal'] = $products[$key]['product_base_price'];
        //need to add count
        $products[$key]['product_name'] = get_the_title($product_id);
        $products[$key]['product_id'] = $product_id;
        $products[$key]['count'] = 1;
        $description[] = $products[$key]['product_name'];
        $description = apply_filters('it_exchange_get_cart_description', join(', ', $description), $description);
        // Package it up and send it to the transaction method add-on
        $total = empty($args['paid']) ? 0 : it_exchange_convert_to_database_number($args['paid']);
        $object = new stdClass();
        $object->total = number_format(it_exchange_convert_from_database_number($total), 2, '.', '');
        $object->currency = $currency;
        $object->description = $description;
        $object->products = $products;
        remove_action('it_exchange_add_transaction_success', 'ITELIC\\on_add_transaction_generate_license_keys');
        $uniquid = it_exchange_manual_purchases_addon_transaction_uniqid();
        $txn_args = array();
        if (isset($args['date'])) {
            $date = \ITELIC\make_date_time($args['date']);
            $txn_args['post_date'] = \ITELIC\convert_gmt_to_local($date)->format('Y-m-d H:i:s');
            $txn_args['post_date_gmt'] = $date->format('Y-m-d H:i:s');
        }
        $tid = it_exchange_add_transaction('manual-purchases', $uniquid, 'Completed', $customer->id, $object, $txn_args);
        add_action('it_exchange_add_transaction_success', 'ITELIC\\on_add_transaction_generate_license_keys');
        $transaction = it_exchange_get_transaction($tid);
    }
    $factory = new \ITELIC\Key\Factory($product, $customer, $transaction);
    $key = \ITELIC\generate_key_for_transaction_product($transaction, $product, $factory, $args['status'], $args['key']);
    if (isset($args['limit'])) {
        if (empty($args['limit']) || $args['limit'] == '-') {
            $limit = '';
        } else {
            $limit = $args['limit'];
        }
        $key->set_max($limit);
    }
    if (isset($args['expires'])) {
        if (is_string($args['expires'])) {
            $expires = \ITELIC\make_date_time($args['expires']);
        } else {
            $expires = $args['expires'];
        }
        if (!$expires instanceof DateTime) {
            $expires = null;
        }
        $key->set_expires($expires);
    }
    return $key;
}
Пример #2
0
/**
 * Create a renewal transaction key.
 *
 * @api
 *
 * @param array $args {
 *
 * @type string $key  The license key to be used. If empty, one will be
 *       generated.
 * @type float  $paid If manually generating a transaction, the amount paid.
 * @tpye string $date When the transaction occurred. GMT.
 * }
 *
 * @return IT_Exchange_Transaction
 */
function itelic_create_renewal_transaction($args)
{
    $defaults = array('key' => '', 'paid' => '', 'date' => '');
    $args = ITUtility::merge_defaults($args, $defaults);
    $key = itelic_get_key($args['key']);
    if (!$key) {
        return new WP_Error('invalid_key', __("Invalid key", \ITELIC\Plugin::SLUG));
    }
    $product = $key->get_product();
    if (!function_exists('it_exchange_manual_purchases_addon_transaction_uniqid')) {
        return new WP_Error('no_manual_purchases', __("Manual purchases add-on is not installed.", \ITELIC\Plugin::SLUG));
    }
    // Grab default currency
    $settings = it_exchange_get_option('settings_general');
    $currency = $settings['default-currency'];
    $description = array();
    $product_id = $product->ID;
    $itemized_data = apply_filters('it_exchange_add_itemized_data_to_cart_product', array(), $product_id);
    if (!is_serialized($itemized_data)) {
        $itemized_data = maybe_serialize($itemized_data);
    }
    $i = $product_id . '-' . md5($itemized_data);
    $discounted = new \ITELIC\Renewal\Discount($key);
    $discounted = $discounted->get_discount_price();
    $products[$i]['product_base_price'] = $discounted;
    $products[$i]['product_subtotal'] = $products[$i]['product_base_price'];
    //need to add count
    $products[$i]['product_name'] = get_the_title($product_id);
    $products[$i]['product_id'] = $product_id;
    $products[$i]['count'] = 1;
    $description[] = $products[$i]['product_name'];
    $description = apply_filters('it_exchange_get_cart_description', join(', ', $description), $description);
    // Package it up and send it to the transaction method add-on
    $total = empty($args['paid']) ? $discounted : it_exchange_convert_to_database_number($args['paid']);
    $object = new stdClass();
    $object->total = number_format(it_exchange_convert_from_database_number($total), 2, '.', '');
    $object->currency = $currency;
    $object->description = $description;
    $object->products = $products;
    remove_action('it_exchange_add_transaction_success', 'ITELIC\\renew_key_on_renewal_purchase');
    $uniquid = it_exchange_manual_purchases_addon_transaction_uniqid();
    $txn_args = array();
    if (isset($args['date'])) {
        $date = \ITELIC\make_date_time($args['date']);
        $txn_args['post_date'] = \ITELIC\convert_gmt_to_local($date)->format('Y-m-d H:i:s');
        $txn_args['post_date_gmt'] = $date->format('Y-m-d H:i:s');
    }
    $customer = $key->get_customer()->id;
    $tid = it_exchange_add_transaction('manual-purchases', $uniquid, 'Completed', $customer, $object, $txn_args);
    add_action('it_exchange_add_transaction_success', 'ITELIC\\renew_key_on_renewal_purchase');
    return it_exchange_get_transaction($tid);
}