/**
* Get form fields for manipulating the current order,
* according to the responsibility of this component.
*
* @param Order $order
* @param Form $form
*
* @return FieldList
*/
public function getFormFields(Order $order, Form $form = null)
{
$gateway = $this->getGateway($order);
if (!$this->isBraintree) {
return parent::getFormFields($order);
}
// Generate the token for the javascript to use
$clientToken = $gateway->clientToken()->send()->getToken();
// Generate the standard set of fields and allow it to be customised
$fields = FieldList::create([LiteralField::create('BraintreePlaceholder', '<div id="braintree-ui"></div>'), HiddenField::create('payment_method_nonce', '', '')->setAttribute('data-braintree', 'nonce')]);
$this->extend('updateFormFields', $fields);
// Generate a basic config and allow it to be customised
$config = ['id' => $form ? $form->getHTMLID() : 'PaymentForm_OrderForm', 'container' => 'braintree-ui'];
$this->extend('updateBraintreeConfig', $config);
$rawConfig = json_encode($config);
$rawConfig = $this->injectCallbacks($rawConfig);
$this->extend('updateRawBraintreeConfig', $rawConfig);
// Finally, add the javascript to the page
Requirements::javascript('https://js.braintreegateway.com/js/braintree-2.20.0.min.js');
Requirements::customScript("braintree.setup('{$clientToken}', 'dropin', {$rawConfig});", 'BraintreeJS');
return $fields;
}