The typical Vanilo Payments workflow with Braintree consists of the following steps:
- Create an Order (or any "Payable")
- Obtain the payment method from the checkout*
- Get the appropriate gateway instance associated with the payment method
- Generate a payment request using the gateway which bascially creates a
clientToken
via Braintee API - Inject the HTML snippet on the checkout/thankyou page
- The HTML snippet will render out the Braintree Drop In containing the
clientToken
- Fill the drop in and submit
- The gateway creates the transaction in Braintree and return a
PaymentResponse
Once you have an order (or any other payable), then the starting point of payment operations is obtaining a gateway instance:
$gateway = \Vanilo\Payment\PaymentGateways::make('braintree');
// Vanilo\Braintree\BraintreePaymentGateway
The gateway provides you two essential methods:
createPaymentRequest
- Gets theclientToken
from Braintree and provides the HTML snippet which can be injected to render the Drop-IncreateTransaction
- Creates the transaction from an order (payable)processPaymentResponse
- Processes theTransaction
object returned bycreateTransaction
function
Controller:
use Vanilo\Framework\Models\Order;
use Vanilo\Payment\Factories\PaymentFactory;
use Vanilo\Payment\Models\PaymentMethod;
use Vanilo\Payment\PaymentGateways;
class OrderController
{
public function submit(Request $request)
{
$order = Order::createFrom($request);
$paymentMethod = PaymentMethod::find($request->get('paymentMethod'));
$payment = PaymentFactory::createFromPayable($order, $paymentMethod);
$gateway = PaymentGateways::make('braintree');
$paymentRequest = $gateway->createPaymentRequest($payment, options: ['submitUrl' => route('payment.braintree.submit', $payment->hash)]);
return view('order.confirmation', [
'order' => $order,
'paymentRequest' => $paymentRequest
]);
}
}
Blade Template:
{!! $paymentRequest->getHtmlSnippet(); !!}
The generated HTML snippet will contain a prepared, Braintree Drop-In.
The gateway's createPaymentRequest
method accepts additional
parameters that can be used to customize the generated request.
The signature of the method is the following:
public function createPaymentRequest(
Payment $payment,
Address $shippingAddress = null,
array $options = []
): PaymentRequest
- The first parameter is the
$payment
. Every attempt to settle a payable is a newPayment
record. - The second one is the
$shippingAddress
in case it differs from billing address. It can be left NULL. - The third parameters is an array with possible
$options
.
You can pass the following values in the $options
array:
Array Key | Example | Description |
---|---|---|
view |
vanilo.braintree._form |
By default it's braintree::_request You can use a custom blade view to render the HTML snippet instead of the default one this library provides. |
submitUrl |
route('payment.braintree.submit', $payment->hash) |
You must set this url which the action where the Drop-In will be submitted. |
Example:
$options = [
'view' => 'vanilo.braintree._form',
'submitUrl' => route('payment.braintree.submit', $payment->hash),
];
$gateway->createPaymentRequest($payment, null, $options);
Apart from passing the view
option to the createPaymentRequest
(see
above), there's an even more simple way: Laravel lets you
override the views from vendor packages
like this.
Simply put, if you create the
resources/views/vendor/braintree/_request.blade.php
file in your
application, then this blade view will be used instead of the one
supplied by the package.
To get the default view from the package and start customizing it, use this command:
php artisan vendor:publish --tag=vanilo-braintree
This will copy the default blade view used to render the HTML form into
the resources/views/vendor/braintree/
folder of your application. After
that, the getHtmlSnippet()
method will use the copied blade template
to render the HTML snippet for Braintree payment requests.
Since Braintree doesn't have webhook for the Transaction
s basically the entire flow is a sync call of endpoints.
Therefore there is not return/cancel urls involved.
Next: Examples »