Skip to main content
se eliminaron 172 caracteres en el cuerpo
Origen Enlace
Maiker Leon
  • 285
  • 1
  • 4
  • 21
    <?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51Hcv37L6gSe4NjQqvnsWCxLmFguRk8m01hkQy8VbZvIluRI9KjpDfrKSmi3qBEqqyOCd76oYUS28c8HZLBTci7yC00tFB0BQsM''sk_test_51Hcv***');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://midominio.com/';
$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount' => 2000,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
            <script src="https://js.stripe.com/v3/"></script>
            <link rel="stylesheet" href="css/btn-pagar.css">
            <div class="pagar" style="display:none; margin-top: 10px;">
                <button class="btn-pagar" id="checkout-button"><a href="sesion.php">Pagar</a></button>
            </div>
        
    
<script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51Hcv37L6gSe4NjQqZRTxmZFj7cY1qS43b1SaZWiV6zxhyJfNKFB4Kgyn8I3vYwKXx3oc4cRghtAvofhOBgCf8x4n00PznJtvyi""pk_test_51Hcv37L6gSe4N****");
    var checkoutButton = document.getElementById("checkout-button");
    checkoutButton.addEventListener("click", function () {
      fetch("/sesion.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>
    <?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51Hcv37L6gSe4NjQqvnsWCxLmFguRk8m01hkQy8VbZvIluRI9KjpDfrKSmi3qBEqqyOCd76oYUS28c8HZLBTci7yC00tFB0BQsM');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://midominio.com/';
$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount' => 2000,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
            <script src="https://js.stripe.com/v3/"></script>
            <link rel="stylesheet" href="css/btn-pagar.css">
            <div class="pagar" style="display:none; margin-top: 10px;">
                <button class="btn-pagar" id="checkout-button"><a href="sesion.php">Pagar</a></button>
            </div>
        
    
<script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51Hcv37L6gSe4NjQqZRTxmZFj7cY1qS43b1SaZWiV6zxhyJfNKFB4Kgyn8I3vYwKXx3oc4cRghtAvofhOBgCf8x4n00PznJtvyi");
    var checkoutButton = document.getElementById("checkout-button");
    checkoutButton.addEventListener("click", function () {
      fetch("/sesion.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>
    <?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51Hcv***');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://midominio.com/';
$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount' => 2000,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
            <script src="https://js.stripe.com/v3/"></script>
            <link rel="stylesheet" href="css/btn-pagar.css">
            <div class="pagar" style="display:none; margin-top: 10px;">
                <button class="btn-pagar" id="checkout-button"><a href="sesion.php">Pagar</a></button>
            </div>
        
    
<script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51Hcv37L6gSe4N****");
    var checkoutButton = document.getElementById("checkout-button");
    checkoutButton.addEventListener("click", function () {
      fetch("/sesion.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>
Origen Enlace
Maiker Leon
  • 285
  • 1
  • 4
  • 21

Pasarela de pago con Stripe

Actualmente estoy tratando de desarrollar una pasarela de pago con Stripe en PHP sin embargo no logro entender que es lo que estoy haciendo mal, en PHP tengo esto

    <?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51Hcv37L6gSe4NjQqvnsWCxLmFguRk8m01hkQy8VbZvIluRI9KjpDfrKSmi3qBEqqyOCd76oYUS28c8HZLBTci7yC00tFB0BQsM');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://midominio.com/';
$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount' => 2000,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);

Basado en el codigo de la documentacion de Stripe https://stripe.com/docs/checkout/integration-builder

En el HTML tengo

<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
            <script src="https://js.stripe.com/v3/"></script>
            <link rel="stylesheet" href="css/btn-pagar.css">
            <div class="pagar" style="display:none; margin-top: 10px;">
                <button class="btn-pagar" id="checkout-button"><a href="sesion.php">Pagar</a></button>
            </div>
        
    
<script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51Hcv37L6gSe4NjQqZRTxmZFj7cY1qS43b1SaZWiV6zxhyJfNKFB4Kgyn8I3vYwKXx3oc4cRghtAvofhOBgCf8x4n00PznJtvyi");
    var checkoutButton = document.getElementById("checkout-button");
    checkoutButton.addEventListener("click", function () {
      fetch("/sesion.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>

Ahora pese a que lo tengo casi igual al original del ejemplo en la documentación no me funciona para nada, al inicio pensé que era porque estaba en local, pero lo intente en el servidor y tampoco me funcionaba, simplemente me da error 500 cuando redirecciona al PHP... Por ahora esta todo estático porque no entiendo porque no me funciona.