Forum

Thread tagged as: Question, Problem

 Pay and Stripe

Hi Drew,

Can I use the perch_shop_checkout('stripe') etc to take an  Pay payment via this method, https://stripe.com/docs/apple-pay/web?

I have tried the example code, and the post to the cart with the token seems to result in the website hanging (guessing infinite loop!)?

Any help, super appreciated as always.

Ryan Gittings

Ryan Gittings 1 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You've not really given anyone anything to go on there. Where would we start?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Okay, so in stripe_payment_form.html, as per the guide, I've added the following:

<script>
      Stripe.setPublishableKey('<perch:shop id="publishable_key" escape="true" />');

      Stripe.applePay.checkAvailability(function(available) {
        if (available) {
             document.getElementById('apple-pay-button').style.display = 'block';
        }
      });

      document.getElementById('apple-pay-button').addEventListener('click', beginApplePay);

      function beginApplePay(e) {
        e.preventDefault();

        var paymentRequest = {
          countryCode: 'GB',
          currencyCode: '<perch:shop id="currency" escape="true" />',
          total: {
            label: 'Items (<perch:shop id="amount_formatted" escape="true" />)',
            amount: '<perch:shop id="amount_formatted" escape="true" />'
          }
        };

        var session = Stripe.applePay.buildSession(paymentRequest,
          function(result, completion) {

          $.post('/basket', { stripeToken: result.token.id }).done(function() {
            completion(ApplePaySession.STATUS_SUCCESS);
            // You can now redirect the user to a receipt page, etc.
            window.location.href = '/payment';
          }).fail(function() {
            completion(ApplePaySession.STATUS_FAILURE);
          });

        }, function(error) {
          console.log(error.message);
        });

        session.oncancel = function() {
          console.log("User hit the cancel button in the payment window");
        };

        session.begin();
      }
    </script>

This works,  Pay shows and the Stripe side processes. However, the POST to /basket which works for a standard Stripe payment with the stripeToken, doesn't work for the post request, and just seems to hang, almost like there's too many redirects.

You should be able to recreate in Safari, using the script above! Any questions, or if you need anymore, please let me know.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What have you done so far to debug it?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

I’ve tried changing the post request to firstly just a blank page, which worked okay obviously. Then added just the stripe checkout via ‘perch_shop_checkout’, which immiedialy recreates my issue. The post request never actually completes which makes me think it’s some sort of redirect issue, but wasn’t really sure how’s best to debug further! As I say, standard stripe checkout seems to work fine!

Drew McLellan

Drew McLellan 2638 points
Perch Support

What happens when you go directly to /basket? Do you get redirected to /basket/ ?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

No, just get served the cart as usual, no redirects or anything happening there. All I have at the top of that page is:

```<?php if (!perch_member_logged_in()) { PerchUtil::redirect("/account/login?r=/basket"); } else { // your 'success' and 'failure' URLs $return_url = '/payment'; $cancel_url = '/';

if (perch_post('stripeToken')) { perch_shop_checkout('stripe', [ 'return_url' => $return_url, 'cancel_url' => $cancel_url, 'token' => perch_post('stripeToken') ]); }
}```

Drew McLellan

Drew McLellan 2638 points
Perch Support

The JavaScript will be posted with your cookies, so if you're logged in, that request should be logged in.

I'd suggest inspecting $_POST at that point to see if what you're getting is what you're expecting.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

The request doesn't seem to complete, so I can't analyse POST, that's what I can't workout. Doesn't appear in the console, only other requests the site has made, so strange!

Drew McLellan

Drew McLellan 2638 points
Perch Support

So post to a completely different page until you see what you're getting.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Yep, stripeToken is in the POST, anything else I need to be looking for?

Drew McLellan

Drew McLellan 2638 points
Perch Support

If stripeToken is there, you should be able to call perch_shop_checkout()

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

``if (perch_member_logged_in()) { // your 'success' and 'failure' URLs $return_url = '/payment'; $cancel_url = '/';

if (perch_post('stripeToken')) { perch_shop_checkout('stripe', [ 'return_url' => $return_url, 'cancel_url' => $cancel_url, 'token' => perch_post('stripeToken') ]); }
}```

That's in my basket, but as soon as that's called, it doesn't post and the site hangs!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Just as before, break it down and isolate where it's going wrong.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

It's in the perch_shop_checkout, the minute I remove that, no hang

Drew McLellan

Drew McLellan 2638 points
Perch Support

But the input is the same?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Both post with the stripeToken as far as I’m aware!

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's curious then. The same input should have the same result. Does your failure URL point somewhere intelligent?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Failure goes to /, homepage. No redirects or other stuff happening there. I just can't figure out how to easily debug it, because the post doesn't give me a response...

Drew McLellan

Drew McLellan 2638 points
Perch Support

Any clues if you hold redirects?

Before anything that causes a redirect, add:

PerchUtil::hold_redirects();

and obviously turn on debug.