Skip to content

2. Fraud Protection Advanced

Michel Bade edited this page Jul 11, 2025 · 2 revisions

Custom fields allow you to customize your checkout experience by collecting specific information about your customers and their purchases.

You can include custom fields when:

Read more in the Braintree documentation.

Custom fields for transactions (Server Side)

Fill the custom field swag_braintree_app_custom_fields of an order with an json object containing the custom fields for Braintree.

As Plugin

Subscribe to the Shopware\Core\Checkout\Cart\Order\CartConvertedEvent and add the swag_braintree_app_custom_fields to the custom fields of the converted cart:

use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyCartConvertedEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CartConvertedEvent::class => 'addBraintreeCustomFields',
        ];
    }

    public function addBraintreeCustomFields(CartConvertedEvent $event): void
    {
        $orderData = $event->getConvertedCart();
        $orderData['custom_fields'] ??= [];
        $orderData['custom_fields']['swag_braintree_app_custom_fields'] = \json_encode([
            'someField' => 'true',
        ], \JSON_THROW_ON_ERROR);

        $event->setConvertedCart($orderData);
    }
}

Custom Fields for 3D Secure authentication (Client Side)

Via page extension

As App

Custom fields can be added via the checkout-confirm-page-loaded app script hook:

{# Resources/scripts/checkout-confirm-page-loaded/add-custom-fields.twig #}

{# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
{# @var hook.page \Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage #}
{# @var hook.salesChannelContext \Shopware\Core\System\SalesChannel\SalesChannelContext #}

{% do hook.page.addArrayExtension('swag_braintree_app_custom_fields', {
   'someField': 'true',
}) %}
As Plugin

Custom fields can be added via Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent:

use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyCheckoutConfirmPageLoadedEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CheckoutConfirmPageLoadedEvent::class => 'addBraintreeCustomFields',
        ];
    }

    public function addBraintreeCustomFields(CheckoutConfirmPageLoadedEvent $event): void
    {
        $event->getPage()->addArrayExtension('swag_braintree_app_custom_fields', [
            'someField' => 'true',
        ]);
    }
}

Via Twig

{% sw_extends '@SwagBraintreeApp/storefront/braintree/component/checkout/braintree-hosted-fields.html.twig' %}

{% block component_braintree_hosted_fields %}
   {% set pluginOptions = pluginOptions|merge({'customFields': {'someField': 'true'}}) %}

   {% parent %}
{% endblock %}

Via an JS Event

The HTML element containing Braintree's hosted fields will trigger an event before the verifyCard call. This allows you to change or add custom fields:

document.querySelector('.swag-braintree-hosted-fields').addEventListener('braintreeVerifyCardCustomFields', (event) => {
   // options passed to the js plugin
   console.log(event.detail.options);
   // custom fields that will be passed to braintree, could already be prefilled by plugin options
   console.log(event.detail.customFields);

   event.detail.customFields['someField'] = 'true';
});

Clone this wiki locally