Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/ts 67 bug with issuing refund programmatically #19

Merged
merged 9 commits into from
Jun 22, 2022
2 changes: 1 addition & 1 deletion globalpayments-gateway-provider-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: GlobalPayments WooCommerce
* Plugin URI: https://github.com/globalpayments/globalpayments-woocommerce
* Description: This extension allows WooCommerce to use the available Global Payments payment gateways. All card data is tokenized using the respective gateway's tokenization service.
* Version: 1.2.2
* Version: 1.2.3
* Requires PHP: 5.5.9
* WC tested up to: 6.3.1
* Author: Global Payments
Expand Down
4 changes: 3 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: globalpayments
Tags: woocommerce, woo, unified, commerce, platform, global, payments, heartland, payment, systems, tsys, genius, 3DS, gateway, token, tokenize, save cards
Requires at least: 5.4
Tested up to: 5.9.3
Stable tag: 1.2.2
Stable tag: 1.2.3
License: MIT
License URI: https://github.com/globalpayments/globalpayments-woocommerce/blob/main/LICENSE

Expand Down Expand Up @@ -46,6 +46,8 @@ Access to our Unified Payments requires sandbox credentials which you can retrie
4. Click ‘Create a New App’. An app is a set of credentials used to access the API and generate access tokens.

== Changelog ==
= 1.2.3 =
* Bug fix - Refund issue when create_refund is called programmatically

= 1.2.2 =
* Bug fix - 3DS/Digital Wallets amount not updated when a customer added/removed a coupon
Expand Down
41 changes: 40 additions & 1 deletion src/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function tokenization_script() {
WC()->version,
true
);

wp_localize_script(
'globalpayments-helper',
'globalpayments_helper_params',
Expand Down Expand Up @@ -769,6 +769,41 @@ protected function add_hooks() {
'woocommerce_available_payment_gateways'
) );
}

add_action( 'woocommerce_api_globalpayments_test_refund', array(
$this,
'test_refund_programmatically'
) );
}

/**
* Test endpoint for testing a programmatically refund instead of using admin
*
* URL ENDPOINT www.domain.com/?wc-api=globalpayments_test_refund&order-id=136&reason=something&amount=1
*
* @param int $order-id // valid order-id from a refundable order.
*
* @param string $reason // reason of refund.
*
* @param int $amount // amount of refund , only integer for testing.
*
* @return array
*/
public function test_refund_programmatically () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiercrac if we want to use this endpoint for QA, it shouldn't do any validation data. It should just send the data and print the response.

These are things that the process_refund method should be able to deal with and respond accordingly.

e.g. null amount, invalid amount format "0,99", etc.

In case of any error the endpoint response should replicate the response received after clicking "Refund" in the Admin Dashboard.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed those validations in the endpoint.

$data = array(
'amount' => $_GET[ 'amount'] ?? null,
'reason' => $_GET[ 'reason'] ?? null,
'order_id' => $_GET[ 'order-id'] ?? null,
'refund_payment' => true
);

$refund = wc_create_refund( $data );
if ( is_wp_error( $refund ) ) {
echo '<pre>';
print_r( $refund->get_error_message() );
echo '</pre>';
}
die;
}

/**
Expand Down Expand Up @@ -831,6 +866,10 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {

$order = new WC_Order( $order_id );
$request = $this->prepare_request( $txn_type, $order );
$request->set_request_data( array(
'refund_amount' => $amount,
'refund_reason' => $reason,
));
$request_args = $request->get_args();
if ( 0 >= (float)$request_args[ RequestArg::AMOUNT ] ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested, and if we pass a refund value > refundable order value, there will be an error the same as in the admin.

{ ["error"]=> array(1) { [0]=> string(22) "Invalid refund amount." } } is resolved.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiercrac I still consider that we need to do our own validation.
e.g. an amount of "0,12" will create a refund of "12"

throw new Exception( __( 'Refund amount must be greater than zero.', 'globalpayments-gateway-provider-for-woocommerce' ) );
Expand Down
13 changes: 13 additions & 0 deletions src/Gateways/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,17 @@ public function get_request_data( $key = null ) {

return wc_clean( $this->data[ $key ] );
}

/**
* Set Request Data
*
* @param array
*/
public function set_request_data ( array $data ) {
if ( ! empty( $this->data ) ) {
$this->data = array_merge( $this->data, $data );
} else {
$this->data = $data;
}
}
}
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Plugin {
*
* @var string
*/
const VERSION = '1.2.2';
const VERSION = '1.2.3';

/**
* Init the package.
Expand Down