You are here

private function SimpleCheckoutForm::createCharge in Stripe 8

Helper function for test charge.

Parameters

string $stripe_token: Stripe API token.

int $amount: Amount for charge.

Return value

/Stripe/Charge Charge object.

1 call to SimpleCheckoutForm::createCharge()
SimpleCheckoutForm::submitForm in modules/stripe_examples/src/Form/SimpleCheckoutForm.php
Form submission handler.

File

modules/stripe_examples/src/Form/SimpleCheckoutForm.php, line 113

Class

SimpleCheckoutForm
Class SimpleCheckout.

Namespace

Drupal\stripe_examples\Form

Code

private function createCharge($stripe_token, $amount) {
  try {
    $config = \Drupal::config('stripe.settings');
    Stripe::setApiKey($config
      ->get('apikey.test.secret'));
    $charge = Charge::create([
      'amount' => $amount * 100,
      'currency' => 'usd',
      'description' => "Example charge",
      'source' => $stripe_token,
    ]);
    return $charge;
  } catch (StripeBaseException $e) {
    $this
      ->messenger()
      ->addError($this
      ->t('Stripe error: %error', [
      '%error' => $e
        ->getMessage(),
    ]));
  }
}