Merge pull request #345 from woocommerce/pcp-352-fix-rounding-check

Fix/refactor rounding check
This commit is contained in:
Emili Castells 2021-11-04 11:10:50 +01:00 committed by GitHub
commit e640eae060
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 92 deletions

View file

@ -303,55 +303,79 @@ class PurchaseUnit {
* @return bool * @return bool
*/ */
private function ditch_items_when_mismatch( Amount $amount, Item ...$items ): bool { private function ditch_items_when_mismatch( Amount $amount, Item ...$items ): bool {
$fee_items_total = ( $amount->breakdown() && $amount->breakdown()->item_total() ) ? $breakdown = $amount->breakdown();
$amount->breakdown()->item_total()->value() : null;
$fee_tax_total = ( $amount->breakdown() && $amount->breakdown()->tax_total() ) ?
$amount->breakdown()->tax_total()->value() : null;
foreach ( $items as $item ) {
if ( null !== $fee_items_total ) {
$fee_items_total -= $item->unit_amount()->value() * $item->quantity();
}
if ( null !== $fee_tax_total ) {
$fee_tax_total -= $item->tax()->value() * $item->quantity();
}
}
$fee_items_total = round( (float) $fee_items_total, 2 );
$fee_tax_total = round( (float) $fee_tax_total, 2 );
if ( 0.0 !== $fee_items_total || 0.0 !== $fee_tax_total ) {
return true;
}
$breakdown = $this->amount()->breakdown();
if ( ! $breakdown ) { if ( ! $breakdown ) {
return false; return false;
} }
$amount_total = 0;
if ( $breakdown->shipping() ) { $item_total = $breakdown->item_total();
$amount_total += $breakdown->shipping()->value(); if ( $item_total ) {
} $remaining_item_total = array_reduce(
if ( $breakdown->item_total() ) { $items,
$amount_total += $breakdown->item_total()->value(); function ( float $total, Item $item ): float {
} return $total - $item->unit_amount()->value() * (float) $item->quantity();
if ( $breakdown->discount() ) { },
$amount_total -= $breakdown->discount()->value(); $item_total->value()
} );
if ( $breakdown->tax_total() ) {
$amount_total += $breakdown->tax_total()->value(); $remaining_item_total = round( $remaining_item_total, 2 );
}
if ( $breakdown->shipping_discount() ) { if ( 0.0 !== $remaining_item_total ) {
$amount_total -= $breakdown->shipping_discount()->value(); return true;
} }
if ( $breakdown->handling() ) {
$amount_total += $breakdown->handling()->value();
}
if ( $breakdown->insurance() ) {
$amount_total += $breakdown->insurance()->value();
} }
$amount_value = $this->amount()->value(); $tax_total = $breakdown->tax_total();
if ( $tax_total ) {
$remaining_tax_total = array_reduce(
$items,
function ( float $total, Item $item ): float {
$tax = $item->tax();
if ( $tax ) {
$total -= $tax->value() * (float) $item->quantity();
}
return $total;
},
$tax_total->value()
);
$remaining_tax_total = round( $remaining_tax_total, 2 );
if ( 0.0 !== $remaining_tax_total ) {
return true;
}
}
$shipping = $breakdown->shipping();
$discount = $breakdown->discount();
$shipping_discount = $breakdown->shipping_discount();
$handling = $breakdown->handling();
$insurance = $breakdown->insurance();
$amount_total = 0.0;
if ( $shipping ) {
$amount_total += $shipping->value();
}
if ( $item_total ) {
$amount_total += $item_total->value();
}
if ( $discount ) {
$amount_total -= $discount->value();
}
if ( $tax_total ) {
$amount_total += $tax_total->value();
}
if ( $shipping_discount ) {
$amount_total -= $shipping_discount->value();
}
if ( $handling ) {
$amount_total += $handling->value();
}
if ( $insurance ) {
$amount_total += $insurance->value();
}
$amount_value = $amount->value();
$needs_to_ditch = (string) $amount_total !== (string) $amount_value; $needs_to_ditch = (string) $amount_total !== (string) $amount_value;
return $needs_to_ditch; return $needs_to_ditch;
} }

View file

@ -159,35 +159,10 @@
</PossiblyNullReference> </PossiblyNullReference>
</file> </file>
<file src="modules/ppcp-api-client/src/Entity/PurchaseUnit.php"> <file src="modules/ppcp-api-client/src/Entity/PurchaseUnit.php">
<InvalidOperand occurrences="9"> <PossiblyNullReference occurrences="3">
<code>$amount_total += $breakdown-&gt;handling()-&gt;value()</code>
<code>$amount_total += $breakdown-&gt;insurance()-&gt;value()</code>
<code>$amount_total += $breakdown-&gt;item_total()-&gt;value()</code>
<code>$amount_total += $breakdown-&gt;shipping()-&gt;value()</code>
<code>$amount_total += $breakdown-&gt;tax_total()-&gt;value()</code>
<code>$amount_total -= $breakdown-&gt;discount()-&gt;value()</code>
<code>$amount_total -= $breakdown-&gt;shipping_discount()-&gt;value()</code>
<code>$item-&gt;tax()-&gt;value() * $item-&gt;quantity()</code>
<code>$item-&gt;unit_amount()-&gt;value() * $item-&gt;quantity()</code>
</InvalidOperand>
<PossiblyNullReference occurrences="17">
<code>item_total</code>
<code>item_total</code>
<code>tax_total</code>
<code>tax_total</code>
<code>to_array</code> <code>to_array</code>
<code>to_array</code> <code>to_array</code>
<code>to_array</code> <code>to_array</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
<code>value</code>
</PossiblyNullReference> </PossiblyNullReference>
</file> </file>
<file src="modules/ppcp-api-client/src/Entity/Refund.php"> <file src="modules/ppcp-api-client/src/Entity/Refund.php">
@ -768,9 +743,6 @@
</PropertyNotSetInConstructor> </PropertyNotSetInConstructor>
</file> </file>
<file src="modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php"> <file src="modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php">
<MissingReturnType occurrences="1">
<code>set_order_transaction_id</code>
</MissingReturnType>
<PossiblyNullReference occurrences="1"> <PossiblyNullReference occurrences="1">
<code>card</code> <code>card</code>
</PossiblyNullReference> </PossiblyNullReference>
@ -928,25 +900,6 @@
<code>$request['resource']['purchase_units']</code> <code>$request['resource']['purchase_units']</code>
</PossiblyNullArrayAccess> </PossiblyNullArrayAccess>
</file> </file>
<file src="modules/ppcp-webhooks/src/Handler/PaymentCaptureCompleted.php">
<InvalidReturnStatement occurrences="4">
<code>rest_ensure_response( $response )</code>
<code>rest_ensure_response( $response )</code>
<code>rest_ensure_response( $response )</code>
<code>rest_ensure_response( $response )</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="1">
<code>\WP_REST_Response</code>
</InvalidReturnType>
<PossiblyNullArgument occurrences="3">
<code>$request['resource']['custom_id']</code>
<code>isset( $request['id'] ) ? $request['id'] : ''</code>
<code>isset( $request['id'] ) ? $request['id'] : ''</code>
</PossiblyNullArgument>
<PossiblyNullArrayAccess occurrences="1">
<code>$request['resource']['custom_id']</code>
</PossiblyNullArrayAccess>
</file>
<file src="modules/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php"> <file src="modules/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php">
<InvalidReturnStatement occurrences="4"> <InvalidReturnStatement occurrences="4">
<code>rest_ensure_response( $response )</code> <code>rest_ensure_response( $response )</code>