Skip to content

Commit

Permalink
updated integrator
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Mar 6, 2024
1 parent bda9afa commit 93a4ea7
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
}
},
"require": {
"beycanpress/cryptopay-integrator": "^0.1.3"
"beycanpress/cryptopay-integrator": "^0.1.5"
}
}
14 changes: 7 additions & 7 deletions composer.lock

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

2 changes: 1 addition & 1 deletion vendor/beycanpress/cryptopay-integrator/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beycanpress/cryptopay-integrator",
"version": "0.1.3",
"version": "0.1.5",
"description": "CryptoPay and CryptoPay Lite integration wrapper",
"type": "library",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
BeycanPressStandard
</description>
<file>./**/*.php</file>
<exclude-pattern>*/views/*</exclude-pattern>
<arg name="extensions" value="php"/>
<arg name="standard" value="PSR12"/>
<rule ref="Generic.Files.InlineHTML"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Squiz.Commenting.FunctionComment.Missing"/>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamName"/>
<rule ref="Squiz.Commenting.FunctionComment.ParamNameNoMatch"/>
<rule ref="Squiz.Commenting.FunctionComment.IncorrectTypeHint"/>
<rule ref="Squiz.Commenting.FunctionComment.MissingReturn"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireYodaComparison"/>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.IncorrectStrictTypesFormat" />
</rule>
Expand Down
81 changes: 81 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace BeycanPress\CryptoPay\Integrator;

use BeycanPress\CryptoPay\Loader;
use BeycanPress\CryptoPay\Payment;
use BeycanPress\CryptoPay\PluginHero\Hook;
use BeycanPress\CryptoPay\Helpers as ProHelpers;
use BeycanPress\CryptoPay\Pages\TransactionPage;
use BeycanPress\CryptoPay\Types\Order\OrderType;
use BeycanPress\CryptoPay\PluginHero\Http\Response;
// Lite
use BeycanPress\CryptoPayLite\Loader as LiteLoader;
use BeycanPress\CryptoPayLite\Payment as LitePayment;
use BeycanPress\CryptoPayLite\Helpers as LiteHelpers;
use BeycanPress\CryptoPayLite\PluginHero\Hook as LiteHook;
use BeycanPress\CryptoPayLite\Types\Order\OrderType as LiteOrderType;
use BeycanPress\CryptoPayLite\PluginHero\Http\Response as LiteResponse;
use BeycanPress\CryptoPayLite\Pages\TransactionPage as LiteTransactionPage;

Expand Down Expand Up @@ -136,4 +140,81 @@ public static function response(string $method, ...$args): mixed
return LiteResponse::$method(...$args);
}
}

/**
* @param array<mixed> $data
* @return string
* @throws \Exception
*/
public static function createSPP(array $data): string
{
if (!isset($data['addon'])) {
throw new \Exception('Addon is required');
}

if (!isset($data['order'])) {
throw new \Exception('Order is required');
}

if (!isset($data['order']['amount'])) {
throw new \Exception('Order amount is required');
}

if (!isset($data['order']['currency'])) {
throw new \Exception('Order currency is required');
}

if (!isset($data['type'])) {
throw new \Exception('CryptoPay type is required');
}

if (!($data['type'] instanceof Type)) {
throw new \Exception('Invalid CryptoPay type');
}

$token = md5(json_encode($data) . time());

Session::set($token, $data);

return home_url("/?cp_spp={$token}");
}

/**
* @return void
*/
public static function listenSPP(): void
{
$token = isset($_GET['cp_spp']) ? sanitize_text_field($_GET['cp_spp']) : null;

if ($token && Session::has($token)) {
extract(Session::get($token));
if (Type::PRO === $type) {
$cryptopay = self::createProPayment($addon, (array) $order);
} else {
$cryptopay = self::createLitePayment($addon, (array) $order);
}
require dirname(__DIR__) . '/views/pay.php';
exit;
}
}

/**
* @param string $addon
* @param array<mixed> $order
* @return string
*/
public static function createProPayment(string $addon, array $order): string
{
return (new Payment($addon))->setOrder(OrderType::fromArray($order))->html(loading:true);
}

/**
* @param string $addon
* @param array<mixed> $order
* @return string
*/
public static function createLitePayment(string $addon, array $order): string
{
return (new LitePayment($addon))->setOrder(LiteOrderType::fromArray($order))->html(loading:true);
}
}
91 changes: 91 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/src/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\Integrator;

class Session
{
/**
* @return void
*/
public static function start(): void
{
if (PHP_SESSION_NONE === session_status()) {
session_start();
}
}

/**
* @param string $key
* @param mixed $value
* @return void
*/
public static function set(string $key, mixed $value): void
{
self::start();
if (!isset($_SESSION['cp_integrator'])) {
$_SESSION['cp_integrator'] = [];
}
$_SESSION['cp_integrator'][$key] = $value;
}

/**
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get(string $key, mixed $default = null): mixed
{
self::start();
return $_SESSION['cp_integrator'][$key] ?? $default;
}

/**
* @param string $key
* @return bool
*/
public static function has(string $key): bool
{
self::start();
return isset($_SESSION['cp_integrator'][$key]);
}


/**
* @param string $key
* @return void
*/
public static function remove(string $key): void
{
self::start();
unset($_SESSION['cp_integrator'][$key]);
}

/**
* @return void
*/
public static function clear(): void
{
self::start();
session_unset();
}

/**
* @return void
*/
public static function destroy(): void
{
self::start();
session_destroy();
}

/**
* @return void
*/
public static function regenerate(): void
{
self::start();
session_regenerate_id();
}
}
11 changes: 11 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/src/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace BeycanPress\CryptoPay\Integrator;

enum Type
{
case PRO;
case LITE;
}
89 changes: 89 additions & 0 deletions vendor/beycanpress/cryptopay-integrator/views/pay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo esc_html__('CryptoPay Single Page Payment') ?></title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
box-sizing: border-box;
background-color: #f0f0f0;
color: #333;
}

.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: flex-start;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin: 20px auto;
max-width: 800px;
}

.payment-info {
flex: 1;
max-width: 400px;
margin-right: 20px;
}

.payment-info h2 {
margin-top: 0;
}

.payment-form {
flex: 1;
max-width: 400px;
}

.payment-info img {
max-width: 100%;
height: auto;
border-radius: 5px;
}

@media screen and (max-width: 768px) {
.container {
flex-direction: column;
}

.payment-info,
.payment-form {
margin-right: 0;
margin-bottom: 20px;
max-width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<div class="payment-info">
<h2><?php echo esc_html__('Payment Information') ?></h2>
<?php if (isset($order['id'])): ?>
<p><strong><?php echo esc_html__('Order ID') ?>:</strong> <?php echo esc_html($order['id']) ?></p>
<?php endif; ?>
<?php if (isset($order['amount'])): ?>
<p><strong><?php echo esc_html__('Amount') ?>:</strong> <?php echo esc_html($order['amount']) ?> <?php echo esc_html($order['currency']) ?></p>
<?php endif; ?>
<?php if (isset($addon)): ?>
<p><strong><?php echo esc_html__('Addon') ?>:</strong> <?php echo esc_html($addon) ?></p>
<?php endif; ?>
<p>
<?php echo esc_html__('This page is the single payment page, after you make your payment on this page you will be returned to the addon\'s page that redirected you to this page.') ?>
</p>
</div>
<div class="payment-form">
<?php echo $cryptopay; ?>
</div>
</div>
<?php do_action('wp_print_footer_scripts'); ?>
</body>
</html>
Loading

0 comments on commit 93a4ea7

Please sign in to comment.