Skip to content

Commit

Permalink
Merge pull request #22 from krokedil/main
Browse files Browse the repository at this point in the history
Version 1.4.0
  • Loading branch information
lighe authored Nov 3, 2023
2 parents e83a645 + 5998335 commit 94da838
Show file tree
Hide file tree
Showing 26 changed files with 3,134 additions and 378 deletions.
3 changes: 3 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ README.md
readme.dev.txt
webpack.config.js
yarn.lock
/tests
phpunit.xml.dist
.phpunit.result.cache
27 changes: 27 additions & 0 deletions .github/workflows/pr-phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run PHPUnit Tests
on:
push:
tags:
- "*"
jobs:
tag:
name: New tag
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@master

- name: Install Composer dependencies
uses: php-actions/composer@v6
with:
dev: yes
php_version: 7.4

- name: Build assets and translation files
run: |
npm ci
npm run build
- name: Run PHPUnit tests
run: |
composer test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ project.properties
node_modules/
vendor

.phpunit.result.cache

# OS X metadata
.DS_Store

Expand Down
96 changes: 78 additions & 18 deletions classes/admin/class-truelayer-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
}
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use Defuse\Crypto\Key;

/**
* Returns error messages depending on
*
* @class Truelayer_Admin_Notices
* @author Krokedil
*/
class Truelayer_Admin_Notices {

/**
* The reference the *Singleton* instance of this class.
*
Expand Down Expand Up @@ -52,11 +52,10 @@ public function __construct() {
$settings = get_option( 'woocommerce_truelayer_settings', array() );
$this->enabled = $settings['enabled'] ?? 'no';
add_action( 'admin_notices', array( $this, 'check_truelayer_key' ), 15 );
add_action( 'admin_notices', array( $this, 'check_truelayer_encryption_error' ), 20 );
add_action( 'admin_notices', array( $this, 'check_truelayer_encryption_key_error' ), 25 );
}




/**
* Check if truelayer key exist.
*
Expand All @@ -66,24 +65,85 @@ public function check_truelayer_key() {
if ( defined( 'TRUELAYER_KEY' ) || did_action( 'true_layer_key_set' ) ) {
return;
}
$message = __( "<b>TrueLayer for WooCommerce</b> was unable to save data to the <code>wp-config.php</code> file. The following code needs to be manually added to <code>wp-config.php</code> for the plugin to function properly.\n ", 'truelayer-for-woocommerce' );
?>
<div class="truelayer-message notice woocommerce-message notice-error">
<?php
$key = Key::createNewRandomKey();
$generated_key = $key->saveToAsciiSafeString();
echo wp_kses_post(
wpautop(
'<p>' . __( "<b>TrueLayer for WooCommerce</b> was unable to save data to the <code>wp-config.php</code> file. The following code needs to be manually added to <code>wp-config.php</code> for the plugin to function properly.\n " . "<code>define( 'TRUELAYER_KEY', " . "'" . $generated_key . "' );</code>" ),
'truelayer-for-woocommerce'
) .
'</p>'
);
?>
</div>
<?php
<div class="truelayer-message notice woocommerce-message notice-error">
<p>
<?php echo wp_kses_post($message); ?>
<code>define( 'TRUELAYER_KEY', "<?php echo esc_html($this->generate_new_key()) ?>");</code>
</p>
</div>
<?php
}

/**
* Check if we have a encryption error saved in the database.
*
* @return void
*/
public function check_truelayer_encryption_error() {
if ( did_action( 'true_layer_key_set' ) ) {
return;
}

// Check if we have a encryption error saved in the database.
$encryption_error = get_option( 'truelayer_encryption_error', false );

// If we don't have one, just return.
if ( ! $encryption_error ) {
return;
}

// If we have one, then display it.
?>
<div class="truelayer-message notice woocommerce-message notice-error">
<p>
<?php echo wp_kses_post($encryption_error); ?>
</p>
</div>
<?php
}

/**
* Check if we have a encryption error saved in the database.
*
* @return void
*/
public function check_truelayer_encryption_key_error() {
if ( did_action( 'true_layer_key_set' ) ) {
return;
}

// Check if we have a encryption error saved in the database.
$encryption_error = get_option( 'truelayer_encryption_key_error', false );

// If we don't have one, just return.
if ( ! $encryption_error ) {
return;
}

// If we have one, then display it.
?>
<div class="truelayer-message notice woocommerce-message notice-error">
<p>
<?php echo wp_kses_post($encryption_error); ?>
<code>define( 'TRUELAYER_KEY', "<?php echo esc_html($this->generate_new_key()) ?>");</code>
</p>
</div>
<?php
}

/**
* Generate a new encryption key for the merchant to paste into the WP Config file.
*
* @return string
*/
private function generate_new_key() {
$key = Key::createNewRandomKey();
$generated_key = $key->saveToAsciiSafeString();

return $generated_key;
}
}

Truelayer_Admin_Notices::get_instance();
18 changes: 9 additions & 9 deletions classes/class-truelayer-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ function ( &$v ) {
break;
default:
$status = $body['type'];
$order = get_woocommerce_order_from_payment_id( $body['payment_id'] );
$order = $this->get_woocommerce_order_from_payment_id( $body['payment_id'] );
$order_id = is_object( $order ) ? $order->get_id() : '';
TrueLayer_Logger::log( "Unhandled callback for order ${order_id}. Callback type: ${status}" );
TrueLayer_Logger::log( "Unhandled callback for order {$order_id}. Callback type: {$status}" );
break;
}
}
Expand All @@ -113,7 +113,7 @@ public function handle_payment_executed( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle payment_executed callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle payment_executed callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down Expand Up @@ -158,7 +158,7 @@ public function handle_payment_settled( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle payment_settled callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle payment_settled callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down Expand Up @@ -196,7 +196,7 @@ public function handle_payment_failed( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle payment_failed callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle payment_failed callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down Expand Up @@ -236,7 +236,7 @@ public function handle_payout_executed( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle payout_executed callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle payout_executed callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down Expand Up @@ -272,7 +272,7 @@ public function handle_payout_failed( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle handle_payout_failed callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle handle_payout_failed callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down Expand Up @@ -300,7 +300,7 @@ public function handle_refund_executed( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle refund_executed callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle refund_executed callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down Expand Up @@ -336,7 +336,7 @@ public function handle_refund_failed( $body ) {
}

$order_id = $order->get_id();
TrueLayer_Logger::log( "Handle handle_refund_failed callback for order ID ${order_id}." );
TrueLayer_Logger::log( "Handle handle_refund_failed callback for order ID {$order_id}." );

// Error handling.
if ( isset( $body['failure_reason'] ) && ! empty( $body['failure_reason'] ) ) {
Expand Down
Loading

0 comments on commit 94da838

Please sign in to comment.