Jump to content

Prebuilt Checkout page - STRIPE


angelo, italy
 Share

Recommended Posts

I'd like to use Stripe Checkout in processwire. The client- and server-side code redirects to a prebuilt payment page hosted on Stripe. 

I create a form in my page:

$stripe = $pages->get("/stripe/");
<!-- HEAD-->

<h1><?php echo $page->title ?></h1>
              
<?php 
    $myForm1 = "
        <form action='{$stripe->url}' method='post'>
         <button type='submit' id='checkout-button'>Checkout</button>
        </form>"; 
	echo $myForm1;
?>

<!-- FOOTER -->

then, I created a template/page
-----
<?php

include '<?php echo $config->urls->templates ?>stripe-php/init.php';

//require 'vendor/autoload.php';
use Stripe\Stripe;

Stripe\Stripe::setApiKey('xxxxxxxxx');

header('Content-Type: application/json');

$YOUR_DOMAIN = 'https://www.axxxxxxsamia.it/';

$checkout_session = \Stripe\Checkout\Session::create([
  'shipping_address_collection' => [
    'allowed_countries' => ['IT'],
  ],
  'shipping_options' => [
    [
      'shipping_rate_data' => [
        'type' => 'fixed_amount',
        'fixed_amount' => [
          'amount' =>
          700,
          'currency' => 'eur',
        ],
        'display_name' => 'Standard',
        // Delivers between 5-7 business days
        'delivery_estimate' => [
          'minimum' => [
            'unit' => 'business_day',
            'value' => 5,
          ],
          'maximum' => [
            'unit' => 'business_day',
            'value' => 7,
          ],
        ]
      ]
    ],
    [
      'shipping_rate_data' => [
        'type' => 'fixed_amount',
        'fixed_amount' => [
          'amount' => 1000,
          'currency' => 'eur',
        ],
        'display_name' => 'Celere',
        // Delivers in exactly 1 business day
        'delivery_estimate' => [
          'minimum' => [
            'unit' => 'business_day',
            'value' => 1,
          ],
          'maximum' => [
            'unit' => 'business_day',
            'value' => 3,
          ],
        ]
      ]
    ],
  ],
  'line_items' => [[
    # Provide the exact Price ID (e.g. pr_1234) of the product you want to sell
    'price' => 'pricSFL4ZyLp1tjGGqakV4Z',
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);

header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);

---

BUT... ERROR!!

Fatal Error: Class 'Stripe\Stripe\Stripe' not found (line 8 of site/templates/create-checkout-session.php)

Any ideas??

Thanks

This error message was shown because: you are logged in as a Superuser. Error has been logged.

Link to comment
Share on other sites

Seems to be a problem with namespaces. Try adding namespace ProcessWire to the template:

<?php namespace ProcessWire;

include '<?php echo $config->urls->templates ?>stripe-php/init.php';

//require 'vendor/autoload.php';
use Stripe\Stripe;

Stripe\Stripe::setApiKey('xxxxxxxxx');

 

Link to comment
Share on other sites

I'd like to share how I integrated stripe in PW.. and it works!! ?‍♂️  :-D!

 

FORM in product page:
<div style='display:<?php if ($page->Mostra1 == 1) echo "block"; echo "none";?>'>
              
        <form action='<?php echo $pages->get('template=create-checkout-session')->url; ?>' method='POST' >
       
      
      </form>

      <?php

    $myForm1 = "
        <form action='{$stripe->url}' method='post'>
         <button type='submit' id='checkout-button' >Acquista</button>
        </form>";

    echo $myForm1;?>
  </div>

and this is the 'create-checkout-session' template:

<?php 


include 'init.php';




Stripe\Stripe::setApiKey('xxxxxxxxxxxxxxxxxx');



header('Content-Type: application/json');

$YOUR_DOMAIN = 'https://www.xxxxxxxx.it/';


$checkout_session = \Stripe\Checkout\Session::create([
  'shipping_address_collection' => [
    'allowed_countries' => ['IT'],
  ],
  'shipping_options' => [
    [
      'shipping_rate_data' => [
        'type' => 'fixed_amount',
        'fixed_amount' => [
          'amount' =>
          700,
          'currency' => 'eur',
        ],
        'display_name' => 'Standard',
        // Delivers between 5-7 business days
        'delivery_estimate' => [
          'minimum' => [
            'unit' => 'business_day',
            'value' => 5,
          ],
          'maximum' => [
            'unit' => 'business_day',
            'value' => 7,
          ],
        ]
      ]
    ],
    [
      'shipping_rate_data' => [
        'type' => 'fixed_amount',
        'fixed_amount' => [
          'amount' => 1000,
          'currency' => 'eur',
        ],
        'display_name' => 'Celere',
        // Delivers in exactly 1 business day
        'delivery_estimate' => [
          'minimum' => [
            'unit' => 'business_day',
            'value' => 1,
          ],
          'maximum' => [
            'unit' => 'business_day',
            'value' => 3,
          ],
        ]
      ]
    ],
  ],
  'line_items' => [[
    # Provide the exact Price ID (e.g. pr_1234) of the product you want to sell
    #'price' => 'price_1JyahSFL4ZyLp1tjGGqakV4Z',
    #'price' => $price, 
    'name' =>  $session->get(prod),
     'amount' => $session->get(price),
    'currency' => 'eur',
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . 'grazie/',
  'cancel_url' => $YOUR_DOMAIN . 'pagamento-rifiutato/',
]);

header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);

I used:

$session->set(prod, "$page->title");  >>> 'name' =>  $session->get(prod),

I hope this stuff can be useful for someones!


    

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...