JReviews logo
Guides My site
← All guides Community guide

Sell listings with Stripe Checkout

M Alejandro
Thread started by Miguel B.. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Let a guest buy a listing as a product, with optional shipping, on Stripe Checkout.

My site

Platform
JReviews version

On the handmade listing Cedar Mug, the owner turns on Shop now, sets price 24.00 and shipping Small parcel. A guest sees Shop now, pays the product and shipping on Stripe Checkout, and returns to a success page. There is no cart.

Shop now is a radiobutton field the listing owner uses to offer that listing for sale. Stripe Checkout is Stripe's hosted payment page. The guest pays on Stripe and does not enter a card on the listing page.

This checkout charges the guest for the handmade item. PaidListings charges a listing owner to publish or feature a listing. For that add-on, see Paid listing fees.

The payment goes to the Stripe account whose secret key you paste into the controller. This example does not split the payment with the listing owner.

You need a Stripe account, an HTTPS site, and file access to the site's JReviews overrides folder. Stripe Checkout rejects HTTP pages. Create files only in overrides. Do not change JReviews core files. Start each new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;.

The Shop now link sends only the listing id. The controller reads the saved price and shipping from the listing. Do not put amounts in the link. A guest could change a query-string amount.

Prepare the listing type and category

  1. In Setup → Listing Types Manager, create a listing type named Handmade, or open the type you already use for handmade goods.
  2. Open the Access tab. Grant Submit New Listing to the user group that owns handmade listings.
  3. Open the Standard Fields tab. Set Summary and Description to Optional. Set custom fields and photos to optional so the first test listing can save with title, Shop now, price, and shipping.
  4. In Setup → Categories Manager, create Mugs and assign Handmade, or open the category you already use.

Joomla. Create a JReviews → List Pages menu item. Set Type of Menu to Category List, select Mugs, and publish it. Open Cedar Mug later from that category page or from Browse → Listings.

Create the Shop fields

In Setup → Field Groups Manager, create a listing field group named Shop, or reuse an existing listing group. In Setup → Custom Fields Manager, create these fields with Location set to Listing and Field Group set to Shop.

Label Field name Type Who fills it Example on Cedar Mug
Shop now jr_shopnow Radio buttons Listing owner Sell this listing
Price jr_price Decimal number Listing owner 24.00
Shipping jr_shipping Single select list Listing owner Small parcel

Enter shopnow, price, and shipping in Name. JReviews adds the jr_ prefix. In Setup → Listing Types Manager, associate Shop with Handmade. Keep all three fields active, optional, publicly readable, and writable by listing owners, with no field or group dependencies. Include them in applicable PaidListings plans if you use that add-on for publishing. The owner fills the values when creating or editing a listing. The customization does not fill them automatically.

For jr_shopnow, add two predefined options. The displayed label is what the owner sees. The saved value is what the code reads.

Displayed label Saved value
Sell this listing sell
Do not sell hide

In the field editor, turn Show title off for Shop now. Turn Visible in detail page off so guests see the Shop now button, not Sell this listing. Keep the field on the listing form.

For jr_price, enter the amount without a currency symbol or thousands separator. Set Number of decimals to 2, leave Currency format off, and set Output Format to €{fieldtext} if you want a euro sign on the listing. The input 24.00 then displays as €24.00. Checkout still uses the numeric value. This example charges euros. Change the output symbol when you change the checkout currency.

For jr_shipping, add these predefined options. The displayed label is what the owner selects. The saved value is what the code reads. Do not put the euro amounts in the labels. The costs live in the PHP files below.

Displayed label Saved value Cost in PHP
Free free 0
Light light 2.95
Small parcel small 4.95
Medium medium 6.95
Large large 8.95

If Shipping is empty, or the saved value is not one of those five keys, the Shop now button is hidden and checkout is refused.

See Custom Fields for field types and options.

Save the example listings

  1. Create a Handmade listing titled Cedar Mug in Mugs.
  2. Set Shop now to Sell this listing, Price to 24.00, and Shipping to Small parcel.
  3. Publish Cedar Mug.
  4. Create a second listing titled Oak Bowl in Mugs.
  5. Set Shop now to Do not sell. Publish Oak Bowl.
  6. In Browse → Listings, confirm both listings are published. Note the ID for Cedar Mug and the ID for Oak Bowl. You will use those ids when you check checkout.

The public Shop now link appears after you add the section or banner below. On the listing form, Cedar Mug must keep Sell this listing, 24.00, and Small parcel. Oak Bowl must keep Do not sell.

Get a Stripe secret key

  1. Open the Stripe API keys page while signed in to Stripe.
  2. Copy a Secret key. A test key starts with sk_test_.
  3. Keep that key off public pages. Anyone who reads the controller file can charge that Stripe account.

Paste the key into the controller in the next section, in place of sk_test_YOURKEY. Keep the key in the overrides controller. Do not put it in a theme file or a CMS config file.

JReviews 6 Early Access

Open the JReviews dashboard, select Manage Early Access, and turn on Listing Detail Page.

Create the checkout controller

Joomla. Create templates/jreviews_overrides/app/Http/Controllers/StripeListingCheckoutController.php.

WordPress. Create jreviews_overrides/app/Http/Controllers/StripeListingCheckoutController.php.

Save this complete file. Replace sk_test_YOURKEY with your secret key. To charge a different currency, replace eur with another lowercase ISO code such as usd, and change the price Output Format symbol to match.

<?php

namespace JReviewsOverrides\App\Http\Controllers;

defined('MVC_FRAMEWORK') or die;

use FWD\Illuminate\Http\Request;
use FWD\Illuminate\Support\Facades\Http;
use JReviews\App\Models\Listing;

class StripeListingCheckoutController
{
	protected $stripeSecret = 'sk_test_YOURKEY';

	protected $currency = 'eur';

	protected $shippingCosts = [
		'free' => 0,
		'light' => 2.95,
		'small' => 4.95,
		'medium' => 6.95,
		'large' => 8.95,
	];

	public function index(Request $request)
	{
		$listing = $this->sellableListing($request->integer('id'));

		if (! $listing) {
			return fwd_response('<p>This listing is not for sale.</p>', 404);
		}

		$price = (float) $listing->getFieldValue('jr_price', 0);
		$shippingKey = (string) $listing->getFieldValue('jr_shipping', '');
		$shipping = $this->shippingCosts[$shippingKey];

		$lineItems = [
			[
				'quantity' => 1,
				'price_data' => [
					'currency' => $this->currency,
					'unit_amount' => (int) round($price * 100),
					'product_data' => [
						'name' => $listing->title,
					],
				],
			],
		];

		if ($shipping > 0) {
			$lineItems[] = [
				'quantity' => 1,
				'price_data' => [
					'currency' => $this->currency,
					'unit_amount' => (int) round($shipping * 100),
					'product_data' => [
						'name' => 'Shipping',
					],
				],
			];
		}

		$response = Http::withToken($this->stripeSecret)
			->asForm()
			->timeout(15)
			->post('https://api.stripe.com/v1/checkout/sessions', [
				'mode' => 'payment',
				'success_url' => $this->checkoutUrl('success'),
				'cancel_url' => $this->checkoutUrl('cancel'),
				'payment_method_types' => ['card'],
				'line_items' => $lineItems,
				'metadata' => [
					'listing_id' => (string) $listing->id,
					'owner_id' => (string) $listing->owner_id,
				],
			]);

		$checkoutUrl = $response->json('url');

		if ($response->failed() || ! is_string($checkoutUrl) || $checkoutUrl === '') {
			return fwd_response('<p>Checkout could not start.</p>', 502);
		}

		return fwd_redirect($checkoutUrl);
	}

	public function success()
	{
		return fwd_response('<p>Payment complete.</p>');
	}

	public function cancel()
	{
		return fwd_response('<p>Payment cancelled. No charge was made.</p>');
	}

	protected function sellableListing(int $id)
	{
		if ($id < 1) {
			return null;
		}

		$listing = Listing::query()
			->with(['listing_type.listing_field_groups.fields', 'field_data'])
			->published()
			->find($id);

		if (! $listing) {
			return null;
		}

		$shop = (string) $listing->getFieldValue('jr_shopnow', '');
		$price = (float) $listing->getFieldValue('jr_price', 0);
		$shipping = (string) $listing->getFieldValue('jr_shipping', '');

		if ($shop !== 'sell' || $price <= 0 || ! array_key_exists($shipping, $this->shippingCosts)) {
			return null;
		}

		return $listing;
	}

	protected function checkoutUrl(string $action): string
	{
		$path = jr_wordpress()
			? 'index.php?plugin=jreviews&url=stripe/checkout/'.$action
			: 'index.php?option=com_jreviews&url=stripe/checkout/'.$action;

		return WWW_ROOT.$path;
	}
}

Keep the $shippingCosts array in this file identical to the copy in the Shop now section below.

Register the route

Joomla. Create templates/jreviews_overrides/routes/web.php.

WordPress. Create jreviews_overrides/routes/web.php.

If routes/web.php does not exist, save this complete file.

<?php

defined('MVC_FRAMEWORK') or die;

$this->app['router']->get('/stripe/checkout', [\JReviewsOverrides\App\Http\Controllers\StripeListingCheckoutController::class, 'index']);
$this->app['router']->get('/stripe/checkout/success', [\JReviewsOverrides\App\Http\Controllers\StripeListingCheckoutController::class, 'success']);
$this->app['router']->get('/stripe/checkout/cancel', [\JReviewsOverrides\App\Http\Controllers\StripeListingCheckoutController::class, 'cancel']);

If routes/web.php already exists, add the marked lines at the end of the file. The copy button copies only those lines.

<?php
defined('MVC_FRAMEWORK') or die;

$this->app['router']->get('/stripe/checkout', [\JReviewsOverrides\App\Http\Controllers\StripeListingCheckoutController::class, 'index']);
$this->app['router']->get('/stripe/checkout/success', [\JReviewsOverrides\App\Http\Controllers\StripeListingCheckoutController::class, 'success']);
$this->app['router']->get('/stripe/checkout/cancel', [\JReviewsOverrides\App\Http\Controllers\StripeListingCheckoutController::class, 'cancel']);

Keep overrides enabled in JReviews configuration. After you save both files, run Clear Cache & File Registry.

Point Shop now at the site URL

The Shop now link uses the listing id. You do not paste these URLs into Stripe. After you add the section, copy the link on Cedar Mug and confirm it matches this pattern.

Joomla. Checkout starts at:

https://www.example.com/index.php?option=com_jreviews&url=stripe/checkout&id=LISTING_ID

Success returns to:

https://www.example.com/index.php?option=com_jreviews&url=stripe/checkout/success

Cancel returns to:

https://www.example.com/index.php?option=com_jreviews&url=stripe/checkout/cancel

Replace www.example.com with your site host. The controller builds these return URLs with WWW_ROOT. You do not paste them into Stripe.

WordPress. Checkout starts at:

https://www.example.com/index.php?plugin=jreviews&url=stripe/checkout&id=LISTING_ID

Success returns to:

https://www.example.com/index.php?plugin=jreviews&url=stripe/checkout/success

Cancel returns to:

https://www.example.com/index.php?plugin=jreviews&url=stripe/checkout/cancel

Replace www.example.com with your site host. The controller builds these return URLs with WWW_ROOT. You do not paste them into Stripe.

Add the Shop now section

The filter adds a Shop now section on the listing detail page. Empty content is skipped, so Oak Bowl gets no section.

Joomla. Use templates/jreviews_overrides/hooks.php.

WordPress. Use jreviews_overrides/hooks.php.

Add the callback marked with +.

  • If hooks.php already exists, add the marked lines at the end of the file. The copy button copies only those lines.
  • If you are creating hooks.php, type the two dimmed lines first, then add the marked lines below them.
<?php
defined('MVC_FRAMEWORK') or die;

fwd_add_filter('jreviews:listing.detail.sections', function ($sections, $listing) {
	$sections['shop-now'] = [
		'label' => 'Shop now',
		'content' => function ($listing) {
			$costs = [
				'free' => 0,
				'light' => 2.95,
				'small' => 4.95,
				'medium' => 6.95,
				'large' => 8.95,
			];
			$shop = (string) $listing->getFieldValue('jr_shopnow', '');
			$price = (float) $listing->getFieldValue('jr_price', 0);
			$shipping = (string) $listing->getFieldValue('jr_shipping', '');

			if ($shop !== 'sell' || $price <= 0 || ! array_key_exists($shipping, $costs)) {
				return '';
			}

			$id = (int) $listing->id;
			$path = jr_wordpress()
				? 'index.php?plugin=jreviews&url=stripe/checkout&id='.$id
				: 'index.php?option=com_jreviews&url=stripe/checkout&id='.$id;
			$url = WWW_ROOT.$path;

			return '<p><a href="'.htmlspecialchars($url, ENT_QUOTES, 'UTF-8').'">Shop now</a></p>';
		},
	];

	return $sections;
}, 10, 2);

Run Clear Cache & File Registry so JReviews loads the new callback.

Without Early Access

Create the checkout controller

Joomla. Create templates/jreviews_overrides/controllers/stripe_listing_checkout_controller.php.

WordPress. Create jreviews_overrides/controllers/stripe_listing_checkout_controller.php.

Save this complete file. Replace sk_test_YOURKEY with your secret key. To charge a different currency, replace eur with another lowercase ISO code such as usd, and change the price Output Format symbol to match.

<?php

defined('MVC_FRAMEWORK') or die;

use FWD\Illuminate\Support\Facades\Http;
use JReviews\App\Models\Listing;

class StripeListingCheckoutController extends MyController
{
	var $autoRender = false;

	var $autoLayout = false;

	protected $stripeSecret = 'sk_test_YOURKEY';

	protected $currency = 'eur';

	protected $shippingCosts = [
		'free' => 0,
		'light' => 2.95,
		'small' => 4.95,
		'medium' => 6.95,
		'large' => 8.95,
	];

	public function index()
	{
		$listing = $this->sellableListing();

		if (! $listing) {
			return $this->response->html()->not_found('This listing is not for sale.');
		}

		$price = (float) $listing->getFieldValue('jr_price', 0);
		$shippingKey = (string) $listing->getFieldValue('jr_shipping', '');
		$shipping = $this->shippingCosts[$shippingKey];

		$lineItems = [
			[
				'quantity' => 1,
				'price_data' => [
					'currency' => $this->currency,
					'unit_amount' => (int) round($price * 100),
					'product_data' => [
						'name' => $listing->title,
					],
				],
			],
		];

		if ($shipping > 0) {
			$lineItems[] = [
				'quantity' => 1,
				'price_data' => [
					'currency' => $this->currency,
					'unit_amount' => (int) round($shipping * 100),
					'product_data' => [
						'name' => 'Shipping',
					],
				],
			];
		}

		$response = Http::withToken($this->stripeSecret)
			->asForm()
			->timeout(15)
			->post('https://api.stripe.com/v1/checkout/sessions', [
				'mode' => 'payment',
				'success_url' => $this->checkoutUrl('success'),
				'cancel_url' => $this->checkoutUrl('cancel'),
				'payment_method_types' => ['card'],
				'line_items' => $lineItems,
				'metadata' => [
					'listing_id' => (string) $listing->id,
					'owner_id' => (string) $listing->owner_id,
				],
			]);

		$checkoutUrl = $response->json('url');

		if ($response->failed() || ! is_string($checkoutUrl) || $checkoutUrl === '') {
			return $this->response->html()->status(502)->fail('Checkout could not start.');
		}

		cmsFramework::redirect($checkoutUrl);
	}

	public function success()
	{
		return $this->response->html()->msg('Payment complete.')->success();
	}

	public function cancel()
	{
		return $this->response->html()->msg('Payment cancelled. No charge was made.')->success();
	}

	protected function sellableListing()
	{
		$id = Sanitize::getInt($this->params, 'id');

		if ($id < 1) {
			return null;
		}

		$listing = Listing::query()
			->with(['listing_type.listing_field_groups.fields', 'field_data'])
			->published()
			->find($id);

		if (! $listing) {
			return null;
		}

		$shop = (string) $listing->getFieldValue('jr_shopnow', '');
		$price = (float) $listing->getFieldValue('jr_price', 0);
		$shipping = (string) $listing->getFieldValue('jr_shipping', '');

		if ($shop !== 'sell' || $price <= 0 || ! array_key_exists($shipping, $this->shippingCosts)) {
			return null;
		}

		return $listing;
	}

	protected function checkoutUrl($action)
	{
		$path = jr_wordpress()
			? 'index.php?plugin=jreviews&url=stripe_listing_checkout/'.$action
			: 'index.php?option=com_jreviews&url=stripe_listing_checkout/'.$action;

		return WWW_ROOT.$path;
	}
}

Keep the $shippingCosts array in this file identical to the copy in the banner file below. After you create the controller, run Clear Cache & File Registry. See File Overrides.

Point Shop now at the site URL

The Shop now link uses the listing id. You do not paste these URLs into Stripe. After you add the banner, copy the link on Cedar Mug and confirm it matches this pattern.

Joomla. Checkout starts at:

https://www.example.com/index.php?option=com_jreviews&url=stripe_listing_checkout&id=LISTING_ID

Success returns to:

https://www.example.com/index.php?option=com_jreviews&url=stripe_listing_checkout/success

Cancel returns to:

https://www.example.com/index.php?option=com_jreviews&url=stripe_listing_checkout/cancel

Replace www.example.com with your site host. The controller builds these return URLs with WWW_ROOT. You do not paste them into Stripe.

WordPress. Checkout starts at:

https://www.example.com/index.php?plugin=jreviews&url=stripe_listing_checkout&id=LISTING_ID

Success returns to:

https://www.example.com/index.php?plugin=jreviews&url=stripe_listing_checkout/success

Cancel returns to:

https://www.example.com/index.php?plugin=jreviews&url=stripe_listing_checkout/cancel

Replace www.example.com with your site host. The controller builds these return URLs with WWW_ROOT. You do not paste them into Stripe.

Add a Shop now banner

Create a listing custom field labeled Shop CTA, named jr_shopcta, with type Banner (no user input). Enter shopcta in Name. Put it in the Shop field group. Keep it active, optional, and publicly readable. Turn Show title off. Leave the description empty. Turn Visible in detail page on. In PHP output format, put this expression so the banner stays hidden until the hook fills it.

return $text ?: false;

Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.

WordPress. Use jreviews_overrides/filters/filter_functions.php.

If filter_functions.php does not exist, save this complete file.

<?php

defined('MVC_FRAMEWORK') or die;

Clickfwd\Hook\Filter::add('post_get_listing_detailpage_query', function ($listing, $params) {
	if (! isset($listing['Field']['pairs']['jr_shopcta'])) {
		return $listing;
	}

	$costs = [
		'free' => 0,
		'light' => 2.95,
		'small' => 4.95,
		'medium' => 6.95,
		'large' => 8.95,
	];
	$shop = trim((string) ($listing['Field']['pairs']['jr_shopnow']['value'][0] ?? ''), '*');
	$price = (float) ($listing['Field']['pairs']['jr_price']['value'][0] ?? 0);
	$shipping = trim((string) ($listing['Field']['pairs']['jr_shipping']['value'][0] ?? ''), '*');

	if ($shop !== 'sell' || $price <= 0 || ! array_key_exists($shipping, $costs)) {
		$listing['Field']['pairs']['jr_shopcta']['description'] = '';

		return $listing;
	}

	$id = (int) ($listing['Listing']['listing_id'] ?? 0);
	$path = jr_wordpress()
		? 'index.php?plugin=jreviews&url=stripe_listing_checkout&id='.$id
		: 'index.php?option=com_jreviews&url=stripe_listing_checkout&id='.$id;
	$url = WWW_ROOT.$path;
	$listing['Field']['pairs']['jr_shopcta']['description'] = '<p><a href="'.htmlspecialchars($url, ENT_QUOTES, 'UTF-8').'">Shop now</a></p>';

	return $listing;
});

If filter_functions.php already exists, add the marked lines at the end of the file. The copy button copies only those lines.

<?php
defined('MVC_FRAMEWORK') or die;

Clickfwd\Hook\Filter::add('post_get_listing_detailpage_query', function ($listing, $params) {
	if (! isset($listing['Field']['pairs']['jr_shopcta'])) {
		return $listing;
	}

	$costs = [
		'free' => 0,
		'light' => 2.95,
		'small' => 4.95,
		'medium' => 6.95,
		'large' => 8.95,
	];
	$shop = trim((string) ($listing['Field']['pairs']['jr_shopnow']['value'][0] ?? ''), '*');
	$price = (float) ($listing['Field']['pairs']['jr_price']['value'][0] ?? 0);
	$shipping = trim((string) ($listing['Field']['pairs']['jr_shipping']['value'][0] ?? ''), '*');

	if ($shop !== 'sell' || $price <= 0 || ! array_key_exists($shipping, $costs)) {
		$listing['Field']['pairs']['jr_shopcta']['description'] = '';

		return $listing;
	}

	$id = (int) ($listing['Listing']['listing_id'] ?? 0);
	$path = jr_wordpress()
		? 'index.php?plugin=jreviews&url=stripe_listing_checkout&id='.$id
		: 'index.php?option=com_jreviews&url=stripe_listing_checkout&id='.$id;
	$url = WWW_ROOT.$path;
	$listing['Field']['pairs']['jr_shopcta']['description'] = '<p><a href="'.htmlspecialchars($url, ENT_QUOTES, 'UTF-8').'">Shop now</a></p>';

	return $listing;
});

Run Clear Cache & File Registry so JReviews loads the new callback.

Pay for Cedar Mug

  1. While signed in, open Browse → Listings. Confirm the ID values you noted for Cedar Mug and Oak Bowl.
  2. Click Cedar Mug to open it on the site. Confirm a Shop now link is visible.
  3. Right-click Shop now and copy the link. It must include id= and the Cedar Mug id. It must not include 24.00 or small.
  4. Open Oak Bowl. Confirm Shop now is absent.
  5. Open a logged-out browser. As a guest, click Shop now on Cedar Mug. Stripe Checkout must show Cedar Mug at 24.00 and Shipping at 4.95, in euros.
  6. Pay with a Stripe test card, for example 4242424242424242, any future expiry, and any three-digit CVC.
  7. After payment, the site must show Payment complete.
  8. Open Cedar Mug again, click Shop now, then cancel on Stripe. The site must show Payment cancelled. No charge was made.
  9. Copy the Shop now URL. Replace the id with the Oak Bowl id, and open that URL. The site must show This listing is not for sale.

If Stripe Checkout never opens, confirm the secret key, HTTPS, and Clear Cache & File Registry. If the amounts on Stripe are wrong, change Price or Shipping on Cedar Mug and click Shop now again. The new amounts must come from the listing, not from the link.

Learn more