I needed a simple, fast, responsive, self-hosted commerce solution to sell a featured product plus a few accessories. The existing commerce projects I found didn't fit my requirements, so I created my own. Rather than rely on a database backend, all relevant settings (shop, product, shipping and inventory) are instead managed with JSON files. Stripe is used as the payment processor. The shop is still operational, which now serves as a live demo.
After successfully running this side business for several years, I decided to sunset the physical product and open source the software project. This repo is the end result. It's worked more-or-less flawlessly for me and done all that I've asked of it.
Begin by cloning this repository, then install the PHP and JavaScript dependencies by using their associated package managers.
Run Composer to install the PHP dependencies listed in composer.json
:
composer install
This will install the required PHP packages and generate a composer.lock
file.
Either npm
or yarn
can be used to install JavaScript dependencies in package.json
.
If using npm
execute:
npm install
Alternatively, if using Yarn execute:
yarn install
Either of these methods will install the required JavaScript packages and generate a package-lock.json
or yarn.lock
file.
In the project root, create a file called .env
and add the following contents:
# Stripe keys
STRIPE_SECRET_KEY=my-stripe-secret-key
# Stripe checkout session vars
DOMAIN=http://localhost:8888
Replace my-stripe-secret-key
with your Stripe test or live key. Substitute http://localhost:8888
for your local (or live) shop URL.
Products, shipping rates and shop settings are defined in JSON files located in /assets
, as shown below:
βββ assets/
β βββ json/
β βββ products.json
β βββ shipping_rates.json
β βββ shop.json
All relevant details for products you wish to sell are defined in products.json
. I've retained the original products as examples to help you get started. The product detail fields should be mostly self-explanatory. To prevent PHP errors, be sure to only modify the value in JSON's attribute-value pairs:
"id": "3",
"category": "accessory",
"status": "new",
"inventory": 0,
"name": "Upcycled pilot's bag",
...
In the above example, the attribute is "category" and the corresponding value is "accessory". The same applies to the attribute-value pairs in shipping_rates.json
and shop.json
. These may of course be modified or extended by changing the associated PHP code.
Stripe's Node.js library for the Stripe API is used to handle payment processing. At checkout time, create-session.php
creates a Stripe object with \Stripe\Stripe::setApiKey($_ENV['STRIPE_SECRET_KEY'])
. The basket items are enumerated and a checkout session is created with shipping options, payment methods and a success or failure callback URL. For full details see Stripe's checkout session documentation.
I used the PM2 daemon process manager to keep Stripe's Node.js backend online 24/7. Over many years of operation it never missed a beat.
Naturally, other payment processing solutions are available but I found Stripe fairly easy to integrate.
My commerce site ran reliably on a Linode 1GB Nanode shared server and required almost zero maintenance, apart from periodically updating shop settings and inventories. Your hosting needs may differ.