Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Mar 16, 2024
1 parent f996778 commit 3f536f6
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 34 deletions.
96 changes: 96 additions & 0 deletions app/Gateways/GatewayLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\RCP\Gateways;

use BeycanPress\CryptoPay\Integrator\Type;
use BeycanPress\CryptoPay\Integrator\Helpers;

class GatewayLite extends \RCP_Payment_Gateway
{
/**
* @var string
*/
public const ID = 'cryptopay_lite';

/**
* @var array<string>
*/
// @phpcs:ignore
public $supports = [];

/**
* @var int
*/
// @phpcs:ignore
public $user_id;

/**
* @var object
*/
// @phpcs:ignore
public $payment;

/**
* @var string
*/
// @phpcs:ignore
public $discount_code;

/**
* @var string
*/
// @phpcs:ignore
public $return_url;

/**
* @var string
*/
// @phpcs:ignore
public $currency;

/**
* @return void
*/
public function init(): void
{
$this->supports = [
'one-time'
];
}

/**
* @return void
*/
// @phpcs:ignore
public function process_signup(): void
{
/**
* @var \RCP_Payments $rcp_payments_db
*/
global $rcp_payments_db;

// Update payment record with transaction ID.
$rcp_payments_db->update($this->payment->id, [
'payment_type' => self::ID,
]);

$paymentUrl = Helpers::createSPP([
'addon' => 'rcp',
'addonName' => 'Restrict Content Pro',
'order' => [
'id' => $this->payment->id,
'amount' => $this->payment->amount,
'currency' => $this->currency,
],
'params' => [
'returnUrl' => $this->return_url,
],
'type' => Type::LITE,
]);

wp_redirect($paymentUrl);
exit;
}
}
96 changes: 96 additions & 0 deletions app/Gateways/GatewayPro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\RCP\Gateways;

use BeycanPress\CryptoPay\Integrator\Type;
use BeycanPress\CryptoPay\Integrator\Helpers;

class GatewayPro extends \RCP_Payment_Gateway
{
/**
* @var string
*/
public const ID = 'cryptopay';

/**
* @var array<string>
*/
// @phpcs:ignore
public $supports = [];

/**
* @var int
*/
// @phpcs:ignore
public $user_id;

/**
* @var object
*/
// @phpcs:ignore
public $payment;

/**
* @var string
*/
// @phpcs:ignore
public $discount_code;

/**
* @var string
*/
// @phpcs:ignore
public $return_url;

/**
* @var string
*/
// @phpcs:ignore
public $currency;

/**
* @return void
*/
public function init(): void
{
$this->supports = [
'one-time'
];
}

/**
* @return void
*/
// @phpcs:ignore
public function process_signup(): void
{
/**
* @var \RCP_Payments $rcp_payments_db
*/
global $rcp_payments_db;

// Update payment record with transaction ID.
$rcp_payments_db->update($this->payment->id, [
'payment_type' => self::ID,
]);

$paymentUrl = Helpers::createSPP([
'addon' => 'rcp',
'addonName' => 'Restrict Content Pro',
'order' => [
'id' => $this->payment->id,
'amount' => $this->payment->amount,
'currency' => $this->currency,
],
'params' => [
'returnUrl' => $this->return_url,
],
'type' => Type::PRO,
]);

wp_redirect($paymentUrl);
exit;
}
}
121 changes: 121 additions & 0 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,133 @@

namespace BeycanPress\CryptoPay\RCP;

use BeycanPress\CryptoPay\Integrator\Hook;
use BeycanPress\CryptoPay\Integrator\Helpers;
use BeycanPress\CryptoPay\RCP\Gateways\GatewayPro;
use BeycanPress\CryptoPay\RCP\Gateways\GatewayLite;

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

// add transaction page
Helpers::createTransactionPage(
esc_html__('Restrict Content Pro Transactions', 'rcp-cryptopay'),
'rcp',
10,
[
'orderId' => function ($tx) {
return Helpers::run('view', 'components/link', [
'url' => sprintf(admin_url('admin.php?page=rcp-payments&payment_id=%d&view=edit-payment'), $tx->orderId), // @phpcs:ignore
'text' => sprintf(esc_html__('View payment #%d', 'gf-cryptopay'), $tx->orderId)
]);
}
]
);

Hook::addAction('payment_finished_rcp', [$this, 'paymentFinished']);
Hook::addFilter('payment_redirect_urls_rcp', [$this, 'paymentRedirectUrls']);

add_action('init', [Helpers::class, 'listenSPP']);
add_filter('rcp_payment_gateways', [$this, 'registerGateways']);
add_filter('rcp_merchant_transaction_id_link', [$this, 'transactionIdLink'], 10, 2);
}

/**
* @param object $data
* @return void
*/
public function paymentFinished(object $data): void
{
$tx = $data->getModel()->findOneBy([
'hash' => $data->getHash()
]);
global $rcp_payments_db;

update_post_meta($tx->getId(), 'rcp_transaction_id', $data->getHash());

if ($data->getStatus()) {
$rcp_payments_db->update($data->getOrder()->getId(), [
'status' => 'complete',
'transaction_id' => $tx->getId(),
]);
rcp_complete_registration($data->getOrder()->getId());
} else {
$rcp_payments_db->update($data->getOrder()->getId(), [
'status' => 'failed',
'transaction_id' => $tx->getId(),
]);
}
}

/**
* @param object $data
* @return array<string>
*/
public function paymentRedirectUrls(object $data): array
{
return [
'success' => $data->getParams()->get('returnUrl'),
'failed' => add_query_arg([
'rcp-action' => 'download_invoice',
'payment_id' => $data->getOrder()->getId()
], home_url())
];
}

/**
* @param string $transactionId
* @param object $payment
* @return string
*/
public function transactionIdLink(string $transactionId, object $payment): string
{
if (!in_array($payment->gateway, [GatewayLite::ID, GatewayPro::ID], true)) {
return $transactionId;
}

if (empty($transactionId)) {
return esc_html__('Waiting for payment', 'rcp-cryptopay');
}

$txHash = get_post_meta($transactionId, 'rcp_transaction_id', true);

if (empty($txHash)) {
return esc_html__('Transaction not found', 'rcp-cryptopay');
}

return Helpers::run('view', 'components/link', [
'url' => sprintf(admin_url('admin.php?page=%s_rcp_transactions&s=%s'), $payment->gateway, $txHash),
'text' => esc_html__('View transaction', 'rcp-cryptopay')
]);
}

/**
* @param array<string> $gateways
* @return array<string>
*/
public function registerGateways(array $gateways): array
{
if (Helpers::exists()) {
$gateways[GatewayPro::ID] = [
'label' => __('CryptoPay', 'rcp-cryptopay'),
'admin_label' => __('CryptoPay', 'rcp-cryptopay'),
'class' => GatewayPro::class
];
}

if (Helpers::liteExists()) {
$gateways[GatewayLite::ID] = [
'label' => __('CryptoPay Lite', 'rcp-cryptopay'),
'admin_label' => __('CryptoPay Lite', 'rcp-cryptopay'),
'class' => GatewayLite::class
];
}
return $gateways;
}
}
Empty file removed assets/js/main.js
Empty file.
40 changes: 21 additions & 19 deletions rcp-cryptopay-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,24 @@

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

if (!defined('RCP_PLUGIN_FILE')) {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Restrict Content Pro - CryptoPay Gateway: This plugin requires Restrict Content Pro to work. You can download Restrict Content Pro by %s.', 'rcp-cryptopay'), '<a href="https://wordpress.org/plugins/restrict-content/" target="_blank">' . esc_html__('clicking here', 'rcp-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
} elseif (Helpers::bothExists()) {
new BeycanPress\CryptoPay\RCP\Loader();
} else {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Restrict Content Pro - 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.', 'rcp-cryptopay'), '<a href="https://beycanpress.com/product/cryptopay-all-in-one-cryptocurrency-payments-for-wordpress/?utm_source=wp_org_addons&utm_medium=rcp" target="_blank">' . esc_html__('clicking here', 'rcp-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
}
add_action('plugins_loaded', function (): void {
if (!defined('RCP_PLUGIN_FILE')) {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Restrict Content Pro - CryptoPay Gateway: This plugin requires Restrict Content Pro to work. You can download Restrict Content Pro by %s.', 'rcp-cryptopay'), '<a href="https://wordpress.org/plugins/restrict-content/" target="_blank">' . esc_html__('clicking here', 'rcp-cryptopay') . '</a>'); ?></p>
</div>
<?php
});
} elseif (Helpers::bothExists()) {
new BeycanPress\CryptoPay\RCP\Loader();
} else {
add_action('admin_notices', function (): void {
?>
<div class="notice notice-error">
<p><?php echo sprintf(esc_html__('Restrict Content Pro - 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.', 'rcp-cryptopay'), '<a href="https://beycanpress.com/product/cryptopay-all-in-one-cryptocurrency-payments-for-wordpress/?utm_source=wp_org_addons&utm_medium=rcp" target="_blank">' . esc_html__('clicking here', 'rcp-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 ComposerAutoloaderInit0b588e2e3118b6a111006bb6d905d6b6::getLoader();
return ComposerAutoloaderInit6be0de7c2c544c6b51cbdeae0bbe23be::getLoader();
Loading

0 comments on commit 3f536f6

Please sign in to comment.