Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
countries among those PayPlug allows), with live validation in the admin form
- `payplug:ppro:refresh` console command, to store the PayPlug limits of every PPRO payment method
once after the upgrade
- **Scalapay widget** (Scalapay widget v5, CCD2) on the product page, the cart page and under the
Scalapay payment method at checkout, with the merchant token read from the PayPlug account; off by default, each
page can be turned on and its amount CSS selector adapted to the theme on the Scalapay payment
method page (see [UPGRADE.md](UPGRADE.md))
- Hosted Fields card form: a cardholder name field above the card number (5 to 45 characters, checked
in the browser and on the server), sent to PayPlug as the card's full name
- Hosted Fields card form: the network selector is shown inside the card number field; for a
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ Since 1.11.0, the plugin supports the authorized payment feature. You can check

Bancontact, Wero and Scalapay are shown only for the cart amounts and billing countries PayPlug and the merchant accept. You can check the [PPRO display eligibility documentation](doc/ppro_display_eligibility.md).

## Scalapay widget

The Scalapay installment widget can be shown on the product page, the cart page and at checkout once the merchant turns it on (it is off by default). You can check the [Scalapay widget documentation](doc/scalapay_widget.md).
Comment on lines +224 to +226

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium (same point): this section links doc/scalapay_widget.md; it should link the wiki page once the documentation is moved there.


## Doc
- [Development](doc/development.md)
- [Release Process](RELEASE.md)
53 changes: 53 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,59 @@ live validation of the form never runs (the save is still validated on the serve

Then rebuild the admin assets.

# Scalapay widget (PRE-3606)

The Scalapay widget (v5, the version compliant with the consumer credit rules applying from
20 November 2026) can be shown on the product page, the cart page and under the Scalapay payment
method at checkout, once the merchant turns it on. It needs the Scalapay merchant token of the PayPlug account, which
PayPlug returns with the Scalapay limits and which is stored with them (`payment_method.merchant_token` in the gateway
config).

1. **A Scalapay payment method saved before the upgrade has no token until its limits are refreshed**,
and shows no widget until then. To store it right after the upgrade, run once:

```bash
bin/console payplug:ppro:refresh
```

It is also stored on the events that refresh the PayPlug limits (see the PPRO section above). The
widget is not shown while PayPlug returns no token, and a warning is logged on the `payplug` channel
at each refresh.
2. The widget is **off** on the three pages by default: nothing changes on an existing shop until the
merchant turns a page on. Each page can be turned on, and the CSS selector of the amount the
widget reads can be changed, in the **Scalapay widget** block of the Scalapay
payment method page (gateway config key `scalapay_widget`). The defaults match the Sylius 2
default shop theme; **a custom theme that displays the price elsewhere must set its own
selectors**:

| Page | Default amount selector |
| --- | --- |
| Product | `[data-live-name-value="sylius_shop:product:summary"] .fs-3` |
| Cart | `[data-live-name-value="sylius_shop:cart:summary"] .h5.text-end` |
| Checkout | `#sylius-shop-checkout-summary-order-total` |

3. The version 5 widget script of Scalapay (`templates/shop/scalapay/script.html.twig`) is loaded
once, in the `sylius_shop.base.head` hook, on these three pages only. A shop layout that does not
render the `head` hook of `@SyliusShop/shared/layout/base.html.twig` must load it itself, and a
Content Security Policy must allow the host that script is served from.
4. The widget is rendered with `environment="integration"` when the plugin is connected to a PayPlug
platform other than production (`PAYPLUG_OAUTH_BASE_URL` set to anything but the production
platform URL, which is its default value), in TEST and LIVE mode alike, and without the
attribute on the production platform. A QA setup must set `PAYPLUG_OAUTH_BASE_URL` to the OAuth
URL of the QA platform.

For anyone extending the plugin:

| Added / changed | Notes |
| --- | --- |
| `Ppro\PproConfig::__construct()` | new optional last argument `?string $merchantToken = null`; `toArray()` adds `merchant_token` when it is known |
| `Auth\GatewayConnectionRevoker` | a logout also clears the stored merchant token |
| `Scalapay\ScalapayWidgetResolver`, `Scalapay\ScalapayWidgetSettings`, `Scalapay\ScalapayWidgetEnvironment`, `Twig\ScalapayWidgetExtension` | Twig functions `payplug_scalapay_product_widget()`, `payplug_scalapay_cart_widget()`, `payplug_scalapay_checkout_widget()`, `payplug_scalapay_widget_script_required()` |
| `Gateway\Form\Extension\ScalapayWidgetTypeExtension` | fields `scalapay_widget_<page>` and `scalapay_widget_<page>_amount_selector` on the Scalapay configuration type, admin hook `scalapay_widget` |
| Shop Twig hooks | `sylius_shop.base.head` (`payplug_scalapay_widget_script`), `pay_with_scalapay` on the product summary and cart hooks, `scalapay_widget` on `sylius_shop.shared.form.select_payment.payment.choice.payplug_scalapay` |
| `templates/shared/form/select_payment/payment/choice.html.twig` | renders, for each payment method, the hookables of `sylius_shop.shared.form.select_payment.payment.choice.<factory_name>` together with those of `choice`, ordered by priority (the Scalapay widget, priority -100, comes after the payment card, `details` at 0), and adds `parent_form` to the `form` and `method` context. This is what the Sylius template does from 2.0.8; on Sylius 2.0.0 to 2.0.7 it is an addition |
| Hooks of your own on `sylius_shop.shared.form.select_payment.payment.choice.<factory_name>` | are now rendered by the plugin template, on every supported Sylius version |

# Upgrading from 1.0.0

1. Skip the faulty migration
Expand Down
3 changes: 3 additions & 0 deletions config/twig_hooks/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ sylius_twig_hooks:
'sylius_admin.payment_method.create.content.form.sections.gateway_configuration.payplug_scalapay': &scalapayGateway
live_checkbox: *liveCheckbox
ppro_eligibility: *pproEligibility
scalapay_widget:
template: '@PayPlugSyliusPayPlugPlugin/admin/payment_method/form/scalapay_widget.html.twig'
priority: -20

'sylius_admin.payment_method.create.content.form.sections.gateway_configuration.payplug_wero': &weroGateway
live_checkbox: *liveCheckbox
Expand Down
17 changes: 17 additions & 0 deletions config/twig_hooks/shop.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
sylius_twig_hooks:
hooks:
'sylius_shop.base.head':
payplug_scalapay_widget_script:
template: '@PayPlugSyliusPayPlugPlugin/shop/scalapay/script.html.twig'
priority: 0
'sylius_shop.product.show.content.info.summary':
pay_with_scalapay:
template: '@PayPlugSyliusPayPlugPlugin/shop/scalapay/product/widget.html.twig'
priority: 150
pay_with_oney:
template: '@PayPlugSyliusPayPlugPlugin/shop/oney/product/pay_with_oney.html.twig'
priority: 101
'sylius_shop.cart.index.content.form.sections.general#right':
pay_with_scalapay:
template: '@PayPlugSyliusPayPlugPlugin/shop/scalapay/cart/widget.html.twig'
priority: 50
pay_with_oney:
template: '@PayPlugSyliusPayPlugPlugin/shop/oney/cart/pay_with_oney.html.twig'
priority: 1
Expand Down Expand Up @@ -33,6 +43,13 @@ sylius_twig_hooks:
template: '@PayPlugSyliusPayPlugPlugin/shared/form/select_payment/payment/choice.html.twig'
priority: 0

# After the Scalapay payment card (Sylius 'details', priority 0), outside its <label>: a click
# on the widget must not select the payment method.
'sylius_shop.shared.form.select_payment.payment.choice.payplug_scalapay':
scalapay_widget:
template: '@PayPlugSyliusPayPlugPlugin/shop/scalapay/checkout/widget.html.twig'
priority: -100

'sylius_shop.shared.form.select_payment.payment.choice.details':
choice:
template: '@PayPlugSyliusPayPlugPlugin/shop/select_payment/choice.html.twig'
Expand Down
69 changes: 69 additions & 0 deletions doc/scalapay_widget.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Scalapay widget

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: the Scalapay documentation should live in the GitHub wiki, not in the repo.

Move the operator-facing content of this file (when the widget shows, selectors, environment rules, how to check a page) to a wiki page. Keep in UPGRADE.md only what a merchant must do on upgrade (payplug:ppro:refresh, custom-theme selectors, PAYPLUG_OAUTH_BASE_URL, the BC table). Then drop this file and replace the README section with a wiki link.


The Scalapay widget shows the customer the Scalapay installment plans for the price on the page. It
is served by Scalapay (widget v5) and displayed as provided: the plugin adds no text, style or script
around it.

| Page | Where | Amount read |
| --- | --- | --- |
| Product | Under the price, above the add-to-cart form | Price of the selected variant |
| Cart | Under the cart summary, above the checkout button | Order total |
| Checkout | Right under the Scalapay payment method card, outside it, visible before the method is selected | Order total |

## When it is shown

All the following must hold:

- a Scalapay payment method is enabled on the current channel;
- PayPlug returned a Scalapay merchant token for the account, in the mode (TEST or LIVE) the payment
method is in. The token is stored with the PayPlug limits of the method (see
[PPRO display eligibility](ppro_display_eligibility.md)): nothing is entered by the merchant and
PayPlug is not called when a page is displayed;
- the page is turned on in the **Scalapay widget** block of the Scalapay payment method page (the
widget is off on every page until the merchant turns it on);
- the channel base currency is the euro and prices are displayed in euros;
- the amount is within the Scalapay range of the payment method (PayPlug limits narrowed by the
merchant), and the billing country, when the cart has one, is accepted. The product and cart widgets
follow a variant, quantity or coupon change.

## Amount selectors

The widget reads the amount from the element matching the CSS selector set for each page. The
defaults match the Sylius 2 default shop theme:

| Page | Default |
| --- | --- |
| Product | `[data-live-name-value="sylius_shop:product:summary"] .fs-3` |
| Cart | `[data-live-name-value="sylius_shop:cart:summary"] .h5.text-end` |
| Checkout | `#sylius-shop-checkout-summary-order-total` |

With another theme, set the selector of the element that shows the price, and check it in the
browser console with `document.querySelector('<selector>')`. A selector can use ids, classes,
attributes, combinators, pseudo-classes and selector lists; markup, `{`, `}`, `;`, `\` and `/` are
refused.

## Language and environment

- `locale` is the shop language when Scalapay supports it (`en`, `it`, `fr`, `es`, `de`, `pt`),
English otherwise.
- `amount-separator` is the decimal separator of the prices Sylius prints for the shop locale.
- `environment` follows the PayPlug platform the plugin is connected to, in TEST and LIVE mode alike:
the merchant token PayPlug returns is a Scalapay production token on the PayPlug production
platform and a Scalapay integration token on any other platform. The platform is read from
`%payplug.oauth_base_url%` (environment variable `PAYPLUG_OAUTH_BASE_URL`, whose default value
is the production platform of PayPlug):

| `PAYPLUG_OAUTH_BASE_URL` | `environment` attribute |
| --- | --- |
| not set, or the production platform URL (case and trailing slash ignored) | left out |
| any other URL | `environment="integration"` |

A shop connected to the QA platform of PayPlug must set `PAYPLUG_OAUTH_BASE_URL` to the OAuth URL
of that platform, which the PayPlug login needs anyway; otherwise the widget is rendered for the
Scalapay production environment and finds no configuration for the QA token.

## Checking a page

In the browser inspector, the page has one `<script type="module">` in `<head>` that loads the
version 5 widget script of Scalapay, and a `<scalapay-widget>` element with a non-empty `merchant-token`. When nothing appears, the
widget explains why in the browser console (for example, a selector matching no element).
8 changes: 4 additions & 4 deletions src/Auth/GatewayConnectionRevoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ final class GatewayConnectionRevoker
private const CONNECTION_KEYS = ['live_client', 'test_client', 'account_email'];

/**
* The PayPlug limits stored in a PPRO `payment_method` block, fetched from the account being
* disconnected. The merchant's own narrowing (`configured_amount`, `configured_countries`) is a
* setting, not account data, and is kept.
* The PayPlug limits and Scalapay merchant token stored in a PPRO `payment_method` block, fetched
* from the account being disconnected. The merchant's own narrowing (`configured_amount`,
* `configured_countries`) is a setting, not account data, and is kept.
*/
private const PPRO_ACCOUNT_KEYS = ['name', 'enabled', 'allowed_countries', 'default_amount', 'live', 'refreshed_at'];
private const PPRO_ACCOUNT_KEYS = ['name', 'enabled', 'allowed_countries', 'default_amount', 'live', 'refreshed_at', 'merchant_token'];

public function __construct(
private EntityManagerInterface $entityManager,
Expand Down
97 changes: 97 additions & 0 deletions src/Gateway/Form/Extension/ScalapayWidgetTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace PayPlug\SyliusPayPlugPlugin\Gateway\Form\Extension;

use PayPlug\SyliusPayPlugPlugin\Gateway\Form\Type\ScalapayGatewayConfigurationType;
use PayPlug\SyliusPayPlugPlugin\Scalapay\ScalapayWidgetSettings;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Where the Scalapay widget is shown (product page, cart, checkout) and the CSS selector of the
* amount it reads on each of these pages. The fields write into the `scalapay_widget` block of
* the gateway config through nested property paths.
*/
final class ScalapayWidgetTypeExtension extends AbstractTypeExtension
{
public function __construct(
private TranslatorInterface $translator,
) {
}

/**
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
foreach (ScalapayWidgetSettings::PAGES as $page) {
$builder
->add(self::enabledField($page), CheckboxType::class, [
'label' => 'payplug_sylius_payplug_plugin.ui.scalapay_widget.' . $page,
'required' => false,
'property_path' => sprintf('[%s][%s][enabled]', ScalapayWidgetSettings::CONFIG_KEY, $page),
])
->add(self::amountSelectorField($page), TextType::class, [
'label' => 'payplug_sylius_payplug_plugin.ui.scalapay_widget.amount_selector',
'help' => 'payplug_sylius_payplug_plugin.ui.scalapay_widget.amount_selector_help',
'help_translation_parameters' => ['%default%' => ScalapayWidgetSettings::DEFAULT_AMOUNT_SELECTORS[$page]],
'required' => false,
'property_path' => sprintf('[%s][%s][amount_selector]', ScalapayWidgetSettings::CONFIG_KEY, $page),
])
;
}

$builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event): void {
$config = $event->getData();
$event->setData(ScalapayWidgetSettings::withDefaults(\is_array($config) ? $config : []));
});

$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
foreach (ScalapayWidgetSettings::PAGES as $page) {
$this->validateAmountSelector($event->getForm(), $page);
}
});
}

public static function getExtendedTypes(): iterable
{
return [ScalapayGatewayConfigurationType::class];
}

public static function enabledField(string $page): string
{
return 'scalapay_widget_' . $page;
}

public static function amountSelectorField(string $page): string
{
return 'scalapay_widget_' . $page . '_amount_selector';
}

private function validateAmountSelector(FormInterface $form, string $page): void
{
$field = $form->get(self::amountSelectorField($page));
$selector = $field->getData();

if (null === $selector || '' === $selector) {
if (true === $form->get(self::enabledField($page))->getData()) {
$field->addError(new FormError($this->translator->trans('payplug_sylius_payplug_plugin.scalapay_widget.amount_selector_required', [], 'validators')));
}

return;
}

if (!\is_string($selector) || !ScalapayWidgetSettings::isValidAmountSelector($selector)) {
$field->addError(new FormError($this->translator->trans('payplug_sylius_payplug_plugin.scalapay_widget.invalid_amount_selector', [], 'validators')));
}
}
}
20 changes: 15 additions & 5 deletions src/Ppro/PproConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

/**
* Read model of the `payment_method` block a PPRO gateway config stores: the PayPlug limits
* (`allowed_countries`, `default_amount`) and the merchant's narrowing of them
* (`configured_amount`, `configured_countries`). Amounts are EUR cents. A null country list
* means "no restriction"; a null amount side is not compared.
* (`allowed_countries`, `default_amount`), the merchant's narrowing of them
* (`configured_amount`, `configured_countries`) and, for Scalapay, the `merchant_token` its widget
* is rendered with. Amounts are EUR cents. A null country list means "no restriction"; a null
* amount side is not compared.
*/
final class PproConfig
{
Expand All @@ -29,6 +30,7 @@ public function __construct(
public readonly ?array $configuredCountries,
public readonly ?bool $live = null,
public readonly ?string $refreshedAt = null,
public readonly ?string $merchantToken = null,
) {
}

Expand All @@ -50,7 +52,7 @@ public static function fromArray(mixed $stored): ?self
}

/**
* Combines freshly fetched PayPlug limits ($api) with the merchant settings of $previous: a
* Combines freshly fetched PayPlug data ($api) with the merchant settings of $previous: a
* configured amount is kept but clamped into the new bounds (an empty side keeps following the
* PayPlug limit, a range left empty falls back to the PayPlug limits), and configured countries
* are intersected with the new allowed list (an empty intersection hides the method until the
Expand All @@ -76,6 +78,7 @@ public static function fromApiLimits(self $api, ?self $previous): self
PproSettingsAdjuster::intersectCountries($previous?->configuredCountries, $api->allowedCountries),
$api->live,
$api->refreshedAt,
$api->merchantToken,
);
}

Expand Down Expand Up @@ -159,6 +162,7 @@ public function hasEmptyAmountRange(): bool
* configured_countries: list<string>|null,
* live: bool|null,
* refreshed_at: string|null,
* merchant_token?: string,
* }
*/
public function toArray(): array
Expand All @@ -167,7 +171,7 @@ public function toArray(): array
? null
: ['min' => $this->defaultMin, 'max' => $this->defaultMax];

return [
$stored = [
'name' => $this->name,
'enabled' => $this->enabled,
'allowed_countries' => $this->allowedCountries,
Expand All @@ -177,6 +181,12 @@ public function toArray(): array
'live' => $this->live,
'refreshed_at' => $this->refreshedAt,
];

if (null !== $this->merchantToken) {
$stored['merchant_token'] = $this->merchantToken;
}

return $stored;
}

private function isAmountInRange(int $amount): bool
Expand Down
Loading
Loading