<p>Composer!</p>

<p>There's already a package for Stripe: <a href="https://packagist.org/packages/stripe/stripe-php">https://packagist.org/packages/stripe/stripe-php</a></p>

<ol>
<li><p>Install <a href="https://getcomposer.org/doc/00-intro.md#installation-nix">composer</a>.</p></li>
<li><p>Add a <code>composer.json</code> file in your plugin's root folder.</p></li>
<li><p>Add this to the <code>composer.json</code> file:</p>

<p><code>{
    "require": {
        "stripe/stripe-php": "1.17.2"
    }
}</code></p></li>
<li><p>Run <code>php composer.phar update</code> from your plugin's root folder.</p></li>
<li><p>The library will be installed in a <code>/vendor</code> folder in your plugin.</p></li>
<li><p>Add an <code>init()</code> method to your main plugin's class file with this in it:</p>

<p><code>require CRAFT_PLUGINS_PATH.'/pluginhandle/vendor/autoload.php';</code></p></li>
</ol>

<p>And you're done.</p>

<p>Composer will handle autoloading all of the class files, so you don't need to worry about it.</p>

<p>If you ever want to update the Stripe library to a newer release, grab the version number from the packagist site, update your <code>composer.json</code> file with it and run <code>php composer.phar update</code> again.</p>

CC BY-SA 3.0
2014-10-08T21:10:03.967Z
Thanks! You're the master. One problem getting this reply in console: `Could not open input file: composer.phar` I tried using sudo as well.
Douglas McDonald
CC BY-SA 3.0
2014-10-08T21:18:09.067Z
Hold on. Missing step 1. ;)
Douglas McDonald
CC BY-SA 3.0
2014-10-08T21:27:49.000Z
Got it. Thanks. That's great! So, regarding multi-environment config for API key?
Douglas McDonald
CC BY-SA 3.0
2014-10-08T21:50:49.930Z
Oh, didn't even read that part.  Probably better to edit this to have the "dependency" question and add a new one for the API key.
Brad Bell
CC BY-SA 3.0
2014-10-08T20:49:35.837Z
2015-09-13T20:30:20.450Z
2015-09-13T20:30:20.450Z
Brad Bell
<p>My plugin requires vendor library dependencies and api keys set. What is the recommended way to register an external library? I would also like to configure the public api keys separately for stage and production environments — is there a way to set these in craft/config with the rest of the multi-environment configuration? Or is it better to create a plugin config file of some kind?</p>

<p>Currently I am having to include these in each of my service methods (and something similar again in my javascript files):</p>

<pre><code>private function _saveStripeCustomer($account, $token = NULL)
{
    require_once(CRAFT_PLUGINS_PATH . 'businesslogic/vendor/stripe/lib/Stripe.php');
    \Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");

    // ...
}
</code></pre>

<p>I'm not familiar with composer. I don't know if that can perhaps help here.</p>

<plugin-development><config>
What's the recommended way to include vendor library dependencies in your plugins?
# What's the recommended way to include vendor library dependencies in your plugins?

(Asked by: username_0 on 2014-10-08)

My plugin requires vendor library dependencies and api keys set. What is the recommended way to register an external library? I would also like to configure the public api keys separately for stage and production environments — is there a way to set these in craft/config with the rest of the multi-environment configuration? Or is it better to create a plugin config file of some kind?
Currently I am having to include these in each of my service methods (and something similar again in my javascript files):
private function _saveStripeCustomer($account, $token = NULL)
{
    require_once(CRAFT_PLUGINS_PATH . 'businesslogic/vendor/stripe/lib/Stripe.php');
    \Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");

    // ...
}

I'm not familiar with composer. I don't know if that can perhaps help here.

**Tags:** plugin-development, config

---

## 1 answers

### Answer by username_1 on 2014-10-08. Score: 26 | Accepted answer

Composer!

There's already a package for Stripe: <https://packagist.org/packages/stripe/stripe-php>

1. Install [composer](https://getcomposer.org/doc/00-intro.md#installation-nix).
2. Add a `composer.json` file in your plugin's root folder.
3. Add this to the `composer.json` file:

   `{
   "require": {
   "stripe/stripe-php": "1.17.2"
   }
   }`
4. Run `php composer.phar update` from your plugin's root folder.
5. The library will be installed in a `/vendor` folder in your plugin.
6. Add an `init()` method to your main plugin's class file with this in it:

   `require CRAFT_PLUGINS_PATH.'/pluginhandle/vendor/autoload.php';`

And you're done.

Composer will handle autoloading all of the class files, so you don't need to worry about it.

If you ever want to update the Stripe library to a newer release, grab the version number from the packagist site, update your `composer.json` file with it and run `php composer.phar update` again.

> **Comments:**
> **username_0**: Thanks! You're the master. One problem getting this reply in console: `Could not open input file: composer.phar` I tried using sudo as well.  
> **username_0**: Hold on. Missing step 1. ;)  
> **username_0**: Got it. Thanks. That's great! So, regarding multi-environment config for API key?  
> **username_1**: Oh, didn't even read that part.  Probably better to edit this to have the "dependency" question and add a new one for the API key.  

---

