Skip to content

Commit

Permalink
#6 Implement basket parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rinnhofer committed Apr 18, 2017
1 parent b2dcc49 commit c7b4f0a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function get_plugin_version() {
* @return string
*/
function get_order_reference( $order ) {
return sprintf( '%010d', $order->id );
return sprintf( '%010d', $order->get_id() );
}

/**
Expand Down Expand Up @@ -234,4 +234,51 @@ function set_customer_statement( $client, $gateway ) {
}
$client->generateCustomerStatement( $prefix, get_bloginfo( 'name' ) );
}

/**
* Generate shopping basket
*
* @since 1.0.0
* @return WirecardCEE_Stdlib_Basket
*/
function get_shopping_basket() {
global $woocommerce;

$cart = $woocommerce->cart;

$basket = new WirecardCEE_Stdlib_Basket();

foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$article_nr = $cart_item['product_id'];
if ( $cart_item['data']->get_sku() != '' ) {
$article_nr = $cart_item['data']->get_sku();
}

$attachment_ids = $cart_item['data']->get_gallery_image_ids();
foreach ( $attachment_ids as $attachment_id ) {
$image_url = wp_get_attachment_image_url( $attachment_id );
}

$item = new WirecardCEE_Stdlib_Basket_Item( $article_nr );
$item_net_amount = $cart_item['line_total'];
$item_tax_amount = $cart_item['line_tax'];
$item_quantity = $cart_item['quantity'];

// Calculate amounts per unit
$item_unit_net_amount = wc_format_decimal( $item_net_amount / $item_quantity, wc_get_price_decimals() );
$item_unit_tax_amount = wc_format_decimal( $item_tax_amount / $item_quantity, wc_get_price_decimals() );

$item->setUnitGrossAmount( $item_unit_net_amount + $item_unit_tax_amount )
->setUnitNetAmount( $item_unit_net_amount )
->setUnitTaxAmount( $item_unit_tax_amount )
->setUnitTaxRate( number_format( ( $item_unit_tax_amount / $item_unit_net_amount ), 2, '.', '' ) )
->setDescription( substr( strip_tags( $cart_item['data']->get_short_description() ), 0, 127 ) )
->setName( substr( strip_tags( $cart_item['data']->get_name() ), 0, 127 ) )
->setImageUrl( isset( $image_url ) ? $image_url : '' );

$basket->addItem( $item, $item_quantity );
}

return $basket;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,22 @@ function initiate_payment( $order, $payment_type ) {
$config_array = $this->_config->get_client_config( $this );
$client = new WirecardCEE_QMore_FrontendClient( $config_array );

$client->setPluginVersion( $this->_config->get_plugin_version() );
$client->setOrderReference( $this->_config->get_order_reference( $order ) );

$return_url = add_query_arg( 'wc-api', 'WC_Gateway_Wirecard_Checkout_Seamless',
$return_url = add_query_arg( 'wc-api', 'WC_Gateway_Wirecard_Checkout_Seamless',
site_url( '/', is_ssl() ? 'https' : 'http' ) );

$consumer_data = $this->_config->get_consumer_data( $order, $this );
$auto_deposit = $this->get_option( 'woo_wcs_automateddeposit' );
$service_url = $this->get_option( 'woo_wcs_serviceurl' );

$service_url = $this->get_option( 'woo_wcs_serviceurl' );
// Check if service url is valid
if ( filter_var( $service_url, FILTER_VALIDATE_URL ) === false ) {
wc_add_notice( __( "Service URL is invalid", 'woocommerce-wcs' ), 'error' );

return;
}

$client->setPluginVersion( $this->_config->get_plugin_version() );
$client->setOrderReference( $this->_config->get_order_reference( $order ) );

$client->setAmount( $order->get_total() )
->setCurrency( get_woocommerce_currency() )
->setPaymentType( $payment_type )
Expand All @@ -348,7 +348,11 @@ function initiate_payment( $order, $payment_type ) {
//TODO: shop-specific order number
}

$client->wooOrderId = $order->id;
if ( $this->get_option( 'woo_wcs_forwardbasketdata' ) ) {
$client->setBasket( $this->_config->get_shopping_basket() );
}

$client->wooOrderId = $order->get_id();

$initResponse = $client->initiate();

Expand All @@ -363,7 +367,7 @@ function initiate_payment( $order, $payment_type ) {
}

WC()->session->wirecard_checkout_seamless_redirect_url = array(
'id' => $order->id,
'id' => $order->get_id(),
'url' => $initResponse->getRedirectUrl()
);

Expand Down Expand Up @@ -456,7 +460,7 @@ function confirm() {
switch ( $return->getPaymentState() ) {
case WirecardCEE_QMore_ReturnFactory::STATE_SUCCESS:
update_post_meta( $order->get_id(), 'wcs_gateway_reference_number',
$return->getGatewayReferenceNumber() );
$return->getGatewayReferenceNumber() );
update_post_meta( $order->get_id(), 'wcs_order_number', $return->getOrderNumber() );
$order->payment_complete();
break;
Expand Down

0 comments on commit c7b4f0a

Please sign in to comment.