Variable Amount with Stripe Checkout (SCK Plugin) and Kirby

I’m using the SCK (Stripe Checkout) plugin by @jordanmerrick to try to accept a payment through my site.

The payment needs to be a variable amount, so I’ve had to customize things a bit to try allow for that. However, I’m stuck now as I can’t get the charge amount to go to Stripe on the server-side.

With variable amounts for Stripe, it has to be done via JS on the client. For my case, I’m setting the amount based on what a user enters in an input field, grabbing the amount, converting it to cents, and then setting it as the Stripe Checkout variable. This all works, and shows correctly on the front-end in the checkout module.

However, to send the actual payment to Stripe, it seems you need to send the amount via PHP (which makes sense) – and in the case of this plugin, it’s sent using sck.charge.php, where s::get('stripeAmount'); is typically a fixed amount set by the Kirby page.

  // sck.charge.php
  $amount = s::get('stripeAmount');
  $charge = \Stripe\Charge::create(array(
      'amount' => $amount,
       ...
  ));

I’ve tried setting the variable via Ajax post, but it doesn’t seem to work.

// sck.php (customized)
<script>
var handler = StripeCheckout.configure({
  key: '<?php echo $pk ?>',
  },
  token: function(token, args) {
    token_triggered = true;
    var finalAmount = parseInt(document.getElementById('payment-amt'));
    var amount = finalAmount.value * 100;
    $.ajax({
        url: '/site/plugins/sck/sck.charge.php',
        type: 'POST',
        data: {tokenid: token.id, tokenAmt: amount},
        dataType: 'json',
        success: function(data) {
              // Success
        },
        error: function(data) {
             // Ajax Error
        }
      });
  }
});
// sck.charge.php (then, trying to pull in amount)
$amount = $_POST['tokenAmt'];
$charge = \Stripe\Charge::create(array(
  'amount' => $amount,
));

I’ve tried several other things too, with no luck.

Am I missing something? Or is there another way to send the variable payment through to Stripe?

Without looking further into this, one problem is that you can’t call the script directly like this. You need a URL you can call, either via a page or a route.

Oh, thanks, @texnixe? Is this because Kirby blocks direct access to php files?

Yes, that’s why (and some more characters)

Thanks, @texnixe. Appreciate the help — will look into routing.