diff --git a/app/Gateways/AbstractGateway.php b/app/Gateways/AbstractGateway.php index db20f0b..8f43891 100644 --- a/app/Gateways/AbstractGateway.php +++ b/app/Gateways/AbstractGateway.php @@ -76,7 +76,8 @@ public function __construct(array $properties = []) add_action('gform_editor_js', [$this, 'editor_script']); } - add_filter('gform_submit_button', array($this, 'form_submit_button'), 10, 2); + add_filter('gform_submit_button', [$this, 'form_submit_button'], 10, 2); + add_filter('gform_entry_post_save', [$this, 'add_entry_id_to_tx'], 10, 2); // Creating multiple fields, when this fixed, enable this. // add_action('gform_field_standard_settings', [ $this, 'field_standard_settings' ], 10, 2); } @@ -186,13 +187,28 @@ public function editor_script(): void $(document).ready(() => { let currentFieldId = 0; const fieldList = $("#gform_fields"); - const fieldSubmit = $("#field_submit"); - gform.addAction('gform_page_loaded', function(event, formId, currentPage){ - console.log(event, formId, currentPage); - }); + const customSubmit = $(".custom-submit-placeholder"); + const customSubmitParent = customSubmit.closest('#field_submit'); + if (customSubmitParent) { + customSubmitParent.hide(); + } + + const hideSubmit = () => { + currentFieldId = 0; + customSubmit.hide(); + customSubmitParent.hide(); + $('#field_submit').hide(); + } + + const showSubmit = () => { + customSubmit.show(); + customSubmitParent.show(); + $('#field_submit').show(); + } + $(document).on('gform_field_added', function (event, form, field) { if (field.type === 'type); ?>') { - fieldSubmit.hide(); + hideSubmit() currentFieldId = parseInt(field.id); } if (form.fields.some(field => field.type === 'total')) { @@ -203,14 +219,16 @@ public function editor_script(): void }); $(document).on('gform_field_deleted', function (event, form, fieldId) { if (parseInt(fieldId) === currentFieldId) { - fieldSubmit.show(); - currentFieldId = 0; + showSubmit(); } if (!form.fields.some(field => field.type === 'total')) { $('#field_' + currentFieldId + ' .ginput_container')?.html( 'get_field_works_or_expect_msg(false); ?>' ); } + if (!form.fields.some(field => field.type === 'type); ?>')) { + showSubmit(); + } }); }) })(jQuery); @@ -358,12 +376,35 @@ private function create_custom_submit_button(string $formId): string public function form_submit_button($button, $form): string { if ($this->is_this_field_works($form) && $this->is_this_form_needs_payment($form)) { - return '
'; + if (!$this->is_admin_side()) { + return '
'; + } else { + return ''; + } } return $button; } + /** + * @param array $entry + * @param array $form + * @return mixed + */ + // @phpcs:ignore + public function add_entry_id_to_tx($entry, $form): mixed + { + if (!$this->form_has_this_field($form)) { + return $entry; + } + + $model = Helpers::run('getModelByAddon', 'gravityforms'); + $txHash = sanitize_text_field($_POST[$this->field_input_id] ?? ''); + $model->updateOrderIdByTxHash($txHash, intval($entry['id'])); + + return $entry; + } + /** * @param string $formId * @param array $deps @@ -383,6 +424,7 @@ public function custom_enqueue_scripts(string $formId, array $deps): void 'cryptopay_main_js', 'gf_cryptopay_vars', array( + 'formId' => $formId, 'fieldId' => $this->id, 'fieldInputId' => $this->field_input_id, 'currency' => \GFCommon::get_currency(), @@ -392,6 +434,16 @@ public function custom_enqueue_scripts(string $formId, array $deps): void ); } + /** + * @return bool + */ + private function is_admin_side(): bool + { + $isEntryDetail = $this->is_entry_detail(); + $isFormEditor = $this->is_form_editor(); + return $isEntryDetail || $isFormEditor; + } + /** * @param string $value * @param bool $forceFrontendLabel @@ -401,14 +453,11 @@ public function custom_enqueue_scripts(string $formId, array $deps): void // @phpcs:ignore public function get_field_content($value, $forceFrontendLabel, $form): string { - $formId = absint($form['id']); - $adminButtons = $this->get_admin_buttons(); - $isEntryDetail = $this->is_entry_detail(); - $isFormEditor = $this->is_form_editor(); - $isAdmin = $isEntryDetail || $isFormEditor; - $fieldLabel = $this->get_field_label($forceFrontendLabel, $value); + $formId = absint($form['id']); + $adminButtons = $this->get_admin_buttons(); + $fieldLabel = $this->get_field_label($forceFrontendLabel, $value); - if ($isAdmin) { + if ($this->is_admin_side()) { return sprintf( "%s{FIELD}", $adminButtons, diff --git a/app/Gateways/CryptoPayLite.php b/app/Gateways/CryptoPayLite.php index 1a0bed6..0d7b198 100644 --- a/app/Gateways/CryptoPayLite.php +++ b/app/Gateways/CryptoPayLite.php @@ -15,7 +15,7 @@ class CryptoPayLite extends AbstractGateway * @var string */ // @phpcs:ignore - public $type = 'cryptopay-lite'; + public $type = 'cryptopay_lite'; /** * @param array $properties diff --git a/app/Loader.php b/app/Loader.php index ba8f875..e1ff7e2 100644 --- a/app/Loader.php +++ b/app/Loader.php @@ -21,13 +21,20 @@ public function __construct() esc_html__('GravityForms transactions', 'gf-cryptopay'), 'gravityforms', 10, - [], - ['orderId'] + [ + 'orderId' => function ($tx) { + return Helpers::run('view', 'components/link', [ + 'url' => sprintf(admin_url('admin.php?page=gf_entries&view=entry&id=%d&lid=%d&order=ASC&filter&paged=1&pos=0&field_id&operator'), $tx->params->formId, $tx->orderId), // @phpcs:ignore + 'text' => sprintf(esc_html__('View entry #%d', 'gf-cryptopay'), $tx->orderId) + ]); + } + ], ); Hook::addFilter('payment_redirect_urls_gravityforms', [$this, 'paymentRedirectUrls']); } + /** * Payment redirect urls * @param object $data @@ -35,9 +42,10 @@ public function __construct() */ public function paymentRedirectUrls(object $data): array { + $formId = $data->getParams()->get('formId'); return [ - 'success' => '#', - 'failed' => 'reload' + 'success' => '#gform_wrapper_' . $formId, + 'failed' => '#gform_wrapper_' . $formId ]; } diff --git a/app/Models/TransactionsLite.php b/app/Models/TransactionsLite.php index efc8d89..019ffcc 100644 --- a/app/Models/TransactionsLite.php +++ b/app/Models/TransactionsLite.php @@ -35,4 +35,21 @@ public function findOneByUserAndFormId(int $userId, string $formId): ?object LIMIT 1" )); } + + /** + * @param string $hash + * @param int $orderId + * @return bool + */ + public function updateOrderIdByTxHash(string $hash, int $orderId): bool + { + return (bool) $this->update( + [ + 'orderId' => $orderId, + ], + [ + 'hash' => $hash, + ] + ); + } } diff --git a/app/Models/TransactionsPro.php b/app/Models/TransactionsPro.php index 99fa9dd..1d72a0b 100644 --- a/app/Models/TransactionsPro.php +++ b/app/Models/TransactionsPro.php @@ -35,4 +35,21 @@ public function findOneByUserAndFormId(int $userId, string $formId): object LIMIT 1" )); } + + /** + * @param string $hash + * @param int $orderId + * @return bool + */ + public function updateOrderIdByTxHash(string $hash, int $orderId): bool + { + return (bool) $this->update( + [ + 'orderId' => $orderId, + ], + [ + 'hash' => $hash, + ] + ); + } } diff --git a/assets/js/main.js b/assets/js/main.js index 44af647..8ae5fbd 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -2,27 +2,53 @@ $(document).ready(() => { let startedApp; let oldAmount = 0; - const currency = gf_cryptopay_vars.currency; - const submitButton = gf_cryptopay_vars.submitButton; - const fieldInputId = gf_cryptopay_vars.fieldInputId; + let completed = false; + const { + formId, + currency, + submitButton, + fieldInputId, + pleaseFillForm + } = window.gf_cryptopay_vars; - $(".gform_body [aria-required='true']").each(function() { - $(this).attr('required', true); - }); + const checkFormEmpty = () => { + var isEmpty = false; + const el = $('#cryptopay, #cryptopay-lite'); + + $('#gform_' + formId).find('[aria-required="true"]').each(function() { + var elementType = $(this).prop('tagName').toLowerCase(); + var value = ''; + if (elementType === 'input' || elementType === 'select') { + value = $(this).val(); + } else if (elementType === 'textarea') { + value = $(this).text(); + } else if (elementType === 'checkbox') { + value = $(this).is(':checked') ? 'checked' : ''; + } + if (!value) { + isEmpty = true; + return false; + } + }); + + if (isEmpty) { + el.hide(); + if ($('#cpEmptyMessage').length === 0) { + $('
' + pleaseFillForm + '
').insertBefore(el); + } + } else { + el.show(); + $('#cpEmptyMessage').remove(); + } + } + + $('#gform_' + formId).on('change', checkFormEmpty); + $('#gform_' + formId).on('keyup', checkFormEmpty); const paymentCompleted = async (ctx, formId) => { const form = $('#gform_' + formId); const helpers = window.cpHelpers || window.cplHelpers; const txHash = ctx.transaction.hash || ctx.transaction.id; - - console.log(ctx) - // Modal yapısına geçilecek ve manuel olarak required alan kontrolü yapılacak - // Create temporary payment completed record - // Submit form - // entry'ler ile transactionlar ilişkilendirilecek, - // eğer bir kullanıcıya ve form id'sine ait tx varsa ödeme yapılmıştır sayılacak - // ve entry oluşturulduğunda tx ile ilişkilendirilecek - // eğer yoksa ödeme yapılması zorunlu olacak helpers.closePopup(); await helpers.sleep(100); helpers.successPopup('Payment completed successfully!').then(() => { @@ -41,11 +67,12 @@ // submit form form.find('#custom-submit-placeholder').append(submitButton); form.submit(); + completed = true; }); } gform?.addFilter('gform_product_total', function (amount, formId) { - if (amount !== oldAmount) { + if (amount !== oldAmount && !completed) { oldAmount = amount; if (window.CryptoPayApp) { CryptoPayApp.events.add('confirmationCompleted', async (ctx) => {