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.
<?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;
<script src="https://js.stripe.com/v2/"></script> <script>Stripe.setPublishableKey('STRIPE_PUBLISH_KEY');</script>
$(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(); } }; });
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(); } }; });
<script src="https://js.stripe.com/v2/"></script> <script>Stripe.setPublishableKey('STRIPE_PUBLISH_KEY');</script>
<!--?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;
$(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(); } }; });
I think the snippet is to be used as a posthook to the form?