We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49499
    • 48 Posts
    @crecorn Hey I figured it out this morning! It turns out I was using syntax from the old API and including the incorrect file.

    Here's a quote from an article I found referenced on Stripe's website:
    In 2015, Stripe updated its PHP library to version 2, which requires PHP 5.3 or greater. It also assumes you’re using Composer (which you really ought to be using). Version 2 of the library also uses namespaces, so its syntax is quite different.

    And here is the article. I recommend you read his entire series.
    http://www.larryullman.com/2013/01/09/writing-the-php-code-to-process-payments-with-stripe/

    Rather than including the "Stripe.php" file, include the "init.php" file found in the root of the "lib" folder in the new Stripe PHP API library.
    require_once('path/to/stripe-php/init.php');


    Here is an excerpt from the code that I have gotten to work so far. I hope this helps point you in the right direction!

    // create the charge on Stripe's servers - this will charge the user's card
    try {
    
    	// Include the Stripe library:
    	require_once ($modx->getOption('core_path').'components/stripe/init.php');
    
    	// set your secret key: remember to change this to your live secret key in production
    	// see your keys here https://manage.stripe.com/account
    	\Stripe\Stripe::setApiKey($secret_key);
    
    	// Charge the order:
    	$charge = \Stripe\Charge::create(array(
    		"amount" => $amount, // amount in cents, again
    		"currency" => "usd",
    		"source" => $token,
    		"description" => "Description of transaction."
    	));
    
    	// Check that it was paid:
    	if ($charge->paid == true) {
    
    		// Store the order in the database.
    		// Send the email.
    		// Celebrate!
    		return true;
    	} else { // Charge was not paid!
    		return false;
    	}
    
    } catch (\Stripe\Error\Card $e) {
    	// Card was declined.
    	$e_json = $e->getJsonBody();
    	$err = $e_json['error'];
    	$errors['stripe'] = $err['message'];
    } catch (\Stripe\Error\ApiConnection $e) {
    	// Network problem, perhaps try again.
    } catch (\Stripe\Error\InvalidRequest $e) {
    	// You screwed up in your programming. Shouldn't happen!
    } catch (\Stripe\Error\Api $e) {
    	// Stripe's servers are down!
    } catch (\Stripe\Error\Base $e) {
    	// Something else that's not the customer's fault.
    }
    
      • 48795
      • 7 Posts
      Awesome code, patrickappelman. Very timely for me. Thanks!