Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
- fix IPN response on api and inline modules
- make sure jquery loads first when loading frontend scripts
  • Loading branch information
Craig Christenson committed Feb 8, 2024
1 parent 10c03ad commit 91c5c5a
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 51 deletions.
Binary file added twocheckout-convert-plus-v2.3.1.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion twocheckout-convert-plus/wc-twocheckout-convert-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: 2Checkout Convert Plus Payment Gateway
Plugin URI:
Description: Allows you to use 2Checkout payment gateway with the WooCommerce plugin.
Version: 2.3.0
Version: 2.3.1
Author: 2Checkout
Author URI: https://www.2checkout.com
*/
Expand Down
Binary file added twocheckout-inline-v2.3.1.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function _is_order_refunded() {
/**
* @return string
*/
private function _calculate_ipn_response() {
private function _calculate_ipn_response($algo='sha3-256') {
$result_response = '';
$ipn_params_response = [];
// we're assuming that these always exist, if they don't then the problem is on avangate side
Expand All @@ -144,11 +144,19 @@ private function _calculate_ipn_response() {
$result_response .= $this->array_expand( (array) $val );
}

return sprintf(
'<EPAYMENT>%s|%s</EPAYMENT>',
$ipn_params_response['DATE'],
$this->generate_hash( $this->secret_key, $result_response )
);
if ('md5' === $algo)
return sprintf(
'<EPAYMENT>%s|%s</EPAYMENT>',
$ipn_params_response['DATE'],
$this->generate_hash($this->secret_key, $result_response, $algo)
);
else
return sprintf(
'<sig algo="%s" date="%s">%s</sig>',
$algo,
$ipn_params_response['DATE'],
$this->generate_hash($this->secret_key, $result_response, $algo)
);
}

/**
Expand Down Expand Up @@ -214,7 +222,7 @@ public function is_ipn_response_valid() {
$receivedHash = $this->request_params['SIGNATURE_SHA3_256'];

if(!$receivedHash){
$receivedAlgo='sha2-256';
$receivedAlgo='sha256';
$receivedHash = $this->request_params['SIGNATURE_SHA2_256'];
}

Expand Down Expand Up @@ -276,6 +284,7 @@ public function generate_hash($key, $data, $algo = 'sha3-256') {
* @return string
*/
public function process_ipn() {
$hash = $this->extractHashFromRequest();
try {
if ( ! isset( $this->request_params['REFNO'] ) && empty( $this->request_params['REFNO'] ) ) {
self::log( 'Cannot identify order: "%s".', $this->request_params['REFNOEXT'] );
Expand All @@ -294,7 +303,7 @@ public function process_ipn() {
} catch ( Exception $ex ) {
self::log( 'Exception processing IPN: ' . $ex->getMessage() );
}
echo $this->_calculate_ipn_response();
echo $this->_calculate_ipn_response($hash['algo']);
exit();
}

Expand Down Expand Up @@ -326,4 +335,25 @@ private function array_expand( $array ) {

return $retval;
}

/**
* @return array [hash, algo]
*/
protected function extractHashFromRequest():array {
$receivedAlgo = 'sha3-256';
$receivedHash = $this->request_params['SIGNATURE_SHA3_256'];

if (!$receivedHash) {
$receivedAlgo = 'sha256';
$receivedHash = $this->request_params['SIGNATURE_SHA2_256'];
}

if (!$receivedHash) {
$receivedAlgo = 'md5';
$receivedHash = $this->request_params['HASH'];
}

return ['hash' => $receivedHash, 'algo' => $receivedAlgo];
}

}
8 changes: 6 additions & 2 deletions twocheckout-inline/wc-twocheckout-inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: 2Checkout Inline Payment Gateway
Plugin URI:
Description: Allows you to use 2Checkout payment gateway with the WooCommerce plugin.
Version: 2.3.0
Version: 2.3.1
Author: 2Checkout
Author URI: https://www.2checkout.com
*/
Expand Down Expand Up @@ -129,7 +129,7 @@ function enqueue_style() {
//enqueue a script
function enqueue_script() {
wp_enqueue_script( 'twocheckout_inline_script',
'/wp-content/plugins/twocheckout-inline/assets/js/twocheckout_inline.js' );
'/wp-content/plugins/twocheckout-inline/assets/js/twocheckout_inline.js', ['jquery'] );
}

/**
Expand Down Expand Up @@ -548,7 +548,11 @@ public function check_ipn_response_inline() {
if ( $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
return;
}

$params = $_POST;
if(empty($params))
$params=json_decode(file_get_contents('php://input'),true);

unset( $params['wc-api'] );
if ( isset( $params['REFNOEXT'] ) && ! empty( $params['REFNOEXT'] ) ) {
$order = wc_get_order( $params['REFNOEXT'] );
Expand Down
Binary file added twocheckout-v2.3.1.zip
Binary file not shown.
92 changes: 55 additions & 37 deletions twocheckout/src/Twocheckout/TwoCheckoutIpnHelperApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,36 @@ public function processorder_status(string $order_status,string $refNo) {
}
}

/**
* Validate Ipn request
* @return bool
*/
public function is_ipn_response_valid() {
$result = '';

$receivedAlgo='sha3-256';
/**
* @return array [hash, algo]
*/
protected function extractHashFromRequest():array {
$receivedAlgo = 'sha3-256';
$receivedHash = $this->request_params['SIGNATURE_SHA3_256'];

if(!$receivedHash){
$receivedAlgo='sha256';
if (!$receivedHash) {
$receivedAlgo = 'sha256';
$receivedHash = $this->request_params['SIGNATURE_SHA2_256'];
}

if(!$receivedHash){
$receivedAlgo='md5';
if (!$receivedHash) {
$receivedAlgo = 'md5';
$receivedHash = $this->request_params['HASH'];
}

foreach ( $this->request_params as $key => $val ) {
return ['hash' => $receivedHash, 'algo' => $receivedAlgo];
}

/**
* Validate Ipn request
* @return bool
*/
public function is_ipn_response_valid() {
$result = '';

$hash = $this->extractHashFromRequest();

foreach ( $this->request_params as $key => $val ) {
if ( !in_array($key ,["HASH", "SIGNATURE_SHA2_256", "SIGNATURE_SHA3_256"]) ) {
if ( is_array( $val ) ) {
$result .= $this->array_expand( $val );
Expand All @@ -204,8 +212,8 @@ public function is_ipn_response_valid() {
}

if ( isset( $this->request_params['REFNO'] ) && ! empty( $this->request_params['REFNO'] ) ) {
$calc_hash = $this->generate_hash( $this->secret_key, $result, $receivedAlgo );
if ( $receivedHash === $calc_hash ) {
$calc_hash = $this->generate_hash( $this->secret_key, $result, $hash['algo'] );
if ( $hash['hash'] === $calc_hash ) {
return true;
}
}
Expand Down Expand Up @@ -258,6 +266,8 @@ public static function log( $message ) {
* @return string
*/
public function process_ipn() {
$hash = $this->extractHashFromRequest();

try {
if ( ! isset( $this->request_params['REFNO'] ) && empty( $this->request_params['REFNO'] ) ) {
self::log( 'Cannot identify order: "%s".', $this->request_params['REFNOEXT'] );
Expand All @@ -276,7 +286,7 @@ public function process_ipn() {
} catch ( Exception $ex ) {
self::log( 'Exception processing IPN: ' . $ex->getMessage() );
}
echo $this->_calculate_ipn_response();
echo $this->_calculate_ipn_response($hash['algo']);
exit();
}

Expand Down Expand Up @@ -305,26 +315,34 @@ protected function _process_fraud() {
}
}

/**
* @return string
*/
private function _calculate_ipn_response() {
$result_response = '';
$ipn_params_response = [];
// we're assuming that these always exist, if they don't then the problem is on avangate side
$ipn_params_response['IPN_PID'][0] = $this->request_params['IPN_PID'][0];
$ipn_params_response['IPN_PNAME'][0] = $this->request_params['IPN_PNAME'][0];
$ipn_params_response['IPN_DATE'] = $this->request_params['IPN_DATE'];
$ipn_params_response['DATE'] = date( 'YmdHis' );

foreach ( $ipn_params_response as $key => $val ) {
$result_response .= $this->array_expand( (array) $val );
}
/**
* @return string
*/
private function _calculate_ipn_response($algo='sha3-256') {
$resultResponse = '';
$ipn_params_response = [];
// we're assuming that these always exist, if they don't then the problem is on avangate side
$ipn_params_response['IPN_PID'][0] = $this->request_params['IPN_PID'][0];
$ipn_params_response['IPN_PNAME'][0] = $this->request_params['IPN_PNAME'][0];
$ipn_params_response['IPN_DATE'] = $this->request_params['IPN_DATE'];
$ipn_params_response['DATE'] = date( 'YmdHis' );

foreach ( $ipn_params_response as $key => $val ) {
$resultResponse .= $this->array_expand( (array) $val );
}

return sprintf(
'<EPAYMENT>%s|%s</EPAYMENT>',
$ipn_params_response['DATE'],
$this->generate_hash( $this->secret_key, $result_response )
);
}
if ('md5' === $algo)
return sprintf(
'<EPAYMENT>%s|%s</EPAYMENT>',
$ipn_params_response['DATE'],
$this->generate_hash($this->secret_key, $resultResponse, $algo)
);
else
return sprintf(
'<sig algo="%s" date="%s">%s</sig>',
$algo,
$ipn_params_response['DATE'],
$this->generate_hash($this->secret_key, $resultResponse, $algo)
);
}
}
11 changes: 8 additions & 3 deletions twocheckout/wc-twocheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: 2Checkout Payment Gateway
Plugin URI:
Description: Allows you to use 2Checkout payment gateway with the WooCommerce plugin.
Version: 2.3.0
Version: 2.3.1
Author: 2Checkout
Author URI: https://www.2checkout.com
*/
Expand Down Expand Up @@ -141,8 +141,8 @@ public function init_form_fields() {
*/
public function payment_fields() {

wp_enqueue_script( '2payjs', 'https://2pay-js.2checkout.com/v1/2pay.js' );
wp_enqueue_script( 'twocheckout_script', '/wp-content/plugins/twocheckout/assets/js/twocheckout.js' );
wp_enqueue_script( '2payjs', 'https://2pay-js.2checkout.com/v1/2pay.js', ['jquery'] );
wp_enqueue_script( 'twocheckout_script', '/wp-content/plugins/twocheckout/assets/js/twocheckout.js', ['jquery'] );
wp_enqueue_style( 'twocheckout_style', '/wp-content/plugins/twocheckout/assets/css/twocheckout.css' );
$twocheckout_is_checkout = ( is_checkout() && empty( $_GET['pay_for_order'] ) ) ? 'yes' : 'no';
require_once plugin_dir_path( __FILE__ ) . 'templates/payment-fields.php';
Expand Down Expand Up @@ -602,16 +602,21 @@ public function check_ipn_response_api() {
return;
}
$params = $_POST;
if(empty($params))
$params=json_decode(file_get_contents('php://input'),true);

unset( $params['wc-api'] );
if ( isset( $params['REFNOEXT'] ) && ! empty( $params['REFNOEXT'] ) ) {
$order = wc_get_order( $params['REFNOEXT'] );

if ( $order && $order->get_payment_method() == 'twocheckout' ) {
try {
$ipn_helper = new Two_Checkout_Ipn_Helper_Api( $params, $this->secret_key, $this->complete_order_on_payment, $this->debug, $order );
} catch ( Exception $ex ) {
$this->log( 'Unable to find order with RefNo: ' . $params['REFNOEXT'] );
throw new Exception( 'An error occurred!' );
}

if ( ! $ipn_helper->is_ipn_response_valid() ) {
self::log( sprintf( 'SHA3 hash mismatch for 2Checkout IPN with date: "%s" . ',
$params['IPN_DATE'] ) );
Expand Down

0 comments on commit 91c5c5a

Please sign in to comment.