mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Merge pull request #345 from woocommerce/pcp-352-fix-rounding-check
Fix/refactor rounding check
This commit is contained in:
commit
e640eae060
2 changed files with 69 additions and 92 deletions
|
@ -303,55 +303,79 @@ class PurchaseUnit {
|
|||
* @return bool
|
||||
*/
|
||||
private function ditch_items_when_mismatch( Amount $amount, Item ...$items ): bool {
|
||||
$fee_items_total = ( $amount->breakdown() && $amount->breakdown()->item_total() ) ?
|
||||
$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();
|
||||
$breakdown = $amount->breakdown();
|
||||
if ( ! $breakdown ) {
|
||||
return false;
|
||||
}
|
||||
$amount_total = 0;
|
||||
if ( $breakdown->shipping() ) {
|
||||
$amount_total += $breakdown->shipping()->value();
|
||||
}
|
||||
if ( $breakdown->item_total() ) {
|
||||
$amount_total += $breakdown->item_total()->value();
|
||||
}
|
||||
if ( $breakdown->discount() ) {
|
||||
$amount_total -= $breakdown->discount()->value();
|
||||
}
|
||||
if ( $breakdown->tax_total() ) {
|
||||
$amount_total += $breakdown->tax_total()->value();
|
||||
}
|
||||
if ( $breakdown->shipping_discount() ) {
|
||||
$amount_total -= $breakdown->shipping_discount()->value();
|
||||
}
|
||||
if ( $breakdown->handling() ) {
|
||||
$amount_total += $breakdown->handling()->value();
|
||||
}
|
||||
if ( $breakdown->insurance() ) {
|
||||
$amount_total += $breakdown->insurance()->value();
|
||||
|
||||
$item_total = $breakdown->item_total();
|
||||
if ( $item_total ) {
|
||||
$remaining_item_total = array_reduce(
|
||||
$items,
|
||||
function ( float $total, Item $item ): float {
|
||||
return $total - $item->unit_amount()->value() * (float) $item->quantity();
|
||||
},
|
||||
$item_total->value()
|
||||
);
|
||||
|
||||
$remaining_item_total = round( $remaining_item_total, 2 );
|
||||
|
||||
if ( 0.0 !== $remaining_item_total ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$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;
|
||||
return $needs_to_ditch;
|
||||
}
|
||||
|
|
|
@ -159,35 +159,10 @@
|
|||
</PossiblyNullReference>
|
||||
</file>
|
||||
<file src="modules/ppcp-api-client/src/Entity/PurchaseUnit.php">
|
||||
<InvalidOperand occurrences="9">
|
||||
<code>$amount_total += $breakdown->handling()->value()</code>
|
||||
<code>$amount_total += $breakdown->insurance()->value()</code>
|
||||
<code>$amount_total += $breakdown->item_total()->value()</code>
|
||||
<code>$amount_total += $breakdown->shipping()->value()</code>
|
||||
<code>$amount_total += $breakdown->tax_total()->value()</code>
|
||||
<code>$amount_total -= $breakdown->discount()->value()</code>
|
||||
<code>$amount_total -= $breakdown->shipping_discount()->value()</code>
|
||||
<code>$item->tax()->value() * $item->quantity()</code>
|
||||
<code>$item->unit_amount()->value() * $item->quantity()</code>
|
||||
</InvalidOperand>
|
||||
<PossiblyNullReference occurrences="17">
|
||||
<code>item_total</code>
|
||||
<code>item_total</code>
|
||||
<code>tax_total</code>
|
||||
<code>tax_total</code>
|
||||
<PossiblyNullReference occurrences="3">
|
||||
<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>
|
||||
</file>
|
||||
<file src="modules/ppcp-api-client/src/Entity/Refund.php">
|
||||
|
@ -768,9 +743,6 @@
|
|||
</PropertyNotSetInConstructor>
|
||||
</file>
|
||||
<file src="modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php">
|
||||
<MissingReturnType occurrences="1">
|
||||
<code>set_order_transaction_id</code>
|
||||
</MissingReturnType>
|
||||
<PossiblyNullReference occurrences="1">
|
||||
<code>card</code>
|
||||
</PossiblyNullReference>
|
||||
|
@ -928,25 +900,6 @@
|
|||
<code>$request['resource']['purchase_units']</code>
|
||||
</PossiblyNullArrayAccess>
|
||||
</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">
|
||||
<InvalidReturnStatement occurrences="4">
|
||||
<code>rest_ensure_response( $response )</code>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue