Skip to content

Commit ee7feb8

Browse files
wjrosamalithsen
authored andcommitted
Fix division by zero fatal error (#4368)
* Fix division by zero fatal error * Changelog and readme entries * Logging when the division by zero is avoided * Fix order param * Replacing the default quantity behavior with a new exception
1 parent 2f01e06 commit ee7feb8

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
= 9.5.3 - xxxx-xx-xx =
44
* Fix - Reimplement mapping of Express Checkout state values to align with WooCommerce's expected state formats
5+
* Fix - Adds an exception to be thrown when the order item quantity is zero, during the retrieval of level 3 data from an order.
56
* Tweak - Track charge completed via webhooks in order notes
67
* Fix - Ensure that we migrate payment_request_button_size=medium on upgrade
78

includes/abstracts/abstract-wc-stripe-payment-gateway.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,15 +1436,17 @@ public function generate_create_intent_request( $order, $prepared_source ) {
14361436
*
14371437
* @param WC_Order $order The order that is being paid for.
14381438
* @return array The level 3 data to send to Stripe.
1439+
* @throws WC_Stripe_Exception If an order item has no quantity set.
14391440
*/
14401441
public function get_level3_data_from_order( $order ) {
14411442
// Get the order items. Don't need their keys, only their values.
14421443
// Order item IDs are used as keys in the original order items array.
14431444
$order_items = array_values( $order->get_items( [ 'line_item', 'fee' ] ) );
14441445
$currency = $order->get_currency();
1446+
$order_id = $order->get_id();
14451447

14461448
$stripe_line_items = array_map(
1447-
function ( $item ) use ( $currency ) {
1449+
function ( $item ) use ( $currency, $order_id ) {
14481450
if ( is_a( $item, 'WC_Order_Item_Product' ) ) {
14491451
$product_id = $item->get_variation_id()
14501452
? $item->get_variation_id()
@@ -1456,9 +1458,14 @@ function ( $item ) use ( $currency ) {
14561458
}
14571459
$product_description = substr( $item->get_name(), 0, 26 );
14581460
$quantity = $item->get_quantity();
1459-
$unit_cost = WC_Stripe_Helper::get_stripe_amount( ( $subtotal / $quantity ), $currency );
1460-
$tax_amount = WC_Stripe_Helper::get_stripe_amount( $item->get_total_tax(), $currency );
1461-
$discount_amount = WC_Stripe_Helper::get_stripe_amount( $subtotal - $item->get_total(), $currency );
1461+
if ( ! $quantity ) {
1462+
$error_msg = "Stripe Level 3 data: Order item with ID {$item->get_id()} from order ID {$order_id} has no quantity set.";
1463+
WC_Stripe_Logger::error( $error_msg );
1464+
throw new WC_Stripe_Exception( $error_msg );
1465+
}
1466+
$unit_cost = WC_Stripe_Helper::get_stripe_amount( ( $subtotal / $quantity ), $currency );
1467+
$tax_amount = WC_Stripe_Helper::get_stripe_amount( $item->get_total_tax(), $currency );
1468+
$discount_amount = WC_Stripe_Helper::get_stripe_amount( $subtotal - $item->get_total(), $currency );
14621469

14631470
return (object) [
14641471
'product_code' => (string) $product_id, // Up to 12 characters that uniquely identify the product.

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
113113
= 9.5.3 - xxxx-xx-xx =
114114

115115
* Fix - Reimplement mapping of Express Checkout state values to align with WooCommerce's expected state formats
116+
* Fix - Adds an exception to be thrown when the order item quantity is zero, during the retrieval of level 3 data from an order.
116117
* Tweak - Track charge completed via webhooks in order notes
117118
* Fix - Ensure that we migrate payment_request_button_size=medium on upgrade
118119

0 commit comments

Comments
 (0)