Using stripe api inside sage theme

I’m trying to figure out how i can use Stripe check out inside my theme im building. I installed bedrock as the base and the sage theme is inside. I’m trying to figure out how to access Stripe through the vendor directory. Would i include it in the sage_includes array? If so how do i navigate to the vendor directory?

If not would i have to just download the release and put it in the lib folder then link to it that way?

Just wondering which way is best…

The PHP API? Add it with Composer and Bedrock automatically will load it.

@ben I have a class i made inside lib folder in my sage theme to process Stripe transactions, but it doesnt have access to Stripe. How would i give it access to Stripe? use \Stripe\Stripe; doesnt work, is there a proper way to give my class access to it?


<?php namespace Roots\Sage\Charge;

if(isset($_POST)){    
    $charge = new StripeCharge($_POST);
    $charge->process();
}

class StripeCharge{

    private $data;

    public function __construct($data){
        $this->data = $data;
    }
    
    public function process(){
            
        $token = $this->data['stripeToken'];
    
        $customer = Stripe\Customer::create(array(
            'email' => $this->data['stripeEmail'],
            'card'  => $token
        ));
        
        $charge = Stripe\Charge::create(array(
            'customer' => $customer->id,
            'amount'   => 5000,
            'currency' => 'usd'
        ));
        
        echo '<h1>Successfully charged $50.00!</h1>';

    }
    
}
use Stripe\Stripe\StripeCustomer;
use Stripe\Stripe\StripeCharge;

You have to import any class you are using when it’s in another namespace. Also no need for a forward slash when using use.

Hey @kalenjohnson i’m still getting an error with that addition to my code… not sure why?

<?php 
    
namespace Roots\Sage\Charge;

use Stripe\Stripe\StripeCustomer;
use Stripe\Stripe\StripeCharge;

if(isset($_POST)){    
    $charge = new ChargeForToob($_POST);
    $charge->process();
}

class ChargeForToob{

    private $data;

    public function __construct($data){
        $this->data = $data;                
    }
    
    public function process(){
            
        $token = $this->data['stripeToken'];
    
        $customer = \Stripe\Customer::create(array(
            'email' => $this->data['stripeEmail'],
            'card'  => $token
        ));
        
        $charge = \Stripe\Charge::create(array(
            'customer' => $customer->id,
            'amount'   => 5000,
            'currency' => 'usd'
        ));
        
        echo '<h1>Successfully charged $50.00!</h1>';

    }
    
}

If you haven’t already, please read the section here on namespaces: https://roots.io/upping-php-requirements-in-your-wordpress-themes-and-plugins/

Although classes may not have been covered as in depth as functions, the same rules apply. You are referencing the Stripe Customer and Charge classes directly, since you are appending the forward slash:

So that looks correct according to how Stripe-php is set up: https://github.com/stripe/stripe-php/blob/master/lib/Charge.php#L3-L5

In that case you don’t need to use the use keyword. If you use the use keyword then it would be this:

use Stripe\Customer;
use Stripe\Charge;

...

$customer = Customer::create();
$charge = Charge::create()

However, since you are still getting an unfound class error, did you run composer install or most likely composer update if you added stripe-php manually to your composer.json file to ensure that the stripe-php package is actually downloaded and autoloaded?

@kalenjohnson Alright that all makes sense, but i’m not sure why its still not working… I ran composer update and made sure the stripe package is there, which it is.

This is still the error with the updated code:

<?php 
    
namespace Roots\Sage\Charge;

use Stripe\Customer;
use Stripe\Charge;

if(isset($_POST)){    
    $charge = new ChargeForToob($_POST);
    $charge->process();
}

class ChargeForToob{

    private $data;

    public function __construct($data){
        $this->data = $data;                
    }
    
    public function process(){
            
        $token = $this->data['stripeToken'];
    
        $customer = Customer::create(array(
            'email' => $this->data['stripeEmail'],
            'card'  => $token
        ));
        
        $charge = Charge::create(array(
            'customer' => $customer->id,
            'amount'   => 5000,
            'currency' => 'usd'
        ));
        
        echo '<h1>Successfully charged $50.00!</h1>';

    }
    
}