Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Aug 17, 2024
1 parent 470e705 commit 0f0531c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 95 deletions.
92 changes: 92 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
(($) => {

$(document).ready(() => {
let CryptoPayApp;
const __ = wp.i18n.__;

$(document).on('click', '.pay-with-cryptopay', async function() {
let key = $(this).data('key');
let parent = $(this).closest('tr');
let details = $(this).data('details');
let currency = DokanCryptoPay.currency;
let helpers = window.cpHelpers ?? window.cplHelpers;
let approve = parent.find(".actions .button-group button:eq(0)");
const modal = window.CryptoPayApp?.modal ?? window.CryptoPayLiteApp?.modal
let amount = parseFloat(parent.find(".amount div").text().replace(/[^a-zA-Z0-9.,]/g, "").trim());

details = {
receiver: details.address,
network: JSON.parse(details.network),
currency: JSON.parse(details.currency),
}

if (key == 'dokan_cryptopay') {
let order = {
amount,
currency,
}

let params = {
receiver: details.receiver,
}

if (!CryptoPayApp) {
CryptoPayApp = window.CryptoPayApp.start(order, params);
} else {
await CryptoPayApp.reStart(order, params);
}

modal.open();

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

window.CryptoPayApp.events.add('transactionReceived', ({transaction}) => {
approve.trigger('click');
$(this).prop('disabled', true)
$(this).text(__('Processing...'))
helpers.successPopup(window.CryptoPayLang.transactionSent, `
<a href="${transaction.getUrl()}" target="_blank">
${window.CryptoPayLang.openInExplorer}
</a>
`).then(() => {
modal.close();
});
}, 'dokan');
} else if (key == 'dokan_cryptopay_lite') {
let order = {
amount,
currency,
}

let params = {
receiver: details.receiver,
}

if (!CryptoPayApp) {
CryptoPayApp = window.CryptoPayLiteApp.start(order, params);
} else {
await CryptoPayApp.reStart(order, params);
}

modal.open();

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

window.CryptoPayLiteApp.events.add('transactionReceived', ({transaction}) => {
approve.trigger('click');
$(this).prop('disabled', true)
$(this).text(__('Processing...'))
helpers.successPopup(window.CryptoPayLiteLang.transactionSent, `
<a href="${transaction.getUrl()}" target="_blank">
${window.CryptoPayLiteLang.openInExplorer}
</a>
`).then(() => {
modal.close();
});
}, 'dokan');
}

});
});

})(jQuery);
92 changes: 6 additions & 86 deletions assets/js/main.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,11 @@
(($) => {
;(($) => {

$(document).ready(() => {
let CryptoPayApp;
const __ = wp.i18n.__;

$(document).on('click', '.pay-with-cryptopay', async function() {
let key = $(this).data('key');
let parent = $(this).closest('tr');
let details = $(this).data('details');
let currency = DokanCryptoPay.currency;
let helpers = window.cpHelpers ?? window.cplHelpers;
let approve = parent.find(".actions .button-group button:eq(0)");
const modal = window.CryptoPayApp?.modal ?? window.CryptoPayLiteApp?.modal
let amount = parseFloat(parent.find(".amount div").text().replace(/[^a-zA-Z0-9.,]/g, "").trim());

details = {
receiver: details.address,
network: JSON.parse(details.network),
currency: JSON.parse(details.currency),
}

if (key == 'dokan_cryptopay') {
let order = {
amount,
currency,
}

let params = {
receiver: details.receiver,
}

if (!CryptoPayApp) {
CryptoPayApp = window.CryptoPayApp.start(order, params);
} else {
await CryptoPayApp.reStart(order, params);
}

modal.open();

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

window.CryptoPayApp.events.add('transactionReceived', ({transaction}) => {
approve.trigger('click');
$(this).prop('disabled', true)
$(this).text(__('Processing...'))
helpers.successPopup(window.CryptoPayLang.transactionSent, `
<a href="${transaction.getUrl()}" target="_blank">
${window.CryptoPayLang.openInExplorer}
</a>
`).then(() => {
modal.close();
});
}, 'dokan');
} else if (key == 'dokan_cryptopay_lite') {
let order = {
amount,
currency,
}

let params = {
receiver: details.receiver,
}

if (!CryptoPayApp) {
CryptoPayApp = window.CryptoPayLiteApp.start(order, params);
} else {
await CryptoPayApp.reStart(order, params);
}

modal.open();

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

window.CryptoPayLiteApp.events.add('transactionReceived', ({transaction}) => {
approve.trigger('click');
$(this).prop('disabled', true)
$(this).text(__('Processing...'))
helpers.successPopup(window.CryptoPayLiteLang.transactionSent, `
<a href="${transaction.getUrl()}" target="_blank">
${window.CryptoPayLiteLang.openInExplorer}
</a>
`).then(() => {
modal.close();
});
}, 'dokan');
}

$(document).on('change', '.dokan-cryptopay-network', function(e) {
let currencies = JSON.parse($(this).val()).currencies;
$('.dokan-cryptopay-currency').html(`
${currencies.map(currency => `<option value='${JSON.stringify(currency)}'>${currency.symbol}</option>`).join('')}
`);
});
});

Expand Down
10 changes: 2 additions & 8 deletions classes/DokanCryptoPayWithdrawal.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ public function userSettingForm(mixed $args): void
$this->networks = $this->getNetworksById();
}

wp_enqueue_script('dokan-cryptopay', plugin_dir_url(__FILE__) . 'assets/js/main.js', ['jquery'], DOKAN_CRYPTOPAY_VERSION, true);

?>
<div class="dokan-form-group">
<div>
Expand Down Expand Up @@ -233,14 +235,6 @@ public function userSettingForm(mixed $args): void
<input value="<?php echo esc_attr($address); ?>" name="settings[<?php echo esc_attr($this->key) ?>][address]" class="dokan-form-control" type="text">
</div>
</div>
<script>
jQuery(document).on('change', '.dokan-cryptopay-network', function(e) {
let currencies = JSON.parse(jQuery(this).val()).currencies;
jQuery('.dokan-cryptopay-currency').html(`
${currencies.map(currency => `<option value='${JSON.stringify(currency)}'>${currency.symbol}</option>`).join('')}
`);
});
</script>
<?php
}

Expand Down
2 changes: 1 addition & 1 deletion cryptopay-withdrawal-for-dokan.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
// This a WordPress page detection
if (isset($_GET['page']) && 'dokan' === $_GET['page']) {
add_action('admin_enqueue_scripts', function (): void {
wp_enqueue_script('dokan-cryptopay', plugin_dir_url(__FILE__) . 'assets/js/main.js', ['jquery', 'wp-i18n'], DOKAN_CRYPTOPAY_VERSION, true);
wp_enqueue_script('dokan-cryptopay', plugin_dir_url(__FILE__) . 'assets/js/admin.js', ['jquery', 'wp-i18n'], DOKAN_CRYPTOPAY_VERSION, true);
wp_localize_script('dokan-cryptopay', 'DokanCryptoPay', [
'currency' => get_woocommerce_currency(),
'apiUrl' => home_url('/wp-json/dokan/v1/withdraw/')
Expand Down

0 comments on commit 0f0531c

Please sign in to comment.