Repository navigation
PRE-3606: Show the Scalapay installment widget #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Scalapay widget | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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). | ||
| 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'))); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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.