Skip to content

Commit

Permalink
Merge pull request #115 from SwedbankPay/hotfix/transactions-fixes
Browse files Browse the repository at this point in the history
Improve data verification of transaction
  • Loading branch information
olegisk authored Sep 11, 2021
2 parents ed75174 + be79682 commit 7d68faa
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 56 deletions.
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 4.4.2
* Improve data verification of transaction

Version 4.4.1
* Fixed transaction table bug

Expand Down
6 changes: 5 additions & 1 deletion includes/class-wc-gateway-swedbank-pay-cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,11 @@ public function ajax_check_payment() {
wp_send_json_success( array(
'state' => 'unknown',
) );
} catch ( \SwedbankPay\Core\Exception $exception ) {
} catch ( Exception $exception ) {
$this->core->log(
LogLevel::WARNING, sprintf( '%s %s', __METHOD__, $exception->getMessage() )
);

wp_send_json_success( array(
'state' => 'failed',
'message' => $exception->getMessage()
Expand Down
80 changes: 29 additions & 51 deletions includes/class-wc-swedbank-pay-transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ public function install_schema() {
public function add( $fields ) {
global $wpdb;

// Verify data
foreach ( $fields as $key => $value ) {
if ( ! in_array( $key, self::$_allowed_fields ) ) {
unset( $fields[ $key ] );
}
if ( ! isset( $fields['created'] ) ) {
$fields['created'] = gmdate( 'Y-m-d H:i:s' );
}

if ( ! isset( $fields['updated'] ) ) {
$fields['updated'] = gmdate( 'Y-m-d H:i:s' );
}

$fields = $this->prepare( $fields );
$result = $wpdb->insert( $wpdb->prefix . 'payex_transactions', $fields );
if ( $result > 0 ) {
return $wpdb->insert_id;
Expand All @@ -146,7 +148,7 @@ public function delete( $transaction_id ) {

$result = $wpdb->delete(
$wpdb->prefix . 'payex_transactions',
array( 'id' => (int) $transaction_id )
array( 'transaction_id' => (int) $transaction_id )
);

if ( false === $result ) {
Expand All @@ -170,13 +172,11 @@ public function delete( $transaction_id ) {
public function update( $transaction_id, $fields ) {
global $wpdb;

// Verify data
foreach ( $fields as $key => $value ) {
if ( ! in_array( $key, self::$_allowed_fields ) ) {
unset( $fields[ $key ] );
}
if ( ! isset( $fields['updated'] ) ) {
$fields['updated'] = gmdate( 'Y-m-d H:i:s' );
}

$fields = $this->prepare( $fields );
$result = $wpdb->update(
$wpdb->prefix . 'payex_transactions',
$fields,
Expand Down Expand Up @@ -276,25 +276,18 @@ public function select( array $conditionals ) {
* Prepare data
*
* @param $data
* @param $order_id
*
* @return array
*/
public function prepare( $data, $order_id ) {
$allowed = self::$_allowed_fields;
unset( $allowed['transaction_id'], $allowed['transaction_data'], $allowed['order_id'] );
$data = array_filter(
$data,
function ( $value, $key ) use ( $allowed ) {
return in_array( $key, $allowed, true );
},
ARRAY_FILTER_USE_BOTH
);

public function prepare( $data ) {
$data['transaction_data'] = json_encode( $data, true );
$data['order_id'] = $order_id;
$data['created'] = gmdate( 'Y-m-d H:i:s', strtotime( $data['created'] ) );
$data['updated'] = gmdate( 'Y-m-d H:i:s', strtotime( $data['updated'] ) );

// Verify data
foreach ( $data as $key => $value ) {
if ( ! in_array( $key, self::$_allowed_fields ) ) {
unset( $data[ $key ] );
}
}

return $data;
}
Expand All @@ -305,17 +298,20 @@ function ( $value, $key ) use ( $allowed ) {
* @param $data
* @param $order_id
*
* @return bool|int|mixed
* @return bool|int
*
* @throws Exception
*/
public function import( $data, $order_id ) {
global $wpdb;
$id = $data['id'];
$data = $this->prepare( $data );
$data['order_id'] = $order_id;

$id = $data['id'];
$saved = $this->get_by( 'id', $id );
if ( ! $saved ) {
$data = $this->prepare( $data, $order_id );
$data['created'] = gmdate( 'Y-m-d H:i:s' );
$data['updated'] = gmdate( 'Y-m-d H:i:s' );

$row_id = $this->add( $data );
if ( is_wp_error( $row_id ) ) {
/** @var WP_Error $row_id */
Expand All @@ -326,28 +322,10 @@ public function import( $data, $order_id ) {
}

// Data should be updated
$data['transaction_data'] = json_encode( $data, true );
if ( isset( $data['updated'] ) ) {
$data['updated'] = gmdate( 'Y-m-d H:i:s', strtotime( $data['updated'] ) );
} else {
$data['updated'] = gmdate( 'Y-m-d H:i:s' );
}

$result = $wpdb->update(
$wpdb->prefix . 'payex_transactions',
$data,
array(
'id' => $id,
)
);

if ( false === $result ) {
throw new Exception(
__( 'Failed to update the transaction in the table.', 'swedbank-pay-woocommerce-payments' )
);
}
$data['updated'] = gmdate( 'Y-m-d H:i:s' );
$this->update( $saved['transaction_id'], $data );

return $saved['transaction_id'];
return (int) $saved['transaction_id'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swedbank-pay-woocommerce-payments",
"version": "4.4.1",
"version": "4.4.2",
"description": "",
"main": "gulpfile.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This plugin provides the Swedbank Pay Payments Gateway for WooCommerce.
* Requires at least: 5.3
* Tested up to: 5.6
* Requires PHP: 7.0
* Stable tag: 4.4.1
* Stable tag: 4.4.2
* [License: Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0)

## Description
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: swedbankpay
Tags: ecommerce, e-commerce, commerce, woothemes, wordpress ecommerce, swedbank, payex, payment gateway
Requires at least: 5.3
Tested up to: 5.6
Stable tag: 4.4.1
Stable tag: 4.4.2
License: Apache License 2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0

Expand Down
2 changes: 1 addition & 1 deletion swedbank-pay-woocommerce-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://profiles.wordpress.org/swedbankpay/
* License: Apache License 2.0
* License URI: http://www.apache.org/licenses/LICENSE-2.0
* Version: 4.4.1
* Version: 4.4.2
* Text Domain: swedbank-pay-woocommerce-payments
* Domain Path: /languages
* WC requires at least: 5.5.1
Expand Down
30 changes: 30 additions & 0 deletions tests/unit-tests/wc-swedbank-pay-transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@ public function test_data() {
$result = $transactions->delete( 123 );
$this->assertIsInt( $result );
}

public function test_import() {
$order = WC_Helper_Order::create_order();
$transactions = WC_Swedbank_Pay_Transactions::instance();

$transactions->install_schema();

$fields = array(
'transaction_id' => 123,
'order_id' => $order->get_id(),
'payeeReference' => 'zz',
'id' => 1,
'type' => 'zz',
'state' => 'zz',
'number' => '123',
'amount' => $order->get_total(),
'vatAmount' => 0,
'description' => 'zz',
'created' => 'One minute ago',
'updated' => 'now',
'INVALID_FIELD' => 'zz' // Should be ignored
);
// Run import on an order which does not have a corresponding row in the transactions table.
$add = $transactions->import( $fields, $order->get_id() );
$this->assertIsInt( $add );

// Run import again, this time the transactions table should be updated.
$update = $transactions->import( $fields, $order->get_id() );
$this->assertIsInt( $update );
}
}

0 comments on commit 7d68faa

Please sign in to comment.