Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpha #22

Merged
merged 21 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
174 changes: 93 additions & 81 deletions app/Constants.php

Large diffs are not rendered by default.

119 changes: 96 additions & 23 deletions app/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace BeycanPress\CryptoPayLite;

// Classes
use MultipleChain\EvmChains\Provider;
use BeycanPress\CryptoPayLite\PluginHero\Hook;
use BeycanPress\CryptoPayLite\Settings\Settings;
use MultipleChain\EvmChains\Models\Transaction;
use MultipleChain\EvmChains\Models\CoinTransaction;
use MultipleChain\EvmChains\Models\TokenTransaction;
use BeycanPress\CryptoPayLite\Models\OrderTransaction;
use BeycanPress\CryptoPayLite\Models\AbstractTransaction;
use MultipleChain\EvmChains\Provider as EvmChainsProvider;
use BeycanPress\CryptoPayLite\PluginHero\Helpers as PhHelpers;
// Types
use BeycanPress\CryptoPayLite\Types\Network\CurrencyType;
Expand Down Expand Up @@ -137,11 +140,79 @@ public static function getMode(string $addon): string

/**
* @param string $addon
* @return string
* @return array<mixed>
*/
public static function getTheme(string $addon): string
public static function getTheme(string $addon): array
{
return Hook::callFilter('theme_' . $addon, Hook::callFilter('theme', Helpers::getSetting('theme', 'light')));
$themeLight = Helpers::getSetting('themeLight', [
'text' => '#3d3d3d',
'discount' => 'red',
'border' => '#e2e4ec',
'primary' => '#f5f7f9',
'secondary' => '#888',
'tertiary' => '#555',
'background' => '#ffffff',
'boxShadow' => 'rgba(0, 0, 0, 0.2)',
'buttonColor' => '#fff',
'buttonDisabled' => '#cccccc',
'buttonBackground' => '#409eff',
'buttonHoverBackground' => '#79bbff',
]);

$themeDark = Helpers::getSetting('themeDark', [
'text' => 'rgb(168, 174, 182)',
'discount' => 'rgb(210, 58, 58)',
'border' => 'rgb(5, 11, 19)',
'primary' => 'rgb(20, 29, 32)',
'secondary' => 'rgba(5, 11, 19, 0.8)',
'tertiary' => 'rgb(3, 6, 11)',
'background' => 'rgb(28, 34, 43)',
'boxShadow' => 'rgba(255, 255, 255, 0.1)',
'buttonColor' => '#fff',
'buttonDisabled' => '#cccccc',
'buttonBackground' => '#1da87c',
'buttonHoverBackground' => '#116c4f',
]);

$theme = [
'mode' => Helpers::getSetting('themeMode', 'light'),
'style' => [
'light' => [
'text' => $themeLight['text'],
'discount' => $themeLight['discount'],
'border' => $themeLight['border'],
'primary' => $themeLight['primary'],
'secondary' => $themeLight['secondary'],
'tertiary' => $themeLight['tertiary'],
'background' => $themeLight['background'],
'boxShadow' => $themeLight['boxShadow'],
'button' => [
'color' => $themeLight['buttonColor'],
'disabled' => $themeLight['buttonDisabled'],
'background' => $themeLight['buttonBackground'],
'hoverBackground' => $themeLight['buttonHoverBackground']
]
],
'dark' => [
'text' => $themeDark['text'],
'discount' => $themeDark['discount'],
'border' => $themeDark['border'],
'primary' => $themeDark['primary'],
'secondary' => $themeDark['secondary'],
'tertiary' => $themeDark['tertiary'],
'background' => $themeDark['background'],
'boxShadow' => $themeDark['boxShadow'],
'button' => [
'color' => $themeDark['buttonColor'],
'disabled' => $themeDark['buttonDisabled'],
'background' => $themeDark['buttonBackground'],
'hoverBackground' => $themeDark['buttonHoverBackground']
]
]
]
];

return Hook::callFilter('theme_' . $addon, Hook::callFilter('theme', $theme));
}

/**
Expand All @@ -164,23 +235,14 @@ public static function getTestnetStatus(): bool

/**
* @param TransactionType $transaction
* @return object
* @return Provider
*/
public static function getProvider(TransactionType $transaction): object
public static function getProvider(TransactionType $transaction): Provider
{
$providers = [
'evmchains' => Provider::class
];

$provider = $providers[$transaction->getCode()] ?? null;
$provider = self::getProviders()[$transaction->getCode()] ?? null;

if ($provider) {
$network = $transaction->getNetwork();
return new $provider([
'network' => $network->toObject(),
'rpcUrl' => $network->getRpcUrl(),
'testnet' => $transaction->getTestnet(),
]);
return new Provider($transaction, $provider);
} else {
throw new \Exception('Provider not found!');
}
Expand All @@ -192,11 +254,22 @@ public static function getProvider(TransactionType $transaction): object
*/
public static function providerExists(string $code): bool
{
$providers = [
'evmchains' => Provider::class
];
return isset(self::getProviders()[$code]);
}

return isset($providers[$code]);
/**
* @return array<mixed>
*/
public static function getProviders(): array
{
return Hook::callFilter('php_providers', [
'evmchains' => [
'transaction' => Transaction::class,
'provider' => EvmChainsProvider::class,
'coinTransaction' => CoinTransaction::class,
'tokenTransaction' => TokenTransaction::class
]
]);
}

/**
Expand Down Expand Up @@ -243,7 +316,7 @@ public static function getPaymentHtmlDetails(TransactionType $transaction): stri
$amount = self::toString($order->getPaymentAmount(), $currency->getDecimals());

if (Helpers::providerExists($transaction->getCode())) {
$transactionUrl = (Helpers::getProvider($transaction))->Transaction($transaction->getHash())->getUrl();
$transactionUrl = (Helpers::getProvider($transaction))->transaction($transaction->getHash())->getUrl();
} else {
$transactionUrl = null;
}
Expand Down Expand Up @@ -315,6 +388,6 @@ public static function prepareCurrencies(array $mainnetCurrencies, array $testne
public static function networkWillNotWorkMessage(string $networkName): void
{
// @phpcs:ignore
self::adminNotice(str_replace('{networkName}', $networkName, esc_html__('You did not specify a wallet address in the "CryptoPay Lite {networkName} settings", {networkName} network will not work. Please specify a wallet address first.', 'cryptopay_lite')), 'error');
self::adminNotice(str_replace('{networkName}', $networkName, esc_html__('You did not specify a wallet address in the "CryptoPay Lite {networkName} settings", {networkName} network will not work. Please specify a wallet address first.', 'cryptopay')), 'error');
}
}
26 changes: 21 additions & 5 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function __construct(string $pluginFile)
{
parent::__construct([
'pluginFile' => $pluginFile,
'textDomain' => 'cryptopay',
'pluginKey' => 'cryptopay_lite',
'textDomain' => 'cryptopay_lite',
'settingKey' => 'cryptopay_lite_settings',
]);

Expand All @@ -26,13 +26,14 @@ public function __construct(string $pluginFile)
if (Helpers::getSetting('evmchainsWalletAddress')) {
add_action('plugins_loaded', function (): void {
new RestAPI();
new Services\Initialize();
new WooCommerce\Initialize();
});
} else {
Helpers::adminNotice(
__(
'CryptoPay Lite: Please enter your wallet address in the settings section for CryptoPay Lite run.',
'cryptopay_lite'
'cryptopay'
),
'error'
);
Expand All @@ -51,9 +52,9 @@ public function __construct(string $pluginFile)
public function pluginActionLinks(array $links): array
{
// @phpcs:disable
$links[] = '<a href="https://beycanpress.com/chekcout/?add-to-cart=800&utm_source=lite_version&utm_medium=plugins_list" style="color: #389e38;font-weight: bold;" target="_blank">' . __('Buy Premium', 'cryptopay_lite') . '</a>';
$links[] = '<a href="' . admin_url('admin.php?page=cryptopay_lite_settings') . '">' . __('Settings', 'cryptopay_lite') . '</a>';
$links[] = '<a href="https://beycanpress.gitbook.io/cryptopay-docs/" target="_blank">' . __('Documentation', 'cryptopay_lite') . '</a>';
$links[] = '<a href="https://beycanpress.com/chekcout/?add-to-cart=800&utm_source=lite_version&utm_medium=plugins_list" style="color: #389e38;font-weight: bold;" target="_blank">' . __('Buy Premium', 'cryptopay') . '</a>';
$links[] = '<a href="' . admin_url('admin.php?page=cryptopay_lite_settings') . '">' . __('Settings', 'cryptopay') . '</a>';
$links[] = '<a href="https://beycanpress.gitbook.io/cryptopay-docs/" target="_blank">' . __('Documentation', 'cryptopay') . '</a>';
// @phpcs:enable

return $links;
Expand All @@ -66,11 +67,26 @@ public function adminProcess(): void
{
new Pages\HomePage();
new Pages\Integrations();
new Pages\PendingReminders();

if (file_exists(Helpers::getProp('pluginDir') . '/debug.log')) {
new Pages\DebugLogs();
}

add_filter('safe_style_css', function ($styles): array {
$styles[] = 'display';
$styles[] = 'justify-content';
$styles[] = 'max-width';
$styles[] = 'height';
return $styles;
});

Helpers::adminNotice(
'<b style="display:block;font-size:22px;text-align:center">If you want to receive commission-free cryptocurrency payments for life, don\'t miss this opportunity!</b><br><a href="https://beycanpress.com/checkout?add-to-cart=800&utm_source=lite_banner&utm_campaign=summer_discount" target="_blank" style="display:flex;justify-content:center;"><img src="'. Helpers::getImageUrl('banner.png') .'" alt="Summer Discount" style="max-width:800px;height:auto;"></a>', // @phpcs:ignore
'info',
true
);

add_action('init', function (): void {
new Settings\Settings();
}, 9);
Expand Down
89 changes: 20 additions & 69 deletions app/Models/AbstractTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ abstract class AbstractTransaction extends AbstractModel
* @var string
* Because abstract class don't have property type, that's from php side giving error
*/
public string $version = '1.3.0';

/**
* @var string
*/
public string $updateVersion = '1.3.0';
public string $version = '1.3.1';

/**
* @var string
Expand Down Expand Up @@ -73,6 +68,11 @@ public function __construct(string $tableName)
'testnet' => [
'type' => 'boolean',
],
'reminderEmail' => [
'type' => 'string',
'length' => 250,
'nullable' => true,
],
'addresses' => [
'type' => 'json',
],
Expand All @@ -89,67 +89,6 @@ public function __construct(string $tableName)
],
]);

// column updates for 1.2.0
// TODO: remove in next versions, maybe 2.5.0+
if ($this->updateVersion != get_option($this->tableName . '_update_version')) {
update_option($this->tableName . '_update_version', $this->updateVersion);

/**
* For paymentPrice values to paymentAmount
* @since 2.1.0
*/
// @phpcs:ignore
$oldOrderColumns = $this->getResults(
"SELECT `id`, `order` FROM {$this->tableName} WHERE `order` LIKE '%paymentPrice%';"
);

if (!empty($oldOrderColumns)) {
$totalQuery = "UPDATE {$this->tableName} SET `order` = CASE ";
foreach ($oldOrderColumns as $value) {
$newOrder = str_ireplace('paymentPrice', 'paymentAmount', $value->order);
$totalQuery .= "WHEN `id` = {$value->id} THEN '{$newOrder}' ";
}

$totalQuery .= "ELSE `order` END WHERE `order` LIKE '%paymentPrice%';";

$this->query($totalQuery);
}

/**
* This for new typing system because ParamsType cannot be null
* @since 2.1.0
*/
$this->query("ALTER TABLE {$this->tableName} MODIFY COLUMN `params` json NOT NULL;");
$this->query(
"UPDATE {$this->tableName} SET `params` = '{}'
WHERE `params` IS NULL OR `params` = 'null' OR `params` = '';"
);

/**
* Addresses column added in 2.0.0 and with this version types needed not null addresses data
* that's why we need to update all null addresses to empty object
* @since 2.1.0
*/
$this->query("ALTER TABLE {$this->tableName} MODIFY COLUMN `addresses` json NOT NULL;");
$this->query(
"UPDATE {$this->tableName} SET `addresses` = '{}'
WHERE `addresses` IS NULL OR `addresses` = 'null' OR `addresses` = '';"
);

/**
* Network and order also updating to json type
* @since 2.1.0
*/
$this->query("ALTER TABLE {$this->tableName} MODIFY COLUMN `network` json NOT NULL;");
$this->query("ALTER TABLE {$this->tableName} MODIFY COLUMN `order` json NOT NULL;");

/**
* Because evm networks code change to evmchains from evmBased, that's why we need to update all
* @since 2.1.0
*/
$this->query("UPDATE {$this->tableName} SET `code` = 'evmchains' WHERE `code` = 'evmBased';");
}

$this->createTable();
}

Expand Down Expand Up @@ -215,8 +154,8 @@ public function updateWithPaymentData(PaymentDataType $data, TransactionType $tx
$status = $data->getStatus() ? Status::VERIFIED : Status::FAILED;

$provider = Helpers::getProvider($tx);
$pTx = $provider->Transaction($tx->getHash());
$tx->getAddresses()->setSender($pTx->getFrom());
$pTx = $provider->transaction($tx->getHash());
$tx->getAddresses()->setSender($pTx->getSigner());

return (bool) $this->update([
'hash' => $data->getHash(),
Expand Down Expand Up @@ -345,6 +284,18 @@ public function getTransactionByOrderId(int $orderId): ?TransactionType
], ['id', 'DESC']);
}

/**
* @param array<mixed> $params
* @return TransactionsType
*/
public function getRemindedPendingTransactions(array $params): TransactionsType
{
return $this->findBy(array_merge($params, [
'status' => Status::PENDING->getValue(),
['reminderEmail', 'IS NOT', 'NULL']
]));
}

/**
* @return array<mixed>
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Pages/DebugLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct()
{
parent::__construct([
'priority' => 11,
'pageName' => esc_html__('Debug logs', 'cryptopay_lite'),
'pageName' => esc_html__('Debug logs', 'cryptopay'),
'parent' => Helpers::getPage('HomePage')->getSlug(),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Pages/HomePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class HomePage extends Page
public function __construct()
{
parent::__construct([
'pageName' => esc_html__('CryptoPay Lite', 'cryptopay_lite'),
'subMenuPageName' => esc_html__('Buy premium', 'cryptopay_lite'),
'pageName' => esc_html__('CryptoPay Lite', 'cryptopay'),
'subMenuPageName' => esc_html__('Buy premium', 'cryptopay'),
'slug' => 'cryptopay_lite_home',
'icon' => Helpers::getImageUrl('menu.png'),
'subMenu' => true,
Expand Down
Loading
Loading