@@ -1436,15 +1436,17 @@ public function generate_create_intent_request( $order, $prepared_source ) {
1436
1436
*
1437
1437
* @param WC_Order $order The order that is being paid for.
1438
1438
* @return array The level 3 data to send to Stripe.
1439
+ * @throws WC_Stripe_Exception If an order item has no quantity set.
1439
1440
*/
1440
1441
public function get_level3_data_from_order ( $ order ) {
1441
1442
// Get the order items. Don't need their keys, only their values.
1442
1443
// Order item IDs are used as keys in the original order items array.
1443
1444
$ order_items = array_values ( $ order ->get_items ( [ 'line_item ' , 'fee ' ] ) );
1444
1445
$ currency = $ order ->get_currency ();
1446
+ $ order_id = $ order ->get_id ();
1445
1447
1446
1448
$ stripe_line_items = array_map (
1447
- function ( $ item ) use ( $ currency ) {
1449
+ function ( $ item ) use ( $ currency, $ order_id ) {
1448
1450
if ( is_a ( $ item , 'WC_Order_Item_Product ' ) ) {
1449
1451
$ product_id = $ item ->get_variation_id ()
1450
1452
? $ item ->get_variation_id ()
@@ -1456,9 +1458,14 @@ function ( $item ) use ( $currency ) {
1456
1458
}
1457
1459
$ product_description = substr ( $ item ->get_name (), 0 , 26 );
1458
1460
$ 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 );
1462
1469
1463
1470
return (object ) [
1464
1471
'product_code ' => (string ) $ product_id , // Up to 12 characters that uniquely identify the product.
0 commit comments