/** * Get a random customer. * * @return IT_Exchange_Customer */ protected function get_random_customer() { /** * @var \wpdb $wpdb */ global $wpdb; $ID = (int) $wpdb->get_var("SELECT ID FROM {$wpdb->users} ORDER BY RAND() LIMIT 1"); return it_exchange_get_customer($ID); }
/** * 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; }
/** * Initialize this object. * * @param \stdClass $data */ protected function init(\stdClass $data) { $this->key = $data->lkey; $this->transaction = it_exchange_get_transaction($data->transaction_id); $this->product = itelic_get_product($data->product); // account for guest checkouts if ($data->customer) { $this->customer = it_exchange_get_customer($data->customer); } else { $customer = it_exchange_get_transaction_customer($this->transaction); if (!$customer instanceof \IT_Exchange_Customer) { $customer = new \IT_Exchange_Customer($customer); $customer->wp_user->display_name = sprintf(__("Guest (%s)", Plugin::SLUG), $customer->wp_user->user_email); } $this->customer = $customer; } $this->status = $data->status; $this->max = $data->max; if (!empty($data->expires) && $data->expires != '0000-00-00 00:00:00') { $this->expires = make_date_time($data->expires); } foreach (array('transaction', 'product', 'customer') as $maybe_error) { if (!$this->{$maybe_error} || is_wp_error($this->{$maybe_error})) { throw new \InvalidArgumentException("Invalid {$maybe_error}"); } } }