This package integrates Klarna Payments into your Laravel application.
-
Install the package via Composer:
composer require shasan-dev/laravel-klarna-payments
-
Publish the configuration file:
php artisan vendor:publish --provider="LaravelKlarna\KlarnaPayments\KlarnaPaymentsServiceProvider" --tag="klarna-payments-config"
-
Update your
.env
file:KLARNA_USER_ID=your_user_id KLARNA_PASSWORD=your_password KLARNA_TESTMODE=true_or_false KLARNA_REGION=NA/OC/EU (default EU)
php artisan make:controller KlarnaController
Route::get('payment/klarna-checkout', [KlarnaController::class, 'payment'])->name('klarna.checkout');
Route::get('payment/klarna-success', [KlarnaController::class, 'paymentSuccess'])->name('klarna.success');
In KlarnaController
, handle the payment logic as follows:
use LaravelKlarna\KlarnaPayments\Facades\KlarnaPayments;
public function payment() {
$data = [
'order_amount' => 20 * 100,
'order_lines' => [
[
'name' => 'Running shoe',
'quantity' => 1,
'quantity_unit' => 'pcs',
'total_amount' => 20 * 100,
'type' => 'physical',
'unit_price' => 20 * 100,
]
],
'purchase_country' => 'US',
'purchase_currency' => 'USD',
'intent' => 'buy',
];
$sessionId = KlarnaPayments::createSession($data)->json()['session_id'];
$redirectUrl = KlarnaPayments::createHppSession($sessionId, [
'success' => route('klarna.success') . "?sid={{session_id}}&order_id={{order_id}}",
'cancel' => route('home'),
'back' => route('home'),
'failure' => route('home') . "?sid={{session_id}}",
'error' => route('home') . "?sid={{session_id}}",
])->json()['redirect_url'];
return redirect()->to($redirectUrl);
}
public function paymentSuccess(Request $request) {
$checkPayment = KlarnaPayments::checkPayment($request->get('order_id'));
if ($checkPayment->json()['status'] === 'AUTHORIZED') {
return 'Payment Success. Reference No. ' . $checkPayment->json()['klarna_reference'];
}
return 'Payment Not Successful';
}
Visit /payment/klarna-checkout
to initiate a payment.
If you find this package helpful, don't forget to star it! Thanks.
MIT License.