Enable vault
Store customers must have the ability to enable and disable credit cards details storing. Magento out-of-the-box provides mechanisms for adding this ability, but your still need to add modifications in your payment method implementation.
These modifications are the following:
- Adding vault enabling controls.
- Modifying the payment component (updating of the
additional_data
property must be added). - Creating a request data builder.
The following paragraphs describe these points in details.
Add vault enabling controls
Add the vault enabling controls to the payment form. In the following example, a check box bound to the Vault enabler is added.
Example (Magento/Braintree/view/frontend/web/template/payment/form.html):
Modifying the payment component
The payment component must process the state of the vault-enabling control and update payment additional_data
before it is sent to the backend.
Magento has a default vault enabler UI component (Magento_Vault/js/view/payment/vault-enabler
). In the payment component, you just need to call its visitAdditionalData
to update the additional_data
property. The rest is done by the \Magento\Vault\Observer\VaultEnableAssigner
observer.
Example: the Braintree payment UI component
Add request data builder
Now when we have information about enabling or disabling vault, the payment must send it to the payment processor. This is done in the request builder.
You can create a new request builder, or update the existing request builder of the payment method.
In the Braintree request builder, to pass the data, we set storeInVaultOnSuccess
in transaction request:
class VaultDataBuilder implements BuilderInterface
{
/**
* Additional options in request to gateway
*/
const OPTIONS = 'options';
/**
* The option that determines whether the payment method associated with
* the successful transaction should be stored in the Vault.
*/
const STORE_IN_VAULT_ON_SUCCESS = 'storeInVaultOnSuccess';
/**
* @inheritdoc
*/
public function build(array $buildSubject)
{
return [
self::OPTIONS => [
self::STORE_IN_VAULT_ON_SUCCESS => true
]
];
}
}
The builder must be added to the payment authorize request in the DI configuration.
Example from the Braintree di.xml
: