<![CDATA[ Stripe/Formit integration - MODX Community Forums]]> https://forums.modx.com/thread/?thread=104750 <![CDATA[Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563345
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]]>
tm2000 Dec 21, 2018, 02:14 PM https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563345
<![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563528
Try to build in error warnings so we can see what works and what doesn't.

We can also play with pre and post hooks, the js could/should be a prehook...and we might want to goad the snippet to fire as a post-hook, but these are fairly minor issues as far as structure goes.

I do think if you can pass the data to stripe it could well be ok.]]>
nuan88 Jan 11, 2019, 11:17 AM https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563528
<![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563525 Quote from: nuan88 at Jan 11, 2019, 01:08 AM
Ok here's how it should work:

Create your form.

Save the php code as a snippet, I believe the name should be stripeResponseHandler, as included in the js (I think , a little unsure about this part)

The js will be needed on that page, I believe its ok to put it down at the bottom of the template for that page, usually people put the js in the footer, it gets rendered when the page loads.

The js apparently will run the snippet, so my earlier idea of putting the snippet as a posthook may not be correct.

Its also possible we would want the js to run as a pre-hook, but I've never really seen js used that way, bottom of the page should work (it will load and then be ready for the order event)

Also get that script in there, in your

I assume that the stripe will take over and probably move the buyer to stripe for confirmation, possibly not, but I sort of expect the output to be there automatically. So let's see if we can get output from it.

Why not set it up and let's see where we are, if we can narrow this down to a few specific issues others can help.

Hi there - than you so much for you time on this.
Yes I was confused about whether or not there was a post/pre hook, which is why I didn't understand the process.

I will put all the elements together as you suggested, and see where we go from there!

Thanks again!
Andy]]>
tm2000 Jan 11, 2019, 08:51 AM https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563525
<![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563521
Create your form.

Save the php code as a snippet, I believe the name should be stripeResponseHandler, as included in the js (I think , a little unsure about this part)

The js will be needed on that page, I believe its ok to put it down at the bottom of the template for that page, usually people put the js in the footer, it gets rendered when the page loads.

The js apparently will run the snippet, so my earlier idea of putting the snippet as a posthook may not be correct.

Its also possible we would want the js to run as a pre-hook, but I've never really seen js used that way, bottom of the page should work (it will load and then be ready for the order event)

Also get that script in there, in your <head>

I assume that the stripe will take over and probably move the buyer to stripe for confirmation, possibly not, but I sort of expect the output to be there automatically. So let's see if we can get output from it.

Why not set it up and let's see where we are, if we can narrow this down to a few specific issues others can help.]]>
nuan88 Jan 11, 2019, 01:08 AM https://forums.modx.com/thread/104750/stripe-formit-integration?page=2#dis-post-563521
<![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563510 Quote from: nuan88 at Jan 10, 2019, 03:22 AM
what output do you want? most of it is already defined, but the success output is most important of course

Thanks - sorry for being vague.

So I have three products to sell all on one page.
Two of those will have size options (but they don't affect the price).
So potentially, someone could buy 2 x product A, 1 x product B, and 1 x product C.
I can obviously create a simple form using formit for the products/select menus etc etc, but I just don't know how to integrate this so that it adds up the total, and passes that to stripe.
The customer will then get a confirmation email, and I will get a product request email so I can organise the shipping etc.
It's for a local charity, so I can't really afford to invest in other software, or pay for a developer to put it all together.

Thanks for any help you can give!

Andy]]>
tm2000 Jan 10, 2019, 09:03 AM https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563510
<![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563509 nuan88 Jan 10, 2019, 03:22 AM https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563509 <![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563508 nuan88 Jan 10, 2019, 03:21 AM https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563508 <![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563503 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.
]]>
tm2000 Jan 09, 2019, 09:29 PM https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563503
<![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563494 nuan88 Jan 09, 2019, 03:23 PM https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563494 <![CDATA[Re: Stripe/Formit integration]]> https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563492
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]]>
tm2000 Jan 09, 2019, 01:50 PM https://forums.modx.com/thread/104750/stripe-formit-integration#dis-post-563492