Skip to content

Commit

Permalink
phase 1 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
BeycanDeveloper committed Mar 2, 2024
1 parent 46ce282 commit 9934802
Show file tree
Hide file tree
Showing 13 changed files with 724 additions and 57 deletions.
459 changes: 459 additions & 0 deletions app/Gateways/AbstractGateway.php

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions app/Gateways/CryptoPayLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\GravityForms\Gateways;

// @phpcs:disable Generic.Files.InlineHTML
// @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps

use BeycanPress\CryptoPayLite\Payment;

class CryptoPayLite extends AbstractGateway
{
/**
* @var string
*/
// @phpcs:ignore
public $type = 'cryptopay-lite';

/**
* @param array<mixed> $properties
*/
public function __construct(array $properties = [])
{
parent::__construct($properties);
}

/**
* @return string
*/
public function get_form_editor_field_title(): string
{
return esc_attr__('CryptoPay Lite', 'gf-cryptopay');
}

/**
* @return string
*/
public function get_form_editor_field_description(): string
{
return esc_attr__('Adds cryptocurrency payments to your form.', 'gf-cryptopay');
}

/**
* @param int $formId
* @return string
*/
public function run(int $formId): string
{
return (new Payment('gravityforms'))->html();
}
}
46 changes: 46 additions & 0 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@

namespace BeycanPress\CryptoPay\GravityForms;

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

class Loader
{
/**
* Loader constructor.
*/
public function __construct()
{
add_action('gform_loaded', [$this, 'register'], 5);

Helpers::registerIntegration('gravityforms');
Helpers::createTransactionPage(
esc_html__('GravityForms transactions', 'gf-cryptopay'),
'gravityforms',
10,
[],
['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
{
return [
'success' => '#',
'failed' => 'reload'
];
}

/**
* @return void
*/
public function register(): void
{
if (Helpers::exists()) {
\GF_Fields::register(new Gateways\CryptoPayLite());
} else {
\GF_Fields::register(new Gateways\CryptoPayLite());
}
}
}
22 changes: 20 additions & 2 deletions app/Models/TransactionsLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@

class TransactionsLite extends AbstractTransaction
{
public string $addon = 'gravity_forms';
public string $addon = 'gravityforms';

/**
* @return void
*/
public function __construct()
{
parent::__construct('gravity_forms_transaction');
parent::__construct('gravityforms_transaction');
}

/**
* @param int $userId
* @param string $formId
* @return object|null
*/
public function findOneByUserAndFormId(int $userId, string $formId): ?object
{
return $this->getRow(str_ireplace(
['%d', '%s'],
[$userId, $formId],
"SELECT * FROM {$this->tableName}
WHERE `userId` = %d
AND `params` LIKE '%{\"formId\":\"%s\"}%'
ORDER BY `id` DESC
LIMIT 1"
));
}
}
22 changes: 20 additions & 2 deletions app/Models/TransactionsPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@

class TransactionsPro extends AbstractTransaction
{
public string $addon = 'gravity_forms';
public string $addon = 'gravityforms';

/**
* @return void
*/
public function __construct()
{
parent::__construct('gravity_forms_transaction');
parent::__construct('gravityforms_transaction');
}

/**
* @param int $userId
* @param string $formId
* @return object
*/
public function findOneByUserAndFormId(int $userId, string $formId): object
{
return $this->getRow(str_ireplace(
['%d', '%s'],
[$userId, $formId],
"SELECT * FROM {$this->tableName}
WHERE `userId` = %d
AND `params` LIKE '%{\"formId\":\"%s\"}%'
ORDER BY `id` DESC
LIMIT 1"
));
}
}
Binary file added assets/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/images/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions assets/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
;(($) => {
$(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;

$(".gform_body [aria-required='true']").each(function() {
$(this).attr('required', true);
});

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(() => {
$('.overlay').remove();
startedApp.store.payment.$reset();
$('#cryptopay, #cryptopay-lite').remove();

// scroll to form
const wrapperOffset = $('.gform_wrapper').offset().top;
$('html, body').animate({scrollTop: wrapperOffset}, 1000);

// set tx hash to hidden input
$('#' + fieldInputId).val(txHash);
$('#' + fieldInputId).closest('.gfield').hide();

// submit form
form.find('#custom-submit-placeholder').append(submitButton);
form.submit();
});
}

gform?.addFilter('gform_product_total', function (amount, formId) {
if (amount !== oldAmount) {
oldAmount = amount;
if (window.CryptoPayApp) {
CryptoPayApp.events.add('confirmationCompleted', async (ctx) => {
paymentCompleted(ctx, formId);
}, 'gravity_forms');
if (!startedApp) {
startedApp = window.CryptoPayApp.start({
amount,
currency,
}, { formId });
} else {
startedApp.reStart({
amount,
currency,
}, { formId })
}
} else if (window.CryptoPayLiteApp) {
CryptoPayLiteApp.events.add('confirmationCompleted', async (ctx) => {
paymentCompleted(ctx, formId);
}, 'gravity_forms');
if (!startedApp) {
startedApp = window.CryptoPayLiteApp.start({
amount,
currency,
}, { formId });
} else {
startedApp.reStart({
amount,
currency,
}, { formId })
}
}
}
return amount;
});
});
})(jQuery);
70 changes: 28 additions & 42 deletions gravity-forms-cryptopay-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Author URI: https://beycanpress.com
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: md-cryptopay
* Text Domain: gf-cryptopay
* Tags: Cryptopay, Cryptocurrency, WooCommerce, WordPress, MetaMask, Trust, Binance, Wallet, Ethereum, Bitcoin, Binance smart chain, Payment, Plugin, Gateway, Moralis, Converter, API, coin market cap, CMC
* Requires at least: 5.0
* Tested up to: 6.4.3
Expand All @@ -28,50 +28,36 @@
// Autoload
require_once __DIR__ . '/vendor/autoload.php';

use BeycanPress\CryptoPay\Integrator\Helpers;

define('GF_CRYPTOPAY_FILE', __FILE__);
define('GF_CRYPTOPAY_VERSION', '1.0.0');
define('GF_CRYPTOPAY_KEY', basename(__DIR__));
define('GF_CRYPTOPAY_URL', plugin_dir_url(__FILE__));
define('GF_CRYPTOPAY_DIR', plugin_dir_path(__FILE__));
define('GF_CRYPTOPAY_SLUG', plugin_basename(__FILE__));

/**
* @return void
*/
function gf_cryptopay_addModels(): void
{
Helpers::registerModel(BeycanPress\CryptoPay\GravityForms\Models\TransactionsPro::class);
Helpers::registerLiteModel(BeycanPress\CryptoPay\GravityForms\Models\TransactionsLite::class);
}

gf_cryptopay_addModels();

add_action('plugins_loaded', function (): void {

gf_cryptopay_addModels();

load_plugin_textdomain('md-cryptopay', false, basename(__DIR__) . '/languages');

if (!defined('GF_MIN_WP_VERSION')) {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Gravity Forms - CryptoPay Gateway: This plugin requires Gravity Forms to work. You can buy Gravity Forms by %s.', 'md-cryptopay'), '<a href="https://www.gravityforms.com/" target="_blank">' . esc_html__('clicking here', 'md-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
return;
}
use BeycanPress\CryptoPay\Integrator\Helpers;

if (Helpers::bothExists()) {
new BeycanPress\CryptoPay\GravityForms\Loader();
} else {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Gravity Forms - CryptoPay Gateway: This plugin is an extra feature plugin so it cannot do anything on its own. It needs CryptoPay to work. You can buy CryptoPay by %s.', 'md-cryptopay'), '<a href="https://beycanpress.com/product/cryptopay-all-in-one-cryptocurrency-payments-for-wordpress/?utm_source=wp_org_addons&utm_medium=gravity_forms" target="_blank">' . esc_html__('clicking here', 'md-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
}
});
Helpers::registerModel(BeycanPress\CryptoPay\GravityForms\Models\TransactionsPro::class);
Helpers::registerLiteModel(BeycanPress\CryptoPay\GravityForms\Models\TransactionsLite::class);

load_plugin_textdomain('gf-cryptopay', false, basename(__DIR__) . '/languages');

if (!defined('GF_MIN_WP_VERSION')) {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Gravity Forms - CryptoPay Gateway: This plugin requires Gravity Forms to work. You can buy Gravity Forms by %s.', 'gf-cryptopay'), '<a href="https://www.gravityforms.com/" target="_blank">' . esc_html__('clicking here', 'gf-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
} elseif (Helpers::bothExists()) {
new BeycanPress\CryptoPay\GravityForms\Loader();
} else {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Gravity Forms - CryptoPay Gateway: This plugin is an extra feature plugin so it cannot do anything on its own. It needs CryptoPay to work. You can buy CryptoPay by %s.', 'gf-cryptopay'), '<a href="https://beycanpress.com/product/cryptopay-all-in-one-cryptocurrency-payments-for-wordpress/?utm_source=wp_org_addons&utm_medium=gravity_forms" target="_blank">' . esc_html__('clicking here', 'gf-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
}
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit6be0de7c2c544c6b51cbdeae0bbe23be::getLoader();
return ComposerAutoloaderInitf99ffda5f6e370daee8d691b9e8af453::getLoader();
8 changes: 4 additions & 4 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit6be0de7c2c544c6b51cbdeae0bbe23be
class ComposerAutoloaderInitf99ffda5f6e370daee8d691b9e8af453
{
private static $loader;

Expand All @@ -24,12 +24,12 @@ public static function getLoader()

require __DIR__ . '/platform_check.php';

spl_autoload_register(array('ComposerAutoloaderInit6be0de7c2c544c6b51cbdeae0bbe23be', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitf99ffda5f6e370daee8d691b9e8af453', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit6be0de7c2c544c6b51cbdeae0bbe23be', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitf99ffda5f6e370daee8d691b9e8af453', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit6be0de7c2c544c6b51cbdeae0bbe23be::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitf99ffda5f6e370daee8d691b9e8af453::getInitializer($loader));

$loader->register(true);

Expand Down
Loading

0 comments on commit 9934802

Please sign in to comment.