Skip to content

Commit

Permalink
Merge branch 'bokuqlFo' into 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPetakNeuralab committed Mar 22, 2024
2 parents b9adede + 7b8b8d4 commit 5db0ef0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions includes/core/class-kekspay-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function refund( $order, $amount ) {
'amount' => $refund_amount,
'epochtime' => $timestamp,
'hash' => $hash,
'algo' => Kekspay_Data::get_algo(),
'currency' => $refund_currency,
);

Expand Down
23 changes: 22 additions & 1 deletion includes/utilities/class-kekspay-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class Kekspay_Data {
*/
private static $test_kekspay_api = 'https://kekspayuat.erstebank.hr/eretailer/';

/**
* Per Kekpay documentation, parametar "algo" needs to be sent with the requests that use hash.
* Value of "algo" tells Kekspay servers which cipher was used for hashing:
* 0 : 3DES
* 1 : AES
*
* @var string
*/
private static $algo;

/**
* Init data class.
*/
Expand Down Expand Up @@ -251,6 +261,15 @@ public static function get_order_id_by_bill_id( $bill_id ) {
return str_replace( self::get_settings( 'webshop-tid', true ) . '-', '', $bill_id );
}

/**
* Extract order id from kekspay bill id.
*
* @return string
*/
public static function get_algo() {
return self::$algo;
}

/**
* Gathers all data needed for payment and formats it as array.
*
Expand Down Expand Up @@ -296,12 +315,14 @@ public static function get_cipher( $key ) {

if ( ctype_xdigit( $key ) ) {
if ( $key_size === 24 ) {
self::$algo = 0;
return 'des-ede3-cbc';
} else {
throw new Exception( 'Secret key must be 24 bytes.' );
}
} else {
if ( in_array( $key_size, array( 16, 24, 32 ), true ) ) {
self::$algo = 1;
return 'aes-' . ( $key_size * 8 ) . '-cbc';
} else {
throw new Exception( 'Secret key must be 16, 24 or 32 bytes.' );
Expand Down Expand Up @@ -329,7 +350,7 @@ public static function get_hash( $order, $amount, $timestamp ) {
$payload_checksum = pack( 'H*', md5( $payload ) );
// Create 8 or 16 (depending on cipher) byte binary initialization vector.
$iv = str_repeat( pack( 'c', 0 ), false !== strpos( $cipher, 'aes' ) ? 16 : 8 );
// Encrypt data using 3DES CBC algorithm and convert it to hex.
// Encrypt data using 3DES or AES cipher and convert it to hex.
$hash = bin2hex( openssl_encrypt( $payload_checksum, $cipher, $key, OPENSSL_RAW_DATA, $iv ) );

return strtoupper( $hash );
Expand Down

0 comments on commit 5db0ef0

Please sign in to comment.