/** * Get form fields for manipulating the current order, * according to the responsibilty 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([BraintreeHostedField::create('number', _t('BraintreePaymentCheckoutComponent.CardNumber', 'Debit/Credit Card Number')), BraintreeHostedField::create('cvv', _t('BraintreePaymentCheckoutComponent.CVV', 'Security Code')), BraintreeHostedField::create('expirationDate', _t('BraintreePaymentCheckoutComponent.ExpirationDate', 'Expiration Date (MM/YYYY)'))]); if ($this->config()->use_placeholders) { foreach ($fields as $field) { if ($field->Title()) { $field->setAttribute('placeholder', $field->Title())->setTitle(null); } } } $this->extend('updateFormFields', $fields); // Generate a basic config and allow it to be customised $config = ['id' => $form ? $form->getHTMLID() : 'PaymentForm_OrderForm', 'hostedFields' => $this->getFieldConfig($fields)]; $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}', 'custom', {$rawConfig});", 'BrainTreeJS'); return $fields; }
public function testRequestsClientToken() { $this->sut->getFormFields($this->cart); $js = Requirements::backend()->get_custom_scripts(); $this->assertContains('abc123', $js); }