182 lines
7.9 KiB
PHP
182 lines
7.9 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* What can be sold, read from the API and kept for an hour:
|
|
* plans (GET /v1/catalog/products and /v1/catalog/products/{id}), extension prices (GET /v1/catalog/tlds)
|
|
* and the SMS credit prices (GET /v1/sms/credits/prices). With a Reseller Program key the prices come with your discount.
|
|
*/
|
|
class INTERWEB_AO_RESELLER_Catalog
|
|
{
|
|
/** Kinds of plan bought with POST /v1/services/purchase, and the write permission each one needs. */
|
|
public static function kinds()
|
|
{
|
|
return array(
|
|
'hosting' => array(__('Hosting', 'interweb-ao-reseller'), 'hosting:write'),
|
|
'reseller_hosting' => array(__('Reseller hosting', 'interweb-ao-reseller'), 'hosting:write'),
|
|
'radio' => array(__('Radio streaming', 'interweb-ao-reseller'), 'hosting:write'),
|
|
'vps' => array(__('VPS and cloud servers', 'interweb-ao-reseller'), 'vps:write'),
|
|
'dedicated' => array(__('Dedicated servers', 'interweb-ao-reseller'), 'vps:write'),
|
|
);
|
|
}
|
|
|
|
public static function cycles()
|
|
{
|
|
return array(
|
|
'monthly' => __('Monthly', 'interweb-ao-reseller'),
|
|
'quarterly' => __('Every 3 months', 'interweb-ao-reseller'),
|
|
'semi_annually' => __('Every 6 months', 'interweb-ao-reseller'),
|
|
'annually' => __('Yearly', 'interweb-ao-reseller'),
|
|
'biennially' => __('Every 2 years', 'interweb-ao-reseller'),
|
|
'triennially' => __('Every 3 years', 'interweb-ao-reseller'),
|
|
'one_time' => __('One time', 'interweb-ao-reseller'),
|
|
);
|
|
}
|
|
|
|
private static function cached($key, $ttl, $fn)
|
|
{
|
|
$full = 'interweb_ao_reseller_cat_' . md5(INTERWEB_AO_RESELLER_Brands::current_id() . '|' . $key);
|
|
$hit = get_transient($full);
|
|
if ($hit !== false) {
|
|
return $hit;
|
|
}
|
|
$v = call_user_func($fn);
|
|
if (!is_wp_error($v)) {
|
|
set_transient($full, $v, $ttl);
|
|
}
|
|
return $v;
|
|
}
|
|
|
|
/** Purchasable plans of the kinds above. @return array|WP_Error id => array(id, name, kind, group, prices) */
|
|
public static function plans()
|
|
{
|
|
return self::cached('plans', HOUR_IN_SECONDS, function () {
|
|
$out = array();
|
|
for ($page = 1; $page <= 5; $page++) {
|
|
$res = INTERWEB_AO_RESELLER_Api::request('GET', '/v1/catalog/products', null, array('query' => array('page' => $page, 'per_page' => 100), 'timeout' => 20));
|
|
if (is_wp_error($res)) {
|
|
return $page === 1 ? $res : $out;
|
|
}
|
|
foreach ((array) $res['data'] as $p) {
|
|
if (empty($p['purchasable']) || !isset($p['kind']) || !array_key_exists($p['kind'], INTERWEB_AO_RESELLER_Catalog::kinds())) {
|
|
continue;
|
|
}
|
|
$prices = array();
|
|
foreach ((array) $p['prices'] as $c) {
|
|
$prices[$c['billing_cycle']] = (float) $c['price'] + (float) (isset($c['setup_fee']) ? $c['setup_fee'] : 0);
|
|
}
|
|
$out[(int) $p['id']] = array(
|
|
'id' => (int) $p['id'],
|
|
'name' => (string) $p['name'],
|
|
'kind' => (string) $p['kind'],
|
|
'group' => isset($p['group']['name']) ? (string) $p['group']['name'] : '',
|
|
'currency' => isset($p['currency']) ? (string) $p['currency'] : '',
|
|
'prices' => $prices,
|
|
);
|
|
}
|
|
if ($page >= (isset($res['meta']['total_pages']) ? (int) $res['meta']['total_pages'] : 1)) {
|
|
break;
|
|
}
|
|
}
|
|
return $out;
|
|
});
|
|
}
|
|
|
|
/** One plan with its billing cycles and options. @return array|WP_Error */
|
|
public static function plan($id)
|
|
{
|
|
$id = (int) $id;
|
|
return self::cached('plan_' . $id, HOUR_IN_SECONDS, function () use ($id) {
|
|
$res = INTERWEB_AO_RESELLER_Api::request('GET', '/v1/catalog/products/' . $id, null, array('timeout' => 20));
|
|
return is_wp_error($res) ? $res : $res['data'];
|
|
});
|
|
}
|
|
|
|
/** Cost of a plan for a cycle with the chosen options (price plus setup fee). Null when unknown. */
|
|
public static function plan_cost($id, $cycle, array $options = array())
|
|
{
|
|
$p = self::plan($id);
|
|
if (is_wp_error($p) || !is_array($p)) {
|
|
return null;
|
|
}
|
|
$cost = null;
|
|
foreach ((array) $p['billing_cycles'] as $c) {
|
|
if ($c['billing_cycle'] === $cycle) {
|
|
$cost = (float) $c['price'] + (float) $c['setup_fee'];
|
|
}
|
|
}
|
|
if ($cost === null) {
|
|
return null;
|
|
}
|
|
foreach ((array) $p['config_options'] as $o) {
|
|
$choice = isset($options[$o['id']]) ? (int) $options[$o['id']] : (int) $o['default_choice_id'];
|
|
foreach ((array) $o['choices'] as $ch) {
|
|
if ((int) $ch['id'] !== $choice) {
|
|
continue;
|
|
}
|
|
foreach ((array) $ch['prices'] as $cp) {
|
|
if ($cp['billing_cycle'] === $cycle) {
|
|
$cost += (float) $cp['price'] + (float) $cp['setup_fee'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return array('cost' => round($cost, 2), 'currency' => isset($p['currency']) ? $p['currency'] : '');
|
|
}
|
|
|
|
/** Register, renew and transfer cost of an extension for a number of years. Null when unknown. */
|
|
public static function tld_cost($ext, $kind = 'register', $years = 1)
|
|
{
|
|
$ext = '.' . ltrim(strtolower((string) $ext), '.');
|
|
$row = self::cached('tld_' . $ext, 6 * HOUR_IN_SECONDS, function () use ($ext) {
|
|
$res = INTERWEB_AO_RESELLER_Api::request('GET', '/v1/catalog/tlds', null, array('query' => array('extension' => $ext), 'timeout' => 15));
|
|
if (is_wp_error($res)) {
|
|
return (int) INTERWEB_AO_RESELLER_Admin::error_status($res) === 404 ? array() : $res;
|
|
}
|
|
foreach ((array) $res['data'] as $t) {
|
|
if (isset($t['extension']) && strtolower($t['extension']) === $ext) {
|
|
return $t;
|
|
}
|
|
}
|
|
return array();
|
|
});
|
|
if (is_wp_error($row) || !$row || !isset($row[$kind][(string) $years])) {
|
|
return null;
|
|
}
|
|
return array('cost' => (float) $row[$kind][(string) $years], 'currency' => isset($row['currency']) ? $row['currency'] : '');
|
|
}
|
|
|
|
/** Price of each SMS credit for a quantity (the whole quantity takes the price of its tier). Null when unknown. */
|
|
public static function sms_cost($qty)
|
|
{
|
|
$t = self::cached('sms', HOUR_IN_SECONDS, function () {
|
|
$res = INTERWEB_AO_RESELLER_Api::request('GET', '/v1/sms/credits/prices', null, array('timeout' => 15));
|
|
return is_wp_error($res) ? $res : $res['data'];
|
|
});
|
|
if (is_wp_error($t) || !is_array($t) || empty($t['tiers'])) {
|
|
return null;
|
|
}
|
|
$unit = null;
|
|
foreach ((array) $t['tiers'] as $tier) {
|
|
if ((int) $qty >= (int) $tier['from']) {
|
|
$unit = (float) $tier['unit_price'];
|
|
}
|
|
}
|
|
if ($unit === null) {
|
|
return null;
|
|
}
|
|
return array('cost' => round($unit * (int) $qty, 2), 'currency' => isset($t['currency']) ? $t['currency'] : '', 'tax_added' => !empty($t['tax_added']));
|
|
}
|
|
|
|
public static function forget()
|
|
{
|
|
global $wpdb;
|
|
$wpdb->query($wpdb->prepare(
|
|
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
|
|
$wpdb->esc_like('_transient_interweb_ao_reseller_cat_') . '%',
|
|
$wpdb->esc_like('_transient_timeout_interweb_ao_reseller_cat_') . '%'
|
|
));
|
|
}
|
|
}
|