title: Completely stuck with Stripe Checkout
body: <p><strong>The Goal:</strong> There are a few buttons on the screen, like so –</p>
  
  <p>[Product 1 – $20]</p>
  
  <p>[Product 2 – $35]</p>
  
  <p>and so on...</p>
  
  <p>Users can select as many products as they like. In the JavaScript, I have a variable <code>amount</code> that increments by how much ever the product costs when the user selects it. So the flow is like this:</p>
  
  <p>User selects product 1 &amp; 2 --> amount = 20 + 35 = 55</p>
  
  <p>Then, the user can press the Pay with Card button to launch the Stripe Checkout modal. Fill that out, pay, done.</p>
  
  <p><strong>The Problem:</strong> Now, I'm using the custom button version of Checkout, so I can style the button my way and put this variable amount in.  So, that implementation is pretty simple:</p>
  
  <pre><code>       var handler = StripeCheckout.configure({
              key: 'pk_test_XXXXXX',
              image: 'assets/logo-stripe.png',
              locale: 'auto',
              token: function(token, args) {
                  // Use the token to create the charge with a server-side script.
                  // You can access the token ID with `token.id`
                  $.ajax({
                      url: 'php/charge.php',
                      type: 'post',
                      data: {tokenid: token.id, email: token.email, amount: amount},
                      success: function(data) {
                          if (data == 'success') {
                              console.log("Card successfully charged!");
                          }
                          else if (data == 'failure') {
                              console.log("Card error");
                          } else {
                              console.log("Other error");
                          }
                      },
                      error: function(data) {
                          console.log("AJAX error!");
                          console.log(data);
                      }
                  });
              }
          });
  
          $('#pay-button').on('click', function(e) {
              if (amount == 0) {
                  $('#pay-button').addClass('animated shake'); // Animates the button and refuses to open the modal if user didn't select any products
                  $('#pay-button').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', removeAnimation);
              } else {
                  handler.open({
                      name: 'Company, Inc.',
                      description: "Description",
                      amount: amount // Here's the variable
                  });
                  e.preventDefault();
              }
          });
  
          // Close Checkout on page navigation
          $(window).on('popstate', function() {
              handler.close();
          });
  </code></pre>
  
  <p>This is the JavaScript logic. I left out the call to checkout.js, since that's not really of any consequence to this question.</p>
  
  <p>From a front end perspective, this is peachy. Now comes tokenization and actually charging the card. Here's where things start having issues. If you look at the token handling, I'm trying to POST email, amount, and token data to charge.php. Based on the fact that I don't get any errors from the console about this, I think that's working.</p>
  
  <p>In charge.php, I have this: <strong>(updated Apr. 20: This is working code)</strong></p>
  
  <pre><code>&lt;?php
    require_once('stripe-php/init.php');
  
    // Set secret key
    \Stripe\Stripe::setApiKey("sk_test_XXXXXXX");
  
    // Get the credit card details submitted by the form
    $token = $_POST['tokenid'];
  
    // Create the charge on Stripe's servers - this will charge the user's card
    try {
      $customer = \Stripe\Customer::create(array(
        'email' =&gt; $_POST['email'],
        'source'  =&gt; $token
      ));
  
      $charge = \Stripe\Charge::create(array(
        "amount" =&gt; $_POST['amount'],
        "currency" =&gt; "usd",
        "customer" =&gt; $customer-&gt;id
        ));
  
      echo "success";
    } catch(\Stripe\Error\Card $e) {
      // The card has been declined
      echo "failure";
    }
  
  ?&gt;
  </code></pre>
  
  <p>Whenever I try to make a purchase, the log gives me "Success Error." I'm loosely following code presented here – <a href="https://stackoverflow.com/questions/22445078/how-to-create-a-stripe-charge-with-a-checkout-token-sent-via-ajax-to-php">How to create a stripe charge with a checkout token sent via AJAX to php</a>. So, I don't know what Success Error is, but my guess is that the data was sent to charge.php fine, but the card wasn't charged. This is supported by the fact that my Stripe dashboard doesn't show any record of payment.</p>
  
  <p><strong>What I have tried:</strong> </p>
  
  <p>1) I thought the error might be my config.php. I'm not using Composer (because I have no idea what that is or how to even start using it), so instead I'm calling init.php from stripe-php. This is an acceptable alternative to Composer, according to Stripe's own documentation.</p>
  
  <p><strike>Here's the config.php:</p>
  
  <pre><code>&lt;?php
    require_once('php/stripe-php/init.php');
    $stripe = array(
      secret_key      =&gt; getenv('sk_test_XXXXXX'),
        publishable_key =&gt; getenv('pk_test_XXXXXX')
    );
    \Stripe\Stripe::setApiKey($stripe['secret_key']);
  ?&gt;
  </code></pre>
  
  <p></strike>
  <strong>No longer using config.php, all combined into charge.php</strong></p>
  
  <p>2) In my POST call, I pass a tokenid but in my charge.php, I use stripeToken. This probably happened because I tried using a Stack Overflow answer and the Stripe docs to craft a complete solution. I thought it was odd that stripeToken isn't being sent from my front-end. So, I tried using tokenid in my charge.php (replace  <code>stripeToken</code> with <code>tokenid</code>. Changing to tokenid didn't work and presented safety concerns (I'd rather stick to the compliant stripeToken, but I don't know how to implement it).</p>
  
  <p>3) I worried about sending the amount variable to charge.php. I didn't want users to change it in their console, but if I don't send it, I don't know how to have variable pricing. I need variable pricing, since users are selecting multiple products with different prices (see: The Goal). So, that's another conundrum for me.</p>
  
  <p>It might be worth noting that I had to test this in production to even get Success Error. In localhost, I get a 500 Internal Server Error. Don't know why, but guessing it has to do with the lack of PHP environment or file structure.</p>
  
  <p>Any help would be greatly appreciated! Thank you.</p>