Skip to content

Commit

Permalink
Cartão e Boleto: Melhoria no cálculo e exibição dos descontos
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoliveiraGN committed Feb 16, 2023
1 parent 5d0fe0c commit 33863c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
28 changes: 20 additions & 8 deletions includes/payment-methods/class-wc-gerencianet-boleto.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ public function payment_fields() {
echo wpautop( wp_kses_post( $this->description ) );
}

if(intval( $this->get_option( 'gn_billet_discount' ) ) > 0){
$discountMessage = '';
if ( $this->get_option( 'gn_billet_discount_shipping' ) == 'total' ) {
$discountMessage = ' no valor total da compra (frete incluso)';
}else{
$discountMessage = ' no valor total dos produtos (frete não incluso)';
}

$discountWarn = '<div class="warning-payment" id="wc-gerencianet-messages-sandbox">
<div class="woocommerce-info">' .esc_html( $this->get_option( 'gn_billet_discount' ) ) . '% de Desconto'.$discountMessage. '</div>
</div>';
echo wpautop( wp_kses_post( $discountWarn ) );
}

$is_sandbox = $this->get_option( 'gn_sandbox' ) == 'yes' ? true : false;
if ( $is_sandbox ) {
$sandboxWarn = '<div class="warning-payment" id="wc-gerencianet-messages-sandbox">
Expand Down Expand Up @@ -241,11 +255,11 @@ public function process_payment( $order_id ) {
case 'line_item':
$product = $item->get_product();
$newItem = array(
'name' => $item->get_name(),
'name' => $product->get_name(),
'amount' => $item->get_quantity(),
'value' => $item->get_total() * 100,
'value' => $product->get_price() * 100,
);
$orderTotal += $item->get_total() * 100 * $item->get_quantity();
$orderTotal += $product->get_price() * 100 * $item->get_quantity();
$items[] = $newItem;
break;
default:
Expand Down Expand Up @@ -300,17 +314,15 @@ public function process_payment( $order_id ) {
$discount_gn = 0;

if ( $this->get_option( 'gn_billet_discount_shipping' ) == 'total' ) {
$discountMessage = ' no valor total da compra';
if ( isset( $shipping[0]['value'] ) ) {
$discount_gn = ( $orderTotal + $shipping[0]['value'] ) * ( intval( $this->get_option( 'gn_billet_discount' ) ) / 100 );
$discount_gn = round(( $orderTotal + $shipping[0]['value'] ) * ( intval( $this->get_option( 'gn_billet_discount' ) ) / 100 ), 0, PHP_ROUND_HALF_UP);
$discount['value'] += $discount_gn;
} else {
$discount_gn = ( ( $orderTotal ) * ( intval( $this->get_option( 'gn_billet_discount' ) ) / 100 ) );
$discount_gn = round( ( $orderTotal ) * ( intval( $this->get_option( 'gn_billet_discount' ) ) / 100 ), 0, PHP_ROUND_HALF_UP );
$discount['value'] += $discount_gn;
}
} else {
$discountMessage = ' no valor total dos produtos (frete não incluso)';
$discount_gn = ( $orderTotal * ( intval( $this->get_option( 'gn_billet_discount' ) ) / 100 ) );
$discount_gn = round( $orderTotal * ( intval( $this->get_option( 'gn_billet_discount' ) ) / 100 ), 0, PHP_ROUND_HALF_UP );
$discount['value'] += $discount_gn;
}
$order_item_id = wc_add_order_item(
Expand Down
13 changes: 5 additions & 8 deletions includes/payment-methods/class-wc-gerencianet-cartao.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,32 +222,30 @@ public function process_payment( $order_id ) {
'amount' => 1,
'value' => $item->get_subtotal() * 100,
);
$orderTotal += $item->get_subtotal() * 100;
$items[] = $newFee;
break;
case 'shipping':
if ( $item->get_total() > 0 ) {
$shipping[] = array(
'name' => __( 'Frete', Gerencianet_I18n::getTextDomain() ),
'name' => __( 'Frete', Gerencianet_I18n::getTextDomain()),
'value' => $item->get_total() * 100,
);
}
break;
case 'coupon':
$newDiscount = array(
'type' => 'currency',
'value' => $item->get_total() * 100,
'value' => $item->get_discount() * 100,
);
$discount = $newDiscount;
break;
case 'line_item':
$product = $item->get_product();
$newItem = array(
'name' => $item->get_name(),
'name' => $product->get_name(),
'amount' => $item->get_quantity(),
'value' => $item->get_total() * 100,
'value' => $product->get_price() * 100,
);
$orderTotal += $item->get_total() * 100 * $item->get_quantity();
$items[] = $newItem;
break;
default:
Expand All @@ -257,7 +255,6 @@ public function process_payment( $order_id ) {
'amount' => $item->get_quantity(),
'value' => $product->get_price() * 100,
);
$orderTotal += $product->get_price() * 100 * $item->get_quantity();
$items[] = $newItem;
break;
}
Expand Down Expand Up @@ -308,7 +305,7 @@ public function process_payment( $order_id ) {
);

try {
$response = $this->gerencianetSDK->one_step_card( $order_id, $items, $shipping, strtolower( $woocommerce->api_request_url( GERENCIANET_CARTAO_ID ) ), $customer, $paymentToken, $installments, $billingAddress );
$response = $this->gerencianetSDK->one_step_card( $order_id, $items, $shipping, strtolower( $woocommerce->api_request_url( GERENCIANET_CARTAO_ID ) ), $customer, $paymentToken, $installments, $billingAddress, $discount );
$charge = json_decode( $response, true );

$order->update_status( 'pending-payment' );
Expand Down

0 comments on commit 33863c9

Please sign in to comment.