6

I am new to Symfony. I am trying to use a pre existing library that I have used for my Payment Gateway API, with Symfony (v2.3).

Before using Symfony I would have a composer.json file in the root directory and I would simply use PHP require 'vendor/Braintree.... However with Symfony I am having a really difficult time using the library for payment gateway API by importing it to my Controller.

Note: I am using an Entity in the same controller as follows, which has a similar directory structure and works great:

use Jets\JetsBundle\Entity\Company;

This is what I tried to use the payment gateway API:

use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree;

and the Braintree.php contains:

namespace Widb\WidbBundle\Braintree\braintree\braintree_php\lib;

I keep getting the following error:

FatalErrorException: Error: Class 'Jets\JetsBundle\Controller\Braintree_Configuration' not found in C:\xampp\htdocs\www\symfony\src\Jets\JetsBundle\Controller\DefaultController.php line 239

And the DefaultController.php Line 239 contains:

Braintree_Configuration::environment('sandbox');

As a side note, I didn't do anything other than drag / drop the ready configured library directory from my old server to the Symfony directory on the new server. Am I missing some configuration script or cmd set up or something?

I appreciate any assistance in this regard. I will forever be grateful is someone can help me troubleshoot this.

Here is my DefaultController.php code:

http://pastebin.com/kwisEBzL

Many thanks in advance!

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231

3 Answers3

9

This Braintree project seems to be PSR-0, so you should be able to use it pretty easily.

Don't do this:

use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree;

There is no such namespace.

Include the Braintree project using Composer (notice they have the PSR-0 autoloading config already: https://github.com/braintree/braintree_php/blob/master/composer.json).

EDIT: add this to your composer.json and don't forget to run composer update (or php composer.phar update)

Update 2: As pointed out in the comments, it is a good idea to use a stable tag/branch. That way, if there are breaking changes, you can make sure to fix them before updating. So always check the version of the library (2.23 may not be the latest version any more).

"require": {
    ...
    "braintree/braintree_php": "~2.23"
},

The autoloader should pickup all of the classes if you use the global namespace (notice the leading slash):

\Braintree_Configuration::environment('sandbox');

Let me know if this works.

Side note, you should probably create a bundle that does the configuration for you in a centralized place.

Matt
  • 5,478
  • 9
  • 56
  • 95
  • Can you please explain what you mean by "Side note, you should probably create a bundle that does the configuration for you in a centralized place." ??? – AnchovyLegend Oct 31 '13 at 20:19
  • I personally would wrap the library in a bundle. The bundle would handle packaging the library as services and it could take your config values and pass them into the `\Braintree_Configuration::` calls. @Andrew has a link where someone has already done this. I'm not sure what else you want to know... – Matt Oct 31 '13 at 20:44
  • 1
    Nice answer. Slightly off topic: I would change the required version from a generic `dev-master` to something more stable, for example `~2.23`. This minimizes the chances of breaking the application by installing a vendor library in an unstable state. – kgilden Nov 17 '13 at 01:01
2

You really missed out one of the first comments.. which was spot on and will most likely take away alot of headaches.

Follow the instructions given at https://github.com/cometcult/CometCultBraintreeBundle, this is a bundle that provides "Braintree". A bundle in this case actually is a "module" you can hook in, and configure by configuration files.

Relevant commands that are missing for handling composer:

As soon as you have finished it up you should take a look at the bottom two parts, besides conifgurating everything.

$factory = $this->get('comet_cult_braintree.factory');
$customerService = $factory->get('customer');

If you need more help let me know

Florent
  • 12,310
  • 10
  • 49
  • 58
wtfzdotnet
  • 1,022
  • 8
  • 11
1

I'm don't uderstand what you actually try to do in your DefaultController.php, but I have some assumptions.

UPD:

FIRST and SECOND point was deleted.

THIRD:

When you trying to make use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree; you have load only class Braintree from file Braintree.php. Not whole file with another data like require_once instructions.

FOURTH:

/vendor directory of Symfony2 projects usually contains third-part libs. So you can put your lib here.

Hope some of this points helps you.

P.S. Sorry for poor English.

Serge Kvashnin
  • 4,332
  • 4
  • 23
  • 37
  • Thanks for the reply. Have you tested your answer? it hasn't worked for me, I'm still getting the same message. Please test your answer before answering, also options 1-3 pertain the unedited version of my code... I appreciate the reply though. – AnchovyLegend Oct 29 '13 at 13:39
  • I have update my answer. Do you try to `require_once Braintree.php;` instead of `use`? – Serge Kvashnin Oct 29 '13 at 14:32