Skip to content

Commit

Permalink
Merge branch 'alpha'
Browse files Browse the repository at this point in the history
  • Loading branch information
BeycanDeveloper committed Mar 3, 2024
2 parents 7cf2b8b + bd0b05b commit a213299
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 31 deletions.
34 changes: 18 additions & 16 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
$(window).on('click', function(e) {
if (e.target == modal[0]) {
modal.hide();
if (CryptoPayApp.reset) {
CryptoPayApp.reset();
}
}
});

Expand Down Expand Up @@ -58,28 +55,33 @@
});
});
} else if (key == 'dokan_cryptopay_lite') {
CryptoPayLite.networks = [
details.network,
];

CryptoPayApp = CryptoPayLite.startPayment({
let order = {
amount,
currency,
}, {
}

let params = {
receiver: details.receiver,
});
}

CryptoPayLite.hooks.transactionSent = (n, tx) => {
if (!CryptoPayApp) {
CryptoPayApp = window.CryptoPayLiteApp.start(order, params);
} else {
CryptoPayApp.reStart(order, params);
}

CryptoPayApp.store.config.set('networks', [details.network]);

window.CryptoPayLiteApp.events.add('transactionReceived', ({transaction}) => {
approve.trigger('click');
cpHelpers.successPopup(CryptoPayLite.lang.transactionSent, `
<a href="${tx.getUrl()}" target="_blank">
${CryptoPayLite.lang.openInExplorer}
cplHelpers.successPopup(window.CryptoPayLiteLang.transactionSent, `
<a href="${transaction.getUrl()}" target="_blank">
${window.CryptoPayLiteLang.openInExplorer}
</a>
`).then(() => {
modal.hide();
CryptoPayApp.reset();
});
}
});
}

});
Expand Down
52 changes: 41 additions & 11 deletions classes/DokanCryptoPayWithdrawal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use BeycanPress\CryptoPay\Helpers;
use BeycanPress\CryptoPay\Payment;
use BeycanPress\CryptoPay\PluginHero\Hook;
use BeycanPress\CryptoPay\Types\Data\PaymentDataType;
use BeycanPress\CryptoPayLite\Services as LiteServices;
// Lite
use BeycanPress\CryptoPayLite\Settings\EvmChains;
use BeycanPress\CryptoPayLite\Helpers as LiteHelpers;
use BeycanPress\CryptoPayLite\Payment as LitePayment;
use BeycanPress\CryptoPayLite\PluginHero\Hook as LiteHook;

// @phpcs:ignore
Expand All @@ -28,6 +30,11 @@ class DokanCryptoPayWithdrawal
*/
private string $key;

/**
* @var array<mixed>|null
*/
private ?array $networks = null;

/**
* @var array<mixed>|null
*/
Expand Down Expand Up @@ -59,15 +66,15 @@ public function __construct(string $title, string $key)
if ($this->key == 'dokan_cryptopay') {
Helpers::registerIntegration($this->key);
Hook::addFilter('apply_discount_' . $this->key, '__return_false');
Hook::addFilter('receiver_' . $this->key, function (string $receiver, PaymentDataType $data) {
Hook::addFilter('receiver_' . $this->key, function (string $receiver, object $data) {
if ($data->getParams()->get('receiver')) {
return $data->getParams()->get('receiver');
}

return $receiver;
}, 10, 2);
} else {
LiteServices::registerAddon($this->key);
LiteHelpers::registerIntegration($this->key);
LiteHook::addFilter('apply_discount_' . $this->key, '__return_false');
LiteHook::addFilter('receiver_' . $this->key, function (string $receiver, object $data) {
if (isset($data->params->receiver)) {
Expand Down Expand Up @@ -123,7 +130,7 @@ private function isSelected(object $network, array $networkItem): bool
return false;
}

if ($networkItem['code'] == 'evmBased') {
if ($networkItem['code'] == 'evmchains') {
$res = $networkItem['id'] == $network->id;
} else {
$res = $networkItem['code'] == $network->code;
Expand All @@ -136,6 +143,24 @@ private function isSelected(object $network, array $networkItem): bool
return $res;
}

/**
* @return array<mixed>
*/
private function getNetworksById(): array
{
if (LiteHelpers::getTestnetStatus()) {
$networks = file_get_contents(DOKAN_CRYPTOPAY_PATH . '/resources/testnets.json');
} else {
$networks = file_get_contents(DOKAN_CRYPTOPAY_PATH . '/resources/mainnets.json');
}

$networks = json_decode($networks, true);

return array_filter($networks, function ($network) {
return in_array($network['id'], EvmChains::getNetworks());
});
}

/**
* @param array<mixed> $args
* @return void
Expand All @@ -148,9 +173,9 @@ public function userSettingForm(array $args): void
$address = isset($settings['address']) ? $settings['address'] : '';

if ($this->key == 'dokan_cryptopay') {
$networks = Helpers::getNetworks()->toArray();
$this->networks = Helpers::getNetworks()->toArray();
} else {
$networks = LiteServices::getNetworks();
$this->networks = $this->getNetworksById();
}

?>
Expand All @@ -162,7 +187,7 @@ public function userSettingForm(array $args): void
</div>
<div class="dokan-w12">
<select name="settings[<?php echo esc_attr($this->key) ?>][network]" class="dokan-form-control dokan-cryptopay-network">
<?php foreach ($networks as $networkItem) : ?>
<?php foreach ($this->networks as $networkItem) : ?>
<option value='<?php echo json_encode($networkItem) ?>' <?php echo $this->isSelected($network, $networkItem) ? 'selected' : ''; ?>>
<?php echo esc_html($networkItem['name']) ?>
</option>
Expand All @@ -178,11 +203,16 @@ public function userSettingForm(array $args): void
</div>
<div class="dokan-w12">
<select name="settings[<?php echo esc_attr($this->key) ?>][currency]" class="dokan-form-control dokan-cryptopay-currency">
<?php foreach ($this->currentNetwork['currencies'] as $currencyItem) : ?>
<?php
if (!$this->currentNetwork) {
$this->currentNetwork = $this->networks[0];
}
foreach ($this->currentNetwork['currencies'] as $currencyItem) : ?>
<option value='<?php echo json_encode($currencyItem) ?>' <?php echo isset($currency->symbol) && $currencyItem['symbol'] == $currency->symbol ? 'selected' : ''; ?>>
<?php echo esc_html($currencyItem['symbol']) ?>
</option>
<?php endforeach; ?>
<?php endforeach;
?>
</select>
</div>
</div>
Expand Down Expand Up @@ -370,7 +400,7 @@ public function runCryptoPay(): string
if ($this->key == 'dokan_cryptopay') {
return (new Payment($this->key))->setConfirmation(false)->html();
} else {
return LiteServices::preparePaymentProcess($this->key, false);
return (new LitePayment($this->key))->setConfirmation(false)->html();
}
}
}
5 changes: 3 additions & 2 deletions dokan-cryptopay-withdrawal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* Plugin Name: Dokan - CryptoPay Withdrawal
* Version: 1.0.2
* Version: 1.0.3
* Plugin URI: https://beycanpress.com/cryptopay/
* Description: Add custom cryptocurrency withdrawal method to Dokan plugin
* Author: BeycanPress LLC
Expand All @@ -29,8 +29,9 @@
use BeycanPress\CryptoPayLite\Loader as LiteLoader;

define('DOKAN_CRYPTOPAY_FILE', __FILE__);
define('DOKAN_CRYPTOPAY_VERSION', '1.0.2');
define('DOKAN_CRYPTOPAY_VERSION', '1.0.3');
define('DOKAN_CRYPTOPAY_URL', plugin_dir_url(__FILE__));
define('DOKAN_CRYPTOPAY_PATH', plugin_dir_path(__FILE__));

add_action('plugins_loaded', function (): void {
require_once __DIR__ . '/classes/DokanCryptoPayWithdrawal.php';
Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Tags: Bitcoin, Ethereum, Binance Smart Chain, Blockchain, Networks, Cryptocurren
Requires at least: 5.0
Tested up to: 6.4.2
Requires PHP: 8.1
Stable Tag: 1.0.2
Version: 1.0.2
Stable Tag: 1.0.3
Version: 1.0.3
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
155 changes: 155 additions & 0 deletions resources/mainnets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
[
{
"id" : 1,
"hexId" : "0x1",
"name" : "Ethereum",
"code" : "evmchains",
"rpcUrl" : "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
"explorerUrl" : "https://etherscan.io/",
"nativeCurrency" : {
"symbol" : "ETH",
"decimals" : 18
},
"wallets": [
"metamask",
"trustwallet",
"bitget",
"okx",
"xdefi"
],
"currencies": [
{
"symbol" : "ETH"
},
{
"symbol" : "USDT",
"address" : "0xdac17f958d2ee523a2206206994597c13d831ec7"
},
{
"symbol" : "USDC",
"address" : "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
},
{
"symbol" : "BUSD",
"address" : "0x4Fabb145d64652a948d72533023f6E7A623C7C53"
}
]
},
{
"id" : 56,
"hexId" : "0x38",
"name" : "BNB Smart Chain",
"code" : "evmchains",
"rpcUrl" : "https://bsc-dataseed.binance.org/",
"explorerUrl" : "https://bscscan.com/",
"nativeCurrency" : {
"symbol" : "BNB",
"decimals" : 18
},
"wallets": [
"metamask",
"trustwallet",
"bitget",
"okx",
"xdefi",
"binancewallet"
],
"currencies": [
{
"symbol" : "BNB"
},
{
"symbol" : "BUSD",
"address" : "0xe9e7cea3dedca5984780bafc599bd69add087d56"
},
{
"symbol" : "USDT",
"address" : "0x55d398326f99059ff775485246999027b3197955"
},
{
"symbol" : "USDC",
"address" : "0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d"
}
]
},
{
"id" : 43114,
"hexId" : "0xa86a",
"name" : "Avalanche C-Chain",
"code" : "evmchains",
"rpcUrl" : "https://api.avax.network/ext/bc/C/rpc",
"explorerUrl" : "https://cchain.explorer.avax.network/",
"nativeCurrency" : {
"symbol" : "AVAX",
"decimals" : 18
},
"wallets": [
"metamask",
"trustwallet",
"bitget",
"okx",
"xdefi"
],
"currencies": [
{
"symbol" : "AVAX"
},
{
"symbol" : "USDT",
"address" : "0xde3a24028580884448a5397872046a019649b084"
}
]
},
{
"id" : 137,
"hexId" : "0x89",
"name" : "Polygon",
"code" : "evmchains",
"rpcUrl" : "https://polygon-rpc.com/",
"explorerUrl" : "https://polygonscan.com/",
"nativeCurrency" : {
"symbol" : "MATIC",
"decimals" : 18
},
"wallets": [
"metamask",
"trustwallet",
"bitget",
"okx",
"xdefi"
],
"currencies": [
{
"symbol" : "MATIC"
},
{
"symbol" : "USDT",
"address" : "0xc2132d05d31c914a87c6611c10748aeb04b58e8f"
}
]
},
{
"id" : 250,
"hexId" : "0xfa",
"name" : "Fantom",
"code" : "evmchains",
"rpcUrl" : "https://rpc.fantom.network",
"explorerUrl" : "https://ftmscan.com/",
"nativeCurrency" : {
"symbol" : "FTM",
"decimals" : 18
},
"wallets": [
"metamask",
"trustwallet",
"bitget",
"okx",
"xdefi"
],
"currencies": [
{
"symbol" : "FTM"
}
]
}
]
Loading

0 comments on commit a213299

Please sign in to comment.