Skip to content

Commit

Permalink
buyCred gateway is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Mar 27, 2024
1 parent 2eb4336 commit 0571797
Show file tree
Hide file tree
Showing 29 changed files with 2,213 additions and 7 deletions.
168 changes: 168 additions & 0 deletions app/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\MyCred;

// 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\CryptoPayLite\Payment as PaymentLite;
use BeycanPress\CryptoPay\Types\Order\OrderType;
use BeycanPress\CryptoPay\Types\Transaction\ParamsType;
use BeycanPress\CryptoPayLite\Types\Order\OrderType as LiteOrderType;
use BeycanPress\CryptoPayLite\Types\Transaction\ParamsType as LiteParamsType;

class Gateway extends \myCRED_Payment_Gateway
{
/**
* @var mixed
*/
public $prefs;

/**
* @var int|float
*/
public $cost;

/**
* @var string
*/
public $post_id;

/**
* @var string
*/
public $transaction_id;

/**
* @var string
*/
public $currency;

/**
* @param mixed $prefs
* Gateway constructor.
*/
public function __construct($prefs)
{
$types = mycred_get_types();
$defaultExchange = [];

foreach ($types as $type => $label) {
$defaultExchange[$type] = 1;
}

parent::__construct([
'id' => 'cryptopay',
'label' => Helpers::exists() ? 'CryptoPay' : 'CryptoPay Lite',
'gateway_logo_url' => MYCRED_CRYPTOPAY_URL . 'assets/images/icon.png',
'defaults' => [
'theme' => 'light',
'currency' => 'USD',
'exchange' => $defaultExchange
]
], $prefs);
}

/**
* Preferences (settings)
* @return void
*/
public function preferences(): void
{
include MYCRED_CRYPTOPAY_DIR . 'views/preferences.php';
}

/**
* Sanitise preferences
* @param array<mixed> $data
* @return array<mixed>
*/
public function sanitise_preferences($data): array
{
$newData = [];

$newData['theme'] = sanitize_text_field($data['theme']);
$newData['currency'] = sanitize_text_field($data['currency']);

// If exchange is less then 1 we must start with a zero
if (isset($data['exchange'])) {
foreach ((array) $data['exchange'] as $type => $rate) {
if (1 != $rate && in_array(substr($rate, 0, 1), ['.', ','])) {
$data['exchange'][$type] = (float) '0' . $rate;
}
}
}

$newData['exchange'] = $data['exchange'];

return $newData;
}

/**
* Payment form frontend side
* @return void
*/
public function checkout_page_body(): void
{
echo wp_kses_post($this->checkout_header());
echo wp_kses_post($this->checkout_order());

$order = [
'id' => $this->post_id,
'amount' => floatval($this->cost),
'currency' => ($this->prefs['currency'] ?? $this->currency) ?? 'USD',
];

$params = [
'transactionId' => $this->transaction_id
];

if (Helpers::exists()) {
$cp = (new Payment('mycred'))
->setOrder(OrderType::fromArray($order))
->setParams(ParamsType::fromArray($params));
} else {
$cp = (new PaymentLite('mycred'))
->setOrder(LiteOrderType::fromArray($order))
->setParams(LiteParamsType::fromArray($params));
}

echo Helpers::run('ksesEcho', $cp->html(loading: true));

echo wp_kses_post($this->checkout_cancel());
}

/**
* Ajax buy process
* @return void
*/
public function ajax_buy(): void
{
$this->send_json(esc_html__('This gateway does not support AJAX payments.', 'mycred-cryptopay'));
}

/**
* Payment request process
* @return void
*/
public function buy(): void
{
// Buy
}

/**
* Payment return process
* @return void
*/
public function process(): void
{
// Process payment
}
}
72 changes: 72 additions & 0 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,84 @@

namespace BeycanPress\CryptoPay\MyCred;

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

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

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

Hook::addAction('payment_finished_mycred', [$this, 'paymentFinished']);
Hook::addFilter('payment_redirect_urls_mycred', [$this, 'paymentRedirectUrls']);

add_filter('mycred_setup_gateways', [$this, 'registerGateway']);
}

/**
* @param object $data
* @return void
*/
public function paymentFinished(object $data): void
{
$order = $data->getOrder();
$gateway = buycred_gateway('cryptopay');
$pendingPayment = $gateway->get_pending_payment($order->getId());
if ($data->getStatus()) {
if ($gateway->complete_payment($pendingPayment, $data->getHash())) {
$gateway->trash_pending_payment($order->getId());
} else {
$gateway->log_call($order->getId(), [
sprintf(esc_html__('Failed to credit users account.', 'mycred-cryptopay'))
]);
}
} else {
$gateway->log_call($order->getId(), [
sprintf(__('Payment not completed. Transaction hash: %s', 'mycred-cryptopay'), $data->getHash())
]);
}
}

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

/**
* @param array<mixed> $gateways
* @return array<mixed>
*/
public function registerGateway(array $gateways): array
{
$gateways['cryptopay'] = [
'title' => Helpers::exists() ? 'CryptoPay' : 'CryptoPay Lite',
'documentation' => 'https://beycanpress.gitbook.io/cryptopay-docs/overview/welcome',
'callback' => [Gateway::class],
'icon' => 'dashicons-admin-generic',
'external' => true,
'custom_rate' => true
];

return $gateways;
}
}
Empty file removed assets/js/main.js
Empty file.
54 changes: 54 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions mycred-cryptopay-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@

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

if (!defined('GF_MIN_WP_VERSION') /* TODO: check the plugin */) {
Helpers::requirePluginMessage('myCred', 'https://wordpress.org/plugins/mycred/');
} elseif (Helpers::bothExists()) {
new BeycanPress\CryptoPay\MyCred\Loader();
} else {
Helpers::requireCryptoPayMessage('myCred');
}
add_action('plugins_loaded', function (): void {
if (!class_exists('myCRED_Core')) {
Helpers::requirePluginMessage('myCred', 'https://wordpress.org/plugins/mycred/');
} elseif (Helpers::bothExists()) {
new BeycanPress\CryptoPay\MyCred\Loader();
} else {
Helpers::requireCryptoPayMessage('myCred');
}
});
25 changes: 25 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
}

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

return ComposerAutoloaderInit623994210b0626aad6edc565f7c7bab2::getLoader();
1 change: 1 addition & 0 deletions vendor/beycanpress/cryptopay-integrator/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
5 changes: 5 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CryptoPay Integrator Wrapper

```bash
composer require beycanpress/cryptopay-integrator
```
25 changes: 25 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "beycanpress/cryptopay-integrator",
"version": "0.1.10",
"description": "CryptoPay and CryptoPay Lite integration wrapper",
"type": "library",
"license": "MIT",
"author": "BeycanPress",
"support": {
"issues": "https://github.com/BeycanPress/cryptopay-integrator/issues"
},
"homepage": "https://github.com/BeycanPress/cryptopay-integrator",
"require": {
"php": ">=8.1"
},
"scripts": {
"phpcs": "phpcs --standard=phpcs.xml .",
"phpcbf": "phpcbf --standard=phpcs.xml .",
"install-phpcs": "composer config --global --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true && composer global require --dev squizlabs/php_codesniffer=* slevomat/coding-standard"
},
"autoload": {
"psr-4": {
"BeycanPress\\CryptoPay\\Integrator\\": "src"
}
}
}
Loading

0 comments on commit 0571797

Please sign in to comment.