PHP code example of fearless-ajs / smart-payment-routing

1. Go to this page and download the library: Download fearless-ajs/smart-payment-routing 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/ */

    

fearless-ajs / smart-payment-routing example snippets


namespace App\Http\Controllers;

use App\Http\Requests\CreatePaymentRequest;
use App\Http\Services\StripeProcessor;
use Fearless\SmartPaymentRouting\core\adapter\ProcessorManager;
use Fearless\SmartPaymentRouting\core\PaymentRoutingService;
use Fearless\SmartPaymentRouting\core\processors\FlutterwaveProcessor;
use Fearless\SmartPaymentRouting\core\processors\PaystackProcessor;
use Illuminate\Http\Request;


class TransactionController extends Controller
{
   private readonly ProcessorManager $processorManager;

    public function __construct(ProcessorManager $processorManager)
    {
        $this->processorManager = $processorManager;
        // Register the processor for transactions within this class
        $processorManager->initialize([
            [
                'name' => 'paystack',
                'processor' => new PaystackProcessor(),
            ],
            [
                'name'  => 'flutterwave',
                'processor' => new FlutterwaveProcessor(),
            ]
        ]);
    }

}

namespace App\Http\Controllers;

use App\Http\Requests\CreatePaymentRequest;
use App\Http\Services\StripeProcessor;
use Fearless\SmartPaymentRouting\core\adapter\ProcessorManager;
use Fearless\SmartPaymentRouting\core\PaymentRoutingService;
use Fearless\SmartPaymentRouting\core\processors\FlutterwaveProcessor;
use Fearless\SmartPaymentRouting\core\processors\PaystackProcessor;
use Illuminate\Http\Request;

class TransactionController extends Controller
{
        private readonly ProcessorManager $processorManager;

    public function __construct(ProcessorManager $processorManager)
    {
        $this->processorManager = $processorManager;
        // Register the processor for transactions within this class
        $processorManager->initialize([
            [
                'name' => 'paystack',
                'processor' => new PaystackProcessor(),
            ],
            [
                'name'  => 'flutterwave',
                'processor' => new FlutterwaveProcessor(),
            ]
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(CreatePaymentRequest $request)
    {

        // Initialize the payment routing service
        $paymentRoutingService = new PaymentRoutingService($this->processorManager);
        $transaction = [
            'user_email' => $request->get('email'),
            'amount' => $request->get('amount'),
            'currency' => $request->get('currency'),
        ];

        // Route transaction to the best processor
        return $paymentRoutingService->makeCharge($this->processorManager, $transaction);
    }

}



namespace App\Http\Services;

use Fearless\SmartPaymentRouting\core\contract\PaymentProcessor;
use GuzzleHttp\Exception\GuzzleException;
use MusahMusah\LaravelMultipaymentGateways\Exceptions\HttpMethodFoundException;
use MusahMusah\LaravelMultipaymentGateways\Exceptions\InvalidConfigurationException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

class StripeProcessor implements PaymentProcessor
{
    public function createPayment($user_email, $amount, $currency)
    {
        $priceId = '***************';

        try {

            $stripe = new \Stripe\StripeClient(getenv('STRIPE_SECRET'));
           return $stripe->checkout->sessions->create([
                'success_url' => 'https://example.com/success',
                'line_items' => [
                    [
                        'price' => $priceId,
                        'quantity' => 2,
                    ],
                ],
                'mode' => 'payment',
            ]);
        } catch (GuzzleException|HttpMethodFoundException|InvalidConfigurationException $e) {
            throw new NotAcceptableHttpException($e->getMessage());
        }
    }
}


class TransactionController extends Controller
{
   private readonly ProcessorManager $processorManager;

    public function __construct(ProcessorManager $processorManager)
    {
        $this->processorManager = $processorManager;
        // Register the processor for transactions within this class
        $processorManager->initialize([
            [
                'name' => 'paystack',
                'processor' => new PaystackProcessor(),
            ],
            [
                'name'  => 'flutterwave',
                'processor' => new FlutterwaveProcessor(),
            ],
            [
                'name'  => 'stripe',
                'processor' => new StripeProcessor(),
            ]
        ]);
    }

}
bash
  php artisan vendor:publish --tag smart-payment-routing-config