Getting Stripe API setup with Nova Framework

David Carr

Stripe API Tutorials Nova Framework

This post will guide you on using Stripe’s API with Nova Framework

Install Stripe’s PHP library by including it in your composer.json file:

composer require stripe/stripe-php

Before starting with Stripe create a Stripe.php file in app/Config and enter your API secret and public key, also set your webhookSecret if you plan on using Stripe’s webhooks.

Go to https://dashboard.stripe.com/account/apikeys to manage your API keys.

return array(
    'apiSecretKey' => 'your-secret-key-here',
    'apiPublicKey' => 'your-public-key-here',
    'webhookSecret' => 'your-webhook-key-here',
);

This allows the API to access the keys without having them littered in your code.

In the controller, you’ll be using with Stripe import the classes you’ll be using. 

The Config facade should also be imported: 

use Config;

use Stripe\Customer;
use Stripe\Charge;
use Stripe\Invoice;
use Stripe\Plan;
use Stripe\Stripe;
use Stripe\Subscription;
use Stripe\Webhook;

Now we need to provide Stripe the API Secret key this will be used for all API calls, using a before() method means all methods of the class will have access to this. Retrieve the key by calling Config::get followed by stripe, then the item in this case apiSecretKey. 

public function before()
{
    Stripe::setApiKey(Config::get('stripe.apiSecretKey'));
}

That’s it Nova is now ready to start making API calls with Stripe. 

Fathom Analytics $10 discount on your first invoice using this link

Help support the blog so that I can continue creating new content!

Sponsor

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.