We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Hi there,

    Has anyone set up a simple stripe integration?
    I have three products to sell on a charity web site, so i want to be able to take payments online.
    I've got s stripe account, but I'm struggling with the docs.
    I
    s there a simple way to integrate using a simple Formit call, or do I need an eCommerce solution such as simpleCart?

    It's only three products, so I don't want to crack a nut with a sledgehammer unless I can help it!


    Thanks for any advice.

    Andy
      • 17301
      • 932 Posts
      I haven't touched it personally but I know that Graeme from Gel Studios has mentioned having some exisiting code for this functionality on the MODX facebook group at one point. It may worth posting your question on the facebook group as I don't think he checks the forums.
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 51020
        • 670 Posts
        Quote from: lkfranklin at Dec 21, 2018, 02:22 PM
        I haven't touched it personally but I know that Graeme from Gel Studios has mentioned having some exisiting code for this functionality on the MODX facebook group at one point. It may worth posting your question on the facebook group as I don't think he checks the forums.

        Great thanks - will do!
          • 17301
          • 932 Posts
          Posting the response here for future use.


          <?php
          // require the autoload
          require MODX_CORE_PATH . 'components/stripe/vendor/autoload.php';
          
          $total = $total * 100; //Stripe takes payment in pence
          
          \Stripe\Stripe::setApiKey('PRIVATE KEY HERE');
          
          try {
              $charge = \Stripe\Charge::create([
                  'currency'      => 'gbp',
                  'amount'        => $total, // this is in pence: ie £20 2000
                  'card'          => $hook->getValue('stripeToken'), //From your form
                  'description'   => 'DESCRIPTION',
                  'receipt_email' => 'CUSTOMER EMAIL',
              ]);
          } catch(\Stripe\Error\Card $e) {
              // The card has been declined
               $modx->log(modX::LOG_LEVEL_ERROR, $e);
              //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
              return false;
          } catch(\Stripe\Error\Base $e) {
               $modx->log(modX::LOG_LEVEL_ERROR, $e);
              //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
              return false;
          } catch(Exception $e) {
               $modx->log(modX::LOG_LEVEL_ERROR, $e);
              //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
              return false;
          }
          
          // If we have gotten this far, then we have successfully placed a transaction, and our $charge variable contains information.
          print_r($charge); //Contains everything you need in here
          
          return true;


          Add in this library to core/components under stripe.

          https://github.com/stripe/stripe-php

          Add to your page this

          <script src="https://js.stripe.com/v2/"></script>
          <script>Stripe.setPublishableKey('STRIPE_PUBLISH_KEY');</script>


          This will then do the creation of your token, and submission of the form (which then runs the snippet

          	$(function () {
          		var $form = $('#payment-form');
          		var btn_text;
          
          		$form.submit(function (event) {
          			btn_text = $form.find('.button button').html();
          			$form.find('.button button').prop('disabled', true).html('Processing payment...');
          			Stripe.card.createToken($form, stripeResponseHandler);
          			return false;
          		});
          
          		function stripeResponseHandler(status, response) {
          			var $form = $('#payment-form');
          
          			if (response.error) { 
          				$form.find('.payment-errors').html('<p>' + response.error.message + '</p>');
          				$form.find('.button button').prop('disabled', false).html(btn_text); 
          
          			} else { 
          
          				var token = response.id;
          
          				$form.append($('<input type="hidden" name="stripeToken">').val(token));
          
          				$form.get(0).submit();
          			}
          		};
          	});
            ■ email: [email protected] | ■ website: https://alienbuild.uk

            The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
            • 51020
            • 670 Posts
            Quote from: lkfranklin at Dec 22, 2018, 10:22 AM
            Posting the response here for future use.


            <!--?php
            // require the autoload
            require MODX_CORE_PATH . 'components/stripe/vendor/autoload.php';
            
            $total = $total * 100; //Stripe takes payment in pence
            
            \Stripe\Stripe::setApiKey('PRIVATE KEY HERE');
            
            try {
                $charge = \Stripe\Charge::create([
                    'currency'      =--> 'gbp',
                    'amount'        => $total, // this is in pence: ie £20 2000
                    'card'          => $hook->getValue('stripeToken'), //From your form
                    'description'   => 'DESCRIPTION',
                    'receipt_email' => 'CUSTOMER EMAIL',
                ]);
            } catch(\Stripe\Error\Card $e) {
                // The card has been declined
                 $modx->log(modX::LOG_LEVEL_ERROR, $e);
                //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
                return false;
            } catch(\Stripe\Error\Base $e) {
                 $modx->log(modX::LOG_LEVEL_ERROR, $e);
                //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
                return false;
            } catch(Exception $e) {
                 $modx->log(modX::LOG_LEVEL_ERROR, $e);
                //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
                return false;
            }
            
            // If we have gotten this far, then we have successfully placed a transaction, and our $charge variable contains information.
            print_r($charge); //Contains everything you need in here
            
            return true;


            Add in this library to core/components under stripe.

            https://github.com/stripe/stripe-php

            Add to your page this

            <script src="https://js.stripe.com/v2/"></script>
            <script>Stripe.setPublishableKey('STRIPE_PUBLISH_KEY');</script>


            This will then do the creation of your token, and submission of the form (which then runs the snippet

            	$(function () {
            		var $form = $('#payment-form');
            		var btn_text;
            
            		$form.submit(function (event) {
            			btn_text = $form.find('.button button').html();
            			$form.find('.button button').prop('disabled', true).html('Processing payment...');
            			Stripe.card.createToken($form, stripeResponseHandler);
            			return false;
            		});
            
            		function stripeResponseHandler(status, response) {
            			var $form = $('#payment-form');
            
            			if (response.error) { 
            				$form.find('.payment-errors').html('<p>' + response.error.message + '</p>');
            				$form.find('.button button').prop('disabled', false).html(btn_text); 
            
            			} else { 
            
            				var token = response.id;
            
            				$form.append($('<input type="hidden" name="stripeToken">').val(token));
            
            				$form.get(0).submit();
            			}
            		};
            	});


            Great idea thanks.
            I’m a little confused as to what goes where. I get the first part but then it says

            This will then do the creation of your token, and submission of the form (which then runs the snippet

            Then there is more code afterwards. Not sure what to do with it

              • 51020
              • 670 Posts
              I have tried to piece this together, but I am totally lost.

              I downloaded and added in this library to core/components under stripe.
              https://github.com/stripe/stripe-php

              I added this code to my page:
              <script src="https://js.stripe.com/v2/"></script>
              <script>Stripe.setPublishableKey('STRIPE_PUBLISH_KEY');</script>
              


              But after that I'm lost.

              I don't know what to do with this snippet code:

              <!--?php
              // require the autoload
              require MODX_CORE_PATH . 'components/stripe/vendor/autoload.php';
               
              $total = $total * 100; //Stripe takes payment in pence
               
              \Stripe\Stripe::setApiKey('PRIVATE KEY HERE');
               
              try {
                  $charge = \Stripe\Charge::create([
                      'currency'      =--> 'gbp',
                      'amount'        => $total, // this is in pence: ie £20 2000
                      'card'          => $hook->getValue('stripeToken'), //From your form
                      'description'   => 'DESCRIPTION',
                      'receipt_email' => 'CUSTOMER EMAIL',
                  ]);
              } catch(\Stripe\Error\Card $e) {
                  // The card has been declined
                   $modx->log(modX::LOG_LEVEL_ERROR, $e);
                  //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
                  return false;
              } catch(\Stripe\Error\Base $e) {
                   $modx->log(modX::LOG_LEVEL_ERROR, $e);
                  //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
                  return false;
              } catch(Exception $e) {
                   $modx->log(modX::LOG_LEVEL_ERROR, $e);
                  //$hook->addError('stripe', 'Unfortunately your card was declined. Please double check your details, or contact your card issuer.');
                  return false;
              }
               
              // If we have gotten this far, then we have successfully placed a transaction, and our $charge variable contains information.
              print_r($charge); //Contains everything you need in here
               
              return true;
              


              Then I don't know what to do with this JS code:

              $(function () {
                  var $form = $('#payment-form');
                  var btn_text;
               
                  $form.submit(function (event) {
                      btn_text = $form.find('.button button').html();
                      $form.find('.button button').prop('disabled', true).html('Processing payment...');
                      Stripe.card.createToken($form, stripeResponseHandler);
                      return false;
                  });
               
                  function stripeResponseHandler(status, response) {
                      var $form = $('#payment-form');
               
                      if (response.error) { 
                          $form.find('.payment-errors').html('<p>' + response.error.message + '</p>');
                          $form.find('.button button').prop('disabled', false).html(btn_text); 
               
                      } else { 
               
                          var token = response.id;
               
                          $form.append($('<input type="hidden" name="stripeToken">').val(token));
               
                          $form.get(0).submit();
                      }
                  };
              });
              


              Finally I don't understand how this all links together with my form?

              Sorry - but this is a little over my head, so need more of a walk through! I've tried various things, but I just cannot get anywhere at all, as I'm not understanding the fundamental process I need to go through.

              Thanks to anyone who can help me!

              Andy
                • 46886
                • 1,154 Posts
                I think the snippet is to be used as a posthook to the form?
                  • 51020
                  • 670 Posts

                  Quote from: nuan88 at Jan 09, 2019, 03:23 PM
                  I think the snippet is to be used as a posthook to the form?

                  Ok thanks - but not sure how to format the form and everything else.
                    • 46886
                    • 1,154 Posts
                    We would have to build in the tpl to the code, after return I suppose, with Modx php can define the tpl precisely and send exactly the data we want, (then of course we can style it) but snippets also can just sort of dump the data out and, I believe, a tpl with the right code can 'catch' the data as well.
                      • 46886
                      • 1,154 Posts
                      what output do you want? most of it is already defined, but the success output is most important of course