PHP code example of aflorea4 / laravel-netopia-payments

1. Go to this page and download the library: Download aflorea4/laravel-netopia-payments library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aflorea4 / laravel-netopia-payments example snippets


// config/netopia.php
return [
    // The Netopia merchant signature (merchant identifier)
    'signature' => env('NETOPIA_SIGNATURE', ''),

    // The path to the public key file
    'public_key_path' => env('NETOPIA_PUBLIC_KEY_PATH', storage_path('app/keys/netopia/public.cer')),

    // The path to the private key file
    'private_key_path' => env('NETOPIA_PRIVATE_KEY_PATH', storage_path('app/keys/netopia/private.key')),

    // Whether to use the live environment or the sandbox environment
    'live_mode' => env('NETOPIA_LIVE_MODE', false),

    // The currency to use for payments
    'default_currency' => env('NETOPIA_DEFAULT_CURRENCY', 'RON'),
];

use Aflorea4\NetopiaPayments\Facades\NetopiaPayments;

// Create a payment request
$paymentData = NetopiaPayments::createPaymentRequest(
    'ORDER123', // Order ID
    100.00, // Amount
    'RON', // Currency
    route('payment.return'), // Return URL
    route('netopia.confirm'), // Confirm URL
    [
        'type' => 'person', // 'person' or 'company'
        'firstName' => 'John',
        'lastName' => 'Doe',
        'email' => '[email protected]',
        'address' => '123 Main St',
        'mobilePhone' => '0712345678',
    ],
    'Payment for Order #123' // Description
);

// The payment data contains:
// - env_key: The encrypted envelope key (REQUIRED)
// - data: The encrypted payment data (REQUIRED)
// - cipher: The cipher algorithm used for encryption (REQUIRED, defaults to 'aes-256-cbc')
// - iv: The initialization vector for AES encryption (REQUIRED when using aes-256-cbc)
// - url: The payment URL (sandbox or live)
//
// IMPORTANT: All parameters (env_key, data, cipher, and iv) must be 

use Aflorea4\NetopiaPayments\Events\NetopiaPaymentConfirmed;
use Aflorea4\NetopiaPayments\Events\NetopiaPaymentPending;
use Aflorea4\NetopiaPayments\Events\NetopiaPaymentCanceled;

// In your EventServiceProvider.php
protected $listen = [
    NetopiaPaymentConfirmed::class => [
        // Your listeners here
    ],
    NetopiaPaymentPending::class => [
        // Your listeners here
    ],
    NetopiaPaymentCanceled::class => [
        // Your listeners here
    ],
];

namespace App\Listeners;

use Aflorea4\NetopiaPayments\Events\NetopiaPaymentConfirmed;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\Order;

class HandleConfirmedPayment implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Handle the event.
     *
     * @param  NetopiaPaymentConfirmed  $event
     * @return void
     */
    public function handle(NetopiaPaymentConfirmed $event)
    {
        // Get the payment response
        $response = $event->response;

        // Find the order
        $order = Order::where('order_id', $response->orderId)->first();

        if ($order) {
            // Update the order status
            $order->status = 'paid';
            $order->payment_amount = $response->processedAmount;
            $order->save();

            // Additional logic...
        }
    }
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Aflorea4\NetopiaPayments\Facades\NetopiaPayments;
use Illuminate\Support\Facades\Log;

class PaymentController extends Controller
{
    /**
     * Initiate a test payment
     */
    public function initiatePayment()
    {
        // Create a payment request for 1.00 RON
        $paymentData = NetopiaPayments::createPaymentRequest(
            'TEST'.time(), // Order ID with timestamp to make it unique
            1.00, // Amount (1.00 RON for testing)
            'RON', // Currency
            route('payment.return'), // Return URL
            route('payment.confirm'), // Confirm URL
            [
                'type' => 'person',
                'firstName' => 'Test',
                'lastName' => 'User',
                'email' => '[email protected]',
                'address' => 'Test Address',
                'mobilePhone' => '0700000000',
            ],
            'Test payment of 1.00 RON' // Description
        );

        // Redirect to the payment page
        return view('payment.redirect', [
            'paymentData' => $paymentData,
        ]);
    }

    /**
     * Handle the payment confirmation (IPN)
     */
    public function confirmPayment(Request $request)
    {
        try {
            // Process the payment response
            $response = NetopiaPayments::processResponse(
                $request->input('env_key'),
                $request->input('data'),
                null,
                $request->input('iv')
            );

            // Log the payment response
            Log::info('Payment confirmation received', [
                'orderId' => $response->orderId,
                'action' => $response->action,
                'errorCode' => $response->errorCode ?? null,
                'errorMessage' => $response->errorMessage ?? null,
                'processedAmount' => $response->processedAmount ?? null,
            ]);

            // Handle the payment based on the action
            if ($response->isConfirmed()) {
                // Payment was confirmed/approved
                // Update your order status in the database
                // For example:
                // Order::where('order_id', $response->orderId)->update(['status' => 'paid']);

                // Your business logic here...
            } elseif ($response->isPending()) {
                // Payment is pending
                // Update your order status in the database
                // For example:
                // Order::where('order_id', $response->orderId)->update(['status' => 'pending']);
            } elseif ($response->isCanceled()) {
                // Payment was canceled or failed
                // Update your order status in the database
                // For example:
                // Order::where('order_id', $response->orderId)->update(['status' => 'canceled']);
            }

            // Return the appropriate response to Netopia
            return response(
                NetopiaPayments::generatePaymentResponse(),
                200,
                ['Content-Type' => 'application/xml']
            );
        } catch (\Exception $e) {
            // Log the error
            Log::error('Payment confirmation error', [
                'message' => $e->getMessage(),
            ]);

            // Return an error response to Netopia
            return response(
                NetopiaPayments::generatePaymentResponse(1, 1, $e->getMessage()),
                200,
                ['Content-Type' => 'application/xml']
            );
        }
    }

    /**
     * Handle the return from payment page
     */
    public function returnFromPayment(Request $request)
    {
        // This is where the user is redirected after the payment
        // You can show a thank you page or order summary

        // Check if all 

// routes/web.php
Route::get('/payment/initiate', [PaymentController::class, 'initiatePayment'])->name('payment.initiate');

// Custom routes if you want to handle payment processing in your own controller
Route::post('/payment/confirm', [PaymentController::class, 'confirmPayment'])->name('payment.confirm');
Route::get('/payment/return', [PaymentController::class, 'returnFromPayment'])->name('payment.return');

it('can create a payment request', function () {
    $netopiaPayments = new \Aflorea4\NetopiaPayments\NetopiaPayments();

    $paymentData = $netopiaPayments->createPaymentRequest(
        'ORDER123',
        100.00,
        'RON',
        'https://example.com/return',
        'https://example.com/confirm',
        [
            'type' => 'person',
            'firstName' => 'John',
            'lastName' => 'Doe',
            'email' => '[email protected]',
            'address' => '123 Main St',
            'mobilePhone' => '0712345678',
        ],
        'Payment for Order #123'
    );

    expect($paymentData)->toBeArray()
        ->toHaveKeys(['env_key', 'data', 'url']);
});
bash
php artisan vendor:publish --provider="Aflorea4\NetopiaPayments\NetopiaPaymentsServiceProvider" --tag="config"