Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Mar 15, 2024
1 parent 267f2f0 commit ef22410
Show file tree
Hide file tree
Showing 31 changed files with 2,319 additions and 23 deletions.
182 changes: 182 additions & 0 deletions app/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\PP\Gateways;

// @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps

use BeycanPress\CryptoPay\Integrator\Type;
use BeycanPress\CryptoPay\Integrator\Helpers;
use ProfilePress\Core\Membership\Models\Order\OrderEntity;
use ProfilePress\Core\Membership\Models\Order\OrderFactory;
use ProfilePress\Core\Membership\Controllers\CheckoutResponse;
use ProfilePress\Core\Membership\PaymentMethods\AbstractPaymentMethod;

abstract class AbstractGateway extends AbstractPaymentMethod
{
/**
* @var string
*/
// @phpcs:ignore
protected $id;

/**
* @var bool
*/
// @phpcs:ignore
protected $backend_only = false;

/**
* @var string
*/
// @phpcs:ignore
protected $method_title;

/**
* @var string
*/
// @phpcs:ignore
protected $method_description;

/**
* @var string
*/
// @phpcs:ignore
protected $title;

/**
* @var string
*/
// @phpcs:ignore
protected $description;

/**
* @var array<string>
*/
// @phpcs:ignore
protected $supports;

/**
* @param string $id
* @param string $title
* Gateway constructor.
*/
public function __construct(string $id, string $title)
{
$this->id = $id;
$this->method_title = $title;
$this->title = $title;
$this->method_description = esc_html__(
'Your customers can pay with supported blockchain networks and currencies under these networks',
'pp-cryptopay'
);
$this->description = esc_html__(
'You can pay with supported blockchain networks and currencies under these networks.',
'pp-cryptopay'
);

/** @disregard */
$this->supports = [
self::SUBSCRIPTIONS
];
}

/**
* @return array<string,array<mixed>>
*/
public function admin_settings(): array
{
return parent::admin_settings();
}

/**
* @return string
*/
public function get_icon(): string
{
return '<img src="' . esc_url_raw(\PP_CRYPTOPAY_URL . '/assets/images/icon.png') . '" alt="' . esc_attr($this->get_title()) . '" />'; // phpcs:ignore
}

/**
* @param string $transactionId
* @param OrderEntity $order
* @return string
*/
// @phpcs:ignore
public function link_transaction_id($transactionId, $order)
{
return Helpers::run('view', 'components/link', [
'url' => sprintf(admin_url('admin.php?page=%s_profilepress_transactions&s=%s'), $this->id, $transactionId),
'text' => esc_html__('View transaction', 'pp-cryptopay')
]);
}

/**
* @return bool|\WP_Error
*/
public function validate_fields(): bool|\WP_Error
{
return true;
}

/**
* @param int $orderId
* @param int $subscriptionId
* @param int $customerId
* @return CheckoutResponse
*/
// @phpcs:ignore
public function process_payment($orderId, $subscriptionId, $customerId): CheckoutResponse
{
$order = OrderFactory::fromId($orderId);

$paymentUrl = self::createSPPFromOrder($order);

$order->add_meta('cryptopay_payment_url', $paymentUrl);
$order->add_meta('cryptopay_id', $this->id);

return (new CheckoutResponse())
->set_is_success(true)
->set_gateway_response(
esc_html__('You will be redirected to the payment page.', 'pp-cryptopay')
)
->set_redirect_url($paymentUrl);
}

/**
* @return Type
*/
abstract public static function getType(): Type;

/**
* @param OrderEntity $order
* @return string
*/
public static function createSPPFromOrder(OrderEntity $order): string
{
return Helpers::createSPP([
'addon' => 'profilepress',
'addonName' => 'ProfilePress',
'type' => static::getType(),
'order' => [
'id' => $order->get_id(),
'currency' => ppress_get_currency(),
'amount' => ppress_sanitize_amount($order->get_total())
],
'params' => [
'orderKey' => $order->get_order_key(),
'customerId' => $order->get_customer_id(),
'subscriptionId' => $order->get_subscription_id()
],
]);
}

/**
* @return void
*/
public function process_webhook(): void
{
// don't need to do anything here
}
}
31 changes: 31 additions & 0 deletions app/Gateways/GatewayLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\PP\Gateways;

// @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps

use BeycanPress\CryptoPay\Integrator\Type;

class GatewayLite extends AbstractGateway
{
/**
* GatewayLite constructor.
*/
public function __construct()
{
parent::__construct(
'cryptopay_lite',
esc_html__('CryptoPay Lite', 'pp-cryptopay')
);
}

/**
* @return Type
*/
public static function getType(): Type
{
return Type::LITE;
}
}
31 changes: 31 additions & 0 deletions app/Gateways/GatewayPro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\PP\Gateways;

// @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps

use BeycanPress\CryptoPay\Integrator\Type;

class GatewayPro extends AbstractGateway
{
/**
* Gateway constructor.
*/
public function __construct()
{
parent::__construct(
'cryptopay',
esc_html__('CryptoPay', 'pp-cryptopay')
);
}

/**
* @return Type
*/
public static function getType(): Type
{
return Type::PRO;
}
}
116 changes: 116 additions & 0 deletions app/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,128 @@

namespace BeycanPress\CryptoPay\PP;

use BeycanPress\CryptoPay\Integrator\Hook;
use BeycanPress\CryptoPay\Integrator\Helpers;
use BeycanPress\CryptoPay\Integrator\Session;
use ProfilePress\Core\Membership\Models\Order\OrderEntity;
use ProfilePress\Core\Membership\Models\Order\OrderFactory;
use ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory;
use ProfilePress\Core\Membership\PaymentMethods\AbstractPaymentMethod;

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

// add transaction page
Helpers::createTransactionPage(
esc_html__('ProfilePress Transactions', 'pp-cryptopay'),
'profilepress',
10,
[
'orderId' => function ($tx) {
return Helpers::run('view', 'components/link', [
'url' => sprintf(admin_url('admin.php?page=ppress-orders&ppress_order_action=edit&id=1'), $tx->orderId), // @phpcs:ignore
'text' => sprintf(esc_html__('View order #%d', 'gf-cryptopay'), $tx->orderId)
]);
}
]
);

Hook::addAction('payment_finished_profilepress', [$this, 'paymentFinished']);
Hook::addFilter('payment_redirect_urls_profilepress', [$this, 'paymentRedirectUrls']);

add_action('init', [Helpers::class, 'listenSPP']);
add_filter('ppress_payment_methods', [$this, 'registerGateways']);
add_action('ppress_myaccount_order_header_actions', [$this, 'addOrderActions']);
}

/**
* @param object $data
* @return void
*/
public function paymentFinished(object $data): void
{
$orderKey = $data->getParams()->get('orderKey');
$order = OrderFactory::fromOrderKey($orderKey);

if ($data->getStatus()) {
$order->complete_order($data->getHash());
$order->delete_meta('cryptopay_payment_url');
$subscription = SubscriptionFactory::fromId($order->subscription_id);
if ($subscription->exists()) {
$subscription->activate_subscription();
}
} else {
$order->fail_order();
}
}

/**
* @param object $data
* @return array<string>
*/
public function paymentRedirectUrls(object $data): array
{
$orderKey = $data->getParams()->get('orderKey');
$gateway = $data->getParams()->get('gateway');

return [
'success' => ppress_get_success_url($orderKey, $gateway),
'failed' => ppress_get_cancel_url($orderKey)
];
}

/**
* @param OrderEntity $order
* @return void
*/
public function addOrderActions(OrderEntity $order): void
{
if ($order->is_pending() || $order->is_failed()) {
$paymentUrl = $order->get_meta('cryptopay_payment_url');
if (!Session::has((string) $this->getSPPToken($paymentUrl))) {
$paymentUrl = Gateways\GatewayLite::createSPPFromOrder($order);
}

$text = esc_html__('Pay now', 'pp-cryptopay');

if ($order->is_failed()) {
$text = esc_html__('Retry payment', 'pp-cryptopay');
}

echo Helpers::run('view', 'components/link', [
'url' => esc_url_raw($paymentUrl),
'text' => $text
]);
}
}

/**
* @param string $url
* @return string|null
*/
private function getSPPToken(string $url): ?string
{
/** @var array<mixed> $matches */
preg_match('/[?&]cp_spp=([^&]+)/', $url, $matches);
return isset($matches[1]) ? $matches[1] : null;
}


/**
* @param array<AbstractPaymentMethod> $methods
* @return array<AbstractPaymentMethod>
*/
public function registerGateways(array $methods): array
{
return array_merge($methods, [
Gateways\GatewayPro::get_instance(),
Gateways\GatewayLite::get_instance()
]);
}
}
4 changes: 2 additions & 2 deletions app/Models/TransactionsLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

class TransactionsLite extends AbstractTransaction
{
public string $addon = 'pp';
public string $addon = 'profilepress';

/**
* @return void
*/
public function __construct()
{
parent::__construct('pp_transaction');
parent::__construct('profilepress_transaction');
}
}
Loading

0 comments on commit ef22410

Please sign in to comment.