I have a custom plugin set up to speak to Stripe's API and process a card charge. The code triggers in a craft\elements\Entry::EVENT_AFTER_SAVE event because I want to pass along certain data about the entry like its ID to Stripe's metadata parameter. I figure if the charge fails, it would be fine to have the entry already saved, return the user back to the form with Stripe's error message, and let the user try again, saving the entry again after they correct their card info and resubmit.

I'm unsure how to send this information back to my template with entry and the error message so I can inform the user instead of passively log an error.

Here's an abbreviated example of my charge method:

public function charge($data, $savedEntry)
    // set Stripe api key

    $settings = [
        // settings filled with $data

    $charge = null;

    // charge the card
    try {
        $charge = Stripe\Charge::create($settings);
    } catch (Stripe\Error\Card $e) {
        $body = $e->getJsonBody();
        $error  = $body['error'];

        // since there's an error, return user to form with $savedEntry and $error['message']
        // ...

    return $charge;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.