Skip to content

Commit

Permalink
phase 2 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Mar 2, 2024
1 parent 9934802 commit 141b60a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 37 deletions.
81 changes: 65 additions & 16 deletions app/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 === '<?php echo esc_js($this->type); ?>') {
fieldSubmit.hide();
hideSubmit()
currentFieldId = parseInt(field.id);
}
if (form.fields.some(field => field.type === 'total')) {
Expand All @@ -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(
'<?php echo $this->get_field_works_or_expect_msg(false); ?>'
);
}
if (!form.fields.some(field => field.type === '<?php echo esc_js($this->type); ?>')) {
showSubmit();
}
});
})
})(jQuery);
Expand Down Expand Up @@ -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 '<div id="custom-submit-placeholder"></div>';
if (!$this->is_admin_side()) {
return '<div id="custom-submit-placeholder"></div>';
} else {
return '<div class="custom-submit-placeholder" style="display:none">' . $button . '</div>';
}
}

return $button;
}

/**
* @param array<string,mixed> $entry
* @param array<string,mixed> $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<string> $deps
Expand All @@ -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(),
Expand All @@ -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
Expand All @@ -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<label class='gfield_label' for='%s'>%s</label>{FIELD}",
$adminButtons,
Expand Down
2 changes: 1 addition & 1 deletion app/Gateways/CryptoPayLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CryptoPayLite extends AbstractGateway
* @var string
*/
// @phpcs:ignore
public $type = 'cryptopay-lite';
public $type = 'cryptopay_lite';

/**
* @param array<mixed> $properties
Expand Down
16 changes: 12 additions & 4 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ 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
* @return array<string,string>
*/
public function paymentRedirectUrls(object $data): array
{
$formId = $data->getParams()->get('formId');
return [
'success' => '#',
'failed' => 'reload'
'success' => '#gform_wrapper_' . $formId,
'failed' => '#gform_wrapper_' . $formId
];
}

Expand Down
17 changes: 17 additions & 0 deletions app/Models/TransactionsLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
}
17 changes: 17 additions & 0 deletions app/Models/TransactionsPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
}
59 changes: 43 additions & 16 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
$('<div id="cpEmptyMessage" class="gform_validation_errors" style="text-align:center">' + pleaseFillForm + '</div>').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(() => {
Expand All @@ -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) => {
Expand Down

0 comments on commit 141b60a

Please sign in to comment.