How to add vipps payment gateway in laravel

Laravel is one of the most popular PHP frameworks used for building web applications. It provides various features that make the development process easier and faster. One of the most important aspects of web applications is payment integration. Vipps is a popular payment gateway used in Norway. In this article, we will provide a step-by-step guide on how to integrate the Vipps payment gateway in Laravel and how to handle invoice payments, recurring payments, and subscriptions.

Step 1: Sign Up for a Vipps Account

The first step is to sign up for a Vipps account. Go to the Vipps website and create an account. After you have signed up, you will receive an email with your credentials, including your merchant ID and API keys.

Step 2: Install Vipps Package

The next step is to install the Vipps package in your Laravel application. Open your terminal and navigate to your project directory. Run the following command to install the package.

composer require vippsas/vipps-php-sdk

Step 3: Create a Vipps Configuration File

Create a configuration file for Vipps. In this file, you will add your merchant ID and API keys. Create a new file in the config directory called vipps.php.

<?phpreturn [    'merchant_id' => 'your_merchant_id',    'client_id' => 'your_client_id',    'client_secret' => 'your_client_secret',    'subscription_key' => 'your_subscription_key',    'callback_prefix' => 'https://yourapp.com/callback'];

Step 4: Create a Vipps Controller

Create a new controller for Vipps. This controller will handle all the payment-related functions. In this controller, you will create functions for handling invoice payments, recurring payments, and subscriptions.

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Vipps;class VippsController extends Controller{    public function invoicePayment(Request $request)    {        // Create an invoice payment request        $amount = $request->amount;        $orderId = $request->orderId;        $description = $request->description;        $response = Vipps::createInvoicePayment($amount, $orderId, $description);        // Redirect the user to the Vipps payment page        return redirect($response['url']);    }    public function recurringPayment(Request $request)    {        // Create a recurring payment request        $amount = $request->amount;        $description = $request->description;        $startDate = $request->startDate;        $response = Vipps::createRecurringPayment($amount, $description, $startDate);        // Redirect the user to the Vipps payment page        return redirect($response['url']);    }    public function subscription(Request $request)    {        // Create a subscription request        $planId = $request->planId;        $customerEmail = $request->customerEmail;        $response = Vipps::createSubscription($planId, $customerEmail);        // Redirect the user to the Vipps payment page        return redirect($response['url']);    }    public function callback(Request $request)    {        // Handle the callback from Vipps        $orderId = $request->orderId;        $transactionId = $request->transactionId;        $status = $request->status;        // Update your database with the transaction details        // ...        // Return a response to Vipps        return response()->json(['success' => true]);    }}

Step 5: Add Routes

Add routes to your Laravel application for the Vipps controller. Open your routes/web.php file and add the following routes.

Route::post('/vipps/invoice-payment', 'VippsController@invoicePayment');Route::post('/vipps/recurring-payment', 'VippsController@recurringPayment');Route::post('/vipps/subscription', 'VippsController@subscription');Route::post('/vipps/callback', 'VippsController@callback');

Step 6: Create Payment Forms

Create payment forms for invoice payments, recurring payments, and subscriptions. These forms will be used to send payment requests to the Vipps payment gateway. Here is an example of an invoice payment form.

<form action="/vipps/invoice-payment" method="post">    @csrf    <input type="number" name="amount" placeholder="Amount">    <input type="text" name="orderId" placeholder="Order ID">    <input type="text" name="description" placeholder="Description">    <button type="submit">Pay with Vipps</button></form>

Step 7: Handle Callbacks

Handle callbacks from Vipps in your VippsController. When a payment is made, Vipps will send a callback to the callback URL specified in the Vipps configuration file. In the callback function, you can update your database with the transaction details and return a response to Vipps.

Step 8: Test Your Integration

Test your Vipps payment gateway integration by making payments using the payment forms you created. Make sure the payments are being processed correctly and that the callbacks are being handled properly.

Conclusion

In this article, we have provided a step-by-step guide on how to integrate the Vipps payment gateway in Laravel and how to handle invoice payments, recurring payments, and subscriptions. By following these steps, you can easily add Vipps as a payment option in your Laravel application.

Comments