tunnelboy Posted October 14, 2022 Share Posted October 14, 2022 Ok, I'm trying to convert from another payment processor to stripe. The call to Stripe goes like this and this demo works: $session = \Stripe\Checkout\Session::create([ "success_url" => "https://www.domain.com/stripesuccess.php?sid={CHECKOUT_SESSION_ID}", "cancel_url" => "https://www.domain.com/cart.php", "mode" => 'payment', "line_items" => [ [ "price_data" =>[ "currency" =>"usd", "product_data" =>[ "name"=> "WIDGETB", "description" => "Blue Widget" ], "unit_amount" => 5000 ], "quantity" => 1 ], [ "price_data" =>[ "currency" =>"usd", "product_data" =>[ "name"=> "WIDGETG", "description" => "Green Widget" ], "unit_amount" => 2000 ], "quantity" => 2 ] ]); The problem is I'm pulling the line items from a DB. How to format these line items in a loop and format it properly? (Newbie here... not even clear whether the above is an object or array or what), but I know I can't just throw a loop in the middle to add my dynamic line items. // Pulling shopping cart from a DB into $row array. Holds cart info. price, qty,sku and description / Mysql fetchAll here foreach ($resultID as $row) { $line_items_array = array( "price" => $row['price'], "quantity" => $row['qty'], "sku" => $row['sku'], "description" = $row['description'] ); } How do I put this array of multiple items into the Stripe code and preserve the formatting in the first example? I know this is a very vague question, but hoping someone will understand what I'm trying to do. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315422-loop-through-line-items-stripe-pulled-from-database/ Share on other sites More sharing options...
requinix Posted October 15, 2022 Share Posted October 15, 2022 [ ] is shorthand for array( ) so the thing at the top is an array. Which is good because arrays are easier to create than objects. You can use that syntax for your own arrays if you want, or if it helps you can edit the sample to use "array("s instead. Look at how the array structure you need is set up: 1. line_items is an array of items 2. Inside the array is another array 3. That inner array has a price_data and a quantity 4. The price_data is an array with a currency, product_data, and unit_amount 5. product_data is yet another array with a name and description To mirror that, 1. Create a $line_items array 2. Put inside it more arrays, which you'll be getting from your database 3. Each item has an array - let's call it $line_item - with price_data and a quantity. You already know the quantity. 4. There's only the one price_data, so you could use a variable or you could put it directly inside your $line_item, and it has currency (presumably always "USD") and unit_amount (the price per item) and the product_data 5. There's also just the one product_data, and it has a name (which looks like a sku) and a description (which is obvious) Quote Link to comment https://forums.phpfreaks.com/topic/315422-loop-through-line-items-stripe-pulled-from-database/#findComment-1601650 Share on other sites More sharing options...
tunnelboy Posted October 18, 2022 Author Share Posted October 18, 2022 Thank you so much for that info! I'm 64 and this all used to come a LOT easier to me. Now my mind is getting muddy with the way things have changed (specifically shorthand or shortcuts and objects). But I refuse to give in and want to keep exercising my brain. Quote Link to comment https://forums.phpfreaks.com/topic/315422-loop-through-line-items-stripe-pulled-from-database/#findComment-1601732 Share on other sites More sharing options...
Solution Psycho Posted October 18, 2022 Solution Share Posted October 18, 2022 Unless there are additional fields in the query result set, you don't have all the corresponding fields shown in the example data. The example data has a "name" and "description". Your data has a description, but no name. You could use the SKU in place of the name. Also, I assume "unit_amount" is the price. Here is some (untested) code which should do what you need //Create the line items array $lineItems = array(); foreach ($resultID as $row) { $lineItems[] = array( "price_data" => array( "currency" =>"usd", "product_data" => array( "name"=> $row['sku'], "description" => $row['description'] ), "unit_amount" => $row['price'] ), "quantity" => $row['qty'] ) ); //Create full payment array $paymentArray = array( "success_url" => "https://www.domain.com/stripesuccess.php?sid={CHECKOUT_SESSION_ID}", "cancel_url" => "https://www.domain.com/cart.php", "mode" => 'payment', "line_items" => $lineItems ); //Set session for payment call $session = \Stripe\Checkout\Session::create($paymentArray); Quote Link to comment https://forums.phpfreaks.com/topic/315422-loop-through-line-items-stripe-pulled-from-database/#findComment-1601736 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.