PHP code example of donsoft / smartpay

1. Go to this page and download the library: Download donsoft/smartpay 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/ */

    

donsoft / smartpay example snippets


'providers' => [
    Donsoft\\SmartPay\\Providers\\SmartPayServiceProvider::class,
],

return [
    /*
    |--------------------------------------------------------------------------
    | Priority Weights for Payment Processor Selection
    |--------------------------------------------------------------------------
    */
    'priorities' => [
        'currency_support' => 3,  // Most important
        'reliability' => 2,       // Second most important
        'transaction_cost' => 1,  // Least important
    ],

    /*
    |--------------------------------------------------------------------------
    | Supported Payment Processors
    |--------------------------------------------------------------------------
    */
    'processors' => [
        'processorA' => [
            'class' => \\Donsoft\\SmartPay\\Services\\Processors\\ProcessorA::class,
            'cost_per_transaction' => 0.5,
            'supported_currencies' => ['USD', 'EUR'],
            'min_transaction_amount' => 1,
            'reliability' => 0.85,
        ],
        'processorB' => [
            'class' => \\Donsoft\\SmartPay\\Services\\Processors\\ProcessorB::class,
            'cost_per_transaction' => 1.0,
            'supported_currencies' => ['USD'],
            'min_transaction_amount' => 100,
            'reliability' => 0.90,
        ],
    ],

    'default_currency' => 'USD',
];

use Donsoft\\SmartPay\\Services\\Routing\\PaymentRouter;
use Illuminate\\Http\\Request;

class PaymentController extends Controller
{
    protected $paymentRouter;

    public function __construct(PaymentRouter $paymentRouter)
    {
        $this->paymentRouter = $paymentRouter;
    }

    public function processPayment(Request $request)
    {
        $transaction = (object) [
            'amount' => $request->input('amount'),
            'currency' => $request->input('currency', config('smartpay.default_currency')),
        ];

        try {
            $processor = $this->paymentRouter->route($transaction);
            $response = $processor->process($transaction);

            return response()->json($response);
        } catch (\Exception $e) {
            return response()->json(['status' => 'error', 'message' => $e->getMessage()], 400);
        }
    }
}

namespace App\\Services\\Processors;

use Donsoft\\SmartPay\\Services\\Processors\\BaseProcessor;

class CustomProcessor extends BaseProcessor
{
    public function __construct()
    {
        parent::__construct(
            'CustomProcessor',
            0.25,  // Cost per transaction
            ['USD', 'GBP'],  // Supported currencies
            0.95,  // Reliability
            10  // Minimum transaction amount
        );
    }

    public function process($transaction)
    {
        // Custom processing logic here
        return $this->successResponse($transaction);
    }
}

return [
    'processors' => [
        'customProcessor' => [
            'class' => \\App\\Services\\Processors\\CustomProcessor::class,
            'cost_per_transaction' => 0.25,
            'supported_currencies' => ['USD', 'GBP'],
            'min_transaction_amount' => 10,
            'reliability' => 0.95,
        ],
    ],
];

'priorities' => [
    'currency_support' => 4,  // Higher priority
    'reliability' => 2,
    'transaction_cost' => 1,
],
bash
php artisan vendor:publish --tag=config --provider="Donsoft\\SmartPay\\Providers\\SmartPayServiceProvider"