Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
BeycanDeveloper committed Mar 24, 2024
1 parent b6e9d5a commit 09c0b88
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 0 deletions.
193 changes: 193 additions & 0 deletions app/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\NinjaForms;

// phpcs:disable PSR1.Methods.CamelCapsMethodName
// phpcs:disable Squiz.NamingConventions.ValidVariableName
// phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint

use BeycanPress\CryptoPay\Payment;
use BeycanPress\CryptoPay\Integrator\Helpers;
use BeycanPress\CryptoPay\Integrator\Session;
use BeycanPress\CryptoPayLite\Payment as PaymentLite;

class Field extends \NF_Abstracts_Field
{
/**
* @var string
*/
protected $_name;

/**
* @var string
*/
protected $_type;

/**
* @var string
*/
protected $_nicename;

/**
* @var string
*/
protected $_section = 'misc';

/**
* @var string
*/
protected $_icon = 'bitcoin';

/**
* @var string
*/
protected $_templates = 'cryptopay';

/**
* @var string
*/
protected $_test_value = '';

/**
* @var array<string>
*/
protected $_settings = [];

/**
* @var array<string>
*/
protected $_settings_only = [
'default',
];

/**
* @var array<string>
*/
protected $_use_merge_tags_include = [
'calculations'
];

/**
* @var string|null
*/
private ?string $transactionHash;

/**
* constructor.
*/
public function __construct()
{
parent::__construct();
$this->_nicename = Helpers::exists() ? 'CryptoPay' : 'CryptoPay Lite';
$this->_name = $this->_type = 'cryptopay';
$this->_settings['default']['use_merge_tags'] = [
'include' => [
'calcs'
],
'exclude' => [
'form',
'fields',
'label'
],
];

$this->transactionHash = Session::get('ninjaforms_transaction_hash');

add_filter('ninja_forms_localize_field_' . $this->_type, [$this, 'loadScripts']);
}

/**
* @param array<mixed> $field
* @return array<mixed>
*/
public function loadScripts(array $field): array
{
add_action('wp_footer', function (): void {
Helpers::run('addStyle', 'main.min.css');
if (Helpers::exists()) {
Helpers::run('ksesEcho', (new Payment('ninjaforms'))->modal());
} else {
Helpers::run('ksesEcho', (new PaymentLite('ninjaforms'))->modal());
}

wp_enqueue_script(
'ninja-forms-cryptopay',
NINJA_FORMS_CRYPTOPAY_URL . 'assets/js/main.js',
['jquery', 'wp-i18n', Helpers::run('getProp', 'mainJsKey')],
NINJA_FORMS_CRYPTOPAY_VERSION,
true
);

$paidTransaction = null;

if ($this->transactionHash) {
$model = Helpers::run('getModelByAddon', 'ninjaforms');

$transaction = $model->findOneBy([
'hash' => $this->transactionHash,
]);

if ($transaction) {
$paidTransaction = [
'hash' => $transaction->getHash(),
'formId' => $transaction->getParams()->get('formId'),
];
}
}

wp_localize_script(
'ninja-forms-cryptopay',
'ninja_forms_field_cryptopay',
[
'paidTransaction' => $paidTransaction,
'alreadyPaidMessage' => esc_html__('A payment has already been made for this form, but the form has not been sent. Therefore please only submit the form.', 'ninjaforms-cryptopay'), // phpcs:ignore
'paymentCompletedMessage' => esc_html__('Payment completed successfully.', 'ninjaforms-cryptopay')
]
);
});

return $field;
}

/**
* @param array<string> $field
* @param array<string> $data
* @return mixed
*/
public function validate($field, $data): mixed
{
$errors = parent::validate($field, $data);
if (!empty($errors)) {
return $errors;
}

$message = esc_html__(
'Payment is not verified. Sending form has been aborted.',
'ninjaforms-cryptopay'
);

$transactionHash = $data['extra']['cryptopay_transaction_hash'] ?? '';
$sTransactionHash = Session::get('ninjaforms_transaction_hash');

if ($sTransactionHash !== $transactionHash) {
return $message;
}

$model = Helpers::run('getModelByAddon', 'ninjaforms');

$transaction = $model->findOneBy([
'hash' => $transactionHash,
]);

if (!$transaction) {
return $message;
} else {
Session::remove('ninjaforms_transaction_hash');
return null;
}
}
}
46 changes: 46 additions & 0 deletions app/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\NinjaForms;

class Gateway extends \NF_Abstracts_PaymentGateway
{
/**
* @var string
*/
// phpcs:ignore
protected $_name = 'CryptoPay';

/**
* @var string
*/
// phpcs:ignore
protected $_slug = 'cryptopay';

/**
* @var array<mixed>
*/
// phpcs:ignore
protected $_settings = [];

/**
* GatewayLite constructor.
*/
public function __construct()
{
parent::__construct();
}

/**
* @param array<mixed> $actionSettings
* @param string $formId
* @param array<mixed> $data
* @return void
*/
// phpcs:ignore
public function process($actionSettings, $formId, $data): void
{
return;
}
}
76 changes: 76 additions & 0 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,88 @@

namespace BeycanPress\CryptoPay\NinjaForms;

use BeycanPress\CryptoPay\Integrator\Hook;
use BeycanPress\CryptoPay\Integrator\Helpers;
use BeycanPress\CryptoPay\Integrator\Session;

class Loader
{
/**
* Loader constructor.
*/
public function __construct()
{
Helpers::registerIntegration('ninjaforms');

Helpers::createTransactionPage(
esc_html__('Ninja Forms Transactions', 'ninjaforms-cryptopay'),
'ninjaforms',
10,
[],
['orderId']
);

Hook::addAction('payment_finished_ninjaforms', [$this, 'paymentFinished']);
Hook::addFilter('payment_redirect_urls_ninjaforms', [$this, 'paymentRedirectUrls']);

add_filter('ninja_forms_register_fields', [$this, 'registerFields']);
add_filter('ninja_forms_field_template_file_paths', [$this, 'addTemplatePath']);
//add_filter('ninja_forms_register_payment_gateways', [$this, 'registerGateways']);
}

/**
* @param object $data
* @return void
*/
public function paymentFinished(object $data): void
{
Session::set('ninjaforms_transaction_hash', $data->getHash());
}

/**
* @param object $data
* @return array<string>
*/
public function paymentRedirectUrls(object $data): array
{
return [
'success' => '#success',
'failed' => '#failed'
];
}

/**
* @param array<mixed> $fields
* @return array<mixed>
*/
public function registerFields(array $fields): array
{
$field = new Field();
$fields[$field->get_name()] = $field;

return $fields;
}

/**
* @param array<mixed> $paths
* @return array<mixed>
*/
public function addTemplatePath(array $paths): array
{
$paths[] = NINJA_FORMS_CRYPTOPAY_DIR . 'views/';

return $paths;
}

/**
* @param array<mixed> $gateways
* @return array<mixed>
*/
public function registerGateways(array $gateways): array
{
$gateway = new Gateway();
$gateways[$gateway->get_slug()] = $gateway;

return $gateways;
}
}
Loading

0 comments on commit 09c0b88

Please sign in to comment.