Skip to content

Commit

Permalink
Merge pull request #66 from wirecard/TPWDCEE-1124
Browse files Browse the repository at this point in the history
Tpwdcee 1124
  • Loading branch information
tomazpu authored Apr 24, 2018
2 parents 3d34dab + dacbf9d commit 7795df4
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public function print_transaction_details( $data ) {
echo "<div class='wcs-op-group'>";

if ( $brand == 'Invoice' ) {
echo "<input type='hidden' value='$data->amount' name='amount'>";
echo "<input type='hidden' value='".$data->amount."' name='amount'>";
} elseif ( $operation == 'DEPOSIT' or $operation == 'REFUND' ) {
echo "<input type='text' autocomplete='off' value='' name='amount'>";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ public function __construct( $settings ) {
*
* @return bool|WP_Error
*/
public function refund() {
public function refund( $order_id = 0, $amount = 0, $reason = '' ) {
global $wpdb;

$order_id = $_POST['order_id'];
$refund_amount = $_POST['refund_amount'];

if ( $refund_amount <= 0 ) {
$this->_logger->error( __( 'Refund amount must be greater than zero.', 'woocommerce-wirecard-checkout-seamless' ) );

Expand Down Expand Up @@ -132,59 +131,38 @@ public function refund() {

$basket = null;


if ( in_array( 'REFUND', $order->getOperationsAllowed() ) ) {


if (
(
$tx_data->payment_method == WirecardCEE_QMore_PaymentType::INSTALLMENT
$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_invoiceprovider'] != 'payolution'
&& $this->_settings['woo_wcs_installmentprovider'] != 'payolution'
)
) {
if ( $total_items == 0 ) {
// invoice / installment provider is set to ratepay / wirecard and basket items were not sent
$this->_logger->error( __METHOD__ . ': basket needs to be defined for ' . $this->_settings['woo_wcs_invoiceprovider'] . ' during refund.' );
$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 );
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);
$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;
return false;
} else {
$basket = new WirecardCEE_Stdlib_Basket();
foreach ( $refund_items as $item_id => $item ) {
if ( $item['refund_qty'] < 1 ) {// $wc_order_items[ $item_id ] == null ) {
continue;
}
$wc_product = new WC_Product( $wc_order_items[ $item_id ]->get_product_id() );
$basket_item = new WirecardCEE_Stdlib_Basket_Item( $wc_product->get_id(), $item['refund_qty'] );

$price = new stdClass();
$price->net = wc_get_price_excluding_tax( $wc_product );
$price->gross = wc_get_price_including_tax( $wc_product );
$price->tax_amount = $price->gross - $price->net;
$price->tax_rate = $price->tax_amount / $price->net;

$basket_item->setName( $wc_product->get_name() )
->setDescription( $wc_product->get_short_description() )
->setImageUrl( wp_get_attachment_image_url( $wc_product->get_image_id() ) )
->setUnitNetAmount( wc_format_decimal( $price->net, wc_get_price_decimals() ) )
->setUnitGrossAmount( wc_format_decimal( $price->gross, wc_get_price_decimals() ) )
->setUnitTaxAmount( wc_format_decimal( $price->tax_amount, wc_get_price_decimals() ) )
->setUnitTaxRate( wc_format_decimal( $price->tax_rate, 3 ) );

$basket->addItem( $basket_item );
}
$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;
}
return true;
}

} else {
Expand Down Expand Up @@ -216,6 +194,94 @@ public function refund() {
return false;
}

/**
* Create basket with items
*
* @since 1.0.16
*
* @param $refund_items
* @param $wc_order
* @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;
foreach ( $wc_order_items as $item_id => $item ) {
if ( $refund_items[$item_id]['refund_qty'] < 1 ) {
continue;
}
$wc_product = new WC_Product( $wc_order_items[ $item_id ]->get_product_id() );

$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'] );


$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 );

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

$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;
}

/**
* Create basket with items
*
* @since 1.0.16
*
* @param $refund_items
* @param $wc_order
* @return WirecardCEE_Stdlib_Basket
*/
public function create_basket_without_items( $refund_amount, $wc_order ) {
$order_data = $wc_order->get_data();

$basket = new WirecardCEE_Stdlib_Basket();
$basket_item = new WirecardCEE_Stdlib_Basket_Item( 'Total refund ratepay' , 1);

$tax = $order_data['total_tax'];
$net = $order_data['total'] - $order_data['total_tax'];
$item_unit_gross_amount = $order_data['total'];
$item_tax_rate = $tax / $net;

$description = 'Refund full amount of order';
$tax_rate = 0;
if ($tax > 0) {
$tax_rate = floatval(number_format($item_tax_rate, 3));
}

$basket_item->setName('Full Refund Order')
->setDescription($description)
->setImageUrl()
->setUnitNetAmount(wc_format_decimal($net, wc_get_price_decimals()))
->setUnitGrossAmount(wc_format_decimal($item_unit_gross_amount, wc_get_price_decimals()))
->setUnitTaxAmount(wc_format_decimal($tax, wc_get_price_decimals()))
->setUnitTaxRate($tax_rate);
$basket->addItem($basket_item);

return $basket;
}

/**
* get the wirecard order details
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/

define( 'WOOCOMMERCE_GATEWAY_WCS_NAME', 'WirecardCheckoutSeamless' );
define( 'WOOCOMMERCE_GATEWAY_WCS_VERSION', '1.0.15' );
define( 'WOOCOMMERCE_GATEWAY_WCS_VERSION', '1.0.16' );

/**
* Config class
Expand Down Expand Up @@ -240,11 +240,11 @@ public function get_consumer_data( $order, $gateway, $checkout_data ) {
$user_data = get_userdata( $order->get_user_id() );
$consumerData->setEmail( isset( $user_data->user_email ) ? $user_data->user_email : '' );

if ( $gateway->get_option( 'woo_wcs_forwardconsumerbillingdata' ) ) {
if ( $gateway->get_option( 'woo_wcs_forwardconsumerbillingdata' ) || $this->force_consumer_data( $checkout_data['wcs_payment_method'] ) ) {
$billing_address = $this->get_address_data( $order, 'billing' );
$consumerData->addAddressInformation( $billing_address );
}
if ( $gateway->get_option( 'woo_wcs_forwardconsumershippingdata' ) ) {
if ( $gateway->get_option( 'woo_wcs_forwardconsumershippingdata' ) || $this->force_consumer_data( $checkout_data['wcs_payment_method'] ) ) {
$shipping_address = $this->get_address_data( $order, 'shipping' );
$consumerData->addAddressInformation( $shipping_address );
}
Expand All @@ -257,6 +257,50 @@ public function get_consumer_data( $order, $gateway, $checkout_data ) {
return $consumerData;
}

/**
* Force sending data for invoice and installment
*
* @param $payment_method
*
* @since 1.0.16
* @return bool
*/
public function force_consumer_data( $payment_method ) {
switch ( $payment_method ) {
case WirecardCEE_Stdlib_PaymentTypeAbstract::INVOICE:
case WirecardCEE_Stdlib_PaymentTypeAbstract::INSTALLMENT:
return true;
default:
return false;
}
}

/**
* Force sending basket data for invoice and installment via ratepay
*
* @param $payment_method
* @param $gateway
*
* @since 1.0.16
* @return bool
*/
public function force_basket_data( $payment_method, $gateway ) {
switch ( $payment_method ) {
case WirecardCEE_Stdlib_PaymentTypeAbstract::INVOICE:
if ( 'payolution' != $gateway->get_option('woo_wcs_invoiceprovider') ) {
return true;
}
return false;
case WirecardCEE_Stdlib_PaymentTypeAbstract::INSTALLMENT:
if ( 'payolution' != $gateway->get_option('woo_wcs_installmentprovider') ) {
return true;
}
return false;
default:
return false;
}
}

/**
* Generate address data (shipping or billing)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {

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

return $backend_operations->refund();
return $backend_operations->refund( $order_id, $amount, $reason );
}

/**
Expand Down Expand Up @@ -565,7 +565,8 @@ public function initiate_payment( $order ) {
$client->setConfirmMail( get_bloginfo( 'admin_email' ) );
}

if ( $this->get_option( 'woo_wcs_forwardbasketdata' ) ) {
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() );
}

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.15
* Version: 1.0.16
* Author: Wirecard
* Author URI: https://www.wirecard.at/
* License: GPL2
Expand Down

0 comments on commit 7795df4

Please sign in to comment.