We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36573
    • 173 Posts
    I'm attempting to use Stripe checkout in modx with a custom Formit hook. I am able to use Stripe's popup checkout and it sends the information to stripe. I then return to a must submit registration button that submits an entry form but first charge the card. This is my snippet.

    require_once $modx->getOption('assets_path').'/lib/Stripe.php';
        \Stripe::setApiKey("sk_test_Cxxxxxxxxxxxxxxxxxxxxxx");
    $modx->log(xPDO::LOG_LEVEL_ERROR,'Set API.');
    
    // get the credit card details submitted by the form
    $amount = $_POST['total'];
    $token = $_POST['stripeToken'];
    $test = 100;
     $modx->log(xPDO::LOG_LEVEL_ERROR, ($token));
     $modx->log(xPDO::LOG_LEVEL_ERROR, ($amount * 100));
     $modx->log(xPDO::LOG_LEVEL_ERROR, ($test));
    
    try {
    $modx->log(xPDO::LOG_LEVEL_ERROR,'try.');
    
    $charge = \Stripe\Charge::create(array(
      "amount" => ($amount * 100), // amount in cents, again
      "currency" => "usd",
      "source" => $token,
      "description" => "Vertical Runner")
    );
    $modx->log(xPDO::LOG_LEVEL_ERROR,'array.');
    } catch(\Stripe\Error\Card $e) {
      // The card has been declined
    }


    $modx->log(xPDO::LOG_LEVEL_ERROR,'Testing my custom hook.');
    return true;</code>

    The screen freezes on white after submit, and the form is never submitted. The error codes all show up till "Try"

    I have tried the form without this snippet and it works.

    I'm willing to charge the card as part of the pop-up checkout, but I'm unsure on how to do that.
      Everything I know I learned on the internet. Saved me thousands in College tuition,
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      What do your PHP error logs show? Usually a white screen means a fatal PHP error that never even let MODX run at all.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        This may not be your problem, but this line will give you '//' in the path, since assets_path already ends in a slash:

        require_once $modx->getOption('assets_path').'/lib/Stripe.php';


        It should be:

        require_once $modx->getOption('assets_path').'lib/Stripe.php';


        You should have some test of whether the Stripe class loaded OK.

        Also, you should be logging something to the MODX error log in your catch statement.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 36573
          • 173 Posts
          My blurry holiday eyes took 5 minutes to see the difference between the two. LOL. How do I check if the Stripe class loaded? [ed. note: crecorn last edited this post 11 years, 4 months ago.]
            Everything I know I learned on the internet. Saved me thousands in College tuition,
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I put a line at the beginning of the Stripe file - echo "in stripe"; but that's pretty crude. The best thing to do is take the time to set up the Stripe demo.

            Take it one step at a time, get the basic Stripe demo working stand-alone. Then get it working in a MODX resource as a snippet. Then work it into your application.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 36573
              • 173 Posts
              Quote from: sottwell at May 26, 2015, 01:14 PM
              I put a line at the beginning - echo "in stripe"; but that's pretty crude. The best thing to do is take the time to set up the Stripe demo.

              Maybe I'm misunderstanding. I put that at the beginning of the snippet. When I test I get a white screen except for it saying "in Stripe".
                Everything I know I learned on the internet. Saved me thousands in College tuition,
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                I put it at the beginning of the Strip file I want to include. If I wanted to make sure the snippet was running, I'd put echo "in snippet"; Like I said, it's pretty crude, but it lets me know if the file actually got included.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 14951
                  • 151 Posts
                  @crecorn You have two of MODX's greatest community members and assets contributing to this thread so I hesitate to think I could possibly contribute anything beyond their vast knowledge...but I just so happen to have a working example of what you are trying to accomplish.

                  I'm not 100% certain, but quickly glancing at your code it seems to be missing a return which I "believe" is required in a formit hook. In any case, here is a working example. As you can see, I do things slightly different such as including the stripe options as system settings as well as storing the stripe library in the site's core directory. Anyhow, take a look and see if this helps... and questions, let me know!

                  <?php
                  $mode = $modx->getOption('stripe.test_mode',$scriptProperties,1);
                  if ((bool)$mode === true) { $secretkey = $modx->getOption('stripe.test_secret',$scriptProperties,'nokeyset'); } 
                  else { $secretkey = $modx->getOption('stripe.live_secret',$scriptProperties,'nokeyset'); }
                  if ($secretkey == 'nokeyset') { $modx->log(modX::LOG_LEVEL_ERROR,'Please set Stripe API Keys in System Settings'); return false; }
                  
                  require_once ($modx->getOption('core_path').'components/stripe/lib/Stripe.php');
                  
                  Stripe::setApiKey($secretkey);
                  
                  	// Get the credit card details submitted by the form
                  	$token = $hook->getValue('stripeToken');
                  	$email = $hook->getValue('stripeEmail');
                  	$name = $hook->getValue('stripeShippingName');
                  	$plan = $hook->getValue('plan');
                  	
                  	$na = explode(' ', $name, 2);
                  	$hook->setValue('firstname',$na[0]);
                  	$hook->setValue('lastname',$na[1]);
                  	
                  	/* Charge Away */
                  	try {
                  		$customer = Stripe_Customer::create(array(
                  			"card" => $token,
                  			"plan" => $plan,
                  			"email" => $email)
                  		);
                  		
                  		$response = json_decode($customer);
                  
                  		$hook->setValue('custId',$response->id);
                  		
                  		return true;
                  		
                  	} catch (Stripe_CardError $e) {
                  		$hook->addError('error_message',$e->getMessage());
                  		return false;
                  	}
                    • 49499
                    • 48 Posts
                    @crecorn Did you ever get this figured out? I am having the same issue...
                      • 36573
                      • 173 Posts
                      Quote from: patrickappelman at Jun 10, 2015, 05:36 AM
                      @crecorn Did you ever get this figured out? I am having the same issue...

                      Finally got frustrated enough that I'm paying a guy in Russia off elance to make it work. When it's done I'll post the answer. Should have it this week. I'm also going to try to make an extra for it.
                        Everything I know I learned on the internet. Saved me thousands in College tuition,