Jump to content

Recommended Posts

I'm currently working on a school project and I'm using Stripe as a payment method for my 'webshop'.
To create a PaymentIntent you need an Array where you put the product information (Price, amount, etc.).
This wasn't a problem first but now I'm trying to create a pdf (using FPDF) with the same products that I put in the Array.
Currently I was thinking of creating a seperate file where I create the Array and than Include it in the files that require it.
It worked perfectly when I loaded the exact page but once I included it and loaded a different page the Array was empty.
Is there something I'm looking over or a better way to do it, it would be very helpfull to lmk.
 

<?php
    require_once '../../vendor/autoload.php';
    include '../header.php';

    $products = \Stripe\Product::all();

    $producten = array();

    foreach ($products->data as $product) {
            $price = \Stripe\Price::retrieve($product->default_price);
            $amount = isset($_POST['amount'][$product->id]) ? $_POST['amount'][$product->id] : 0;
            echo $product. '<br>';
            if ($amount > 0) {
                $producten[] = array(
                    'price' => $price->id,
                    'quantity' => $amount
                );
            }
        }
    
    var_dump($producten);

    if (isset($_POST['submit'])) {
        Header ('Location: payment.php');
    }



?>
$customer = \Stripe\Customer::retrieve($_SESSION['stripeId']);
    // echo $customer->email;

    $orders = \Stripe\PaymentIntent::all(['customer' => $_SESSION['stripeId']]);
    // var_dump($orders);
    
    echo '<form method="post" action="betaling.php">';
    foreach ($products->data as $product) {
        if ($product->active) {
        $price = \Stripe\Price::retrieve($product->default_price);
        echo '
            <div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden md:max-w-2xl m-4">
                <div class="md:flex">
                    <div class="md:shrink-0">
                        <img class="h-48 w-full object-cover md:h-full md:w-48" src="../../assets/spaghetti.jpg">
                    </div>
                    <div class="p-8">
                        <div class="uppercase tracking-wide text-sm text-sky-900 font-semibold">'.$product->name.'</div>
                        <p class="mt-2 text-lg font-medium">'.$product->description.'</p>
                        <p class="mt-2">€'.$price->unit_amount / 100 .'</p>
                        <div class="flex items-center">
                            <label for="amount_'.$product->id.'" class="block text-gray-700 text-sm font-bold pr-4 ">Aantal: </label>
                            <input max="20" min="0" type="number" value="0" name="amount['.$product->id.']" id="amount_'.$product->id.'" class="shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight mt-2">
                            <input type="hidden" name="id" value="'.$product->id.'">
                        </div>
                    </div>
                </div>
            </div>';
        }
    };
    echo '<div class="flex items-center"><button name="afrekenen" type="submit" class="mx-auto bg-sky-800 hover:bg-blue-900 text-white font-bold py-2 px-4 rounded">Afrekenen</button></div>';
    echo '</form>';   

- Lowie

Link to comment
https://forums.phpfreaks.com/topic/316218-php-array-with-stripe-information/
Share on other sites

Why not use a db table instead of an array?  That way a quick query gives you your data and a small chunk of code can then build that array in any script you are running.  Make it all part of a separate module and just include it and call it.

  • 1 month later...

It seems like you're experiencing an issue where the array containing product information is not accessible or empty when including it in different files. This could be due to the scope of the variables or the order in which the files are included.

To address this, you can consider the following approaches:

1. Use sessions: Store the product information in a session variable instead of an included file. You can set the session variable in one file and access it in other files where you need the product information. Make sure you start the session at the beginning of each file using `session_start()`. For example:

In the file where you create the array:
```php
session_start();
$_SESSION['product_info'] = $productInfoArray;
```

In the file where you generate the PDF or need the product information:
```php
session_start();
$productInfoArray = $_SESSION['product_info'];
```

2. Use a configuration file: Instead of including a file that creates the array, you can define a configuration file where you store the product information as constants or variables. Include this configuration file in the files where you need the product information. For example:

In the configuration file (`config.php`):
```php
$productInfoArray = [
  // Product information here
];
```

In the file where you generate the PDF or need the product information:
```php
include 'config.php';
// Use $productInfoArray here
```

3. Pass the array as a function parameter: Instead of including a separate file, you can create a function that returns the product information array and call that function wherever you need the information. For example:

In a common file (`functions.php`):
```php
function getProductInfo() {
  return [
    // Product information here
  ];
}
```

In the file where you generate the PDF or need the product information:
```php
include 'functions.php';
$productInfoArray = getProductInfo();
```

By using one of these approaches, you can ensure that the product information array is accessible across different files in your project. Choose the approach that best fits your project structure and requirements.

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.