<?php

require 'vendor/autoload.php';
require_once "includes/braintree_init.php";
$app = new \Slim\Slim();
$app->config(['templates.path' => 'templates']);
$app->get('/', function () use($app) {
    $app->redirect('/checkouts');
});
$app->get('/checkouts', function () use($app) {
    $app->render('checkouts/new.php', ['client_token' => Braintree\ClientToken::generate()]);
});
$app->post('/checkouts', function () use($app) {
    $result = Braintree\Transaction::sale(["amount" => $app->request->post('amount'), "paymentMethodNonce" => $app->request->post('payment_method_nonce'), 'options' => ['submitForSettlement' => True]]);
    if ($result->success || $result->transaction) {
        $app->redirect('/checkouts/' . $result->transaction->id);
    } else {
        $errorString = "";
        foreach ($result->errors->deepAll() as $error) {
            $errorString .= 'Error: ' . $error->code . ": " . $error->message . "\n";
        }
        $_SESSION["errors"] = $errorString;
        $app->redirect('/checkouts');
    }
});
$app->get('/checkouts/:transaction_id', function ($transaction_id) use($app) {
    $transaction = Braintree\Transaction::find($transaction_id);
    $transactionSuccessStatuses = [Braintree\Transaction::AUTHORIZED, Braintree\Transaction::AUTHORIZING, Braintree\Transaction::SETTLED, Braintree\Transaction::SETTLING, Braintree\Transaction::SETTLEMENT_CONFIRMED, Braintree\Transaction::SETTLEMENT_PENDING, Braintree\Transaction::SUBMITTED_FOR_SETTLEMENT];
    if (in_array($transaction->status, $transactionSuccessStatuses)) {
        $header = "Sweet Success!";
        $icon = "success";
                    </div>

                    <label for="amount">
                        <span class="input-label">Amount</span>
                        <div class="input-wrapper amount-wrapper">
                            <input id="amount" name="amount" type="tel" min="1" placeholder="Amount" value="10">
                        </div>
                    </label>
                </section>

                <button class="button" type="submit"><span>Test Transaction</span></button>
            </form>
        </div>
    </div>

    <script src="https://js.braintreegateway.com/js/braintree-2.27.0.min.js"></script>
    <script>
        var checkout = new Demo({
            formID: 'payment-form'
        })
        var client_token = "<?php 
echo Braintree\ClientToken::generate();
?>
";
        braintree.setup(client_token, "dropin", {
            container: "bt-dropin"
        });
    </script>
</body>
</html>
Exemple #3
0
<?php

Route::get('/', function () {
    return view('welcome');
});
Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::get('/home', 'HomeController@index');
    Route::group(['prefix' => 'braintree'], function () {
        Route::get('checkout', ['as' => 'braintree.checkout', 'uses' => function () {
            $clientToken = Braintree\ClientToken::generate();
            return view('braintree.checkout', compact('clientToken'));
        }]);
        Route::post('checkout', ['as' => 'braintree.charge', 'uses' => function () {
            return Braintree\Transaction::sale(['amount' => (int) Request::input('amount'), 'paymentMethodNonce' => Request::input('payment_method_nonce')]);
        }]);
    });
    Route::group(['prefix' => 'stripe'], function () {
        Route::get('checkout', ['as' => 'stripe.checkout', 'uses' => function () {
            return view('stripe.checkout');
        }]);
        Route::post('subscribe', ['as' => 'stripe.subscribe', 'uses' => function () {
            $user = App\User::find(1);
            $user->newSubscription('main', 'monthly')->create(Request::input('stripeToken'));
            return $user;
        }]);
        Route::get('invoice', ['as' => 'stripe.invoice', 'uses' => function () {
            $user = App\User::find(1);
            $invoices = $user->invoices();
            return $user->downloadInvoice($invoices[0]->id, ['vendor' => 'Your Company', 'product' => 'Your Product']);
        }]);