SimpleCheckoutForm.php in Stripe 2.x
File
modules/stripe_examples/src/Form/SimpleCheckoutForm.php
View source
<?php
namespace Drupal\stripe_examples\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Stripe\Charge;
use Stripe\Error\Base as StripeBaseException;
class SimpleCheckoutForm extends FormBase {
public function getFormId() {
return 'stripe_examples_simple_checkout';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$link_generator = \Drupal::service('link_generator');
$form['#theme'] = 'stripe-examples-simple-checkout';
$form['button'] = [
'#type' => 'stripe_paymentrequest',
'#title' => $this
->t('Pay with a button'),
'#description' => $this
->t('You can use test card numbers and tokens from @link.', [
'@link' => $link_generator
->generate('stripe docs', Url::fromUri('https://stripe.com/docs/testing')),
]),
'#stripe_paymentintent_unique' => TRUE,
];
$form['first'] = [
'#type' => 'textfield',
'#title' => $this
->t('First name'),
'#description' => $this
->t('Anything other than "John" would give a validation error to test different scenarios.'),
];
$form['last'] = [
'#type' => 'textfield',
'#title' => $this
->t('Last name'),
];
$form['card'] = [
'#type' => 'stripe',
'#title' => $this
->t('Credit card'),
'#description' => $this
->t('You can use test card numbers and tokens from @link.', [
'@link' => $link_generator
->generate('stripe docs', Url::fromUri('https://stripe.com/docs/testing')),
]),
'#stripe_paymentintent_unique' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
$form['submit']['#value'] = $this
->t('Pay $25');
$form['#attached']['library'][] = 'stripe_examples/stripe_examples';
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('first') != 'John') {
$form_state
->setError($form['first'], $this
->t('"John" is the only valid first name on this example.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValues() as $key => $value) {
if ($key == 'card') {
$this
->messenger()
->addStatus('card/payment_intent: ' . $value['payment_intent'] ?? '');
continue;
}
if ($key == 'button') {
$this
->messenger()
->addStatus('button/payment_intent: ' . $value['payment_intent'] ?? '');
continue;
}
$this
->messenger()
->addStatus($key . ': ' . $value);
}
$config = \Drupal::config('stripe.settings');
$apikeySecret = $config
->get('apikey.' . $config
->get('environment') . '.secret');
$stripe = new \Stripe\StripeClient($apikeySecret);
}
}