Jump to content

Loop through line items (Stripe) pulled from database


Go to solution Solved by Psycho,

Recommended Posts

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

[ ] 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)

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.

  • Solution

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);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.