braintree / braintree_php

Braintree PHP library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
546 stars 224 forks source link

Class 'Braintree_Gateway' not found #242

Closed petlys closed 5 years ago

petlys commented 5 years ago

General information

Issue description

I've downloaded your library directly (i.e from https://developers.braintreepayments.com/client_libraries/php/braintree-php-3.38.0.tgz)

Extracted all the contents of the lib folder into a braintree folder, and created a doPayment.php script as follows:

<?php
require_once './braintree/Braintree.php';

header('Content-type: Application/JSON');

$amount = $_POST["amount"];
$nonce = $_POST["nonce"];

$gateway = new Braintree_Gateway([
    'environment' => 'sandbox',
    'merchantId' => 'my_merchantId_goes_here',
    'publicKey' => 'my_publicKey_goes_here',
    'privateKey' => 'my_privateKey_goes_here'
]);

$result = $gateway->transaction()->sale([
  'amount' => $amount,
  'paymentMethodNonce' => $nonce,
  'options' => [ 'submitForSettlement' => true ]
]);

if ($result->success) {
  http_response_code(200);
  echo json_encode(array("data" => $result->transaction->id));
} else if ($result->transaction) {
  $message = $result->transaction->processorResponseText;
  $code = $result->transaction->processorResponseCode;
  $response = "Error processing transaction.\nCode: $code\nMessage: $message";
  http_response_code(503);
  echo json_encode(array("data" => $result->transaction->id));
} else {
  http_response_code(503);
  echo json_encode(array("data" => $result->errors->deepAll()));
}
?>

But I keep getting Class not found errors, e.g Class 'Braintree_Gateway' not found. Been a long time since I did anything in PHP, so might just be a silly mistake somewhere...

crookedneighbor commented 5 years ago

I was able to reproduce the error you are seeing. It's being caused because you need the whole package, not just the lib directory. I believe the autoload.php file won't work properly if you pull out just the lib directory.

After I included the whole thing, it worked just fine.

If you still have issues with your integration after that, please contact our support team.

https://help.braintreepayments.com

JuanDeLeon commented 5 years ago

Hey, I installed the library via composer composer require braintree/braintree_php and get the same error. I just can't find the Braintree_Gateway class anywhere, not sure what you meant by "the whole thing".

crookedneighbor commented 5 years ago

Have you run the install command? https://developers.braintreepayments.com/start/hello-server/php#or-use-composer

Please reach out to our support team so we can troubleshoot your specific integration https://help.braintreepayments.com

JuanDeLeon commented 5 years ago

Yeah I did. I found this in the Gateway.php Class class_alias('Braintree\Gateway', 'Braintree_Gateway'); So I just used the Gateway class instead, and it got me a token, so it seems to work. Thanks.

petlys commented 5 years ago

If you download the library directly, then you don't need the whole thing, just the lib folder. My issue was that the autoload.php file referenced the wrong folder.