PHP code example of dp-soft-co / payments

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

    

dp-soft-co / payments example snippets


use App\Models\Invoice;
use Dpsoft\Payments\Facades\DpsoftPaymentsFacade as DpsoftPayments;
use Illuminate\Http\Request;

public function pay(Request $request)
{
    $data = $request->validate([
        'gateway' => ['s,wallet,fawry',
        'hyperpay' => 'CREDIT',
        default => null,
    };

    $response = DpsoftPayments::gateway($gateway)
        ->setPaymentData([
            'amount' => $data['amount'],
            'user_id' => $user->id,
            'user_first_name' => $user->firstname,
            'user_last_name' => $user->lastname,
            'user_email' => $user->email,
            'user_phone' => $user->phone,
            'source' => $source,
        ])
        ->pay();

    if (empty($response['payment_id'])) {
        abort(422, strip_tags($response['html'] ?? 'Payment creation failed.'));
    }

    Invoice::create([
        'user_id' => $user->id,
        'amount' => $data['amount'],
        'status' => 'pending',
        'pid' => $response['payment_id'],
        'gateway' => $gateway,
    ]);

    if (!empty($response['redirect_url'])) {
        return redirect()->away($response['redirect_url']);
    }

    if (!empty($response['html'])) {
        return view('payments.pay', ['link' => $response['html']]);
    }

    abort(422, 'Invalid gateway response.');
}

public function verify(string $gateway, Request $request)
{
    $result = DpsoftPayments::gateway($gateway)->verify($request);

    $invoice = Invoice::where('pid', $result['payment_id'] ?? null)
        ->where('gateway', $gateway)
        ->firstOrFail();

    if ($result['success']) {
        $invoice->update(['status' => 'paid']);

        return redirect()->route('payments.success');
    }

    return redirect()->route('payments.failed');
}

Route::match(['get', 'post'], '/payments/verify/{gateway}', [PaymentController::class, 'verify'])
    ->name('verify-payment');

  $middleware->validateCsrfTokens(except: [
      '*payment/verify*',
  ]);
  

  protected $except = [
      '*payment/verify*',
  ];
  
bash
php artisan vendor:publish --tag="dpsoft-payments-config"
php artisan vendor:publish --tag="dpsoft-payments-lang"