7

What do I have to change in my code to immigrate from legacy stripe checkout to the new checkout?? I am confused with their wording. And most examples I find are old (2015-1016...and are the "old way") Stripe wants me to upgrade to new checkout because of SCA

This is my working stripe checkout, I have a button that opens the checkout box

<script>
var handler = StripeCheckout.configure({
  key: '<? echo $stripe_p_key;?>',
  image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
  locale: 'auto',
  token: function(token) {
    var $form = $('#f2');
    var token = token.id;
    showloader('loaderid');
        $form.prepend($('<input type="hidden" style="display:none" name="stripeToken">').val(token));
        $form.prepend($('<input type="hidden" style="display:none" name="cc_currency">').val('<? echo $dialog_waehrung_kreditkarte;?>'));
      $form.get(0).submit();  

  }
});

document.getElementById('customButton').addEventListener('click', function(e) {
  // Open Checkout with further options:
  handler.open({
    name: '',
    description: '<? echo $dialog_titel;?>',
    zipCode: true,
    currency: '<? echo $dialog_waehrung_kreditkarte;?>',
    email: '<? echo $dialog_email_kreditkarte;?>',
    amount: <? echo $dialog_preis_kreditkarte;?>
  });
  e.preventDefault();
});

// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
  handler.close();
});
</script>

then I charge the card in the next step

    Stripe::setApiKey($params['private_live_key']);
    $pubkey = $params['public_live_key'];
    try {
        $charge = Stripe_Charge::create(array(       
              "amount" => $amount_cents,
              "currency" => $_SESSION['cc_currency'],
              "source" => $_SESSION['stripeToken'],
              "description" => $description,
        "expand" =>array("balance_transaction")
        )             
        );

If no error is thrown I forward the customer to his download page.

I want a very simple way, I do not need customers, bills, recruing payments or whatever..just single payments. I do not want customers address or such things. Payment and goodbye...

Stripe says I have to change this process. But their example is confusing for me: https://stripe.com/docs/payments/checkout/migration#api-products (I did never create a customer for exampley...why should I?)

Can someone tell me what I have to do to migrate to the new checkout version?

bodomalo
  • 153
  • 1
  • 1
  • 11
  • This should help: https://stripe.com/docs/payments/checkout/migration – Chris White Apr 25 '19 at 19:45
  • Just rip all that old code out, use the v3 js file and plug in their code for the card element. https://stripe.com/docs/stripe-js/elements/quickstart. Then send the token to your php server and follow the docs for that part: https://stripe.com/docs/api/charges/create?lang=php – Bryan Apr 25 '19 at 20:01
  • For a PHP-specific integration of Checkout, you'll likely want to take a look at https://stripe.com/docs/payments/checkout/server – taintedzodiac Apr 25 '19 at 20:03
  • Bryan: On the first link you send me stripe writes: Use of this guide is no longer recommended if you’re based in Europe. Starting in September 2019, a new regulation called Strong Customer Authentication (SCA) will require businesses in Europe to request additional authentication for online payments. – bodomalo Apr 26 '19 at 09:30
  • 3
    ...boys and grils stripe is so confusing, they say I should use payment-intents now instead of checkout (and the old "legacy" checkout) Their goal seems to be to confuse everyone with many different methodes :-( – bodomalo Apr 26 '19 at 10:06
  • 2
    I agree entirely with what you're saying, Bodomalo; Stripe documentation used to be excellent but now runs around in circles (literally) and doesn't answer the basic requests or provide complete examples of new implementation. Sadly Stripe used to be great but has become bloated and confused. – Martin Sep 13 '19 at 11:22

3 Answers3

11

Basic setup (you can build it up from here)

Back-end:

Update your Stripe PHP Library.

Change from \Stripe\Charge to \Stripe\PaymentIntent following this format:

$charge = \Stripe\Charge::create([
    'source' => $token_id,
    'amount' => $amount,
    'currency' => 'usd',
]);

$intent = \Stripe\PaymentIntent::create([
        'payment_method_data' => [
            'type' => 'card',
            'card' => ['token' => $token_id],
        ],
        'amount' => $amount,
        'currency' => 'usd',
        'confirmation_method' => 'manual',
        'confirm' => true,
    ]);

Front-end:

Update your Stripe JS to use v3.

<script src='https://js.stripe.com/v3/' type='text/javascript'></script>

Update JS code that handles your payment form:

document.addEventListener("DOMContentLoaded", function(event) {
    var stripe = Stripe('xxxxxxxxxx'); // test publishable API key
    var elements = stripe.elements();

    var card = elements.create('card');
    // Add an instance of the card UI component into the `card-element` <div>
    card.mount('#card-element');

    // Handle events and errors
    card.addEventListener('change', function(event) {
      var displayError = document.getElementById('card-errors');
      if (event.error) {
        displayError.textContent = event.error.message;
      } else {
        displayError.textContent = '';
      }
    });

    function stripeTokenHandler(token) {
      // Insert the token ID into the form so it gets submitted to the server
      var form = document.getElementById('payment-form');
      var hiddenInput = document.createElement('input');
      hiddenInput.setAttribute('type', 'hidden');
      hiddenInput.setAttribute('name', 'stripeToken');
      hiddenInput.setAttribute('value', token.id);
      form.appendChild(hiddenInput);

      // Submit the form
      form.submit();
    }

    function createToken() {
      stripe.createToken(card).then(function(result) {
        if (result.error) {
          // Inform the user if there was an error
          var errorElement = document.getElementById('card-errors');
          errorElement.textContent = result.error.message;
        } else {
          // Send the token to your server
          stripeTokenHandler(result.token);
        }
      });
    };

    // Create a token when the form is submitted.
    var form = document.getElementById('payment-form');
    form.addEventListener('submit', function(e) {
      e.preventDefault();
      createToken();
    });
});

Edit your HTML form:

    <form action="process_payment.php" method="post" id="payment-form">
          <div class="form-row">
            <label for="card-element">
              Credit or debit card
            </label>
            <div id="card-element"><!-- Your form goes here --></div>                  
            </div>        
            <!-- Used to display form errors -->
            <div id="card-errors" role="alert"></div>
          </div>
        <button type="submit">Pay</button>
    </form>
Fer
  • 111
  • 1
  • 6
  • 2
    Looking for a very seamless way to upgrade to v3 and this was the easiest answer I've seen. Took less than 5 minutes to update the code. I'm still not 100% why there is a need to go from charge to payment intent but charge still works when I just left it "as is". Thanks. – Jonny O Sep 08 '19 at 14:41
  • @JonnyO Payment Intent is for (mainly) SCA accountability. – Martin Sep 13 '19 at 11:23
  • This will probably not work for European countries if the transaction requires Strong Customer Authentication. – black_belt Jun 16 '20 at 22:38
0

With the new SCA regulations coming in as you mentioned in your comment, you need to use Payment methods and Payment intents now.

Basically from taking a look at your sample code you will need to rewrite pretty much everything (if you haven't already)

Their current docs are here -> https://stripe.com/docs/payments/checkout

The reason for Payment Intents and Payment methods is due to SCA - https://stripe.com/docs/strong-customer-authentication

They have a migration guide too which can be found on the side bar.

However from their examples this is how you would create your new payment intent

$intent = \Stripe\PaymentIntent::create([
    'amount' => 1099,
    'currency' => 'gbp',
]);

Here is also their guide for migrating from the charges API - it has a tab for stripe.js V2 if you've been using it https://stripe.com/docs/payments/payment-intents/migration

Greg
  • 37
  • 2
  • 7
  • 3
    While it's good to reference the Stripe documentation, Greg, have you actually read and tried to implement their documentation trail? It's a confused mess `:-(`. – Martin Sep 13 '19 at 11:24
  • @Martin Yes, I have built my own custom integration using the stripe docs and very occasionally talking to their devs on their IRC channel. also I don't know why I was down voted. My answer is pretty dead on. – Greg Feb 11 '20 at 17:36
0

I agree. We have been using Stripe Checkout for about a year. The original implementation was dead easy. Trying to migrate to SCA compliant code is just a mess. Their online Chat is useless, and from the response in Chat, they bascially don't care if you stay with them or go. We're going to revert to PayPal which we used before and look for an alternative payment processor.

Joe Bloggs
  • 69
  • 7