View source
<?php
namespace Drupal\commerce_stripe\PluginForm\Stripe;
use Drupal\commerce_payment\PluginForm\PaymentMethodAddForm as BasePaymentMethodAddForm;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\user\UserInterface;
use Stripe\SetupIntent;
class PaymentMethodAddForm extends BasePaymentMethodAddForm implements TrustedCallbackInterface {
public function buildCreditCardForm(array $element, FormStateInterface $form_state) {
$element['#attributes']['class'][] = 'stripe-form';
$plugin = $this->plugin;
$payment_method = $this->entity;
$payment_method_owner = $payment_method
->getOwner();
$client_secret = NULL;
if ($payment_method_owner instanceof UserInterface && $payment_method_owner
->isAuthenticated()) {
$route_match = \Drupal::routeMatch();
if ($route_match
->getRouteName() === 'entity.commerce_payment_method.add_form') {
$setup_intent = SetupIntent::create([
'usage' => 'off_session',
]);
$client_secret = $setup_intent->client_secret;
}
}
$element['#attached']['library'][] = 'commerce_stripe/stripe';
$element['#attached']['library'][] = 'commerce_stripe/form';
$element['#attached']['drupalSettings']['commerceStripe'] = [
'publishableKey' => $plugin
->getPublishableKey(),
'clientSecret' => $client_secret,
];
$element['stripe_payment_method_id'] = [
'#type' => 'hidden',
'#attributes' => [
'id' => 'stripe-payment-method-id',
],
];
$element['card_number'] = [
'#type' => 'item',
'#title' => t('Card number'),
'#required' => TRUE,
'#validated' => TRUE,
'#markup' => '<div id="card-number-element" class="form-text"></div>',
];
$element['expiration'] = [
'#type' => 'item',
'#title' => t('Expiration date'),
'#required' => TRUE,
'#validated' => TRUE,
'#markup' => '<div id="expiration-element"></div>',
];
$element['security_code'] = [
'#type' => 'item',
'#title' => t('CVC'),
'#required' => TRUE,
'#validated' => TRUE,
'#markup' => '<div id="security-code-element"></div>',
];
$element['payment_errors'] = [
'#type' => 'markup',
'#markup' => '<div id="payment-errors"></div>',
'#weight' => -200,
];
$cacheability = new CacheableMetadata();
$cacheability
->addCacheableDependency($this->entity);
$cacheability
->setCacheMaxAge(0);
$cacheability
->applyTo($element);
return $element;
}
protected function validateCreditCardForm(array &$element, FormStateInterface $form_state) {
}
public function submitCreditCardForm(array $element, FormStateInterface $form_state) {
if ($email = $form_state
->getValue([
'contact_information',
'email',
])) {
$email_parents = array_merge($element['#parents'], [
'email',
]);
$form_state
->setValue($email_parents, $email);
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
if (isset($form['billing_information'])) {
$form['billing_information']['#after_build'][] = [
get_class($this),
'addAddressAttributes',
];
}
return $form;
}
public static function addAddressAttributes(array $element, FormStateInterface $form_state) {
if (isset($element['address'])) {
$element['address']['widget'][0]['address']['given_name']['#attributes']['data-stripe'] = 'given_name';
$element['address']['widget'][0]['address']['family_name']['#attributes']['data-stripe'] = 'family_name';
$element['address']['widget'][0]['address']['address_line1']['#attributes']['data-stripe'] = 'address_line1';
$element['address']['widget'][0]['address']['address_line2']['#attributes']['data-stripe'] = 'address_line2';
$element['address']['widget'][0]['address']['locality']['#attributes']['data-stripe'] = 'address_city';
$element['address']['widget'][0]['address']['postal_code']['#attributes']['data-stripe'] = 'address_zip';
$element['address']['widget'][0]['address']['country_code']['#pre_render'][] = [
get_called_class(),
'addCountryCodeAttributes',
];
}
return $element;
}
public static function addCountryCodeAttributes(array $element) {
$element['country_code']['#attributes']['data-stripe'] = 'address_country';
return $element;
}
public static function trustedCallbacks() {
return [
'addCountryCodeAttributes',
];
}
}