/** * Initialize this object. * * @param \stdClass $data */ protected function init(\stdClass $data) { $this->id = $data->id; $this->key = itelic_get_key($data->lkey); $this->renewal_date = make_date_time($data->renewal_date); $this->key_expired_date = make_date_time($data->key_expired_date); if ($data->transaction_id) { $this->transaction = it_exchange_get_transaction($data->transaction_id); } else { $this->transaction = null; } $this->revenue = (double) $data->revenue; }
/** * 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; }
/** * Constructs the object * @link http://php.net/manual/en/serializable.unserialize.php * * @param string $serialized <p> * The string representation of the object. * </p> * * @return void * @since 5.1.0 */ public function unserialize($serialized) { $data = unserialize($serialized); $this->transaction = it_exchange_get_transaction($data['transaction']); $this->notification = unserialize($data['notification']); }
/** * Renew a license key. * * ## Options * * <key> * : License key * * [<transaction>] * : Optionally tie this renewal to a transaction * * @param $args * @param $assoc_args */ public function renew($args, $assoc_args) { list($key, $transaction) = array_pad($args, 2, 0); if (substr($key, -3) == '...') { $key = substr($key, 0, strlen($key) - 3); if (strlen($key) < 3) { WP_CLI::error('At least the first three characters of the key must be provided to perform a partial match.'); } $keys = itelic_get_keys(array('key_like' => $key)); if (empty($keys)) { WP_CLI::error('No partial match found.'); } if (count($keys) > 1) { WP_CLI::line('Multiple keys found.'); $this->list_(array(), array('key_like' => $key)); return; } $object = reset($keys); } else { $object = $this->fetcher->get_check($key); } if (!empty($transaction)) { $transaction = it_exchange_get_transaction($transaction); if (!$transaction) { WP_CLI::error(sprintf("Invalid transaction with ID %d", $transaction)); } } else { $transaction = null; } try { $result = $object->renew($transaction); if ($result) { WP_CLI::success(sprintf("Key has been renewed. New expiration date is %s", $object->get_expires()->format(DateTime::ISO8601))); return; } } catch (Exception $e) { WP_CLI::error($e->getMessage()); } WP_CLI::error("An unknown error has occurred"); }
/** * 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); }
/** * Render the license keys email notification shortcode tag. * * @internal * * @since 1.0 * * @param \IT_Exchange_Email_Notifications $email_notifications * * @return string */ function render_license_keys_email_notification_shortcode(\IT_Exchange_Email_Notifications $email_notifications) { $transaction = it_exchange_get_transaction($email_notifications->transaction_id); $out = ''; foreach ($transaction->get_products() as $product) { $product = itelic_get_product($product['product_id']); $key = get_key_for_transaction_product($transaction->ID, $product->ID); if ($key) { $out .= "<li>" . $product->post_title . ": " . $key->get_key() . "</li>"; } } if ($out) { $out = "<h4>" . __("License Keys", Plugin::SLUG) . "<h4/>" . "<ul>{$out}</ul>"; } return $out; }
/** * 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}"); } } }