Skip to content

Commit

Permalink
Merge pull request #76 from wirecard/TPWDCEE-1696
Browse files Browse the repository at this point in the history
Tpwdcee 1696
  • Loading branch information
rinnhofer authored Nov 14, 2018
2 parents c174f03 + bcc6342 commit c76ae89
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public function refund( $order_id = 0, $amount = 0, $reason = '' ) {
);
}
}

$wc_order = wc_get_order( $order_id ); // woocommerce order
$wc_order_items = $wc_order->get_items();
$wcs_order_number = $wc_order->get_meta( 'wcs_order_number' );
$order_details = $this->get_order_details( $wcs_order_number );

Expand All @@ -128,22 +126,23 @@ public function refund( $order_id = 0, $amount = 0, $reason = '' ) {
// get transaction informations
$tx_query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wirecard_checkout_seamless_tx WHERE id_order = %d", $order_id );
$tx_data = $wpdb->get_row( $tx_query );
$tx_original = unserialize( $tx_data->request );

$basket = null;

if ( in_array( 'REFUND', $order->getOperationsAllowed() ) ) {
if (
(
$tx_data->payment_method == WirecardCEE_Stdlib_PaymentTypeAbstract::INVOICE
&& $this->_settings['woo_wcs_invoiceprovider'] != 'payolution'
)
or
(
$tx_data->payment_method == WirecardCEE_QMore_PaymentType::INSTALLMENT
&& $this->_settings['woo_wcs_installmentprovider'] != 'payolution'
)
) {
if ( $total_items == 0 ) {
if ( in_array( 'REFUND', $order->getOperationsAllowed() ) ) {
if (
(
$tx_data->payment_method == WirecardCEE_Stdlib_PaymentTypeAbstract::INVOICE
&& $this->_settings['woo_wcs_invoiceprovider'] != 'payolution'
)
or
(
$tx_data->payment_method == WirecardCEE_QMore_PaymentType::INSTALLMENT
&& $this->_settings['woo_wcs_installmentprovider'] != 'payolution'
)
) {
if ( $total_items == 0 ) {
$basket = $this->create_basket_without_items( $refund_amount, $wc_order );
$order_data = $wc_order->get_data();
$response_with_basket = $this->get_client()->refund( $wcs_order_number, $order_data['total'], $order->getCurrency(), $basket );
Expand All @@ -154,30 +153,31 @@ public function refund( $order_id = 0, $amount = 0, $reason = '' ) {
} else {
return true;
}
}
$basket = $this->create_basket( $refund_items, $wc_order);
$response_with_basket = $this->get_client()->refund( $wcs_order_number, $refund_amount, $order->getCurrency(), $basket );
if ( $response_with_basket->hasFailed() ) {
$this->logResponseErrors( __METHOD__, $response_with_basket->getErrors() );
}

return false;
} else {
return true;
}
$basket = $this->create_basket( $refund_items, $wc_order, $tx_original );
$response_with_basket = $this->get_client()->refund( $wcs_order_number, $refund_amount, $order->getCurrency(), $basket );
if ( $response_with_basket->hasFailed() ) {
$this->logResponseErrors( __METHOD__, $response_with_basket->getErrors() );

} else {
return false;
} else {
return true;
}

$response = $this->get_client()->refund( $wcs_order_number, $refund_amount, $order->getCurrency() );
if ( $response->hasFailed() ) {
$this->logResponseErrors( __METHOD__, $response->getErrors() );
} else {

return false;
} else {
return true;
}
}
$response = $this->get_client()->refund( $wcs_order_number, $refund_amount, $order->getCurrency() );
if ( $response->hasFailed() ) {
$this->logResponseErrors( __METHOD__, $response->getErrors() );

} else {
return false;
} else {
return true;
}
}

} else {
// the following have allowed transferFund command
$allowed_payment_methods = array(
WirecardCEE_QMore_PaymentType::IDL,
Expand All @@ -201,45 +201,55 @@ public function refund( $order_id = 0, $amount = 0, $reason = '' ) {
*
* @param $refund_items
* @param $wc_order
* @param $tx_original
* @return WirecardCEE_Stdlib_Basket
*/
public function create_basket( $refund_items, $wc_order ) {
$wc_order_items = $wc_order->get_items();
$basket = new WirecardCEE_Stdlib_Basket();
$sum = 0;
public function create_basket( $refund_items, $wc_order, $tx_original ) {
$wc_order_items = $wc_order->get_items();
$basket = new WirecardCEE_Stdlib_Basket();

$basket_items = 0;
if ( isset( $tx_original['basketItems'] ) ) {
$basket_items = $tx_original['basketItems'];
}
$original_basket = array();
for ( $count = 1; $count <= $basket_items; $count++ ) {
$prefix = 'basketItem'.$count;
$original_basket[$tx_original[$prefix . 'articleNumber']] = array(
'gross' => $tx_original[$prefix . 'unitGrossAmount'],
'net' => $tx_original[$prefix . 'unitNetAmount'],
'tax' => $tx_original[$prefix . 'unitTaxAmount'],
'tax_rate' => $tx_original[$prefix . 'unitTaxRate'],
'description' => $tx_original[$prefix . 'description'],
'name' => $tx_original[$prefix . 'name'],
'imageUrl' => $tx_original[$prefix . 'imageUrl']
);
}

foreach ( $wc_order_items as $item_id => $item ) {
if ( $refund_items[$item_id]['refund_qty'] < 1 ) {
$refund_item_quantity = $refund_items[$item_id]['refund_qty'];
if ( $refund_item_quantity < 1 ) {
continue;
}
$wc_product = new WC_Product( $wc_order_items[ $item_id ]->get_product_id() );
$product = $item->get_product();
$product_data = $product->get_data();

$article_nr = $wc_product->get_id();
if ( $wc_product->get_sku() != '' ) {
$article_nr = $wc_product->get_sku();
}

$sum += number_format( wc_get_price_including_tax( $wc_product ), wc_get_price_decimals() );
$basket_item = new WirecardCEE_Stdlib_Basket_Item( $article_nr, $refund_items[$item_id]['refund_qty'] );

if ( key_exists( $product_data['sku'], $original_basket ) ) {
$refund_item = $original_basket[$product_data['sku']];
$basket_item = new WirecardCEE_Stdlib_Basket_Item( $product_data['sku'] );

$tax = wc_get_price_including_tax($wc_product) - wc_get_price_excluding_tax($wc_product);
$item_tax_rate = $tax / wc_get_price_excluding_tax( $wc_product );
$basket_item->setName( $refund_item['name'] )
->setDescription( $refund_item['description'] )
->setImageUrl( $refund_item['imageUrl'] )
->setUnitNetAmount( $refund_item['net'] )
->setUnitGrossAmount( $refund_item['gross'] )
->setUnitTaxAmount( $refund_item['tax'] )
->setUnitTaxRate( $refund_item['tax_rate'] );

$description = $wc_product->get_short_description();
$tax_rate = 0;
if ( $wc_product->is_taxable() ) {
$tax_rate = floatval(number_format( $item_tax_rate, 3 ));
$basket->addItem( $basket_item, $refund_item_quantity );
}

$basket_item->setName( $wc_product->get_name() )
->setDescription( $description )
->setImageUrl( wp_get_attachment_image_url( $wc_product->get_image_id() ) )
->setUnitNetAmount( wc_format_decimal( wc_get_price_excluding_tax( $wc_product ), wc_get_price_decimals() ) )
->setUnitGrossAmount( wc_format_decimal( wc_get_price_including_tax( $wc_product ), wc_get_price_decimals() ) )
->setUnitTaxAmount( wc_format_decimal( $tax, wc_get_price_decimals() ) )
->setUnitTaxRate( $tax_rate * 100 );

$basket->addItem( $basket_item );
}
return $basket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function get_customer_statement( $order, $payment_type ) {
* @since 1.0.0
* @return WirecardCEE_Stdlib_Basket
*/
public function get_shopping_basket() {
public function get_shopping_basket( $order_amount = 0 ) {
global $woocommerce;

$cart = $woocommerce->cart;
Expand All @@ -409,8 +409,7 @@ public function get_shopping_basket() {
// Calculate amounts per unit
$item_unit_net_amount = $item_net_amount / $item_quantity;
$item_unit_tax_amount = $item_tax_amount / $item_quantity;
$item_unit_gross_amount = wc_format_decimal( $item_unit_net_amount + $item_unit_tax_amount,
wc_get_price_decimals() );
$item_unit_gross_amount = wc_format_decimal( $item_unit_net_amount + $item_unit_tax_amount, wc_get_price_decimals() );

$item->setUnitGrossAmount( $item_unit_gross_amount )
->setUnitNetAmount( wc_format_decimal( $item_unit_net_amount, wc_get_price_decimals() ) )
Expand All @@ -436,6 +435,44 @@ public function get_shopping_basket() {
$basket->addItem( $item );
}

if ( $order_amount > 0 ) {
$rounding_difference = $this->get_rounding_difference( $basket, $order_amount );
if ( $rounding_difference != 0 ) {
$item = new WirecardCEE_Stdlib_Basket_Item( 'rounding' );
$item->setUnitGrossAmount( wc_format_decimal( $rounding_difference, wc_get_price_decimals() ) )
->setUnitNetAmount( wc_format_decimal( $rounding_difference, wc_get_price_decimals() ) )
->setUnitTaxAmount( 0 )
->setUnitTaxRate( 0 )
->setName( 'Rounding' )
->setDescription( 'Rounding' );
$basket->addItem( $item );
}
}

return $basket;
}

/**
* Calculate rounding differences
* @param WirecardCEE_Stdlib_Basket $basket
* @param float $total_amount
* @return float
*/
public function get_rounding_difference( WirecardCEE_Stdlib_Basket $basket, $total_amount ) {
$total_amount_rounded = 0;
$amount_difference = 0;
$basket_data = $basket->getData();
$basket_items = $basket_data['basketItems'];

for ( $count = 1; $count <= $basket_items; $count++ ) {
$prefix_key = 'basketItem'.$count;
$total_amount_rounded += $basket_data[$prefix_key . 'unitGrossAmount'] * $basket_data[$prefix_key . 'quantity'];
}

if ( $total_amount > $total_amount_rounded || $total_amount_rounded > $total_amount ) {
$amount_difference = $total_amount - $total_amount_rounded;
}

return wc_format_decimal( $amount_difference, wc_get_price_decimals() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,13 @@ class="payment_box payment_method_wcs_<?= ( $type->has_payment_fields() ) ? $typ
* @param float $amount
* @param string $reason
*
* @return bool True or false based on success, or a WP_Error object
* @return bool|WP_Error
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {

if ( empty( $this->settings['woo_wcs_backendpassword'] ) ) {
return new WP_Error( 'refund_error', 'No password for backend operations (Toolkit) provided. Please visit your settings!' );
}
$backend_operations = new WC_Gateway_Wirecard_Checkout_Seamless_Backend_Operations( $this->settings );

return $backend_operations->refund( $order_id, $amount, $reason );
Expand Down Expand Up @@ -567,7 +570,7 @@ public function initiate_payment( $order ) {

if ( $this->get_option( 'woo_wcs_forwardbasketdata' )
|| ( $this->_config->force_basket_data( $checkout_data['wcs_payment_method'], $this ) ) ) {
$client->setBasket( $this->_config->get_shopping_basket() );
$client->setBasket( $this->_config->get_shopping_basket( $order->get_total() ) );
}

$client->wooOrderId = $order->get_id();
Expand Down Expand Up @@ -996,12 +999,15 @@ public function datastorage_return() {
*/
public function wirecard_transaction_do_page() {

$backend_operations = new WC_Gateway_Wirecard_Checkout_Seamless_Backend_Operations( $this->settings );

echo "<div class='wrap'>";

$this->_admin->include_backend_header( $this );

if ( empty( $this->settings['woo_wcs_backendpassword'] ) ) {
$this->wirecard_transactions_error_page( 'No password for backend operations (Toolkit) provided. Please visit your settings!' );
return false;
}
$backend_operations = new WC_Gateway_Wirecard_Checkout_Seamless_Backend_Operations( $this->settings );

if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] == 'POST' ) {

Expand Down Expand Up @@ -1074,6 +1080,15 @@ public function wirecard_transactions_do_page() {
echo "</div>";
}

/**
* Handles transaction error overview
*/
public function wirecard_transactions_error_page( $error_msg ) {
echo "<div class='wrap woocommerce'>";
echo htmlentities( $error_msg );
echo "</div>";
}

/**
* Opens the support request form
*
Expand Down
2 changes: 1 addition & 1 deletion woocommerce-wirecard-checkout-seamless/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors:
Tags: ecommerce, e-commerce, sales, checkout, paypal, credit card, woocommerce, payment, gateway
Requires at least: 4.4
Tested up to: 4.8.1
Stable tag: 1.0.4
Stable tag: 1.0.19
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Wirecard Checkout Seamless
* Plugin URI: https://github.com/wirecard/woocommerce-wcs
* Description: Wirecard Checkout Seamless plugin for WooCommerce
* Version: 1.0.18
* Version: 1.0.19
* Author: Wirecard
* Author URI: https://www.wirecard.at/
* License: GPL2
Expand Down

0 comments on commit c76ae89

Please sign in to comment.