ストライプチェックアウトの統合

Aug 25 2020

私は可能な限り簡単なことをしようとしています。1つの製品を含むStripeのホストされたチェックアウトページにユーザーを送ります。

Stripeの例はどれも機能していないようですが、これまでのところ、次のようになっています。

PHP create-checkout-session.php

require_once 'shared.php';

// ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param
$checkout_session = \Stripe\Checkout\Session::create([ 'success_url' => $domain . '/success.html?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => $domain . '/canceled.html', 'payment_method_types' => ['card'], //, 'alipay' 'mode' => 'payment', 'line_items' => [[ 'amount' => $price,
        'currency' => 'usd',
        'name' => $product, 'quantity' => 1, ]] ]); echo json_encode(['sessionId' => $checkout_session['id']]);

そのPHPページはセッションIDを正しく返します。

HTML

<html>
  <head>
    <title>Buy cool new product</title>
    <script src="https://js.stripe.com/v3/"></script>
  </head>
  <body>
    <button id="checkout-button">Checkout</button>

    <script type="text/javascript">
      // Create an instance of the Stripe object with your publishable API key
      var stripe = Stripe('pk_test_key'); // removed for Stackoverflow post
      var checkoutButton = document.getElementById('checkout-button');

      checkoutButton.addEventListener('click', function() {
        // Create a new Checkout Session using the server-side endpoint you
        // created in step 3.
        fetch('create-checkout-session.php', {
          method: 'POST',
        })
        .then(function(response) {
          return response.json();
        })
        .then(function(session) {
          return stripe.redirectToCheckout({ sessionId: session.id });
        })
        .then(function(result) {
          // If `redirectToCheckout` fails due to a browser or network
          // error, you should display the localized error message to your
          // customer using `error.message`.
          if (result.error) {
            alert(result.error.message);
          }
        })
        .catch(function(error) {
          console.error('Error:', error);
        });
      });
    </script>
  </body>
</html>

ボタンをクリックしても何も起こらず、Chromedevtoolsで次のエラーが発生します。

Error: IntegrationError: stripe.redirectToCheckout: You must provide one of lineItems, items, or sessionId.
    at new t (https://js.stripe.com/v3/:1:11100)
    at Lu (https://js.stripe.com/v3/:1:152624)
    at qu (https://js.stripe.com/v3/:1:152923)
    at Fu (https://js.stripe.com/v3/:1:153599)
    at Bu (https://js.stripe.com/v3/:1:153713)
    at e.redirectToCheckout (https://js.stripe.com/v3/:1:154128)
    at https://emu.net/stripetest/test.html:24:25

私はこのエラーを理解していません。sessionIdが正しく渡されていないようです。HTMLコードは、次のStripeドキュメントから直接取得されました。https://stripe.com/docs/payments/checkout/accept-a-payment

正直なところ、どこを見ればいいのかわかりません。Stripeの例はどれも機能していないようです。誰かが私が間違っていることを知っていますか?

回答

2 Mitya Aug 25 2020 at 01:25

sessionあなたの構造から判断すると、合格する必要があります

{ sessionId: session.sessionId }

ない

{ sessionId: session.id }