urfusion
urfusion

Reputation: 5501

braintreegateway Uncaught exception

I am using braintreegateway with the following code in sandbox mode.

Code is used from developer site

require_once 'braintree/lib/Braintree.php';

Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('marchentid');
Braintree_Configuration::publicKey('publickey');
Braintree_Configuration::privateKey('privatekey');

$result = Braintree_Transaction::sale([
            'amount' => '100.00',
            'orderId' => '123',
            'merchantAccountId' => 'marchentid',
            'paymentMethodNonce' => 'nonceFromTheClient',
            'customer' => [
                'firstName' => 'kapil',
                'lastName' => 'Smith',
                'company' => 'mycompany',
                'phone' => '1234567890',
                'website' => 'http://mywebsite.com',
                'email' => 'myemail'
            ],
            'billing' => [
                'firstName' => 'kapil',
                'lastName' => 'Smith',
                'company' => 'mycompany',
                'streetAddress' => 'address',
                'extendedAddress' => 'Suite 403',
                'locality' => 'India',
                'region' => 'IN',
                'postalCode' => 'zipcode',
                'countryCodeAlpha2' => 'IN'
            ],
            'shipping' => [
                'firstName' => 'kapil',
                'lastName' => 'Smith',
                'company' => 'mycompany',
                'streetAddress' => 'address',
                'extendedAddress' => 'Suite 403',
                'locality' => 'India',
                'region' => 'IN',
                'postalCode' => 'zipcode',
                'countryCodeAlpha2' => 'IN'
            ],
            'options' => [
                'submitForSettlement' => true
            ]
        ]);

if ($result->success) {
    print_r("success!: " . $result->transaction->id);
} else if ($result->transaction) {
    print_r("Error processing transaction:");
    print_r("\n  code: " . $result->transaction->processorResponseCode);
    print_r("\n  text: " . $result->transaction->processorResponseText);
} else {
    print_r("Validation errors: \n");
    print_r($result->errors->deepAll());
}

and the result is

Fatal error: Uncaught exception 'Braintree\Exception\Authorization' in /var/www/html/api/braintree/lib/Braintree/Util.php:61 Stack trace: #0 /var/www/html/api/braintree/lib/Braintree/Http.php(47): Braintree\Util::throwStatusCodeException(403) #1 /var/www/html/api/braintree/lib/Braintree/TransactionGateway.php(443): Braintree\Http->post('/merchants/dmmt...', Array) #2 /var/www/html/api/braintree/lib/Braintree/TransactionGateway.php(49): Braintree\TransactionGateway->_doCreate('/transactions', Array) #3 /var/www/html/api/braintree/lib/Braintree/TransactionGateway.php(268): Braintree\TransactionGateway->create(Array) #4 /var/www/html/api/braintree/lib/Braintree/Transaction.php(494): Braintree\TransactionGateway->sale(Array) #5 /var/www/html/api/payment.php(58): Braintree\Transaction::sale(Array)

6 {main} thrown in /var/www/html/api/braintree/lib/Braintree/Util.php on line 61

what's wrong here?

Upvotes: 1

Views: 5288

Answers (2)

Mimarshall
Mimarshall

Reputation: 442

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

Like @mfahadi mentioned, one potential problem may be that that your code doesn't have your keys in the Braintree_Configuration calls, but you should never post your keys to StackOverflow, even for sandbox accounts. So if it was intentionally filtered: well done. If not, check out the guide to get your client keys.

The second problem is you did not replace 'nonceFromTheClient' in the line below with an actual client nonce:

'paymentMethodNonce' => 'nonceFromTheClient',

You can find a test nonce in the testing reference guide.

And replace the line above with the line below to fix your second problem:

'paymentMethodNonce' => 'fake-valid-nonce',

Upvotes: 1

mfahadi
mfahadi

Reputation: 419

You are just trying to run a sample code, which has placeholders for info that you have to provide. You have to add that info for it to work.

Braintree_Configuration::merchantId('marchentid');
Braintree_Configuration::publicKey('publickey');
Braintree_Configuration::privateKey('privatekey');

In above code you have to provie your merchentid, publikey and privatekey form Braintree conrol panel.

'paymentMethodNonce' => 'nonceFromTheClient',

And then add the nounce from client. I would suggest reading up the docs for PHP server implementation and hosted fields to get a detailed understanding.

Hosted Fileds Docs

PHP Server Docs

*Not adding as comment because I don't have enough reputation.

Upvotes: 3

Related Questions