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

PFW-318 #1257

Merged
merged 10 commits into from
Jan 6, 2019
11 changes: 6 additions & 5 deletions angelleye-includes/angelleye-admin-order-payment-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,14 @@ public function angelleye_reference_transaction_request_ec_pp_pf($order, $refere
$PayPalRequestData['ShippingAddress'] = $ShippingAddress;
}
$this->order_param = $this->gateway_calculation->order_calculation($order_id);
if ($this->gateway_settings['send_items']) {
if( $this->order_param['is_calculation_mismatch'] == false ) {
$Payment['order_items'] = $this->order_param['order_items'];
$PaymentDetails['taxamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['taxamt']);
$PaymentDetails['shippingamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['shippingamt']);
$PaymentDetails['itemamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['itemamt']);
} else {
$Payment['order_items'] = array();
}
$PaymentDetails['taxamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['taxamt']);
$PaymentDetails['shippingamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['shippingamt']);
$PaymentDetails['itemamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['itemamt']);
$PayPalRequestData['PaymentDetails'] = $PaymentDetails;
return $PayPalRequestData;
}
Expand All @@ -639,6 +639,7 @@ public function angelleye_load_calculation() {
if (!class_exists('WC_Gateway_Calculation_AngellEYE')) {
require_once( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/wc-gateway-calculations-angelleye.php' );
}
$this->gateway_calculation = new WC_Gateway_Calculation_AngellEYE();
$subtotal_mismatch_behavior = ( isset($this->gateway_settings['subtotal_mismatch_behavior']) && ( $this->gateway_settings['subtotal_mismatch_behavior'] == 'drop') ) ? 'drop' : 'add';
$this->gateway_calculation = new WC_Gateway_Calculation_AngellEYE(null, $subtotal_mismatch_behavior);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct($gateway) {
if (!class_exists('WC_Gateway_Calculation_AngellEYE')) {
require_once( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/wc-gateway-calculations-angelleye.php' );
}
$this->gateway_calculation = new WC_Gateway_Calculation_AngellEYE();
$this->gateway_calculation = new WC_Gateway_Calculation_AngellEYE(null, $this->gateway->subtotal_mismatch_behavior);
if (!class_exists('WC_Gateway_PayPal_Express_Response_AngellEYE')) {
require_once( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-response-angelleye.php' );
}
Expand Down Expand Up @@ -500,7 +500,7 @@ public function angelleye_do_express_checkout_payment_request() {
if (isset($this->gateway->notifyurl) && !empty($this->gateway->notifyurl)) {
$Payment['notifyurl'] = $this->gateway->notifyurl;
}
if ($this->gateway->send_items) {
if( $this->order_param['is_calculation_mismatch'] == false ) {
$Payment['order_items'] = $this->order_param['order_items'];
$Payment['taxamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['taxamt']);
$Payment['shippingamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['shippingamt']);
Expand Down Expand Up @@ -803,14 +803,10 @@ public function angelleye_set_express_checkout_request() {
$Payment['shiptocountrycode'] = $shipping_country;
}
if(isset($this->cart_param['is_calculation_mismatch']) && $this->cart_param['is_calculation_mismatch'] == false) {
if ($this->gateway->send_items) {
$Payment['order_items'] = $this->cart_param['order_items'];
$Payment['taxamt'] = $this->cart_param['taxamt'];
$Payment['shippingamt'] = $this->cart_param['shippingamt'];
$Payment['itemamt'] = $this->cart_param['itemamt'];
} else {
$Payment['order_items'] = array();
}
$Payment['order_items'] = $this->cart_param['order_items'];
$Payment['taxamt'] = $this->cart_param['taxamt'];
$Payment['shippingamt'] = $this->cart_param['shippingamt'];
$Payment['itemamt'] = $this->cart_param['itemamt'];
}
array_push($Payments, $Payment);
$PayPalRequestData = array(
Expand Down Expand Up @@ -1342,14 +1338,14 @@ public function DoReferenceTransaction($order_id) {
$PayPalRequestData['ShippingAddress'] = $ShippingAddress;
}
$this->order_param = $this->gateway_calculation->order_calculation($order_id);
if ($this->gateway->send_items) {
if( $this->order_param['is_calculation_mismatch'] == false ) {
$Payment['order_items'] = $this->order_param['order_items'];
$PaymentDetails['taxamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['taxamt']);
$PaymentDetails['shippingamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['shippingamt']);
$PaymentDetails['itemamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['itemamt']);
} else {
$Payment['order_items'] = array();
}
$PaymentDetails['taxamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['taxamt']);
$PaymentDetails['shippingamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['shippingamt']);
$PaymentDetails['itemamt'] = AngellEYE_Gateway_Paypal::number_format($this->order_param['itemamt']);
$PayPalRequestData['PaymentDetails'] = $PaymentDetails;
$this->paypal_response = $this->paypal->DoReferenceTransaction($PayPalRequestData);
AngellEYE_Gateway_Paypal::angelleye_paypal_for_woocommerce_curl_error_handler($this->paypal_response, $methos_name = 'DoExpressCheckoutPayment', $gateway = 'PayPal Express Checkout', $this->gateway->error_email_notify);
Expand Down
1 change: 0 additions & 1 deletion angelleye-includes/paypal-rest-api-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ public function create_transaction_method_obj() {

$this->card = new CreditCard();
$this->order_item = array();
$this->send_items = true;
}

/**
Expand Down
96 changes: 37 additions & 59 deletions classes/wc-gateway-calculations-angelleye.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class WC_Gateway_Calculation_AngellEYE {
public $temp_total;
public $is_separate_discount;

public function __construct($payment_method = null) {
public function __construct($payment_method = null, $subtotal_mismatch_behavior = 'add') {
$this->order_items = array();
$this->is_adjust = false;
$this->payment = array();
$this->payment['is_calculation_mismatch'] = false;
$this->subtotal_mismatch_behavior = $subtotal_mismatch_behavior;
$this->payment_method = $payment_method;
if ($this->payment_method == 'paypal_pro_payflow' || $this->payment_method == 'paypal_advanced') {
$this->is_separate_discount = true;
Expand All @@ -46,7 +49,7 @@ public function cart_calculation() {
}

WC()->cart->calculate_totals();
$this->payment = array();

$this->itemamt = 0;
$roundedPayPalTotal = 0;
$this->discount_amount = round(WC()->cart->get_cart_discount_total(), $this->decimals);
Expand Down Expand Up @@ -170,14 +173,11 @@ public function cart_calculation() {
$this->payment['discount_amount'] = AngellEYE_Gateway_Paypal::number_format(round($this->discount_amount, $this->decimals));
if ($this->taxamt < 0 || $this->shippingamt < 0) {
$this->payment['is_calculation_mismatch'] = true;
} else {
$this->payment['is_calculation_mismatch'] = false;
}
}
return $this->payment;
}

public function order_calculation($order_id) {
$this->payment = array();
$order = wc_get_order($order_id);
$this->itemamt = 0;
$this->discount_amount = 0;
Expand Down Expand Up @@ -290,8 +290,6 @@ public function order_calculation($order_id) {
$this->payment['discount_amount'] = AngellEYE_Gateway_Paypal::number_format(round($this->discount_amount, $this->decimals));
if ($this->taxamt < 0 || $this->shippingamt < 0) {
$this->payment['is_calculation_mismatch'] = true;
} else {
$this->payment['is_calculation_mismatch'] = false;
}
return $this->payment;
}
Expand All @@ -310,33 +308,22 @@ public function cart_re_calculate() {
$this->temp_total = round($this->itemamt + $this->taxamt + $this->shippingamt, $this->decimals);
}
if (round(WC()->cart->total, $this->decimals) != $this->temp_total) {
$cartItemAmountDifference = round(WC()->cart->total, $this->decimals) - $this->temp_total;
if ($this->shippingamt > 0) {
$this->shippingamt += round($cartItemAmountDifference, $this->decimals);
} elseif ($this->taxamt > 0) {
$this->taxamt += round($cartItemAmountDifference, $this->decimals);
} else {
if (count($this->order_items) == 1 && (!empty($this->order_items[0]['qty']) && $this->order_items[0]['qty'] > 1 )) {
unset($this->order_items);
$this->order_total = WC()->cart->total;
$this->itemamt = WC()->cart->total;
if( $this->subtotal_mismatch_behavior == 'add' ) {
$cartItemAmountDifference = round(WC()->cart->total - $this->temp_total, $this->decimals);
if ( abs( $cartItemAmountDifference ) > 0.000001 && 0.0 !== (float) $cartItemAmountDifference ) {
$item = array(
'name' => 'Line Item Amount Offset',
'desc' => 'Adjust cart calculation discrepancy',
'qty' => 1,
'amt' => round($cartItemAmountDifference, $this->decimals)
);
$this->order_items[] = $item;
$this->itemamt += round($cartItemAmountDifference, $this->decimals);
} else {
foreach ($this->order_items as $key => $value) {
if ($value['qty'] == 1 && $this->is_adjust == false) {
if ($this->order_items[$key]['amt'] * 1000 > 0) {
$this->order_items[$key]['amt'] = $this->order_items[$key]['amt'] + round($cartItemAmountDifference, $this->decimals);
$this->order_total += round($cartItemAmountDifference, $this->decimals);
$this->itemamt += round($cartItemAmountDifference, $this->decimals);
$this->is_adjust = true;
}
}
}
if ($this->is_adjust == false) {
unset($this->order_items);
$this->order_total = WC()->cart->total;
$this->itemamt = WC()->cart->total;
}
$this->payment['is_calculation_mismatch'] = true;
}
} else {
$this->payment['is_calculation_mismatch'] = true;
}
}
$this->angelleye_disable_line_item();
Expand All @@ -356,35 +343,24 @@ public function order_re_calculate($order) {
$this->temp_total = round($this->itemamt + $this->taxamt + $this->shippingamt, $this->decimals);
}
if (round($order->get_total(), $this->decimals) != $this->temp_total) {
$cartItemAmountDifference = round($order->get_total(), $this->decimals) - $this->temp_total;
if ($this->shippingamt > 0) {
$this->shippingamt += round($cartItemAmountDifference, $this->decimals);
} elseif ($this->taxamt > 0) {
$this->taxamt += round($cartItemAmountDifference, $this->decimals);
} else {
if (count($this->order_items) == 1 && (!empty($this->order_items[0]['qty']) && $this->order_items[0]['qty'] > 1 )) {
unset($this->order_items);
$this->order_total = WC()->cart->total;
$this->itemamt = WC()->cart->total;
if( $this->subtotal_mismatch_behavior == 'add' ) {
$cartItemAmountDifference = round($order->get_total() - $this->temp_total, $this->decimals);
if ( abs( $cartItemAmountDifference ) > 0.000001 && 0.0 !== (float) $cartItemAmountDifference ) {
$item = array(
'name' => 'Line Item Amount Offset',
'desc' => 'Adjust cart calculation discrepancy',
'qty' => 1,
'amt' => AngellEYE_Gateway_Paypal::number_format($cartItemAmountDifference)
);
$this->order_items[] = $item;
$this->itemamt += round($cartItemAmountDifference, $this->decimals);
} else {
foreach ($this->order_items as $key => $value) {
if ($value['qty'] == 1 && $this->is_adjust == false) {
if ($this->order_items[$key]['amt'] * 1000 > 0) {
$this->order_items[$key]['amt'] = $this->order_items[$key]['amt'] + round($cartItemAmountDifference, $this->decimals);
$this->order_total += round($cartItemAmountDifference, $this->decimals);
$this->itemamt += round($cartItemAmountDifference, $this->decimals);
$this->is_adjust = true;
}
}
}
if ($this->is_adjust == false) {
unset($this->order_items);
$this->order_total = WC()->cart->total;
$this->itemamt = WC()->cart->total;
}
$this->payment['is_calculation_mismatch'] = true;
}
} else {
$this->payment['is_calculation_mismatch'] = true;
}
}
}
$this->angelleye_disable_line_item();
}

Expand Down Expand Up @@ -426,12 +402,14 @@ public function angelleye_disable_line_item() {
unset($this->order_items);
$this->order_total = WC()->cart->total;
$this->itemamt = WC()->cart->total;
$this->payment['is_calculation_mismatch'] = true;
}

if ($this->itemamt * 1000 < 0) {
unset($this->order_items);
$this->order_total = WC()->cart->total;
$this->itemamt = WC()->cart->total;
$this->payment['is_calculation_mismatch'] = true;
}
}

Expand Down
Loading