Basic Stripe integration fails to load the payment page

蹲街弑〆低调 提交于 2020-05-28 07:28:05

问题


I am using the server quickstart example to send a user to Stripe to make payment

https://stripe.com/docs/payments/checkout/server

The required session id is returned and I am sending it to Stripe. Yet the payment page does not load.

I have swapped my API key for XXXXX.

Set PHP to show all error, and none exist.

Triple checked that my code matches the integration example (with the obvious required modifications)

Checked my Stripe account.

In HEAD

<script src="https://js.stripe.com/v3/"></script>

In PHP

require_once('stripe-php-6.31.5/init.php');

\Stripe\Stripe::setApiKey("pk_test_XXXXXXXXXXXXXXXXXXXXXXX");

$object = \Stripe\Checkout\Session::create([
        'success_url' => 'https://www.example.com/success',
        'cancel_url' => 'https://www.example.com/cancel',
        'payment_method_types' => ['card'],
        'line_items' => [[
        'amount' => 500,
        'currency' => 'gbp',
        'name' => 'T-shirt',
        'description' => 'Comfortable cotton t-shirt',
        'images' => ['https://www.example.com/t-shirt.png'],
        'quantity' => 1,
    ]]
]);

$session_id = $object->id;

if ($session_id) {

    echo "<script>
          var stripe = Stripe('pk_test_XXXXXXXXXXXXXXXXXXXXXXX');
          stripe.redirectToCheckout({
          sessionId: '{{" . $session_id . "}}'
          }).then(function (result) {
          });
          </script>";

} else {

    echo 'No Session ID!';

}

The Stripe payment page should load.

Additionally, the javascript once I have the session_id looks like this

stripe.redirectToCheckout({
                sessionId: 
'{{cs_KmeIFgWSfN5GW6tP2e5IQ0Vb9EA0q3pWGHZNoDfFKAdcc6kW7DwsM6dAbhQ30}}'
            }).then(function (result) {
                // If `redirectToCheckout` fails due to a browser or network
                // error, display the localized error message to your customer
                // using `result.error.message`.
            });

The user then seems this page

<!DOCTYPE html>
<html>
<head>
    <title>KP Balance and Purchase</title>
    <script src="https://js.stripe.com/v3/"></script>

    <script>
        <!--
        function checkout(session_id) {

            var stripe = Stripe('pk_test_yLz5iASFgRnotoAQc79miQGz');

            stripe.redirectToCheckout({
                sessionId: '{{cs_KmeIFgWSfN5GW6tP2e5IQ0Vb9EA0q3pWGHZNoDfFKAdcc6kW7DwsM6dAbhQ30}}'
            }).then(function (result) {
                // If `redirectToCheckout` fails due to a browser or network
                // error, display the localized error message to your customer
                // using `result.error.message`.
            });

            }

        //-->
</script>

</head>
<body>

<form>
    <input type="button" onclick="checkout()" value="Buy Now!" />
</form>

</body>
</html>

回答1:


Additionally, the javascript once I have the session_id looks like this

stripe.redirectToCheckout({
                sessionId: '{{cs_KmeIFgWSfN5GW6tP2e5IQ0Vb9EA0q3…kW7DwsM6dAbhQ30}}'

{{CHECKOUT_SESSION_ID}} is usually used to signal “this is a placeholder” in technical documentation, but that does not mean you’re supposed to include the curly braces in the actual value.

Your sessionId: '{{cs_KmeI…Q30}}' should actually be just sessionId: 'cs_KmeI…Q30' here.




回答2:


I have been struggling with this for days and even put a request into Stripe for assistance. I finally got it to work.

NO curly brackets but also NO quotes. JUST the variable name you(we) assigned to the variable given when the server replies. This is, the id value of the array returned from \Stripe\Checkout\Session::create([..])

From your example, it will be the session_id in PHP and sessionId in the JS. This means your sessionId: '{{cs_KmeI…Q30}}' should actually be just sessionId: 'cs_KmeI…Q30'.

Whoopi .. loaded the checkout page FINALLY! :O)

EDIT: Sorry but it was the end of a 14hr work day. My eyes hurt and I had a headache from them. Good morning all.

One doesn't want to put an actual hard coded session id value in single quotes as the session is volatile. Once that session expires or a different one for a different payment is initiated then using 'examp123le0fID' will not work. So we want to use an assigned dynamic value. That variable should not be enclosed in anything- no curlies, no quotes.

On the initialization of payment file I created a variable for the session id using the array that Stripe returns to us. This I did in PHP, as that is what I am using for this section of code. I then added a script after the body section that assigned that value(variable) to a new java variable. Immediately after that I added a script that passed the java variable to the java file containing the Stripe java redirect code. Inside it I assigned that variable to a new name using const which was then passed to the redirect code itself. This may be the long way to do it. I am not good at java, Jason, Ajax, etc and haven't been doing any real programming for over 10 years but the important thing is this approach worked for us.

So on our donation.php file we have:

                           $session = \Stripe\Checkout\Session::create([
                            'payment_method_types' => ['card'],
                            'line_items' => [[
                              'name' => 'Custom t-shirt',
                              'description' => 'Your custom designed t-shirt',
                              'amount' => 5000, /* order.amount */
                              'currency' => 'usd',
                              'quantity' => 1,
                            ]],
                            'success_url' => 'https://example.com/success',
                            'cancel_url' => 'https://example.com/cancel',
                          ]);

                        $stripeSession = array($session);
                        $sessId = ($stripeSession[0]['id']);

Then immediately after the end of body in the same file I put this:

<script type="text/javascript"> var ssId = "<?php echo $sessId ?>";</script> 

<script src="./client.js"></script>

where client is the js file containing the Stripe redirect code. Inside that file we had this:

const stripe = Stripe('pk_test_UKRcYdXTmraCEZspx8PE7NzB00QZyoUXp3');
const checkout_Id = (ssId);

and the Stripe redirect:

 stripe.redirectToCheckout({
 sessionId: checkout_Id,
   });

And that returned the full page checkout form with payment section.



来源:https://stackoverflow.com/questions/55741449/basic-stripe-integration-fails-to-load-the-payment-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!